2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-04-20 13:21:59 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-10-28 08:54:37 -06:00
|
|
|
// 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
|
|
|
|
|
2022-11-06 14:03:00 -07:00
|
|
|
//go:build !netgo && ((cgo && unix) || darwin)
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-04-20 13:21:59 -06:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-05-08 19:17:59 -06:00
|
|
|
"context"
|
2022-10-28 08:57:14 -06:00
|
|
|
"errors"
|
2023-01-31 02:00:22 -07:00
|
|
|
"net/netip"
|
2011-04-20 13:21:59 -06:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2022-10-28 08:57:14 -06:00
|
|
|
|
|
|
|
"golang.org/x/net/dns/dnsmessage"
|
2011-04-20 13:21:59 -06:00
|
|
|
)
|
|
|
|
|
2023-04-20 17:55:13 -06:00
|
|
|
// cgoAvailable set to true to indicate that the cgo resolver
|
|
|
|
// is available on this system.
|
|
|
|
const cgoAvailable = true
|
|
|
|
|
2015-04-17 02:56:58 -06:00
|
|
|
// 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
|
|
|
|
|
2022-10-28 08:54:37 -06:00
|
|
|
func (eai addrinfoErrno) Error() string { return _C_gai_strerror(_C_int(eai)) }
|
|
|
|
func (eai addrinfoErrno) Temporary() bool { return eai == _C_EAI_AGAIN }
|
2015-04-17 02:56:58 -06:00
|
|
|
func (eai addrinfoErrno) Timeout() bool { return false }
|
|
|
|
|
2023-04-20 17:55:13 -06:00
|
|
|
// isAddrinfoErrno is just for testing purposes.
|
|
|
|
func (eai addrinfoErrno) isAddrinfoErrno() {}
|
|
|
|
|
2023-02-10 02:01:29 -07:00
|
|
|
// 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 doBlockingWithCtx[T any](ctx context.Context, blocking func() (T, error)) (T, error) {
|
|
|
|
if ctx.Done() == nil {
|
|
|
|
return blocking()
|
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
|
2023-02-10 02:01:29 -07:00
|
|
|
type result struct {
|
|
|
|
res T
|
|
|
|
err error
|
|
|
|
}
|
2023-01-31 01:34:13 -07:00
|
|
|
|
2023-02-10 02:01:29 -07:00
|
|
|
res := make(chan result, 1)
|
|
|
|
go func() {
|
|
|
|
var r result
|
|
|
|
r.res, r.err = blocking()
|
|
|
|
res <- r
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case r := <-res:
|
|
|
|
return r.res, r.err
|
|
|
|
case <-ctx.Done():
|
|
|
|
var zero T
|
|
|
|
return zero, mapErr(ctx.Err())
|
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:17:54 -06:00
|
|
|
func cgoLookupHost(ctx context.Context, name string) (hosts []string, err error) {
|
|
|
|
addrs, err := cgoLookupIP(ctx, "ip", name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-17 02:56:58 -06:00
|
|
|
for _, addr := range addrs {
|
|
|
|
hosts = append(hosts, addr.String())
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2023-05-02 08:17:54 -06:00
|
|
|
return hosts, nil
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:17:54 -06:00
|
|
|
func cgoLookupPort(ctx context.Context, network, service string) (port int, err error) {
|
2022-10-28 08:54:37 -06:00
|
|
|
var hints _C_struct_addrinfo
|
2015-04-17 02:56:58 -06:00
|
|
|
switch network {
|
2023-10-02 10:24:26 -06:00
|
|
|
case "ip": // no hints
|
2011-04-20 13:21:59 -06:00
|
|
|
case "tcp", "tcp4", "tcp6":
|
2022-10-28 08:54:37 -06:00
|
|
|
*_C_ai_socktype(&hints) = _C_SOCK_STREAM
|
|
|
|
*_C_ai_protocol(&hints) = _C_IPPROTO_TCP
|
2011-04-20 13:21:59 -06:00
|
|
|
case "udp", "udp4", "udp6":
|
2022-10-28 08:54:37 -06:00
|
|
|
*_C_ai_socktype(&hints) = _C_SOCK_DGRAM
|
|
|
|
*_C_ai_protocol(&hints) = _C_IPPROTO_UDP
|
2011-04-20 13:21:59 -06:00
|
|
|
default:
|
2023-05-02 08:17:54 -06:00
|
|
|
return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2018-06-20 16:23:37 -06:00
|
|
|
switch ipVersion(network) {
|
|
|
|
case '4':
|
2022-10-28 08:54:37 -06:00
|
|
|
*_C_ai_family(&hints) = _C_AF_INET
|
2018-06-20 16:23:37 -06:00
|
|
|
case '6':
|
2022-10-28 08:54:37 -06:00
|
|
|
*_C_ai_family(&hints) = _C_AF_INET6
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2023-02-10 02:01:29 -07:00
|
|
|
|
2023-05-02 08:17:54 -06:00
|
|
|
return doBlockingWithCtx(ctx, func() (int, error) {
|
2023-02-10 02:01:29 -07:00
|
|
|
return cgoLookupServicePort(&hints, network, service)
|
|
|
|
})
|
2016-05-08 19:17:59 -06:00
|
|
|
}
|
2011-04-20 13:21:59 -06:00
|
|
|
|
2022-10-28 08:54:37 -06:00
|
|
|
func cgoLookupServicePort(hints *_C_struct_addrinfo, network, service string) (port int, err error) {
|
2023-04-18 15:48:36 -06:00
|
|
|
cservice, err := syscall.ByteSliceFromString(service)
|
|
|
|
if err != nil {
|
|
|
|
return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}
|
|
|
|
}
|
2017-05-12 22:01:50 -06:00
|
|
|
// Lowercase the C service name.
|
|
|
|
for i, b := range cservice[:len(service)] {
|
|
|
|
cservice[i] = lowerASCII(b)
|
2016-09-09 16:51:11 -06:00
|
|
|
}
|
2022-10-28 08:54:37 -06:00
|
|
|
var res *_C_struct_addrinfo
|
|
|
|
gerrno, err := _C_getaddrinfo(nil, (*_C_char)(unsafe.Pointer(&cservice[0])), hints, &res)
|
2015-04-17 02:56:58 -06:00
|
|
|
if gerrno != 0 {
|
2019-05-01 01:52:44 -06:00
|
|
|
isTemporary := false
|
2015-04-17 02:56:58 -06:00
|
|
|
switch gerrno {
|
2022-10-28 08:54:37 -06:00
|
|
|
case _C_EAI_SYSTEM:
|
2015-04-17 02:56:58 -06:00
|
|
|
if err == nil { // see golang.org/issue/6232
|
|
|
|
err = syscall.EMFILE
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2023-09-28 02:10:04 -06:00
|
|
|
case _C_EAI_SERVICE, _C_EAI_NONAME: // Darwin returns EAI_NONAME.
|
|
|
|
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
|
2015-04-17 02:56:58 -06:00
|
|
|
default:
|
|
|
|
err = addrinfoErrno(gerrno)
|
2019-05-01 01:52:44 -06:00
|
|
|
isTemporary = addrinfoErrno(gerrno).Temporary()
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2019-05-01 01:52:44 -06:00
|
|
|
return 0, &DNSError{Err: err.Error(), Name: network + "/" + service, IsTemporary: isTemporary}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2022-10-28 08:54:37 -06:00
|
|
|
defer _C_freeaddrinfo(res)
|
2015-04-17 02:56:58 -06:00
|
|
|
|
2022-10-28 08:54:37 -06:00
|
|
|
for r := res; r != nil; r = *_C_ai_next(r) {
|
|
|
|
switch *_C_ai_family(r) {
|
|
|
|
case _C_AF_INET:
|
|
|
|
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(*_C_ai_addr(r)))
|
2015-04-17 02:56:58 -06:00
|
|
|
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
|
2016-05-08 19:17:59 -06:00
|
|
|
return int(p[0])<<8 | int(p[1]), nil
|
2022-10-28 08:54:37 -06:00
|
|
|
case _C_AF_INET6:
|
|
|
|
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(*_C_ai_addr(r)))
|
2015-04-17 02:56:58 -06:00
|
|
|
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
|
2016-05-08 19:17:59 -06:00
|
|
|
return int(p[0])<<8 | int(p[1]), nil
|
2015-04-17 02:56:58 -06:00
|
|
|
}
|
|
|
|
}
|
2023-09-28 02:10:04 -06:00
|
|
|
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
|
2023-02-08 01:44:32 -07:00
|
|
|
func cgoLookupHostIP(network, name string) (addrs []IPAddr, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
|
|
|
|
2022-10-28 08:54:37 -06:00
|
|
|
var hints _C_struct_addrinfo
|
|
|
|
*_C_ai_flags(&hints) = cgoAddrInfoFlags
|
|
|
|
*_C_ai_socktype(&hints) = _C_SOCK_STREAM
|
|
|
|
*_C_ai_family(&hints) = _C_AF_UNSPEC
|
2018-06-20 16:23:37 -06:00
|
|
|
switch ipVersion(network) {
|
|
|
|
case '4':
|
2022-10-28 08:54:37 -06:00
|
|
|
*_C_ai_family(&hints) = _C_AF_INET
|
2018-06-20 16:23:37 -06:00
|
|
|
case '6':
|
2022-10-28 08:54:37 -06:00
|
|
|
*_C_ai_family(&hints) = _C_AF_INET6
|
2018-06-20 16:23:37 -06:00
|
|
|
}
|
2011-04-20 13:21:59 -06:00
|
|
|
|
2023-04-18 15:48:36 -06:00
|
|
|
h, err := syscall.BytePtrFromString(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &DNSError{Err: err.Error(), Name: name}
|
|
|
|
}
|
2022-10-28 08:54:37 -06:00
|
|
|
var res *_C_struct_addrinfo
|
2023-04-18 15:48:36 -06:00
|
|
|
gerrno, err := _C_getaddrinfo((*_C_char)(unsafe.Pointer(h)), nil, &hints, &res)
|
2011-04-20 13:21:59 -06:00
|
|
|
if gerrno != 0 {
|
2019-03-21 09:40:12 -06:00
|
|
|
isErrorNoSuchHost := false
|
2019-05-01 01:52:44 -06:00
|
|
|
isTemporary := false
|
2015-04-17 02:56:58 -06:00
|
|
|
switch gerrno {
|
2022-10-28 08:54:37 -06:00
|
|
|
case _C_EAI_SYSTEM:
|
2013-09-11 09:38:56 -06:00
|
|
|
if err == nil {
|
|
|
|
// err should not be nil, but sometimes getaddrinfo returns
|
2022-10-28 08:54:37 -06:00
|
|
|
// gerrno == _C_EAI_SYSTEM with err == nil on Linux.
|
2013-09-11 09:38:56 -06:00
|
|
|
// 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
|
|
|
|
}
|
2023-03-30 01:22:35 -06:00
|
|
|
case _C_EAI_NONAME, _C_EAI_NODATA:
|
2015-04-19 05:54:01 -06:00
|
|
|
err = errNoSuchHost
|
2019-03-21 09:40:12 -06:00
|
|
|
isErrorNoSuchHost = true
|
2015-04-17 02:56:58 -06:00
|
|
|
default:
|
2015-04-19 05:54:01 -06:00
|
|
|
err = addrinfoErrno(gerrno)
|
2019-05-01 01:52:44 -06:00
|
|
|
isTemporary = addrinfoErrno(gerrno).Temporary()
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2019-03-21 09:40:12 -06:00
|
|
|
|
2023-02-08 01:44:32 -07:00
|
|
|
return nil, &DNSError{Err: err.Error(), Name: name, IsNotFound: isErrorNoSuchHost, IsTemporary: isTemporary}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2022-10-28 08:54:37 -06:00
|
|
|
defer _C_freeaddrinfo(res)
|
2015-04-17 02:56:58 -06:00
|
|
|
|
2022-10-28 08:54:37 -06:00
|
|
|
for r := res; r != nil; r = *_C_ai_next(r) {
|
2013-08-09 10:23:27 -06:00
|
|
|
// We only asked for SOCK_STREAM, but check anyhow.
|
2022-10-28 08:54:37 -06:00
|
|
|
if *_C_ai_socktype(r) != _C_SOCK_STREAM {
|
2011-04-20 13:21:59 -06:00
|
|
|
continue
|
|
|
|
}
|
2022-10-28 08:54:37 -06:00
|
|
|
switch *_C_ai_family(r) {
|
|
|
|
case _C_AF_INET:
|
|
|
|
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(*_C_ai_addr(r)))
|
2015-02-12 21:24:47 -07:00
|
|
|
addr := IPAddr{IP: copyIP(sa.Addr[:])}
|
|
|
|
addrs = append(addrs, addr)
|
2022-10-28 08:54:37 -06:00
|
|
|
case _C_AF_INET6:
|
|
|
|
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(*_C_ai_addr(r)))
|
2017-04-27 04:00:09 -06:00
|
|
|
addr := IPAddr{IP: copyIP(sa.Addr[:]), Zone: zoneCache.name(int(sa.Scope_id))}
|
2015-02-12 21:24:47 -07:00
|
|
|
addrs = append(addrs, addr)
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 01:44:32 -07:00
|
|
|
return addrs, nil
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:17:54 -06:00
|
|
|
func cgoLookupIP(ctx context.Context, network, name string) (addrs []IPAddr, err error) {
|
|
|
|
return doBlockingWithCtx(ctx, func() ([]IPAddr, error) {
|
2023-02-10 02:01:29 -07:00
|
|
|
return cgoLookupHostIP(network, name)
|
|
|
|
})
|
2016-05-08 19:17:59 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 04:08:41 -07:00
|
|
|
// These are roughly enough for the following:
|
|
|
|
//
|
2022-02-03 12:12:08 -07:00
|
|
|
// 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
|
2015-01-28 04:08:41 -07:00
|
|
|
const (
|
|
|
|
nameinfoLen = 64
|
|
|
|
maxNameinfoLen = 4096
|
|
|
|
)
|
|
|
|
|
2023-05-02 08:17:54 -06:00
|
|
|
func cgoLookupPTR(ctx context.Context, addr string) (names []string, err error) {
|
2023-01-31 02:00:22 -07:00
|
|
|
ip, err := netip.ParseAddr(addr)
|
|
|
|
if err != nil {
|
2023-05-02 08:17:54 -06:00
|
|
|
return nil, &DNSError{Err: "invalid address", Name: addr}
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
2023-01-31 02:00:22 -07:00
|
|
|
sa, salen := cgoSockaddr(IP(ip.AsSlice()), ip.Zone())
|
2015-01-28 04:08:41 -07:00
|
|
|
if sa == nil {
|
2023-05-02 08:17:54 -06:00
|
|
|
return nil, &DNSError{Err: "invalid address " + ip.String(), Name: addr}
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
2023-02-10 02:01:29 -07:00
|
|
|
|
2023-05-02 08:17:54 -06:00
|
|
|
return doBlockingWithCtx(ctx, func() ([]string, error) {
|
2023-02-10 02:01:29 -07:00
|
|
|
return cgoLookupAddrPTR(addr, sa, salen)
|
|
|
|
})
|
2016-05-08 19:17:59 -06:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:54:37 -06:00
|
|
|
func cgoLookupAddrPTR(addr string, sa *_C_struct_sockaddr, salen _C_socklen_t) (names []string, err error) {
|
2016-05-08 19:17:59 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
|
|
|
|
2015-01-28 04:08:41 -07:00
|
|
|
var gerrno int
|
2016-05-08 19:17:59 -06:00
|
|
|
var b []byte
|
2015-01-28 04:08:41 -07:00
|
|
|
for l := nameinfoLen; l <= maxNameinfoLen; l *= 2 {
|
|
|
|
b = make([]byte, l)
|
|
|
|
gerrno, err = cgoNameinfoPTR(b, sa, salen)
|
2022-10-28 08:54:37 -06:00
|
|
|
if gerrno == 0 || gerrno != _C_EAI_OVERFLOW {
|
2015-01-28 04:08:41 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if gerrno != 0 {
|
2023-01-31 11:38:01 -07:00
|
|
|
isErrorNoSuchHost := false
|
2019-05-01 01:52:44 -06:00
|
|
|
isTemporary := false
|
2015-01-28 04:08:41 -07:00
|
|
|
switch gerrno {
|
2022-10-28 08:54:37 -06:00
|
|
|
case _C_EAI_SYSTEM:
|
2015-01-28 04:08:41 -07:00
|
|
|
if err == nil { // see golang.org/issue/6232
|
|
|
|
err = syscall.EMFILE
|
|
|
|
}
|
2023-01-31 11:38:01 -07:00
|
|
|
case _C_EAI_NONAME:
|
|
|
|
err = errNoSuchHost
|
|
|
|
isErrorNoSuchHost = true
|
2015-01-28 04:08:41 -07:00
|
|
|
default:
|
|
|
|
err = addrinfoErrno(gerrno)
|
2019-05-01 01:52:44 -06:00
|
|
|
isTemporary = addrinfoErrno(gerrno).Temporary()
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
2023-01-31 11:38:01 -07:00
|
|
|
return nil, &DNSError{Err: err.Error(), Name: addr, IsTemporary: isTemporary, IsNotFound: isErrorNoSuchHost}
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
if b[i] == 0 {
|
|
|
|
b = b[:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-06-23 22:50:14 -06:00
|
|
|
return []string{absDomainName(string(b))}, nil
|
2016-05-08 19:17:59 -06:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:54:37 -06:00
|
|
|
func cgoSockaddr(ip IP, zone string) (*_C_struct_sockaddr, _C_socklen_t) {
|
2015-01-28 04:08:41 -07:00
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
2022-10-28 08:54:37 -06:00
|
|
|
return cgoSockaddrInet4(ip4), _C_socklen_t(syscall.SizeofSockaddrInet4)
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
|
|
|
if ip6 := ip.To16(); ip6 != nil {
|
2022-10-28 08:54:37 -06:00
|
|
|
return cgoSockaddrInet6(ip6, zoneCache.index(zone)), _C_socklen_t(syscall.SizeofSockaddrInet6)
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
|
|
|
return nil, 0
|
|
|
|
}
|
2022-10-28 08:57:14 -06:00
|
|
|
|
|
|
|
func cgoLookupCNAME(ctx context.Context, name string) (cname string, err error, completed bool) {
|
|
|
|
resources, err := resSearch(ctx, name, int(dnsmessage.TypeCNAME), int(dnsmessage.ClassINET))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cname, err = parseCNAMEFromResources(resources)
|
|
|
|
if err != nil {
|
|
|
|
return "", err, false
|
|
|
|
}
|
|
|
|
return cname, 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 resSearch(ctx context.Context, hostname string, rtype, class int) ([]dnsmessage.Resource, error) {
|
2023-02-10 02:01:29 -07:00
|
|
|
return doBlockingWithCtx(ctx, func() ([]dnsmessage.Resource, error) {
|
2022-12-03 01:14:33 -07:00
|
|
|
return cgoResSearch(hostname, rtype, class)
|
2023-02-10 02:01:29 -07:00
|
|
|
})
|
2022-12-03 01:14:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func cgoResSearch(hostname string, rtype, class int) ([]dnsmessage.Resource, error) {
|
2022-12-01 02:26:18 -07:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
|
|
|
|
2022-11-08 14:16:44 -07:00
|
|
|
state := (*_C_struct___res_state)(_C_malloc(unsafe.Sizeof(_C_struct___res_state{})))
|
|
|
|
defer _C_free(unsafe.Pointer(state))
|
|
|
|
if err := _C_res_ninit(state); err != nil {
|
2022-10-28 08:57:14 -06:00
|
|
|
return nil, errors.New("res_ninit failure: " + err.Error())
|
|
|
|
}
|
2022-11-08 14:16:44 -07:00
|
|
|
defer _C_res_nclose(state)
|
2022-10-28 08:57:14 -06:00
|
|
|
|
|
|
|
// 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.
|
2022-12-01 10:54:49 -07:00
|
|
|
bufSize := maxDNSPacketSize
|
|
|
|
buf := (*_C_uchar)(_C_malloc(uintptr(bufSize)))
|
2022-11-08 12:04:24 -07:00
|
|
|
defer _C_free(unsafe.Pointer(buf))
|
2022-12-01 10:54:49 -07:00
|
|
|
|
2023-04-18 15:48:36 -06:00
|
|
|
s, err := syscall.BytePtrFromString(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-01 10:54:49 -07:00
|
|
|
|
2022-12-20 08:24:06 -07:00
|
|
|
var size int
|
2022-12-01 10:54:49 -07:00
|
|
|
for {
|
2023-04-18 15:48:36 -06:00
|
|
|
size, _ = _C_res_nsearch(state, (*_C_char)(unsafe.Pointer(s)), class, rtype, buf, bufSize)
|
2022-12-01 10:54:49 -07:00
|
|
|
if size <= 0 || size > 0xffff {
|
|
|
|
return nil, errors.New("res_nsearch failure")
|
|
|
|
}
|
|
|
|
if size <= bufSize {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a bigger buffer to fit the entire msg.
|
|
|
|
_C_free(unsafe.Pointer(buf))
|
|
|
|
bufSize = size
|
|
|
|
buf = (*_C_uchar)(_C_malloc(uintptr(bufSize)))
|
2022-10-28 08:57:14 -06:00
|
|
|
}
|
2022-12-01 10:54:49 -07:00
|
|
|
|
2022-10-28 08:57:14 -06:00
|
|
|
var p dnsmessage.Parser
|
2022-12-20 08:24:06 -07:00
|
|
|
if _, err := p.Start(unsafe.Slice((*byte)(unsafe.Pointer(buf)), size)); err != nil {
|
2022-10-28 08:57:14 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.SkipAllQuestions()
|
|
|
|
resources, err := p.AllAnswers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resources, nil
|
|
|
|
}
|