// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris windows

package net

import (
	
	
	
)

// Boolean to int.
func boolint( bool) int {
	if  {
		return 1
	}
	return 0
}

func ipv4AddrToInterface( IP) (*Interface, error) {
	,  := Interfaces()
	if  != nil {
		return nil, 
	}
	for ,  := range  {
		,  := .Addrs()
		if  != nil {
			return nil, 
		}
		for ,  := range  {
			switch v := .(type) {
			case *IPAddr:
				if .Equal(.IP) {
					return &, nil
				}
			case *IPNet:
				if .Equal(.IP) {
					return &, nil
				}
			}
		}
	}
	if .Equal(IPv4zero) {
		return nil, nil
	}
	return nil, errNoSuchInterface
}

func interfaceToIPv4Addr( *Interface) (IP, error) {
	if  == nil {
		return IPv4zero, nil
	}
	,  := .Addrs()
	if  != nil {
		return nil, 
	}
	for ,  := range  {
		switch v := .(type) {
		case *IPAddr:
			if .IP.To4() != nil {
				return .IP, nil
			}
		case *IPNet:
			if .IP.To4() != nil {
				return .IP, nil
			}
		}
	}
	return nil, errNoSuchInterface
}

func setIPv4MreqToInterface( *syscall.IPMreq,  *Interface) error {
	if  == nil {
		return nil
	}
	,  := .Addrs()
	if  != nil {
		return 
	}
	for ,  := range  {
		switch v := .(type) {
		case *IPAddr:
			if  := .IP.To4();  != nil {
				copy(.Interface[:], )
				goto 
			}
		case *IPNet:
			if  := .IP.To4();  != nil {
				copy(.Interface[:], )
				goto 
			}
		}
	}
:
	if bytealg.Equal(.Multiaddr[:], IPv4zero.To4()) {
		return errNoSuchMulticastInterface
	}
	return nil
}

func setReadBuffer( *netFD,  int) error {
	 := .pfd.SetsockoptInt(syscall.SOL_SOCKET, syscall.SO_RCVBUF, )
	runtime.KeepAlive()
	return wrapSyscallError("setsockopt", )
}

func setWriteBuffer( *netFD,  int) error {
	 := .pfd.SetsockoptInt(syscall.SOL_SOCKET, syscall.SO_SNDBUF, )
	runtime.KeepAlive()
	return wrapSyscallError("setsockopt", )
}

func setKeepAlive( *netFD,  bool) error {
	 := .pfd.SetsockoptInt(syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint())
	runtime.KeepAlive()
	return wrapSyscallError("setsockopt", )
}

func setLinger( *netFD,  int) error {
	var  syscall.Linger
	if  >= 0 {
		.Onoff = 1
		.Linger = int32()
	} else {
		.Onoff = 0
		.Linger = 0
	}
	 := .pfd.SetsockoptLinger(syscall.SOL_SOCKET, syscall.SO_LINGER, &)
	runtime.KeepAlive()
	return wrapSyscallError("setsockopt", )
}