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

Golang

Source file src/pkg/net/sock_linux.go

     1	// Copyright 2009 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	// Sockets for Linux
     6	
     7	package net
     8	
     9	import "syscall"
    10	
    11	func maxListenerBacklog() int {
    12		fd, err := open("/proc/sys/net/core/somaxconn")
    13		if err != nil {
    14			return syscall.SOMAXCONN
    15		}
    16		defer fd.close()
    17		l, ok := fd.readLine()
    18		if !ok {
    19			return syscall.SOMAXCONN
    20		}
    21		f := getFields(l)
    22		n, _, ok := dtoi(f[0], 0)
    23		if n == 0 || !ok {
    24			return syscall.SOMAXCONN
    25		}
    26		return n
    27	}
    28	
    29	func listenerSockaddr(s, f int, la syscall.Sockaddr, toAddr func(syscall.Sockaddr) Addr) (syscall.Sockaddr, error) {
    30		a := toAddr(la)
    31		if a == nil {
    32			return la, nil
    33		}
    34		switch v := a.(type) {
    35		case *TCPAddr, *UnixAddr:
    36			err := setDefaultListenerSockopts(s)
    37			if err != nil {
    38				return nil, err
    39			}
    40		case *UDPAddr:
    41			if v.IP.IsMulticast() {
    42				err := setDefaultMulticastSockopts(s)
    43				if err != nil {
    44					return nil, err
    45				}
    46				switch f {
    47				case syscall.AF_INET:
    48					v.IP = IPv4zero
    49				case syscall.AF_INET6:
    50					v.IP = IPv6unspecified
    51				}
    52				return v.sockaddr(f)
    53			}
    54		}
    55		return la, nil
    56	}