X-Git-Url: http://git.onelab.eu/?p=bind_public.git;a=blobdiff_plain;f=bind_public.c;h=5e32ca178c3772366d8d4a415e046208f04c13c8;hp=697121d87fccf5e96c7681e3c7eba2fd7d74b98e;hb=41be25f2709ea2fca15236331922927ef94305c4;hpb=fd5b3d87b2012ceeb8eddb8e771181ec077a3d52 diff --git a/bind_public.c b/bind_public.c index 697121d..5e32ca1 100644 --- a/bind_public.c +++ b/bind_public.c @@ -186,42 +186,42 @@ static void debug( const struct sockaddr* before, struct sockaddr* after ) { // if the caller attempted to bind to 0.0.0.0 or ::, then change it to // this node's public IP address int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { - - int rc = is_addr_any( addr ); - if( rc > 0 ) { - - // save the original bind() call - if( bind_original == NULL ) { - void *handle = dlopen( LIBC_PATH, RTLD_LAZY ); - if (!handle) { - fprintf( stderr, "Error loading libc.so.6\n" ); - fflush( stderr ); - return -1; - } - bind_original = dlsym(handle, "bind"); - if( bind_original == NULL ) { - fprintf( stderr, "Error loading socket symbol\n" ); - fflush( stderr ); - return -1; - } - } - - // rewrite this address - struct sockaddr_storage new_addr; - memset( &new_addr, 0, sizeof(struct sockaddr_storage)); - - if( copy_nonIP_fields( (struct sockaddr*)&new_addr, addr ) != 0 ) { - errno = EACCES; - rc = -1; - } - else if( get_public_ip( (struct sockaddr*)&new_addr ) != 0 ) { - rc = -1; - } - else { - debug( addr, (struct sockaddr*)&new_addr ); - rc = bind_original( sockfd, (struct sockaddr*)&new_addr, addrlen ); - } - } - - return rc; + + // save the original bind() call + void *handle = dlopen( LIBC_PATH, RTLD_LAZY ); + if (!handle) { + fprintf( stderr, "Error loading libc.so.6\n" ); + fflush( stderr ); + return -1; + } + bind_original = dlsym(handle, "bind"); + if( bind_original == NULL ) { + fprintf( stderr, "Error loading socket symbol\n" ); + fflush( stderr ); + return -1; + } + + int rc = is_addr_any( addr ); + if( rc > 0 ) { + + // rewrite this address + struct sockaddr_storage new_addr; + memset( &new_addr, 0, sizeof(struct sockaddr_storage)); + + if( copy_nonIP_fields( (struct sockaddr*)&new_addr, addr ) != 0 ) { + errno = EACCES; + rc = -1; + } + else if( get_public_ip( (struct sockaddr*)&new_addr ) != 0 ) { + rc = -1; + } + else { + // Un-comment the following line to activate the debug message + //debug( addr, (struct sockaddr*)&new_addr ); + rc = bind_original( sockfd, (struct sockaddr*)&new_addr, addrlen ); + } + } + else { + return bind_original( sockfd, addr, addrlen ); + } }