1
0
mirror of https://github.com/golang/go synced 2024-11-21 15:44:44 -07:00

net: enable SO_REUSEPORT on BSD variants

Fixes #1694.

R=golang-dev, rsc1, rsc
CC=golang-dev
https://golang.org/cl/4445067
This commit is contained in:
Mikio Hara 2011-05-02 10:50:12 -04:00 committed by Russ Cox
parent 756df8e081
commit bc926713c7
5 changed files with 86 additions and 11 deletions

View 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)),)

View 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)

31
src/pkg/net/sock_bsd.go Normal file
View File

@ -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)
}
}

25
src/pkg/net/sock_linux.go Normal file
View File

@ -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)
}
}

View File

@ -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)
}
}