changeset 333:70384e14ba9e

Fixed problem in Linux when ipv6 bind also ipv4 address
author Sebastien Decugis <sdecugis@nict.go.jp>
date Tue, 31 Mar 2009 15:59:00 +0900
parents 436975f6ef4d
children 9a0a3220ad9f
files waaad/peer-tcp.c
diffstat 1 files changed, 19 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/waaad/peer-tcp.c	Tue Mar 31 15:58:27 2009 +0900
+++ b/waaad/peer-tcp.c	Tue Mar 31 15:59:00 2009 +0900
@@ -43,9 +43,10 @@
 #include "peer-internal.h"
 
 #include <netinet/tcp.h>
+#include <netinet/ip6.h>
 
-/* Set the socket options for TCP sockets */
-static int _tcp_setsockopt(int sk)
+/* Set the socket options for TCP sockets, before bind is called */
+static int _tcp_setsockopt(int family, int sk)
 {
 	int ret = 0;
 	int opt;
@@ -76,12 +77,25 @@
 	    - yeah
 	*/
 	
+	/* In case of v6 address, force the v6only option, we use a different socket for v4 */
+	#ifdef IPV6_V6ONLY
+	if (family == AF_INET6) {
+		opt = 1;
+		ret = setsockopt(sk, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
+		if (ret != 0) {
+			ret = errno;
+			TRACE_DEBUG(INFO, "Unable to set the socket IPV6_V6ONLY option: %s", strerror(ret));
+			return ret;
+		}
+	}
+	#endif /* IPV6_V6ONLY */
+	
+	
 	/* There are also others sockopt that can be set, but nothing useful for us AFAICT */
 	
 	return 0;
 }
 
-
 /*
  * FUNCTION:	_peer_tcp_create_server
  *
@@ -107,7 +121,7 @@
 	CHECK_SYS(  *sock = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP)  );
 
 	/* Set the socket options */
-	CHECK_FCT(  _tcp_setsockopt(*sock)  );
+	CHECK_FCT(  _tcp_setsockopt(addr->sa_family, *sock)  );
 	
 	/* Bind the socket */
 	CHECK_SYS(  bind( *sock, addr, salen )  );
@@ -144,7 +158,7 @@
 	CHECK_SYS(  *sock = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP)  );
 	
 	/* Set the socket options */
-	CHECK_FCT(  _tcp_setsockopt(*sock)  );
+	CHECK_FCT(  _tcp_setsockopt(addr->sa_family, *sock)  );
 	
 	sSA_DUMP( FULL, "Attempting TCP connection with peer: ", addr );
 	
"Welcome to our mercurial repository"