Commit bc926713 authored by Mikio Hara's avatar Mikio Hara Committed by Russ Cox

net: enable SO_REUSEPORT on BSD variants

Fixes #1694.

R=golang-dev, rsc1, rsc
CC=golang-dev
https://golang.org/cl/4445067
parent 756df8e0
......@@ -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)),)
......
......@@ -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)
......
// 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)
}
}
// 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)
}
}
// 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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment