diff -ruB --exclude=config* --exclude=Makefile* --exclude=libtool --exclude=makebdb.c --exclude=perditiondb_bdb.h --exclude=perditiondb_ldap.c libvanessa_socket/vanessa_socket.h libvanessa_socket-ipv6libvanessa_socket/vanessa_socket.h --- libvanessa_socket/vanessa_socket.h Mon Mar 22 16:10:06 2004 +++ libvanessa_socket-ipv6libvanessa_socket/vanessa_socket.h Tue Jun 5 17:58:56 2007 @@ -47,6 +47,10 @@ #include +#ifndef SOMAXCONN +#define SOMAXCONN 5 +#endif + /* Needed for Solaris */ #ifdef sun #define timeradd(a, b, result) \ @@ -543,10 +547,10 @@ * maximum_connections: maximum number of active connections * to handle. If 0 then an number of connections * is unlimited. - * return_from: pointer to a struct_in addr where the + * return_from: pointer to a struct sockaddr where the * connecting client's IP address will * be placed. Ignored if NULL - * return_to: pointer to a in addr where the IP address the + * return_to: pointer to a sockaddr where the IP address the * server accepted the connection on will be placed. * Ignored if NULL * flag: If VANESSA_SOCKET_NO_FORK then the process does not for @@ -561,8 +565,10 @@ int vanessa_socket_server_accept(int listen_socket, const unsigned int maximum_connections, - struct sockaddr_in *return_from, - struct sockaddr_in *return_to, + struct sockaddr *return_from, + socklen_t *return_from_salen, + struct sockaddr *return_to, + socklen_t *return_to_salen, vanessa_socket_flag_t flag); diff -ruB --exclude=config* --exclude=Makefile* --exclude=libtool --exclude=makebdb.c --exclude=perditiondb_bdb.h --exclude=perditiondb_ldap.c libvanessa_socket/vanessa_socket_client.c libvanessa_socket-ipv6libvanessa_socket/vanessa_socket_client.c --- libvanessa_socket/vanessa_socket_client.c Mon Mar 22 16:10:04 2004 +++ libvanessa_socket-ipv6libvanessa_socket/vanessa_socket_client.c Tue Jun 5 17:58:56 2007 @@ -179,40 +179,68 @@ const char *dst_port, const vanessa_socket_flag_t flag) { - int s; - struct sockaddr_in to; - struct sockaddr_in from; + int out; + struct addrinfo hints, *tores, *fromres, *fromai; - /* Fill in port information for 'from' */ - memset((struct sockaddr *) &from, 0, sizeof(from)); - if (!(flag & VANESSA_SOCKET_NO_FROM)) { - if (vanessa_socket_host_port_sockaddr_in - (src_host, src_port, &from, flag) < 0) { - VANESSA_LOGGER_DEBUG - ("vanessa_socket_host_port_sockaddr_in from"); + /* Get addrinfo for 'from' */ + if ( (flag & VANESSA_SOCKET_NO_FROM) || + ( src_host == NULL && src_port == NULL ) ) { + fromres == NULL; + } + else { + bzero( &hints, sizeof hints ); + hints.ai_flags = AI_PASSIVE; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + if ( getaddrinfo( src_host, src_port, &hints, &fromres ) + != 0 ) { + VANESSA_LOGGER_DEBUG("getaddrinfo from"); return (-1); } } - - /* Fill in port information for 'to' */ - memset((struct sockaddr *) &to, 0, sizeof(to)); - if (vanessa_socket_host_port_sockaddr_in - (dst_host, dst_port, &to, flag) < 0) { - VANESSA_LOGGER_DEBUG - ("vanessa_socket_host_port_sockaddr_in to"); + + /* Get addrinfo for 'to' */ + bzero( &hints, sizeof hints ); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + if ( getaddrinfo( dst_host, dst_port, &hints, &tores ) != 0 ) { + VANESSA_LOGGER_DEBUG("getaddrinfo to"); return (-1); } - /* Set up connection */ - if ((s = - vanessa_socket_client_open_src_sockaddr_in(from, to, - flag)) < 0) { - VANESSA_LOGGER_DEBUG - ("vanessa_socket_client_open_sockaddr_in"); - return (-1); + /* Try all combinations of 'to' and 'from' until we get a + connection. */ + do { + /* Create socket */ + if ( (out = socket( tores->ai_family, tores->ai_socktype, + tores->ai_protocol )) < 0) { + VANESSA_LOGGER_DEBUG_ERRNO("socket"); + continue; } - return (s); + /* Run through this loop at least once if there is no 'from' */ + fromai = fromres; + do { + if ( fromai != NULL ) { + /* Bind 'from' to socket */ + if ( bind( out, fromai->ai_addr, + fromai->ai_addrlen ) < 0 ) { + VANESSA_LOGGER_DEBUG_ERRNO("bind"); + continue; + } + } + /* Connect to foreign 'to' server */ + if ( connect( out, tores->ai_addr, + tores->ai_addrlen ) == 0 ) + return (out); + VANESSA_LOGGER_DEBUG_ERRNO("connect"); + if ( fromai == NULL ) break; + } while ( (fromai = fromai->ai_next) != NULL ); + + } while ( (tores = tores->ai_next) != NULL ); + + VANESSA_LOGGER_DEBUG("vanessa_socket_client_src_open"); + return (-1); } diff -ruB --exclude=config* --exclude=Makefile* --exclude=libtool --exclude=makebdb.c --exclude=perditiondb_bdb.h --exclude=perditiondb_ldap.c libvanessa_socket/vanessa_socket_server.c libvanessa_socket-ipv6libvanessa_socket/vanessa_socket_server.c --- libvanessa_socket/vanessa_socket_server.c Mon Mar 22 16:10:25 2004 +++ libvanessa_socket-ipv6libvanessa_socket/vanessa_socket_server.c Tue Jun 5 17:58:56 2007 @@ -49,24 +49,60 @@ const char *interface_address, vanessa_socket_flag_t flag) { - int s; - struct sockaddr_in from; + int s, g; + struct addrinfo hints, *res; - /* Fill in informtaion for 'from' */ - if (vanessa_socket_host_port_sockaddr_in(interface_address, - port, &from, flag) < 0) { - VANESSA_LOGGER_DEBUG("vanessa_socket_host_port_sockaddr_in"); + /* Get addrinfo for 'from' */ + bzero( &hints, sizeof hints ); + hints.ai_flags = AI_PASSIVE; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + if ( getaddrinfo( interface_address, port, &hints, &res ) != 0 ) { + VANESSA_LOGGER_DEBUG("getaddrinfo"); return (-1); } - /* Make the connection */ - s = vanessa_socket_server_bind_sockaddr_in(from, flag); - if (s < 0) { - VANESSA_LOGGER_DEBUG("vanessa_socket_server_bind"); - return (-1); - } + /* Loop through all returned addrinfo until we successfully listen */ + do { + if ( (s = socket( res->ai_family, res->ai_socktype, + res->ai_protocol )) < 0 ) { + VANESSA_LOGGER_DEBUG_ERRNO("socket"); + continue; + } + /* + * Set SO_REUSEADDR on the server socket s. Variable g is used + * as a scratch varable. + */ + g = 1; + if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &g, + sizeof g) < 0 ) { + VANESSA_LOGGER_DEBUG_ERRNO("setsockopt"); + close(s); + continue; + } +#ifdef SO_BINDANY + g = 1; + if ( setsockopt( s, SOL_SOCKET, SO_BINDANY, (void *) &g, + sizeof g ) < 0 ) { + VANESSA_LOGGER_DEBUG_ERRNO("setsockopt"); + close(s); + continue; + } +#endif + if ( bind( s, res->ai_addr, res->ai_addrlen ) < 0 ) { + VANESSA_LOGGER_DEBUG_ERRNO("bind"); + close(s); + continue; + } + if ( listen( s, SOMAXCONN ) == 0 ) { + return (s); + } + VANESSA_LOGGER_DEBUG_ERRNO("listen"); + close(s); + } while ( (res = res->ai_next) != NULL ); - return (s); + VANESSA_LOGGER_DEBUG("vanessa_socket_server_bind"); + return (-1); } @@ -149,10 +185,10 @@ * to handle. If 0 then an number of connections * is unlimited. * Not used if flat is VANESSA_SOCKET_NO_FORK - * return_from: pointer to a struct_in addr where the + * return_from: pointer to a struct sockaddr where the * connecting client's IP address will * be placed. Ignored if NULL - * return_to: pointer to a in addr where the IP address the + * return_to: pointer to a sockaddr where the IP address the * server accepted the connection on will be placed. * Ignored if NULL * flag: If VANESSA_SOCKET_NO_FORK then the process does not for @@ -167,13 +203,15 @@ int vanessa_socket_server_accept(int listen_socket, const unsigned int maximum_connections, - struct sockaddr_in *return_from, - struct sockaddr_in *return_to, + struct sockaddr *return_from, + socklen_t *return_from_salen, + struct sockaddr *return_to, + socklen_t *return_to_salen, vanessa_socket_flag_t flag) { int g; int addrlen; - struct sockaddr_in from; + struct sockaddr_storage from; extern unsigned int noconnection; @@ -215,9 +253,9 @@ } if (return_to) { - addrlen = sizeof(struct sockaddr_in); - if (getsockname (g, (struct sockaddr *) return_to, - &addrlen) < 0) { + /* 'from' and 'return_to' are in the same address family + so the sockaddr lengths should be identical. */ + if (getsockname (g, return_to, return_to_salen) < 0) { VANESSA_LOGGER_DEBUG_ERRNO ("warning: getsockname"); /* This is usually (always) a transient error so * close the connection and soldier on */ @@ -226,7 +264,8 @@ } if (return_from) { - memcpy(return_from, &from, sizeof(from)); + memcpy(return_from, &from, addrlen); + *return_from_salen = addrlen; } @@ -336,6 +375,7 @@ { int s; int g; + socklen_t return_from_salen, return_to_salen; s = vanessa_socket_server_bind_sockaddr_in(from, flag); if(s < 0) { @@ -344,7 +384,11 @@ } g = vanessa_socket_server_accept(s, maximum_connections, - return_from, return_to, 0); + (struct sockaddr *) return_from, + &return_from_salen, + (struct sockaddr *) return_to, + &return_to_salen, + 0); if(g < 0) { VANESSA_LOGGER_DEBUG("vanessa_socket_server_accept"); return(-1);