// 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.

// This file is called cgo_unix.go, but to allow syscalls-to-libc-based
// implementations to share the code, it does not use cgo directly.
// Instead of C.foo it uses _C_foo, which is defined in either
// cgo_unix_cgo.go or cgo_unix_syscall.go

//go:build !netgo && ((cgo && unix) || darwin)

package net

import (
	
	
	
	
	

	
)

// cgoAvailable set to true to indicate that the cgo resolver
// is available on this system.
const cgoAvailable = true

// 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 ( addrinfoErrno) () string   { return _C_gai_strerror(_C_int()) }
func ( addrinfoErrno) () bool { return  == _C_EAI_AGAIN }
func ( addrinfoErrno) () bool   { return false }

// isAddrinfoErrno is just for testing purposes.
func ( addrinfoErrno) () {}

// doBlockingWithCtx executes a blocking function in a separate goroutine when the provided
// context is cancellable. It is intended for use with calls that don't support context
// cancellation (cgo, syscalls). blocking func may still be running after this function finishes.
func [ any]( context.Context,  func() (, error)) (, error) {
	if .Done() == nil {
		return ()
	}

	type  struct {
		 
		 error
	}

	 := make(chan , 1)
	go func() {
		var  
		., . = ()
		 <- 
	}()

	select {
	case  := <-:
		return ., .
	case <-.Done():
		var  
		return , mapErr(.Err())
	}
}

func ( context.Context,  string) ( []string,  error) {
	,  := cgoLookupIP(, "ip", )
	if  != nil {
		return nil, 
	}
	for ,  := range  {
		 = append(, .String())
	}
	return , nil
}

func ( context.Context, ,  string) ( int,  error) {
	var  _C_struct_addrinfo
	switch  {
	case "": // no hints
	case "tcp", "tcp4", "tcp6":
		*_C_ai_socktype(&) = _C_SOCK_STREAM
		*_C_ai_protocol(&) = _C_IPPROTO_TCP
	case "udp", "udp4", "udp6":
		*_C_ai_socktype(&) = _C_SOCK_DGRAM
		*_C_ai_protocol(&) = _C_IPPROTO_UDP
	default:
		return 0, &DNSError{Err: "unknown network", Name:  + "/" + }
	}
	switch ipVersion() {
	case '4':
		*_C_ai_family(&) = _C_AF_INET
	case '6':
		*_C_ai_family(&) = _C_AF_INET6
	}

	return doBlockingWithCtx(, func() (int, error) {
		return cgoLookupServicePort(&, , )
	})
}

func ( *_C_struct_addrinfo, ,  string) ( int,  error) {
	,  := syscall.ByteSliceFromString()
	if  != nil {
		return 0, &DNSError{Err: .Error(), Name:  + "/" + }
	}
	// Lowercase the C service name.
	for ,  := range [:len()] {
		[] = lowerASCII()
	}
	var  *_C_struct_addrinfo
	,  := _C_getaddrinfo(nil, (*_C_char)(unsafe.Pointer(&[0])), , &)
	if  != 0 {
		 := false
		switch  {
		case _C_EAI_SYSTEM:
			if  == nil { // see golang.org/issue/6232
				 = syscall.EMFILE
			}
		default:
			 = addrinfoErrno()
			 = addrinfoErrno().Temporary()
		}
		return 0, &DNSError{Err: .Error(), Name:  + "/" + , IsTemporary: }
	}
	defer _C_freeaddrinfo()

	for  := ;  != nil;  = *_C_ai_next() {
		switch *_C_ai_family() {
		case _C_AF_INET:
			 := (*syscall.RawSockaddrInet4)(unsafe.Pointer(*_C_ai_addr()))
			 := (*[2]byte)(unsafe.Pointer(&.Port))
			return int([0])<<8 | int([1]), nil
		case _C_AF_INET6:
			 := (*syscall.RawSockaddrInet6)(unsafe.Pointer(*_C_ai_addr()))
			 := (*[2]byte)(unsafe.Pointer(&.Port))
			return int([0])<<8 | int([1]), nil
		}
	}
	return 0, &DNSError{Err: "unknown port", Name:  + "/" + }
}

func (,  string) ( []IPAddr,  error) {
	acquireThread()
	defer releaseThread()

	var  _C_struct_addrinfo
	*_C_ai_flags(&) = cgoAddrInfoFlags
	*_C_ai_socktype(&) = _C_SOCK_STREAM
	*_C_ai_family(&) = _C_AF_UNSPEC
	switch ipVersion() {
	case '4':
		*_C_ai_family(&) = _C_AF_INET
	case '6':
		*_C_ai_family(&) = _C_AF_INET6
	}

	,  := syscall.BytePtrFromString()
	if  != nil {
		return nil, &DNSError{Err: .Error(), Name: }
	}
	var  *_C_struct_addrinfo
	,  := _C_getaddrinfo((*_C_char)(unsafe.Pointer()), nil, &, &)
	if  != 0 {
		 := false
		 := false
		switch  {
		case _C_EAI_SYSTEM:
			if  == 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.
				 = syscall.EMFILE
			}
		case _C_EAI_NONAME, _C_EAI_NODATA:
			 = errNoSuchHost
			 = true
		default:
			 = addrinfoErrno()
			 = addrinfoErrno().Temporary()
		}

		return nil, &DNSError{Err: .Error(), Name: , IsNotFound: , IsTemporary: }
	}
	defer _C_freeaddrinfo()

	for  := ;  != nil;  = *_C_ai_next() {
		// We only asked for SOCK_STREAM, but check anyhow.
		if *_C_ai_socktype() != _C_SOCK_STREAM {
			continue
		}
		switch *_C_ai_family() {
		case _C_AF_INET:
			 := (*syscall.RawSockaddrInet4)(unsafe.Pointer(*_C_ai_addr()))
			 := IPAddr{IP: copyIP(.Addr[:])}
			 = append(, )
		case _C_AF_INET6:
			 := (*syscall.RawSockaddrInet6)(unsafe.Pointer(*_C_ai_addr()))
			 := IPAddr{IP: copyIP(.Addr[:]), Zone: zoneCache.name(int(.Scope_id))}
			 = append(, )
		}
	}
	return , nil
}

func ( context.Context, ,  string) ( []IPAddr,  error) {
	return doBlockingWithCtx(, func() ([]IPAddr, error) {
		return cgoLookupHostIP(, )
	})
}

// 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 ( context.Context,  string) ( []string,  error) {
	,  := netip.ParseAddr()
	if  != nil {
		return nil, &DNSError{Err: "invalid address", Name: }
	}
	,  := cgoSockaddr(IP(.AsSlice()), .Zone())
	if  == nil {
		return nil, &DNSError{Err: "invalid address " + .String(), Name: }
	}

	return doBlockingWithCtx(, func() ([]string, error) {
		return cgoLookupAddrPTR(, , )
	})
}

func ( string,  *_C_struct_sockaddr,  _C_socklen_t) ( []string,  error) {
	acquireThread()
	defer releaseThread()

	var  int
	var  []byte
	for  := nameinfoLen;  <= maxNameinfoLen;  *= 2 {
		 = make([]byte, )
		,  = cgoNameinfoPTR(, , )
		if  == 0 ||  != _C_EAI_OVERFLOW {
			break
		}
	}
	if  != 0 {
		 := false
		 := false
		switch  {
		case _C_EAI_SYSTEM:
			if  == nil { // see golang.org/issue/6232
				 = syscall.EMFILE
			}
		case _C_EAI_NONAME:
			 = errNoSuchHost
			 = true
		default:
			 = addrinfoErrno()
			 = addrinfoErrno().Temporary()
		}
		return nil, &DNSError{Err: .Error(), Name: , IsTemporary: , IsNotFound: }
	}
	for  := 0;  < len(); ++ {
		if [] == 0 {
			 = [:]
			break
		}
	}
	return []string{absDomainName(string())}, nil
}

func ( IP,  string) (*_C_struct_sockaddr, _C_socklen_t) {
	if  := .To4();  != nil {
		return cgoSockaddrInet4(), _C_socklen_t(syscall.SizeofSockaddrInet4)
	}
	if  := .To16();  != nil {
		return cgoSockaddrInet6(, zoneCache.index()), _C_socklen_t(syscall.SizeofSockaddrInet6)
	}
	return nil, 0
}

func ( context.Context,  string) ( string,  error,  bool) {
	,  := resSearch(, , int(dnsmessage.TypeCNAME), int(dnsmessage.ClassINET))
	if  != nil {
		return
	}
	,  = parseCNAMEFromResources()
	if  != nil {
		return "", , false
	}
	return , nil, true
}

// resSearch will make a call to the 'res_nsearch' routine in the C library
// and parse the output as a slice of DNS resources.
func ( context.Context,  string, ,  int) ([]dnsmessage.Resource, error) {
	return doBlockingWithCtx(, func() ([]dnsmessage.Resource, error) {
		return cgoResSearch(, , )
	})
}

func ( string, ,  int) ([]dnsmessage.Resource, error) {
	acquireThread()
	defer releaseThread()

	 := (*_C_struct___res_state)(_C_malloc(unsafe.Sizeof(_C_struct___res_state{})))
	defer _C_free(unsafe.Pointer())
	if  := _C_res_ninit();  != nil {
		return nil, errors.New("res_ninit failure: " + .Error())
	}
	defer _C_res_nclose()

	// Some res_nsearch implementations (like macOS) do not set errno.
	// They set h_errno, which is not per-thread and useless to us.
	// res_nsearch returns the size of the DNS response packet.
	// But if the DNS response packet contains failure-like response codes,
	// res_search returns -1 even though it has copied the packet into buf,
	// giving us no way to find out how big the packet is.
	// For now, we are willing to take res_search's word that there's nothing
	// useful in the response, even though there *is* a response.
	 := maxDNSPacketSize
	 := (*_C_uchar)(_C_malloc(uintptr()))
	defer _C_free(unsafe.Pointer())

	,  := syscall.BytePtrFromString()
	if  != nil {
		return nil, 
	}

	var  int
	for {
		, _ = _C_res_nsearch(, (*_C_char)(unsafe.Pointer()), , , , )
		if  <= 0 ||  > 0xffff {
			return nil, errors.New("res_nsearch failure")
		}
		if  <=  {
			break
		}

		// Allocate a bigger buffer to fit the entire msg.
		_C_free(unsafe.Pointer())
		 = 
		 = (*_C_uchar)(_C_malloc(uintptr()))
	}

	var  dnsmessage.Parser
	if ,  := .Start(unsafe.Slice((*byte)(unsafe.Pointer()), ));  != nil {
		return nil, 
	}
	.SkipAllQuestions()
	,  := .AllAnswers()
	if  != nil {
		return nil, 
	}
	return , nil
}