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.
|
|
|
|
|
2015-04-17 02:56:58 -06:00
|
|
|
// +build cgo,!netgo
|
2015-03-30 15:07:18 -06:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
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
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2016-05-08 19:17:59 -06:00
|
|
|
"context"
|
2011-04-20 13:21:59 -06:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
2016-05-08 19:17:59 -06:00
|
|
|
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, name)
|
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
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-08 19:17:59 -06:00
|
|
|
func cgoLookupPort(ctx context.Context, network, service string) (port int, err error, completed bool) {
|
2011-04-20 13:21:59 -06:00
|
|
|
var hints C.struct_addrinfo
|
2015-04-17 02:56:58 -06:00
|
|
|
switch network {
|
|
|
|
case "": // no hints
|
2011-04-20 13:21:59 -06:00
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
hints.ai_socktype = C.SOCK_STREAM
|
|
|
|
hints.ai_protocol = C.IPPROTO_TCP
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
hints.ai_socktype = C.SOCK_DGRAM
|
|
|
|
hints.ai_protocol = C.IPPROTO_UDP
|
|
|
|
default:
|
2015-04-19 05:54:01 -06:00
|
|
|
return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}, true
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2015-04-17 02:56:58 -06:00
|
|
|
if len(network) >= 4 {
|
|
|
|
switch network[3] {
|
2011-04-20 13:21:59 -06:00
|
|
|
case '4':
|
|
|
|
hints.ai_family = C.AF_INET
|
|
|
|
case '6':
|
|
|
|
hints.ai_family = C.AF_INET6
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 13:21:59 -06:00
|
|
|
|
2016-05-08 19:17:59 -06:00
|
|
|
func cgoLookupServicePort(hints *C.struct_addrinfo, network, service string) (port int, err error) {
|
2011-04-20 13:21:59 -06:00
|
|
|
s := C.CString(service)
|
2016-09-09 16:51:11 -06:00
|
|
|
// Lowercase the service name in the C-allocated memory.
|
|
|
|
for i := 0; i < len(service); i++ {
|
|
|
|
bp := (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(s)) + uintptr(i)))
|
|
|
|
*bp = lowerASCII(*bp)
|
|
|
|
}
|
2015-04-17 02:56:58 -06:00
|
|
|
var res *C.struct_addrinfo
|
2011-04-20 13:21:59 -06:00
|
|
|
defer C.free(unsafe.Pointer(s))
|
2016-05-08 19:17:59 -06:00
|
|
|
gerrno, err := C.getaddrinfo(nil, s, hints, &res)
|
2015-04-17 02:56:58 -06:00
|
|
|
if gerrno != 0 {
|
|
|
|
switch gerrno {
|
|
|
|
case C.EAI_SYSTEM:
|
|
|
|
if err == nil { // see golang.org/issue/6232
|
|
|
|
err = syscall.EMFILE
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2015-04-17 02:56:58 -06:00
|
|
|
default:
|
|
|
|
err = addrinfoErrno(gerrno)
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2015-04-17 02:56:58 -06:00
|
|
|
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))
|
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
|
|
|
case C.AF_INET6:
|
|
|
|
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.ai_addr))
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
|
2016-05-08 19:17:59 -06:00
|
|
|
func cgoPortLookup(result chan<- portLookupResult, hints *C.struct_addrinfo, network, service string) {
|
|
|
|
port, err := cgoLookupServicePort(hints, network, service)
|
|
|
|
result <- portLookupResult{port, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
|
|
|
|
2011-04-20 13:21:59 -06:00
|
|
|
var hints C.struct_addrinfo
|
2015-04-17 02:56:58 -06:00
|
|
|
hints.ai_flags = cgoAddrInfoFlags
|
2013-08-09 10:23:27 -06:00
|
|
|
hints.ai_socktype = C.SOCK_STREAM
|
2011-04-20 13:21:59 -06:00
|
|
|
|
|
|
|
h := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(h))
|
2015-04-17 02:56:58 -06:00
|
|
|
var res *C.struct_addrinfo
|
2011-04-20 13:21:59 -06:00
|
|
|
gerrno, err := C.getaddrinfo(h, nil, &hints, &res)
|
|
|
|
if gerrno != 0 {
|
2015-04-17 02:56:58 -06:00
|
|
|
switch gerrno {
|
|
|
|
case C.EAI_SYSTEM:
|
2013-09-11 09:38:56 -06:00
|
|
|
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
|
|
|
|
}
|
2015-04-17 02:56:58 -06:00
|
|
|
case C.EAI_NONAME:
|
2015-04-19 05:54:01 -06:00
|
|
|
err = errNoSuchHost
|
2015-04-17 02:56:58 -06:00
|
|
|
default:
|
2015-04-19 05:54:01 -06:00
|
|
|
err = addrinfoErrno(gerrno)
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
return nil, "", &DNSError{Err: err.Error(), Name: name}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
defer C.freeaddrinfo(res)
|
2015-04-17 02:56:58 -06:00
|
|
|
|
2011-04-20 13:21:59 -06:00
|
|
|
if res != nil {
|
|
|
|
cname = C.GoString(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 {
|
2013-08-09 10:23:27 -06:00
|
|
|
// We only asked for SOCK_STREAM, but check anyhow.
|
2011-04-20 13:21:59 -06:00
|
|
|
if r.ai_socktype != C.SOCK_STREAM {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch r.ai_family {
|
|
|
|
case C.AF_INET:
|
|
|
|
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr))
|
2015-02-12 21:24:47 -07:00
|
|
|
addr := IPAddr{IP: copyIP(sa.Addr[:])}
|
|
|
|
addrs = append(addrs, addr)
|
2011-04-20 13:21:59 -06:00
|
|
|
case C.AF_INET6:
|
|
|
|
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.ai_addr))
|
2015-02-12 21:24:47 -07:00
|
|
|
addr := IPAddr{IP: copyIP(sa.Addr[:]), Zone: zoneToString(int(sa.Scope_id))}
|
|
|
|
addrs = append(addrs, addr)
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
return addrs, cname, nil
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
|
2016-05-08 19:17:59 -06:00
|
|
|
func cgoIPLookup(result chan<- ipLookupResult, name string) {
|
|
|
|
addrs, cname, err := cgoLookupIPCNAME(name)
|
|
|
|
result <- ipLookupResult{addrs, cname, err}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
|
2016-05-08 19:17:59 -06:00
|
|
|
func cgoLookupIP(ctx context.Context, name string) (addrs []IPAddr, err error, completed bool) {
|
|
|
|
if ctx.Done() == nil {
|
|
|
|
addrs, _, err = cgoLookupIPCNAME(name)
|
|
|
|
return addrs, err, true
|
|
|
|
}
|
|
|
|
result := make(chan ipLookupResult, 1)
|
|
|
|
go cgoIPLookup(result, 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(name)
|
|
|
|
return cname, err, true
|
|
|
|
}
|
|
|
|
result := make(chan ipLookupResult, 1)
|
|
|
|
go cgoIPLookup(result, name)
|
|
|
|
select {
|
|
|
|
case r := <-result:
|
|
|
|
return r.cname, r.err, true
|
|
|
|
case <-ctx.Done():
|
|
|
|
return "", mapErr(ctx.Err()), false
|
|
|
|
}
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 04:08:41 -07:00
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
2016-05-08 19:17:59 -06:00
|
|
|
func cgoLookupPTR(ctx context.Context, addr string) (names []string, err error, completed bool) {
|
2015-11-26 20:06:11 -07:00
|
|
|
var zone string
|
|
|
|
ip := parseIPv4(addr)
|
|
|
|
if ip == nil {
|
|
|
|
ip, zone = parseIPv6(addr, true)
|
|
|
|
}
|
2015-01-28 04:08:41 -07:00
|
|
|
if ip == nil {
|
|
|
|
return nil, &DNSError{Err: "invalid address", Name: addr}, true
|
|
|
|
}
|
2015-11-26 20:06:11 -07:00
|
|
|
sa, salen := cgoSockaddr(ip, zone)
|
2015-01-28 04:08:41 -07:00
|
|
|
if sa == nil {
|
|
|
|
return nil, &DNSError{Err: "invalid address " + ip.String(), Name: addr}, true
|
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
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 *C.struct_sockaddr, salen C.socklen_t) (names []string, err error) {
|
|
|
|
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)
|
|
|
|
if gerrno == 0 || gerrno != C.EAI_OVERFLOW {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if gerrno != 0 {
|
|
|
|
switch gerrno {
|
|
|
|
case C.EAI_SYSTEM:
|
|
|
|
if err == nil { // see golang.org/issue/6232
|
|
|
|
err = syscall.EMFILE
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = addrinfoErrno(gerrno)
|
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
return nil, &DNSError{Err: err.Error(), Name: addr}
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
if b[i] == 0 {
|
|
|
|
b = b[:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 19:17:59 -06:00
|
|
|
return []string{absDomainName(b)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cgoReverseLookup(result chan<- reverseLookupResult, addr string, sa *C.struct_sockaddr, salen C.socklen_t) {
|
|
|
|
names, err := cgoLookupAddrPTR(addr, sa, salen)
|
|
|
|
result <- reverseLookupResult{names, err}
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
|
|
|
|
2015-11-26 20:06:11 -07: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 {
|
|
|
|
return cgoSockaddrInet4(ip4), C.socklen_t(syscall.SizeofSockaddrInet4)
|
|
|
|
}
|
|
|
|
if ip6 := ip.To16(); ip6 != nil {
|
2015-11-26 20:06:11 -07:00
|
|
|
return cgoSockaddrInet6(ip6, zoneToInt(zone)), C.socklen_t(syscall.SizeofSockaddrInet6)
|
2015-01-28 04:08:41 -07:00
|
|
|
}
|
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
|
2011-04-20 13:21:59 -06:00
|
|
|
func copyIP(x IP) IP {
|
2013-09-23 20:40:24 -06:00
|
|
|
if len(x) < 16 {
|
|
|
|
return x.To16()
|
|
|
|
}
|
2011-04-20 13:21:59 -06:00
|
|
|
y := make(IP, len(x))
|
|
|
|
copy(y, x)
|
|
|
|
return y
|
|
|
|
}
|