1
0
mirror of https://github.com/golang/go synced 2024-11-23 02:20:03 -07:00

net: set IsNotFound for unknown services in LookupPort

Change-Id: I9d5f0ea5edd2c121179e3d2f8d4a890fa25a3fa9
GitHub-Last-Rev: 48a13fe5f5
GitHub-Pull-Request: golang/go#63160
Reviewed-on: https://go-review.googlesource.com/c/go/+/530415
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Mateusz Poliwczak 2023-09-28 08:10:04 +00:00 committed by Gopher Robot
parent 1176052bb4
commit 0ba8ef4702
8 changed files with 32 additions and 11 deletions

View File

@ -19,6 +19,7 @@ const (
EAI_AGAIN = 2
EAI_NODATA = 7
EAI_NONAME = 8
EAI_SERVICE = 9
EAI_SYSTEM = 11
EAI_OVERFLOW = 14

View File

@ -120,6 +120,8 @@ func cgoLookupServicePort(hints *_C_struct_addrinfo, network, service string) (p
if err == nil { // see golang.org/issue/6232
err = syscall.EMFILE
}
case _C_EAI_SERVICE, _C_EAI_NONAME: // Darwin returns EAI_NONAME.
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
default:
err = addrinfoErrno(gerrno)
isTemporary = addrinfoErrno(gerrno).Temporary()
@ -140,7 +142,7 @@ func cgoLookupServicePort(hints *_C_struct_addrinfo, network, service string) (p
return int(p[0])<<8 | int(p[1]), nil
}
}
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service}
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
}
func cgoLookupHostIP(network, name string) (addrs []IPAddr, err error) {

View File

@ -37,6 +37,7 @@ const (
_C_EAI_AGAIN = C.EAI_AGAIN
_C_EAI_NODATA = C.EAI_NODATA
_C_EAI_NONAME = C.EAI_NONAME
_C_EAI_SERVICE = C.EAI_SERVICE
_C_EAI_OVERFLOW = C.EAI_OVERFLOW
_C_EAI_SYSTEM = C.EAI_SYSTEM
_C_IPPROTO_TCP = C.IPPROTO_TCP

View File

@ -19,6 +19,7 @@ const (
_C_AF_UNSPEC = syscall.AF_UNSPEC
_C_EAI_AGAIN = unix.EAI_AGAIN
_C_EAI_NONAME = unix.EAI_NONAME
_C_EAI_SERVICE = unix.EAI_SERVICE
_C_EAI_NODATA = unix.EAI_NODATA
_C_EAI_OVERFLOW = unix.EAI_OVERFLOW
_C_EAI_SYSTEM = unix.EAI_SYSTEM

View File

@ -96,8 +96,9 @@ func lookupPortMap(network, service string) (port int, error error) {
if port, ok := m[string(lowerService[:n])]; ok && n == len(service) {
return port, nil
}
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
}
return 0, &AddrError{Err: "unknown port", Addr: network + "/" + service}
return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}
}
// ipVersion returns the provided network's IP version: '4', '6' or 0

View File

@ -212,15 +212,17 @@ func (*Resolver) lookupPort(ctx context.Context, network, service string) (port
}
lines, err := queryCS(ctx, network, "127.0.0.1", toLower(service))
if err != nil {
if stringsHasSuffix(err.Error(), "can't translate service") {
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
}
return
}
unknownPortError := &AddrError{Err: "unknown port", Addr: network + "/" + service}
if len(lines) == 0 {
return 0, unknownPortError
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
}
f := getFields(lines[0])
if len(f) < 2 {
return 0, unknownPortError
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
}
s := f[1]
if i := bytealg.IndexByteString(s, '!'); i >= 0 {
@ -229,7 +231,7 @@ func (*Resolver) lookupPort(ctx context.Context, network, service string) (port
if n, _, ok := dtoi(s); ok {
return n, nil
}
return 0, unknownPortError
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
}
func (r *Resolver) lookupCNAME(ctx context.Context, name string) (cname string, err error) {

View File

@ -1460,3 +1460,11 @@ func testLookupNoData(t *testing.T, prefix string) {
return
}
}
func TestLookupPortNotFound(t *testing.T) {
_, err := LookupPort("udp", "_-unknown-service-")
var dnsErr *DNSError
if !errors.As(err, &dnsErr) || !dnsErr.IsNotFound {
t.Fatalf("unexpected error: %v", err)
}
}

View File

@ -22,6 +22,7 @@ const cgoAvailable = true
const (
_WSAHOST_NOT_FOUND = syscall.Errno(11001)
_WSATRY_AGAIN = syscall.Errno(11002)
_WSATYPE_NOT_FOUND = syscall.Errno(10109)
)
func winError(call string, err error) error {
@ -217,12 +218,16 @@ func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int
if port, err := lookupPortMap(network, service); err == nil {
return port, nil
}
err := winError("getaddrinfow", e)
dnsError := &DNSError{Err: err.Error(), Name: network + "/" + service}
if err == errNoSuchHost {
dnsError.IsNotFound = true
// The _WSATYPE_NOT_FOUND error is returned by GetAddrInfoW
// when the service name is unknown. We are also checking
// for _WSAHOST_NOT_FOUND here to match the cgo (unix) version
// cgo_unix.go (cgoLookupServicePort).
if e == _WSATYPE_NOT_FOUND || e == _WSAHOST_NOT_FOUND {
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
}
return 0, dnsError
err := os.NewSyscallError("getaddrinfow", e)
return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}
}
defer syscall.FreeAddrInfoW(result)
if result == nil {