Imported Upstream version 4.7.2
[platform/upstream/gcc48.git] / libgo / go / net / sockopt_windows.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 // Socket options for Windows
6
7 package net
8
9 import (
10         "os"
11         "syscall"
12 )
13
14 func setDefaultSockopts(s syscall.Handle, f, t int, ipv6only bool) error {
15         switch f {
16         case syscall.AF_INET6:
17                 if ipv6only {
18                         syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 1)
19                 } else {
20                         // Allow both IP versions even if the OS default
21                         // is otherwise.  Note that some operating systems
22                         // never admit this option.
23                         syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
24                 }
25         }
26         // Allow broadcast.
27         syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
28         return nil
29 }
30
31 func setDefaultListenerSockopts(s syscall.Handle) error {
32         // Windows will reuse recently-used addresses by default.
33         // SO_REUSEADDR should not be used here, as it allows
34         // a socket to forcibly bind to a port in use by another socket.
35         // This could lead to a non-deterministic behavior, where
36         // connection requests over the port cannot be guaranteed
37         // to be handled by the correct socket.
38         return nil
39 }
40
41 func setDefaultMulticastSockopts(s syscall.Handle) error {
42         // Allow multicast UDP and raw IP datagram sockets to listen
43         // concurrently across multiple listeners.
44         err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
45         if err != nil {
46                 return os.NewSyscallError("setsockopt", err)
47         }
48         return nil
49 }