Commit 6d3a7e79 authored by Mikio Hara's avatar Mikio Hara

net: clean up cgo

This change adds a type addrinfoErrno to represent getaddrinfo,
getnameinfo-specific errors, and uses it in cgo-based lookup functions.

Also retags cgo files for clarification and does minor cleanup.

Change-Id: I6db7130ad7bf35bbd4e8839a97759e1364c43828
Reviewed-on: https://go-review.googlesource.com/9020Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 7816a096
...@@ -9,6 +9,4 @@ package net ...@@ -9,6 +9,4 @@ package net
//#include <netdb.h> //#include <netdb.h>
import "C" import "C"
func cgoAddrInfoFlags() C.int { const cgoAddrInfoFlags = C.AI_CANONNAME
return C.AI_CANONNAME
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build !netgo // +build cgo,!netgo
// +build darwin dragonfly freebsd // +build darwin dragonfly freebsd
package net package net
...@@ -12,6 +12,4 @@ package net ...@@ -12,6 +12,4 @@ package net
*/ */
import "C" import "C"
func cgoAddrInfoFlags() C.int { const cgoAddrInfoFlags = (C.AI_CANONNAME | C.AI_V4MAPPED | C.AI_ALL) & C.AI_MASK
return (C.AI_CANONNAME | C.AI_V4MAPPED | C.AI_ALL) & C.AI_MASK
}
...@@ -11,12 +11,10 @@ package net ...@@ -11,12 +11,10 @@ package net
*/ */
import "C" import "C"
func cgoAddrInfoFlags() C.int { // NOTE(rsc): In theory there are approximately balanced
// NOTE(rsc): In theory there are approximately balanced // arguments for and against including AI_ADDRCONFIG
// arguments for and against including AI_ADDRCONFIG // in the flags (it includes IPv4 results only on IPv4 systems,
// in the flags (it includes IPv4 results only on IPv4 systems, // and similarly for IPv6), but in practice setting it causes
// and similarly for IPv6), but in practice setting it causes // getaddrinfo to return the wrong canonical name on Linux.
// getaddrinfo to return the wrong canonical name on Linux. // So definitely leave it out.
// So definitely leave it out. const cgoAddrInfoFlags = C.AI_CANONNAME | C.AI_V4MAPPED | C.AI_ALL
return C.AI_CANONNAME | C.AI_V4MAPPED | C.AI_ALL
}
...@@ -11,6 +11,4 @@ package net ...@@ -11,6 +11,4 @@ package net
*/ */
import "C" import "C"
func cgoAddrInfoFlags() C.int { const cgoAddrInfoFlags = C.AI_CANONNAME
return C.AI_CANONNAME
}
...@@ -11,6 +11,4 @@ package net ...@@ -11,6 +11,4 @@ package net
*/ */
import "C" import "C"
func cgoAddrInfoFlags() C.int { const cgoAddrInfoFlags = C.AI_CANONNAME
return C.AI_CANONNAME
}
...@@ -4,10 +4,14 @@ ...@@ -4,10 +4,14 @@
// +build !cgo netgo // +build !cgo netgo
// Stub cgo routines for systems that do not use cgo to do network lookups.
package net package net
type addrinfoErrno int
func (eai addrinfoErrno) Error() string { return "<nil>" }
func (eai addrinfoErrno) Temporary() bool { return false }
func (eai addrinfoErrno) Timeout() bool { return false }
func cgoLookupHost(name string) (addrs []string, err error, completed bool) { func cgoLookupHost(name string) (addrs []string, err error, completed bool) {
return nil, nil, false return nil, nil, false
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build !netgo // +build cgo,!netgo
// +build darwin dragonfly freebsd linux netbsd openbsd // +build darwin dragonfly freebsd linux netbsd openbsd
package net package net
...@@ -23,24 +23,30 @@ import ( ...@@ -23,24 +23,30 @@ import (
"unsafe" "unsafe"
) )
func cgoLookupHost(name string) (addrs []string, err error, completed bool) { // An addrinfoErrno represents a getaddrinfo, getnameinfo-specific
ip, err, completed := cgoLookupIP(name) // error number. It's a signed number and a zero value is a non-error
for _, p := range ip { // by convention.
addrs = append(addrs, p.String()) type addrinfoErrno int
func (eai addrinfoErrno) Error() string { return C.GoString(C.gai_strerror(C.int(eai))) }
func (eai addrinfoErrno) Temporary() bool { return eai == C.EAI_AGAIN }
func (eai addrinfoErrno) Timeout() bool { return false }
func cgoLookupHost(name string) (hosts []string, err error, completed bool) {
addrs, err, completed := cgoLookupIP(name)
for _, addr := range addrs {
hosts = append(hosts, addr.String())
} }
return return
} }
func cgoLookupPort(net, service string) (port int, err error, completed bool) { func cgoLookupPort(network, service string) (port int, err error, completed bool) {
acquireThread() acquireThread()
defer releaseThread() defer releaseThread()
var res *C.struct_addrinfo
var hints C.struct_addrinfo var hints C.struct_addrinfo
switch network {
switch net { case "": // no hints
case "":
// no hints
case "tcp", "tcp4", "tcp6": case "tcp", "tcp4", "tcp6":
hints.ai_socktype = C.SOCK_STREAM hints.ai_socktype = C.SOCK_STREAM
hints.ai_protocol = C.IPPROTO_TCP hints.ai_protocol = C.IPPROTO_TCP
...@@ -48,10 +54,10 @@ func cgoLookupPort(net, service string) (port int, err error, completed bool) { ...@@ -48,10 +54,10 @@ func cgoLookupPort(net, service string) (port int, err error, completed bool) {
hints.ai_socktype = C.SOCK_DGRAM hints.ai_socktype = C.SOCK_DGRAM
hints.ai_protocol = C.IPPROTO_UDP hints.ai_protocol = C.IPPROTO_UDP
default: default:
return 0, UnknownNetworkError(net), true return 0, UnknownNetworkError(network), true
} }
if len(net) >= 4 { if len(network) >= 4 {
switch net[3] { switch network[3] {
case '4': case '4':
hints.ai_family = C.AF_INET hints.ai_family = C.AF_INET
case '6': case '6':
...@@ -60,45 +66,53 @@ func cgoLookupPort(net, service string) (port int, err error, completed bool) { ...@@ -60,45 +66,53 @@ func cgoLookupPort(net, service string) (port int, err error, completed bool) {
} }
s := C.CString(service) s := C.CString(service)
var res *C.struct_addrinfo
defer C.free(unsafe.Pointer(s)) defer C.free(unsafe.Pointer(s))
if C.getaddrinfo(nil, s, &hints, &res) == 0 { gerrno, err := C.getaddrinfo(nil, s, &hints, &res)
defer C.freeaddrinfo(res) if gerrno != 0 {
for r := res; r != nil; r = r.ai_next { switch gerrno {
switch r.ai_family { case C.EAI_SYSTEM:
default: if err == nil { // see golang.org/issue/6232
continue err = syscall.EMFILE
case C.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
case C.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
} }
default:
err = addrinfoErrno(gerrno)
} }
return 0, err, true
} }
return 0, &AddrError{"unknown port", net + "/" + service}, true defer C.freeaddrinfo(res)
for r := res; r != nil; r = r.ai_next {
switch r.ai_family {
case C.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
case C.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
}
}
return 0, &AddrError{"unknown port", network + "/" + service}, true
} }
func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, completed bool) { func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, completed bool) {
acquireThread() acquireThread()
defer releaseThread() defer releaseThread()
var res *C.struct_addrinfo
var hints C.struct_addrinfo var hints C.struct_addrinfo
hints.ai_flags = cgoAddrInfoFlags
hints.ai_flags = cgoAddrInfoFlags()
hints.ai_socktype = C.SOCK_STREAM hints.ai_socktype = C.SOCK_STREAM
h := C.CString(name) h := C.CString(name)
defer C.free(unsafe.Pointer(h)) defer C.free(unsafe.Pointer(h))
var res *C.struct_addrinfo
gerrno, err := C.getaddrinfo(h, nil, &hints, &res) gerrno, err := C.getaddrinfo(h, nil, &hints, &res)
if gerrno != 0 { if gerrno != 0 {
var str string var str string
if gerrno == C.EAI_NONAME { switch gerrno {
str = noSuchHost case C.EAI_SYSTEM:
} else if gerrno == C.EAI_SYSTEM {
if err == nil { if err == nil {
// err should not be nil, but sometimes getaddrinfo returns // err should not be nil, but sometimes getaddrinfo returns
// gerrno == C.EAI_SYSTEM with err == nil on Linux. // gerrno == C.EAI_SYSTEM with err == nil on Linux.
...@@ -110,12 +124,15 @@ func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, com ...@@ -110,12 +124,15 @@ func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, com
err = syscall.EMFILE err = syscall.EMFILE
} }
str = err.Error() str = err.Error()
} else { case C.EAI_NONAME:
str = C.GoString(C.gai_strerror(gerrno)) str = noSuchHost
default:
str = addrinfoErrno(gerrno).Error()
} }
return nil, "", &DNSError{Err: str, Name: name}, true return nil, "", &DNSError{Err: str, Name: name}, true
} }
defer C.freeaddrinfo(res) defer C.freeaddrinfo(res)
if res != nil { if res != nil {
cname = C.GoString(res.ai_canonname) cname = C.GoString(res.ai_canonname)
if cname == "" { if cname == "" {
...@@ -131,8 +148,6 @@ func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, com ...@@ -131,8 +148,6 @@ func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, com
continue continue
} }
switch r.ai_family { switch r.ai_family {
default:
continue
case C.AF_INET: case C.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr)) sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr))
addr := IPAddr{IP: copyIP(sa.Addr[:])} addr := IPAddr{IP: copyIP(sa.Addr[:])}
......
// Copyright 2015 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
package net
type addrinfoErrno int
func (eai addrinfoErrno) Error() string { return "<nil>" }
func (eai addrinfoErrno) Temporary() bool { return false }
func (eai addrinfoErrno) Timeout() bool { return false }
...@@ -84,7 +84,7 @@ second: ...@@ -84,7 +84,7 @@ second:
return nil return nil
} }
switch err := nestedErr.(type) { switch err := nestedErr.(type) {
case *AddrError, *DNSError, InvalidAddrError, *ParseError, UnknownNetworkError, *timeoutError: case *AddrError, addrinfoErrno, *DNSError, InvalidAddrError, *ParseError, *timeoutError, UnknownNetworkError:
return nil return nil
case *DNSConfigError: case *DNSConfigError:
nestedErr = err.Err nestedErr = err.Err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment