sockopt_linux.go 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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, 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. // Allow broadcast.
  17. return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1))
  18. }
  19. func setDefaultListenerSockopts(s int) error {
  20. // Allow reuse of recently-used addresses.
  21. return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
  22. }
  23. func setDefaultMulticastSockopts(s int) error {
  24. // Allow multicast UDP and raw IP datagram sockets to listen
  25. // concurrently across multiple listeners.
  26. return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
  27. }