added connectfrom
authorAndrew Tridgell <tridge@samba.org>
Tue, 21 Oct 2008 23:47:59 +0000 (10:47 +1100)
committerAndrew Tridgell <tridge@samba.org>
Tue, 21 Oct 2008 23:47:59 +0000 (10:47 +1100)
connectfrom.c [new file with mode: 0644]

diff --git a/connectfrom.c b/connectfrom.c
new file mode 100644 (file)
index 0000000..60e0469
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+  force TCP connections to come from a specific IP
+
+  compile with:
+     gcc -o connectfrom.so -fPIC -shared -o connectfrom.so connectfrom.c
+*/
+
+#include <stdio.h>
+#include <pwd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
+{
+       static int (*real_connect)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
+       const char *address = getenv("CONNECTFROM");
+       
+       if (!real_connect) {
+               real_connect = dlsym((void*)-1, "connect");
+       }
+
+       if (address) {
+               struct sockaddr_in myaddr;
+               socklen_t myaddrlen = sizeof(myaddr);
+               myaddr = *(struct sockaddr_in *)serv_addr;
+               inet_aton(address, &myaddr.sin_addr);
+               myaddr.sin_port = 0;
+               
+               bind(sockfd, (struct sockaddr *)&myaddr, myaddrlen);
+       }
+
+       return real_connect(sockfd, serv_addr, addrlen);
+}