summaryrefslogtreecommitdiff
path: root/net/socketbind/files/socketbind.c
blob: 340083adaefbd163a823ddc6f2fe25e3d15d502d (plain) (blame)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dlfcn.h>
#include <stdlib.h>

static void *socket_p = NULL;

static struct sockaddr_in bind_addr;
static int	   do_bind;

int socket(int domain, int type, int protocol) {
	auto int res;
	if (socket_p == NULL) {
		char *str;
		socket_p = dlsym(RTLD_NEXT, "socket");
		if (!socket_p)
			return -1;
#ifdef DEBUG
		printf("Loaded socket %x\n", socket_p);
#endif

		if ((domain == PF_INET) && (str = getenv("BINDTO")) != NULL) {
#ifdef DEBUG
			printf("Thinking about bind\n");
#endif
			if (inet_aton(str, &bind_addr.sin_addr)) {
				do_bind = 1;
				bind_addr.sin_len = INET_ADDRSTRLEN;
				bind_addr.sin_family = AF_INET;
#ifdef DEBUG
				printf("WILL DO BIND %s, %x\n", str, bind_addr.sin_addr.s_addr);
#endif
			}
		}
	}
	res = ((int(*)(int a, int b, int c))socket_p)(domain, type, protocol); 
	if (do_bind) {
		bind(res, (struct sockaddr*)&bind_addr, INET_ADDRSTRLEN);
	}
	return res;
};