sockopt_windows.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. package net
  5. import (
  6. "os"
  7. "syscall"
  8. )
  9. func setDefaultSockopts(s syscall.Handle, family, sotype int, ipv6only bool) error {
  10. if family == syscall.AF_INET6 && sotype != syscall.SOCK_RAW {
  11. // Allow both IP versions even if the OS default
  12. // is otherwise. Note that some operating systems
  13. // never admit this option.
  14. syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only))
  15. }
  16. if (sotype == syscall.SOCK_DGRAM || sotype == syscall.SOCK_RAW) && family != syscall.AF_UNIX && family != syscall.AF_INET6 {
  17. // Allow broadcast.
  18. return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1))
  19. }
  20. return nil
  21. }
  22. func setDefaultListenerSockopts(s syscall.Handle) error {
  23. // Windows will reuse recently-used addresses by default.
  24. // SO_REUSEADDR should not be used here, as it allows
  25. // a socket to forcibly bind to a port in use by another socket.
  26. // This could lead to a non-deterministic behavior, where
  27. // connection requests over the port cannot be guaranteed
  28. // to be handled by the correct socket.
  29. return nil
  30. }
  31. func setDefaultMulticastSockopts(s syscall.Handle) error {
  32. // Allow multicast UDP and raw IP datagram sockets to listen
  33. // concurrently across multiple listeners.
  34. return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
  35. }