2012-01-25 21:56:26 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2011-05-20 02:18:15 +02:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
// +build darwin freebsd netbsd openbsd
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Sockets for BSD variants
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2012-01-25 21:56:26 +01:00
|
|
|
"runtime"
|
2011-05-20 02:18:15 +02:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func maxListenerBacklog() int {
|
|
|
|
var (
|
|
|
|
n uint32
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin", "freebsd":
|
|
|
|
n, err = syscall.SysctlUint32("kern.ipc.somaxconn")
|
|
|
|
case "netbsd":
|
|
|
|
// NOTE: NetBSD has no somaxconn-like kernel state so far
|
|
|
|
case "openbsd":
|
|
|
|
n, err = syscall.SysctlUint32("kern.somaxconn")
|
|
|
|
}
|
|
|
|
if n == 0 || err != nil {
|
|
|
|
return syscall.SOMAXCONN
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
return int(n)
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
|
|
|
|
func listenerSockaddr(s, f int, la syscall.Sockaddr, toAddr func(syscall.Sockaddr) Addr) (syscall.Sockaddr, error) {
|
|
|
|
a := toAddr(la)
|
|
|
|
if a == nil {
|
|
|
|
return la, nil
|
|
|
|
}
|
|
|
|
switch v := a.(type) {
|
2012-03-02 17:38:43 +01:00
|
|
|
case *TCPAddr, *UnixAddr:
|
|
|
|
err := setDefaultListenerSockopts(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
case *UDPAddr:
|
|
|
|
if v.IP.IsMulticast() {
|
|
|
|
err := setDefaultMulticastSockopts(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch f {
|
|
|
|
case syscall.AF_INET:
|
|
|
|
v.IP = IPv4zero
|
|
|
|
case syscall.AF_INET6:
|
|
|
|
v.IP = IPv6unspecified
|
|
|
|
}
|
|
|
|
return v.sockaddr(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return la, nil
|
|
|
|
}
|