diff options
author | Edwin Groothuis <edwin@FreeBSD.org> | 2003-04-01 00:49:48 +0000 |
---|---|---|
committer | Edwin Groothuis <edwin@FreeBSD.org> | 2003-04-01 00:49:48 +0000 |
commit | fd7e8698abcbdf8abb049c07acbff5238dbd54ff (patch) | |
tree | 96c15bcf8ad29fdce9dd30050b8fd4f7b7f0e5c3 /net/socketbind/files/socketbind.c | |
parent | Update of mail/wmbiff to 0.4.15 and add support for gnutls (SSL). (diff) |
socketbind - new port submission
This library allows you to bind any application which is
dynamically linked with libc to certain IP address. It
provides convient way to bind socket's source IP to one of
the multiple IP's available on computer.
PR: ports/50147
Submitted by: Gaspar Chilingarov <nm@web.am>
Diffstat (limited to 'net/socketbind/files/socketbind.c')
-rw-r--r-- | net/socketbind/files/socketbind.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/net/socketbind/files/socketbind.c b/net/socketbind/files/socketbind.c new file mode 100644 index 000000000000..51d929d9b794 --- /dev/null +++ b/net/socketbind/files/socketbind.c @@ -0,0 +1,46 @@ +#include <stdlib.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <dlfcn.h> + +static void *socket_p, *dl_handle; + +static struct sockaddr_in bind_addr; +static int do_bind; + +int socket(int domain, int type, int protocol) { + auto int res; + if (dl_handle == NULL) { + char *str; + dl_handle = dlopen("/usr/lib/libc.so", RTLD_LAZY); + if (dl_handle == NULL) + return -1; + socket_p = dlsym(dl_handle, "socket"); + if (!socket_p) + return -1; +#ifdef DEBUG + printf("Loaded socket %x\n", socket_p); +#endif + + if ((str = getenv("BINDTO")) != NULL) { +#ifdef DEBUG + printf("Thinking about bind\n"); +#endif + if (ascii2addr(AF_INET, 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; +}; + |