diff --git a/net/socketbind/Makefile b/net/socketbind/Makefile index 3725a84f17b0..bf17612645ba 100644 --- a/net/socketbind/Makefile +++ b/net/socketbind/Makefile @@ -1,28 +1,29 @@ PORTNAME= socketbind PORTVERSION= 1 +PORTREVISION= 1 CATEGORIES= net MASTER_SITES= # none DISTFILES= # none MAINTAINER= nm@web.am COMMENT= Library to bind applications on multihomed machines to specific IP address USE_LDCONFIG= YES INSTALL_TARGET= post-extract: ${MKDIR} ${WRKSRC} ${CP} -R ${PATCHDIR}/ ${WRKSRC} pre-install: ${INSTALL_PROGRAM} ${WRKSRC}/libsocketbind.so.1 ${STAGEDIR}${PREFIX}/lib ${LN} -s ${PREFIX}/lib/libsocketbind.so.1 ${STAGEDIR}${PREFIX}/lib/libsocketbind.so ${MKDIR} ${STAGEDIR}${DOCSDIR} ${ECHO} "This library allows to bind arbitrary program, " >> ${STAGEDIR}${DOCSDIR}/README ${ECHO} "which is dynamically linked to libc.so." >> ${STAGEDIR}${DOCSDIR}/README ${ECHO} "Load library before your program (set environment " >> ${STAGEDIR}${DOCSDIR}/README ${ECHO} "variable LD_PRELOAD=${PREFIX}/lib/libsocketbind.so.1) " >> ${STAGEDIR}${DOCSDIR}/README ${ECHO} "and set address to bind to (set environment variable " >> ${STAGEDIR}${DOCSDIR}/README ${ECHO} "BINDTO=ip address to bind)" >> ${STAGEDIR}${DOCSDIR}/README .include diff --git a/net/socketbind/files/socketbind.c b/net/socketbind/files/socketbind.c index 51d929d9b794..340083adaefb 100644 --- a/net/socketbind/files/socketbind.c +++ b/net/socketbind/files/socketbind.c @@ -1,46 +1,45 @@ #include #include #include #include +#include #include +#include -static void *socket_p, *dl_handle; +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 (dl_handle == NULL) { + if (socket_p == NULL) { char *str; - dl_handle = dlopen("/usr/lib/libc.so", RTLD_LAZY); - if (dl_handle == NULL) - return -1; - socket_p = dlsym(dl_handle, "socket"); + socket_p = dlsym(RTLD_NEXT, "socket"); if (!socket_p) return -1; #ifdef DEBUG printf("Loaded socket %x\n", socket_p); #endif - if ((str = getenv("BINDTO")) != NULL) { + if ((domain == PF_INET) && (str = getenv("BINDTO")) != NULL) { #ifdef DEBUG printf("Thinking about bind\n"); #endif - if (ascii2addr(AF_INET, str, &bind_addr.sin_addr)) { + 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; };