// Code generated by cmd/cgo; DO NOT EDIT.

//line /home/abuild/rpmbuild/BUILD/go/src/net/cgo_unix.go:1:1
// Copyright 2011 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 cgo,!netgo
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package net

/*
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
*/
import _ "unsafe"

import (
	"context"
	"syscall"
	"unsafe"
)

// An addrinfoErrno represents a getaddrinfo, getnameinfo-specific
// error number. It's a signed number and a zero value is a non-error
// by convention.
type addrinfoErrno int

func (eai addrinfoErrno) Error() string   { return ( /*line :31:52*/_Cfunc_GoString /*line :31:61*/)(( /*line :31:63*/_Cfunc_gai_strerror /*line :31:76*/)( /*line :31:78*/_Ctype_int /*line :31:83*/(eai))) }
func (eai addrinfoErrno) Temporary() bool { return eai == ( /*line :32:59*/_Ciconst_EAI_AGAIN /*line :32:69*/) }
func (eai addrinfoErrno) Timeout() bool   { return false }

type portLookupResult struct {
	port int
	err  error
}

type ipLookupResult struct {
	addrs []IPAddr
	cname string
	err   error
}

type reverseLookupResult struct {
	names []string
	err   error
}

func cgoLookupHost(ctx context.Context, name string) (hosts []string, err error, completed bool) {
	addrs, err, completed := cgoLookupIP(ctx, "ip", name)
	for _, addr := range addrs {
		hosts = append(hosts, addr.String())
	}
	return
}

func cgoLookupPort(ctx context.Context, network, service string) (port int, err error, completed bool) {
	var hints  /*line :60:12*/_Ctype_struct_addrinfo /*line :60:29*/
	switch network {
	case "": // no hints
	case "tcp", "tcp4", "tcp6":
		hints.ai_socktype = ( /*line :64:23*/_Ciconst_SOCK_STREAM /*line :64:35*/)
		hints.ai_protocol = ( /*line :65:23*/_Ciconst_IPPROTO_TCP /*line :65:35*/)
	case "udp", "udp4", "udp6":
		hints.ai_socktype = ( /*line :67:23*/_Ciconst_SOCK_DGRAM /*line :67:34*/)
		hints.ai_protocol = ( /*line :68:23*/_Ciconst_IPPROTO_UDP /*line :68:35*/)
	default:
		return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}, true
	}
	switch ipVersion(network) {
	case '4':
		hints.ai_family = ( /*line :74:21*/_Ciconst_AF_INET /*line :74:29*/)
	case '6':
		hints.ai_family = ( /*line :76:21*/_Ciconst_AF_INET6 /*line :76:30*/)
	}
	if ctx.Done() == nil {
		port, err := cgoLookupServicePort(&hints, network, service)
		return port, err, true
	}
	result := make(chan portLookupResult, 1)
	go cgoPortLookup(result, &hints, network, service)
	select {
	case r := <-result:
		return r.port, r.err, true
	case <-ctx.Done():
		// Since there isn't a portable way to cancel the lookup,
		// we just let it finish and write to the buffered channel.
		return 0, mapErr(ctx.Err()), false
	}
}

func cgoLookupServicePort(hints * /*line :94:34*/_Ctype_struct_addrinfo /*line :94:51*/, network, service string) (port int, err error) {
	cservice := make([]byte, len(service)+1)
	copy(cservice, service)
	// Lowercase the C service name.
	for i, b := range cservice[:len(service)] {
		cservice[i] = lowerASCII(b)
	}
	var res * /*line :101:11*/_Ctype_struct_addrinfo /*line :101:28*/
	gerrno, err := func() (_Ctype_int, error){ var _cgo0 *_Ctype_char = /*line :102:31*/nil; var _cgo1 *_Ctype_char = /*line :102:36*/(*_Ctype_char)(unsafe.Pointer(&cservice[0])); _cgo2 := /*line :102:77*/hints; _cgoBase3 := /*line :102:84*/&res; _cgo3 := _cgoBase3; _cgoCheckPointer(_cgo2); _cgoCheckPointer(_cgoBase3, 0 == 0); return _C2func_getaddrinfo(_cgo0, _cgo1, _cgo2, _cgo3); }()
	if gerrno != 0 {
		switch gerrno {
		case ( /*line :105:8*/_Ciconst_EAI_SYSTEM /*line :105:19*/):
			if err == nil { // see golang.org/issue/6232
				err = syscall.EMFILE
			}
		default:
			err = addrinfoErrno(gerrno)
		}
		return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}
	}
	defer func() func() { _cgo0 := /*line :114:23*/res; return func() { _cgoCheckPointer(_cgo0); _Cfunc_freeaddrinfo(_cgo0); }}()()

	for r := res; r != nil; r = r.ai_next {
		switch r.ai_family {
		case ( /*line :118:8*/_Ciconst_AF_INET /*line :118:16*/):
			sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr))
			p := (*[2]byte)(unsafe.Pointer(&sa.Port))
			return int(p[0])<<8 | int(p[1]), nil
		case ( /*line :122:8*/_Ciconst_AF_INET6 /*line :122:17*/):
			sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.ai_addr))
			p := (*[2]byte)(unsafe.Pointer(&sa.Port))
			return int(p[0])<<8 | int(p[1]), nil
		}
	}
	return 0, &DNSError{Err: "unknown port", Name: network + "/" + service}
}

func cgoPortLookup(result chan<- portLookupResult, hints * /*line :131:59*/_Ctype_struct_addrinfo /*line :131:76*/, network, service string) {
	port, err := cgoLookupServicePort(hints, network, service)
	result <- portLookupResult{port, err}
}

func cgoLookupIPCNAME(network, name string) (addrs []IPAddr, cname string, err error) {
	acquireThread()
	defer releaseThread()

	var hints  /*line :140:12*/_Ctype_struct_addrinfo /*line :140:29*/
	hints.ai_flags = cgoAddrInfoFlags
	hints.ai_socktype = ( /*line :142:22*/_Ciconst_SOCK_STREAM /*line :142:34*/)
	hints.ai_family = ( /*line :143:20*/_Ciconst_AF_UNSPEC /*line :143:30*/)
	switch ipVersion(network) {
	case '4':
		hints.ai_family = ( /*line :146:21*/_Ciconst_AF_INET /*line :146:29*/)
	case '6':
		hints.ai_family = ( /*line :148:21*/_Ciconst_AF_INET6 /*line :148:30*/)
	}

	h := make([]byte, len(name)+1)
	copy(h, name)
	var res * /*line :153:11*/_Ctype_struct_addrinfo /*line :153:28*/
	gerrno, err := func() (_Ctype_int, error){ var _cgo0 *_Ctype_char = /*line :154:31*/(*_Ctype_char)(unsafe.Pointer(&h[0])); var _cgo1 *_Ctype_char = /*line :154:65*/nil; _cgoBase2 := /*line :154:70*/&hints; _cgo2 := _cgoBase2; _cgoBase3 := /*line :154:78*/&res; _cgo3 := _cgoBase3; _cgoCheckPointer(_cgoBase2, 0 == 0); _cgoCheckPointer(_cgoBase3, 0 == 0); return _C2func_getaddrinfo(_cgo0, _cgo1, _cgo2, _cgo3); }()
	if gerrno != 0 {
		switch gerrno {
		case ( /*line :157:8*/_Ciconst_EAI_SYSTEM /*line :157:19*/):
			if err == nil {
				// err should not be nil, but sometimes getaddrinfo returns
				// gerrno == C.EAI_SYSTEM with err == nil on Linux.
				// The report claims that it happens when we have too many
				// open files, so use syscall.EMFILE (too many open files in system).
				// Most system calls would return ENFILE (too many open files),
				// so at the least EMFILE should be easy to recognize if this
				// comes up again. golang.org/issue/6232.
				err = syscall.EMFILE
			}
		case ( /*line :168:8*/_Ciconst_EAI_NONAME /*line :168:19*/):
			err = errNoSuchHost
		default:
			err = addrinfoErrno(gerrno)
		}
		return nil, "", &DNSError{Err: err.Error(), Name: name}
	}
	defer func() func() { _cgo0 := /*line :175:23*/res; return func() { _cgoCheckPointer(_cgo0); _Cfunc_freeaddrinfo(_cgo0); }}()()

	if res != nil {
		cname = ( /*line :178:11*/_Cfunc_GoString /*line :178:20*/)(res.ai_canonname)
		if cname == "" {
			cname = name
		}
		if len(cname) > 0 && cname[len(cname)-1] != '.' {
			cname += "."
		}
	}
	for r := res; r != nil; r = r.ai_next {
		// We only asked for SOCK_STREAM, but check anyhow.
		if r.ai_socktype != ( /*line :188:23*/_Ciconst_SOCK_STREAM /*line :188:35*/) {
			continue
		}
		switch r.ai_family {
		case ( /*line :192:8*/_Ciconst_AF_INET /*line :192:16*/):
			sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr))
			addr := IPAddr{IP: copyIP(sa.Addr[:])}
			addrs = append(addrs, addr)
		case ( /*line :196:8*/_Ciconst_AF_INET6 /*line :196:17*/):
			sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.ai_addr))
			addr := IPAddr{IP: copyIP(sa.Addr[:]), Zone: zoneCache.name(int(sa.Scope_id))}
			addrs = append(addrs, addr)
		}
	}
	return addrs, cname, nil
}

func cgoIPLookup(result chan<- ipLookupResult, network, name string) {
	addrs, cname, err := cgoLookupIPCNAME(network, name)
	result <- ipLookupResult{addrs, cname, err}
}

func cgoLookupIP(ctx context.Context, network, name string) (addrs []IPAddr, err error, completed bool) {
	if ctx.Done() == nil {
		addrs, _, err = cgoLookupIPCNAME(network, name)
		return addrs, err, true
	}
	result := make(chan ipLookupResult, 1)
	go cgoIPLookup(result, network, name)
	select {
	case r := <-result:
		return r.addrs, r.err, true
	case <-ctx.Done():
		return nil, mapErr(ctx.Err()), false
	}
}

func cgoLookupCNAME(ctx context.Context, name string) (cname string, err error, completed bool) {
	if ctx.Done() == nil {
		_, cname, err = cgoLookupIPCNAME("ip", name)
		return cname, err, true
	}
	result := make(chan ipLookupResult, 1)
	go cgoIPLookup(result, "ip", name)
	select {
	case r := <-result:
		return r.cname, r.err, true
	case <-ctx.Done():
		return "", mapErr(ctx.Err()), false
	}
}

// These are roughly enough for the following:
//
// Source		Encoding			Maximum length of single name entry
// Unicast DNS		ASCII or			<=253 + a NUL terminator
//			Unicode in RFC 5892		252 * total number of labels + delimiters + a NUL terminator
// Multicast DNS	UTF-8 in RFC 5198 or		<=253 + a NUL terminator
//			the same as unicast DNS ASCII	<=253 + a NUL terminator
// Local database	various				depends on implementation
const (
	nameinfoLen    = 64
	maxNameinfoLen = 4096
)

func cgoLookupPTR(ctx context.Context, addr string) (names []string, err error, completed bool) {
	var zone string
	ip := parseIPv4(addr)
	if ip == nil {
		ip, zone = parseIPv6Zone(addr)
	}
	if ip == nil {
		return nil, &DNSError{Err: "invalid address", Name: addr}, true
	}
	sa, salen := cgoSockaddr(ip, zone)
	if sa == nil {
		return nil, &DNSError{Err: "invalid address " + ip.String(), Name: addr}, true
	}
	if ctx.Done() == nil {
		names, err := cgoLookupAddrPTR(addr, sa, salen)
		return names, err, true
	}
	result := make(chan reverseLookupResult, 1)
	go cgoReverseLookup(result, addr, sa, salen)
	select {
	case r := <-result:
		return r.names, r.err, true
	case <-ctx.Done():
		return nil, mapErr(ctx.Err()), false
	}
}

func cgoLookupAddrPTR(addr string, sa * /*line :280:40*/_Ctype_struct_sockaddr /*line :280:57*/, salen  /*line :280:65*/_Ctype_socklen_t /*line :280:76*/) (names []string, err error) {
	acquireThread()
	defer releaseThread()

	var gerrno int
	var b []byte
	for l := nameinfoLen; l <= maxNameinfoLen; l *= 2 {
		b = make([]byte, l)
		gerrno, err = cgoNameinfoPTR(b, sa, salen)
		if gerrno == 0 || gerrno != ( /*line :289:31*/_Ciconst_EAI_OVERFLOW /*line :289:44*/) {
			break
		}
	}
	if gerrno != 0 {
		switch gerrno {
		case ( /*line :295:8*/_Ciconst_EAI_SYSTEM /*line :295:19*/):
			if err == nil { // see golang.org/issue/6232
				err = syscall.EMFILE
			}
		default:
			err = addrinfoErrno(gerrno)
		}
		return nil, &DNSError{Err: err.Error(), Name: addr}
	}
	for i := 0; i < len(b); i++ {
		if b[i] == 0 {
			b = b[:i]
			break
		}
	}
	return []string{absDomainName(b)}, nil
}

func cgoReverseLookup(result chan<- reverseLookupResult, addr string, sa * /*line :313:75*/_Ctype_struct_sockaddr /*line :313:92*/, salen  /*line :313:100*/_Ctype_socklen_t /*line :313:111*/) {
	names, err := cgoLookupAddrPTR(addr, sa, salen)
	result <- reverseLookupResult{names, err}
}

func cgoSockaddr(ip IP, zone string) (* /*line :318:40*/_Ctype_struct_sockaddr /*line :318:57*/,  /*line :318:59*/_Ctype_socklen_t /*line :318:70*/) {
	if ip4 := ip.To4(); ip4 != nil {
		return cgoSockaddrInet4(ip4),  /*line :320:33*/_Ctype_socklen_t /*line :320:44*/(syscall.SizeofSockaddrInet4)
	}
	if ip6 := ip.To16(); ip6 != nil {
		return cgoSockaddrInet6(ip6, zoneCache.index(zone)),  /*line :323:56*/_Ctype_socklen_t /*line :323:67*/(syscall.SizeofSockaddrInet6)
	}
	return nil, 0
}

func copyIP(x IP) IP {
	if len(x) < 16 {
		return x.To16()
	}
	y := make(IP, len(x))
	copy(y, x)
	return y
}
