2011-05-02 08:50:12 -06:00
|
|
|
// 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.
|
|
|
|
|
2011-12-21 03:44:47 -07:00
|
|
|
// +build darwin freebsd netbsd openbsd
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2012-01-10 17:53:32 -07:00
|
|
|
// Socket options for BSD variants
|
2011-05-02 08:50:12 -06:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2012-01-31 09:53:26 -07:00
|
|
|
"os"
|
2011-05-02 08:50:12 -06:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2012-03-05 08:13:10 -07:00
|
|
|
func setDefaultSockopts(s, f, t int, ipv6only bool) error {
|
2012-01-14 22:19:44 -07:00
|
|
|
switch f {
|
|
|
|
case syscall.AF_INET6:
|
2012-03-05 08:13:10 -07:00
|
|
|
if ipv6only {
|
|
|
|
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 1)
|
|
|
|
} else {
|
|
|
|
// Allow both IP versions even if the OS default
|
|
|
|
// is otherwise. Note that some operating systems
|
|
|
|
// never admit this option.
|
|
|
|
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
|
|
|
|
}
|
2012-01-14 22:19:44 -07:00
|
|
|
}
|
2011-05-02 08:50:12 -06:00
|
|
|
// Allow broadcast.
|
2012-01-31 09:53:26 -07:00
|
|
|
err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
|
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("setsockopt", err)
|
|
|
|
}
|
2012-02-10 19:50:51 -07:00
|
|
|
return nil
|
|
|
|
}
|
2012-01-31 09:53:26 -07:00
|
|
|
|
2012-02-10 19:50:51 -07:00
|
|
|
func setDefaultListenerSockopts(s int) error {
|
|
|
|
// Allow reuse of recently-used addresses.
|
|
|
|
err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("setsockopt", err)
|
|
|
|
}
|
2012-01-31 09:53:26 -07:00
|
|
|
return nil
|
2012-01-14 22:19:44 -07:00
|
|
|
}
|
2011-05-02 08:50:12 -06:00
|
|
|
|
2012-01-31 09:53:26 -07:00
|
|
|
func setDefaultMulticastSockopts(s int) error {
|
2012-01-14 22:19:44 -07:00
|
|
|
// Allow multicast UDP and raw IP datagram sockets to listen
|
|
|
|
// concurrently across multiple listeners.
|
2012-01-31 09:53:26 -07:00
|
|
|
err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("setsockopt", err)
|
|
|
|
}
|
2012-02-12 20:45:59 -07:00
|
|
|
// Allow reuse of recently-used ports.
|
|
|
|
// This option is supported only in descendants of 4.4BSD,
|
|
|
|
// to make an effective multicast application that requires
|
|
|
|
// quick draw possible.
|
2012-01-31 09:53:26 -07:00
|
|
|
err = syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
|
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("setsockopt", err)
|
|
|
|
}
|
|
|
|
return nil
|
2011-05-02 08:50:12 -06:00
|
|
|
}
|