summaryrefslogtreecommitdiff
path: root/src/lib/net/net.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-04-30 13:41:36 -0700
committerRuss Cox <rsc@golang.org>2009-04-30 13:41:36 -0700
commit98cb911d731331479b88a0012d8daec3e725e039 (patch)
tree4d5a3686dc1ad379465e74f06ab59400eec1c4d7 /src/lib/net/net.go
parentf124b7083180534fabe45ab16c4f40673451d5a9 (diff)
downloadgolang-98cb911d731331479b88a0012d8daec3e725e039.tar.gz
auto-detect whether to use IPv6 or IPv4 kernel interface
R=r DELTA=12 (9 added, 0 deleted, 3 changed) OCL=28096 CL=28118
Diffstat (limited to 'src/lib/net/net.go')
-rw-r--r--src/lib/net/net.go15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/lib/net/net.go b/src/lib/net/net.go
index 737af520b..63074470b 100644
--- a/src/lib/net/net.go
+++ b/src/lib/net/net.go
@@ -300,9 +300,18 @@ func (c *connBase) SetLinger(sec int) os.Error {
// only dealing with IPv4 sockets? As long as the host system
// understands IPv6, it's okay to pass IPv4 addresses to the IPv6
// interface. That simplifies our code and is most general.
-// If we need to build on a system without IPv6 support, setting
-// preferIPv4 here should fall back to the IPv4 socket interface when possible.
-const preferIPv4 = false
+// Unfortunately, we need to run on kernels built without IPv6 support too.
+// So probe the kernel to figure it out.
+func kernelSupportsIPv6() bool {
+ fd, e := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP);
+ if fd >= 0 {
+ syscall.Close(fd)
+ }
+ return e == 0
+}
+
+var preferIPv4 = !kernelSupportsIPv6()
+
func internetSocket(net, laddr, raddr string, proto int64, mode string)
(fd *netFD, err os.Error)