From 1beb3867fb02671960ee5f1ab0101a1be125e62a Mon Sep 17 00:00:00 2001 From: "Christopher R. Hertel" Date: Sun, 26 Aug 2001 04:16:51 +0000 Subject: Fussing with debug lines in open_socket_in(). I cleaned up some slightly funky code that was simply setting a local int to 0 or 1 and also added calls to strerror() in some of the debug lines. The use of the dlevel parameter in this function is a little awkward. There should probably be some comments about it in the source. (This used to be commit 3031e7acdc4eac99a736f45161dee9bf81c1cc87) --- source3/lib/util_sock.c | 88 +++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 32 deletions(-) (limited to 'source3/lib') diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index 8f2eceabbc..363e775186 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -775,56 +775,80 @@ BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type) } /**************************************************************************** -open a socket of the specified type, port and address for incoming data +open a socket of the specified type, port, and address for incoming data ****************************************************************************/ -int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind) -{ +int open_socket_in( int type, int port, int dlevel, + uint32 socket_addr, BOOL rebind ) + { struct sockaddr_in sock; int res; - memset((char *)&sock,'\0',sizeof(sock)); + /* Clear the sockaddr_in structure (why not bzero()?). */ + memset( (char *)&sock, '\0', sizeof(sock) ); #ifdef HAVE_SOCK_SIN_LEN - sock.sin_len = sizeof(sock); + sock.sin_len = sizeof(sock); #endif - sock.sin_port = htons( port ); - sock.sin_family = AF_INET; + sock.sin_port = htons( port ); + sock.sin_family = AF_INET; sock.sin_addr.s_addr = socket_addr; - res = socket(AF_INET, type, 0); - if (res == -1) - { DEBUG(0,("socket failed\n")); return -1; } + res = socket( AF_INET, type, 0 ); + if( res < 0 ) + { + if( DEBUGLVL(0) ) + { + dbgtext( "open_socket_in(): socket() call failed: " ); + dbgtext( "%s\n", strerror( errno ) ); + } + return -1; + } + + /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */ { - int val=1; - if(rebind) - val=1; - else - val=0; - if(setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1) - DEBUG(dlevel,("setsockopt: SO_REUSEADDR=%d on port %d failed with error = %s\n", - val, port, strerror(errno) )); + int val = rebind ? 1 : 0; + if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1 ) + { + if( DEBUGLVL( dlevel ) ) + { + dbgtext( "open_socket_in(): setsockopt: " ); + dbgtext( "SO_REUSEADDR = %d ", val?"True":"False" ); + dbgtext( "on port %d failed ", port ); + dbgtext( "with error = %s\n", strerror(errno) ); + } + } #ifdef SO_REUSEPORT - if(setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1) - DEBUG(dlevel,("setsockopt: SO_REUSEPORT=%d on port %d failed with error = %s\n", - val, port, strerror(errno) )); + if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1 ) + { + if( DEBUGLVL( dlevel ) ) + { + dbgtext( "open_socket_in(): setsockopt: " + dbgtext( "SO_REUSEPORT = %d ", val?"True":"False" ); + dbgtext( "on port %d failed ", port ); + dbgtext( "with error = %s\n", strerror(errno) ); + } + } #endif /* SO_REUSEPORT */ } /* now we've got a socket - we need to bind it */ - if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) - { - if (port == SMB_PORT || port == NMB_PORT) - DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n", - port,inet_ntoa(sock.sin_addr),strerror(errno))); - close(res); - - return(-1); + if( bind( res, (struct sockaddr *)&sock, sizeof(sock) ) < 0 ) + { + if( DEBUGLVL(dlevel) && (port == SMB_PORT || port == NMB_PORT) ) + { + dbgtext( "bind failed on port %d ", port ); + dbgtext( "socket_addr = %s.\n", inet_ntoa( sock.sin_addr ) ); + dbgtext( "Error = %s\n", strerror(errno) ); + } + close( res ); + return( -1 ); } - DEBUG(3,("bind succeeded on port %d\n",port)); - return res; -} + DEBUG( 3, ( "bind succeeded on port %d\n", port ) ); + + return( res ); + } /**************************************************************************** create an outgoing socket. timeout is in milliseconds. -- cgit