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.
5 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
15 func boolint(b
bool) int {
22 func ipv4AddrToInterface(ip IP
) (*Interface
, error
) {
23 ift
, err
:= Interfaces()
27 for _
, ifi
:= range ift
{
28 ifat
, err
:= ifi
.Addrs()
32 for _
, ifa
:= range ifat
{
33 switch v
:= ifa
.(type) {
45 if ip
.Equal(IPv4zero
) {
48 return nil, errNoSuchInterface
51 func interfaceToIPv4Addr(ifi
*Interface
) (IP
, error
) {
55 ifat
, err
:= ifi
.Addrs()
59 for _
, ifa
:= range ifat
{
60 switch v
:= ifa
.(type) {
62 if v
.IP
.To4() != nil {
66 if v
.IP
.To4() != nil {
71 return nil, errNoSuchInterface
74 func setIPv4MreqToInterface(mreq
*syscall
.IPMreq
, ifi
*Interface
) error
{
78 ifat
, err
:= ifi
.Addrs()
82 for _
, ifa
:= range ifat
{
83 switch v
:= ifa
.(type) {
85 if a
:= v
.IP
.To4(); a
!= nil {
86 copy(mreq
.Interface
[:], a
)
90 if a
:= v
.IP
.To4(); a
!= nil {
91 copy(mreq
.Interface
[:], a
)
97 if bytesEqual(mreq
.Multiaddr
[:], IPv4zero
.To4()) {
98 return errNoSuchMulticastInterface
103 func setReadBuffer(fd
*netFD
, bytes
int) error
{
104 if err
:= fd
.incref(); err
!= nil {
108 return os
.NewSyscallError("setsockopt", syscall
.SetsockoptInt(fd
.sysfd
, syscall
.SOL_SOCKET
, syscall
.SO_RCVBUF
, bytes
))
111 func setWriteBuffer(fd
*netFD
, bytes
int) error
{
112 if err
:= fd
.incref(); err
!= nil {
116 return os
.NewSyscallError("setsockopt", syscall
.SetsockoptInt(fd
.sysfd
, syscall
.SOL_SOCKET
, syscall
.SO_SNDBUF
, bytes
))
119 func setKeepAlive(fd
*netFD
, keepalive
bool) error
{
120 if err
:= fd
.incref(); err
!= nil {
124 return os
.NewSyscallError("setsockopt", syscall
.SetsockoptInt(fd
.sysfd
, syscall
.SOL_SOCKET
, syscall
.SO_KEEPALIVE
, boolint(keepalive
)))
127 func setLinger(fd
*netFD
, sec
int) error
{
131 l
.Linger
= int32(sec
)
136 if err
:= fd
.incref(); err
!= nil {
140 return os
.NewSyscallError("setsockopt", syscall
.SetsockoptLinger(fd
.sysfd
, syscall
.SOL_SOCKET
, syscall
.SO_LINGER
, &l
))