Update recyclix-flow.svg
[hband-ld-preload-libs.git] / src / forcebindip.c
blob76fb109c7c812897dbbc170b41d4dc21f5cfd8aa
1 /**
2 * forcebindip.c
4 * Compile with: gcc -D _GNU_SOURCE -shared -fPIC -o forcebindip.so forcebindip.c
6 * Usage: LD_PRELOAD=./forcebindip.so BIND_ADDRESS=1.2.3.4 wget ...
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <dlfcn.h>
16 #include <errno.h>
17 #include <err.h>
19 int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
21 char *ip;
22 struct in_addr inaddr;
23 struct sockaddr_in sin;
25 ip = getenv("BIND_ADDRESS");
27 if (ip != NULL)
29 if(inet_aton(ip, &inaddr) == 0)
31 warnx("forcebindip: Invalid address: %s", ip);
33 else
35 sin.sin_family = AF_INET;
36 sin.sin_port = 0;
37 sin.sin_addr = inaddr;
39 if(bind(sockfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
41 warn("forcebindip: bind: %s", ip);
46 int (*orig_connect)(int, const struct sockaddr *, socklen_t) = dlsym(RTLD_NEXT, "connect");
47 return orig_connect(sockfd, addr, addrlen);