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 ...
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
19 int connect(int sockfd
, const struct sockaddr
*addr
, socklen_t addrlen
)
22 struct in_addr inaddr
;
23 struct sockaddr_in sin
;
25 ip
= getenv("BIND_ADDRESS");
29 if(inet_aton(ip
, &inaddr
) == 0)
31 warnx("forcebindip: Invalid address: %s", ip
);
35 sin
.sin_family
= AF_INET
;
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
);