mirror of
https://github.com/golang/go
synced 2024-11-21 21:14:47 -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:
parent
756df8e081
commit
bc926713c7
@ -29,6 +29,7 @@ GOFILES_freebsd=\
|
|||||||
dnsconfig.go\
|
dnsconfig.go\
|
||||||
dnsclient.go\
|
dnsclient.go\
|
||||||
port.go\
|
port.go\
|
||||||
|
sock_bsd.go\
|
||||||
|
|
||||||
CGOFILES_freebsd=\
|
CGOFILES_freebsd=\
|
||||||
cgo_bsd.go\
|
cgo_bsd.go\
|
||||||
@ -41,6 +42,7 @@ GOFILES_darwin=\
|
|||||||
dnsconfig.go\
|
dnsconfig.go\
|
||||||
dnsclient.go\
|
dnsclient.go\
|
||||||
port.go\
|
port.go\
|
||||||
|
sock_bsd.go\
|
||||||
|
|
||||||
CGOFILES_darwin=\
|
CGOFILES_darwin=\
|
||||||
cgo_bsd.go\
|
cgo_bsd.go\
|
||||||
@ -53,6 +55,7 @@ GOFILES_linux=\
|
|||||||
dnsconfig.go\
|
dnsconfig.go\
|
||||||
dnsclient.go\
|
dnsclient.go\
|
||||||
port.go\
|
port.go\
|
||||||
|
sock_linux.go\
|
||||||
|
|
||||||
ifeq ($(GOARCH),arm)
|
ifeq ($(GOARCH),arm)
|
||||||
# ARM has no cgo, so use the stubs.
|
# ARM has no cgo, so use the stubs.
|
||||||
@ -67,6 +70,7 @@ GOFILES_windows=\
|
|||||||
cgo_stub.go\
|
cgo_stub.go\
|
||||||
resolv_windows.go\
|
resolv_windows.go\
|
||||||
file_windows.go\
|
file_windows.go\
|
||||||
|
sock_windows.go\
|
||||||
|
|
||||||
GOFILES+=$(GOFILES_$(GOOS))
|
GOFILES+=$(GOFILES_$(GOOS))
|
||||||
ifneq ($(CGOFILES_$(GOOS)),)
|
ifneq ($(CGOFILES_$(GOOS)),)
|
||||||
|
@ -32,17 +32,7 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal
|
|||||||
syscall.CloseOnExec(s)
|
syscall.CloseOnExec(s)
|
||||||
syscall.ForkLock.RUnlock()
|
syscall.ForkLock.RUnlock()
|
||||||
|
|
||||||
// Allow reuse of recently-used addresses.
|
setKernelSpecificSockopt(s, f)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
if la != nil {
|
if la != nil {
|
||||||
e = syscall.Bind(s, la)
|
e = syscall.Bind(s, la)
|
||||||
|
31
src/pkg/net/sock_bsd.go
Normal file
31
src/pkg/net/sock_bsd.go
Normal 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
25
src/pkg/net/sock_linux.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
25
src/pkg/net/sock_windows.go
Normal file
25
src/pkg/net/sock_windows.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user