Imported Upstream version 4.7.2
[platform/upstream/gcc48.git] / libgo / go / net / sockopt_bsd.go
1 // Copyright 2011 The Go Authors.  All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build darwin freebsd netbsd openbsd
6
7 // Socket options for BSD variants
8
9 package net
10
11 import (
12         "os"
13         "syscall"
14 )
15
16 func setDefaultSockopts(s, f, t int, ipv6only bool) error {
17         switch f {
18         case syscall.AF_INET6:
19                 if ipv6only {
20                         syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 1)
21                 } else {
22                         // Allow both IP versions even if the OS default
23                         // is otherwise.  Note that some operating systems
24                         // never admit this option.
25                         syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
26                 }
27         }
28         // Allow broadcast.
29         err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
30         if err != nil {
31                 return os.NewSyscallError("setsockopt", err)
32         }
33         return nil
34 }
35
36 func setDefaultListenerSockopts(s int) error {
37         // Allow reuse of recently-used addresses.
38         err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
39         if err != nil {
40                 return os.NewSyscallError("setsockopt", err)
41         }
42         return nil
43 }
44
45 func setDefaultMulticastSockopts(s int) error {
46         // Allow multicast UDP and raw IP datagram sockets to listen
47         // concurrently across multiple listeners.
48         err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
49         if err != nil {
50                 return os.NewSyscallError("setsockopt", err)
51         }
52         // Allow reuse of recently-used ports.
53         // This option is supported only in descendants of 4.4BSD,
54         // to make an effective multicast application that requires
55         // quick draw possible.
56         if syscall.SO_REUSEPORT != 0 {
57                 err = syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
58                 if err != nil {
59                         return os.NewSyscallError("setsockopt", err)
60                 }
61         }
62         return nil
63 }