File: redirect_bind.c

package info (click to toggle)
saunafs 5.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,820 kB
  • sloc: cpp: 109,287; sh: 18,755; python: 4,737; ansic: 4,023; makefile: 60; awk: 17
file content (26 lines) | stat: -rw-r--r-- 699 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#define _GNU_SOURCE
#include <dlfcn.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
	static int (*_bind)(int, const struct sockaddr*, socklen_t) = NULL;
	struct sockaddr_in addr_in = * (const struct sockaddr_in*) addr;
	int type;
	socklen_t length = sizeof(type);
	getsockopt(sockfd, SOL_SOCKET, SO_TYPE, &type, &length);
	if (type == SOCK_STREAM) {
		int port = ntohs(addr_in.sin_port);
		if (port) {
			port += 1000;
			addr_in.sin_port = htons(port);
		}
	}
	if (!_bind) {
		_bind = dlsym(RTLD_NEXT, "bind");
	}
	return _bind(sockfd, (const struct sockaddr*) &addr_in, addrlen);
}