]> Cypherpunks repositories - gostls13.git/commitdiff
net: enable SO_REUSEPORT on BSD variants
authorMikio Hara <mikioh.mikioh@gmail.com>
Mon, 2 May 2011 14:50:12 +0000 (10:50 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 2 May 2011 14:50:12 +0000 (10:50 -0400)
Fixes #1694.

R=golang-dev, rsc1, rsc
CC=golang-dev
https://golang.org/cl/4445067

src/pkg/net/Makefile
src/pkg/net/sock.go
src/pkg/net/sock_bsd.go [new file with mode: 0644]
src/pkg/net/sock_linux.go [new file with mode: 0644]
src/pkg/net/sock_windows.go [new file with mode: 0644]

index 221871cb179781764c50ea33aec2457b411b4718..376e9c6dc913b371a70d92abb630a13eb775850c 100644 (file)
@@ -29,6 +29,7 @@ GOFILES_freebsd=\
        dnsconfig.go\
        dnsclient.go\
        port.go\
+       sock_bsd.go\
 
 CGOFILES_freebsd=\
        cgo_bsd.go\
@@ -41,6 +42,7 @@ GOFILES_darwin=\
        dnsconfig.go\
        dnsclient.go\
        port.go\
+       sock_bsd.go\
 
 CGOFILES_darwin=\
        cgo_bsd.go\
@@ -53,6 +55,7 @@ GOFILES_linux=\
        dnsconfig.go\
        dnsclient.go\
        port.go\
+       sock_linux.go\
 
 ifeq ($(GOARCH),arm)
 # ARM has no cgo, so use the stubs.
@@ -67,6 +70,7 @@ GOFILES_windows=\
        cgo_stub.go\
        resolv_windows.go\
        file_windows.go\
+       sock_windows.go\
 
 GOFILES+=$(GOFILES_$(GOOS))
 ifneq ($(CGOFILES_$(GOOS)),)
index bd88f7ece79f4b9a4d4d6d10653e789010fd60c4..21bd5f03e89c6bfd30b27c2df3ef2d3a38b54db7 100644 (file)
@@ -32,17 +32,7 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal
        syscall.CloseOnExec(s)
        syscall.ForkLock.RUnlock()
 
-       // Allow reuse of recently-used addresses.
-       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
-
-       // Allow broadcast.
-       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
-
-       if f == syscall.AF_INET6 {
-               // using ip, tcp, udp, etc.
-               // allow both protocols even if the OS default is otherwise.
-               syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
-       }
+       setKernelSpecificSockopt(s, f)
 
        if la != nil {
                e = syscall.Bind(s, la)
diff --git a/src/pkg/net/sock_bsd.go b/src/pkg/net/sock_bsd.go
new file mode 100644 (file)
index 0000000..5fd5207
--- /dev/null
@@ -0,0 +1,31 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Sockets for BSD variants
+
+package net
+
+import (
+       "syscall"
+)
+
+func setKernelSpecificSockopt(s, f int) {
+       // Allow reuse of recently-used addresses.
+       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
+
+       // Allow reuse of recently-used ports.
+       // This option is supported only in descendants of 4.4BSD,
+       // to make an effective multicast application and an application
+       // that requires quick draw possible.
+       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
+
+       // Allow broadcast.
+       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
+
+       if f == syscall.AF_INET6 {
+               // using ip, tcp, udp, etc.
+               // allow both protocols even if the OS default is otherwise.
+               syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
+       }
+}
diff --git a/src/pkg/net/sock_linux.go b/src/pkg/net/sock_linux.go
new file mode 100644 (file)
index 0000000..ec31e80
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Sockets for Linux
+
+package net
+
+import (
+       "syscall"
+)
+
+func setKernelSpecificSockopt(s, f int) {
+       // Allow reuse of recently-used addresses.
+       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
+
+       // Allow broadcast.
+       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
+
+       if f == syscall.AF_INET6 {
+               // using ip, tcp, udp, etc.
+               // allow both protocols even if the OS default is otherwise.
+               syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
+       }
+}
diff --git a/src/pkg/net/sock_windows.go b/src/pkg/net/sock_windows.go
new file mode 100644 (file)
index 0000000..e17c60b
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Sockets for Windows
+
+package net
+
+import (
+       "syscall"
+)
+
+func setKernelSpecificSockopt(s, f int) {
+       // Allow reuse of recently-used addresses and ports.
+       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
+
+       // Allow broadcast.
+       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
+
+       if f == syscall.AF_INET6 {
+               // using ip, tcp, udp, etc.
+               // allow both protocols even if the OS default is otherwise.
+               syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
+       }
+}