REORG: sock_inet: move v6only_default from proto_tcp.c to sock_inet.c

The v6only_default variable is not specific to TCP but to AF_INET6, so
let's move it to the right file. It's now immediately filled on startup
during the PREPARE stage so that it doesn't have to be tested each time.
The variable's name was changed to sock_inet6_v6only_default.
diff --git a/src/sock_inet.c b/src/sock_inet.c
index db1a8c0..c5b80da 100644
--- a/src/sock_inet.c
+++ b/src/sock_inet.c
@@ -35,6 +35,10 @@
  * mentioned in the comment before the function definition.
  */
 
+/* determine if the operating system uses IPV6_V6ONLY by default. 0=no, 1=yes.
+ * It also remains if IPv6 is not enabled/configured.
+ */
+int sock_inet6_v6only_default = 0;
 
 /* Compares two AF_INET sockaddr addresses. Returns 0 if they match or non-zero
  * if they do not match.
@@ -165,3 +169,22 @@
 	}
 	return 0;
 }
+
+static void sock_inet_prepare()
+{
+	int fd, val;
+	socklen_t len;
+
+	fd = socket(AF_INET6, SOCK_STREAM, 0);
+	if (fd >= 0) {
+#if defined(IPV6_V6ONLY)
+		/* retrieve the OS' bindv6only value */
+		len = sizeof(val);
+		if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) == 0 && val > 0)
+			sock_inet6_v6only_default = 1;
+#endif
+		close(fd);
+	}
+}
+
+INITCALL0(STG_PREPARE, sock_inet_prepare);