src/pkg/net/sockopt_linux.go - The Go Programming Language

Golang

Source file src/pkg/net/sockopt_linux.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 Linux
     6	
     7	package net
     8	
     9	import (
    10		"os"
    11		"syscall"
    12	)
    13	
    14	func setDefaultSockopts(s, 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		err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
    28		if err != nil {
    29			return os.NewSyscallError("setsockopt", err)
    30		}
    31		return nil
    32	}
    33	
    34	func setDefaultListenerSockopts(s int) error {
    35		// Allow reuse of recently-used addresses.
    36		err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
    37		if err != nil {
    38			return os.NewSyscallError("setsockopt", err)
    39		}
    40		return nil
    41	}
    42	
    43	func setDefaultMulticastSockopts(s int) error {
    44		// Allow multicast UDP and raw IP datagram sockets to listen
    45		// concurrently across multiple listeners.
    46		err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
    47		if err != nil {
    48			return os.NewSyscallError("setsockopt", err)
    49		}
    50		return nil
    51	}