2010-07-28 22:58:28 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2013-02-03 19:05:20 -07:00
|
|
|
"runtime"
|
2011-11-02 13:54:16 -06:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2010-07-28 22:58:28 -06:00
|
|
|
)
|
|
|
|
|
2013-01-17 23:05:04 -07:00
|
|
|
var (
|
|
|
|
lookupPort = oldLookupPort
|
|
|
|
lookupIP = oldLookupIP
|
|
|
|
)
|
|
|
|
|
2013-02-03 19:05:20 -07:00
|
|
|
func getprotobyname(name string) (proto int, err error) {
|
2012-01-31 08:36:45 -07:00
|
|
|
p, err := syscall.GetProtoByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return 0, os.NewSyscallError("GetProtoByName", err)
|
2011-10-11 17:29:22 -06:00
|
|
|
}
|
|
|
|
return int(p.Proto), nil
|
|
|
|
}
|
2010-07-28 22:58:28 -06:00
|
|
|
|
2013-02-03 19:05:20 -07:00
|
|
|
// lookupProtocol looks up IP protocol name and returns correspondent protocol number.
|
|
|
|
func lookupProtocol(name string) (proto int, err error) {
|
|
|
|
// GetProtoByName return value is stored in thread local storage.
|
|
|
|
// Start new os thread before the call to prevent races.
|
|
|
|
type result struct {
|
|
|
|
proto int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
ch := make(chan result)
|
|
|
|
go func() {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2013-02-03 19:05:20 -07:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
proto, err := getprotobyname(name)
|
|
|
|
ch <- result{proto: proto, err: err}
|
|
|
|
}()
|
|
|
|
r := <-ch
|
2013-08-21 19:33:37 -06:00
|
|
|
if r.err != nil {
|
|
|
|
if proto, ok := protocols[name]; ok {
|
2013-08-21 20:34:05 -06:00
|
|
|
return proto, nil
|
2013-08-21 19:33:37 -06:00
|
|
|
}
|
|
|
|
}
|
2013-02-03 19:05:20 -07:00
|
|
|
return r.proto, r.err
|
|
|
|
}
|
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func lookupHost(name string) (addrs []string, err error) {
|
2011-06-12 18:22:31 -06:00
|
|
|
ips, err := LookupIP(name)
|
2011-03-30 22:16:18 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addrs = make([]string, 0, len(ips))
|
|
|
|
for _, ip := range ips {
|
|
|
|
addrs = append(addrs, ip.String())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-02-03 19:05:20 -07:00
|
|
|
func gethostbyname(name string) (addrs []IP, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
// caller already acquired thread
|
2012-01-31 08:36:45 -07:00
|
|
|
h, err := syscall.GetHostByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, os.NewSyscallError("GetHostByName", err)
|
2010-07-28 22:58:28 -06:00
|
|
|
}
|
|
|
|
switch h.AddrType {
|
|
|
|
case syscall.AF_INET:
|
|
|
|
i := 0
|
2011-03-30 22:16:18 -06:00
|
|
|
addrs = make([]IP, 100) // plenty of room to grow
|
2010-07-28 22:58:28 -06:00
|
|
|
for p := (*[100](*[4]byte))(unsafe.Pointer(h.AddrList)); i < cap(addrs) && p[i] != nil; i++ {
|
2011-03-30 22:16:18 -06:00
|
|
|
addrs[i] = IPv4(p[i][0], p[i][1], p[i][2], p[i][3])
|
2010-07-28 22:58:28 -06:00
|
|
|
}
|
|
|
|
addrs = addrs[0:i]
|
|
|
|
default: // TODO(vcc): Implement non IPv4 address lookups.
|
2012-11-06 22:58:20 -07:00
|
|
|
return nil, os.NewSyscallError("LookupIP", syscall.EWINDOWS)
|
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2013-02-03 19:05:20 -07:00
|
|
|
func oldLookupIP(name string) (addrs []IP, err error) {
|
|
|
|
// GetHostByName return value is stored in thread local storage.
|
|
|
|
// Start new os thread before the call to prevent races.
|
|
|
|
type result struct {
|
|
|
|
addrs []IP
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
ch := make(chan result)
|
|
|
|
go func() {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2013-02-03 19:05:20 -07:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
addrs, err := gethostbyname(name)
|
|
|
|
ch <- result{addrs: addrs, err: err}
|
|
|
|
}()
|
|
|
|
r := <-ch
|
|
|
|
return r.addrs, r.err
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:58:20 -07:00
|
|
|
func newLookupIP(name string) (addrs []IP, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2012-11-06 22:58:20 -07:00
|
|
|
hints := syscall.AddrinfoW{
|
|
|
|
Family: syscall.AF_UNSPEC,
|
|
|
|
Socktype: syscall.SOCK_STREAM,
|
|
|
|
Protocol: syscall.IPPROTO_IP,
|
|
|
|
}
|
|
|
|
var result *syscall.AddrinfoW
|
|
|
|
e := syscall.GetAddrInfoW(syscall.StringToUTF16Ptr(name), nil, &hints, &result)
|
|
|
|
if e != nil {
|
|
|
|
return nil, os.NewSyscallError("GetAddrInfoW", e)
|
|
|
|
}
|
|
|
|
defer syscall.FreeAddrInfoW(result)
|
|
|
|
addrs = make([]IP, 0, 5)
|
|
|
|
for ; result != nil; result = result.Next {
|
|
|
|
addr := unsafe.Pointer(result.Addr)
|
|
|
|
switch result.Family {
|
|
|
|
case syscall.AF_INET:
|
|
|
|
a := (*syscall.RawSockaddrInet4)(addr).Addr
|
|
|
|
addrs = append(addrs, IPv4(a[0], a[1], a[2], a[3]))
|
|
|
|
case syscall.AF_INET6:
|
|
|
|
a := (*syscall.RawSockaddrInet6)(addr).Addr
|
|
|
|
addrs = append(addrs, IP{a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]})
|
|
|
|
default:
|
|
|
|
return nil, os.NewSyscallError("LookupIP", syscall.EWINDOWS)
|
|
|
|
}
|
2011-03-30 22:16:18 -06:00
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2013-02-03 19:05:20 -07:00
|
|
|
func getservbyname(network, service string) (port int, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2011-06-12 18:22:31 -06:00
|
|
|
switch network {
|
|
|
|
case "tcp4", "tcp6":
|
|
|
|
network = "tcp"
|
|
|
|
case "udp4", "udp6":
|
|
|
|
network = "udp"
|
|
|
|
}
|
2012-01-31 08:36:45 -07:00
|
|
|
s, err := syscall.GetServByName(service, network)
|
|
|
|
if err != nil {
|
|
|
|
return 0, os.NewSyscallError("GetServByName", err)
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
|
|
|
return int(syscall.Ntohs(s.Port)), nil
|
|
|
|
}
|
|
|
|
|
2013-02-03 19:05:20 -07:00
|
|
|
func oldLookupPort(network, service string) (port int, err error) {
|
|
|
|
// GetServByName return value is stored in thread local storage.
|
|
|
|
// Start new os thread before the call to prevent races.
|
|
|
|
type result struct {
|
|
|
|
port int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
ch := make(chan result)
|
|
|
|
go func() {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2013-02-03 19:05:20 -07:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
port, err := getservbyname(network, service)
|
|
|
|
ch <- result{port: port, err: err}
|
|
|
|
}()
|
|
|
|
r := <-ch
|
|
|
|
return r.port, r.err
|
|
|
|
}
|
|
|
|
|
2013-01-17 23:05:04 -07:00
|
|
|
func newLookupPort(network, service string) (port int, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2013-01-17 23:05:04 -07:00
|
|
|
var stype int32
|
|
|
|
switch network {
|
|
|
|
case "tcp4", "tcp6":
|
|
|
|
stype = syscall.SOCK_STREAM
|
|
|
|
case "udp4", "udp6":
|
|
|
|
stype = syscall.SOCK_DGRAM
|
|
|
|
}
|
|
|
|
hints := syscall.AddrinfoW{
|
|
|
|
Family: syscall.AF_UNSPEC,
|
|
|
|
Socktype: stype,
|
|
|
|
Protocol: syscall.IPPROTO_IP,
|
|
|
|
}
|
|
|
|
var result *syscall.AddrinfoW
|
|
|
|
e := syscall.GetAddrInfoW(nil, syscall.StringToUTF16Ptr(service), &hints, &result)
|
|
|
|
if e != nil {
|
|
|
|
return 0, os.NewSyscallError("GetAddrInfoW", e)
|
|
|
|
}
|
|
|
|
defer syscall.FreeAddrInfoW(result)
|
|
|
|
if result == nil {
|
|
|
|
return 0, os.NewSyscallError("LookupPort", syscall.EINVAL)
|
|
|
|
}
|
|
|
|
addr := unsafe.Pointer(result.Addr)
|
|
|
|
switch result.Family {
|
|
|
|
case syscall.AF_INET:
|
|
|
|
a := (*syscall.RawSockaddrInet4)(addr)
|
|
|
|
return int(syscall.Ntohs(a.Port)), nil
|
|
|
|
case syscall.AF_INET6:
|
|
|
|
a := (*syscall.RawSockaddrInet6)(addr)
|
|
|
|
return int(syscall.Ntohs(a.Port)), nil
|
|
|
|
}
|
|
|
|
return 0, os.NewSyscallError("LookupPort", syscall.EINVAL)
|
|
|
|
}
|
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func lookupCNAME(name string) (cname string, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2011-03-30 22:16:18 -06:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(name, syscall.DNS_TYPE_CNAME, 0, nil, &r, nil)
|
2014-08-15 00:37:19 -06:00
|
|
|
// windows returns DNS_INFO_NO_RECORDS if there are no CNAME-s
|
|
|
|
if errno, ok := e.(syscall.Errno); ok && errno == syscall.DNS_INFO_NO_RECORDS {
|
|
|
|
// if there are no aliases, the canonical name is the input name
|
|
|
|
if name == "" || name[len(name)-1] != '.' {
|
|
|
|
return name + ".", nil
|
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
2011-12-07 18:07:21 -07:00
|
|
|
if e != nil {
|
2011-11-13 20:44:52 -07:00
|
|
|
return "", os.NewSyscallError("LookupCNAME", e)
|
2011-03-30 22:16:18 -06:00
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
2014-08-15 00:37:19 -06:00
|
|
|
|
|
|
|
resolved := resolveCNAME(syscall.StringToUTF16Ptr(name), r)
|
|
|
|
cname = syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(resolved))[:]) + "."
|
2011-03-30 22:16:18 -06:00
|
|
|
return
|
2010-07-28 22:58:28 -06:00
|
|
|
}
|
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2011-10-18 11:57:04 -06:00
|
|
|
var target string
|
|
|
|
if service == "" && proto == "" {
|
|
|
|
target = name
|
|
|
|
} else {
|
|
|
|
target = "_" + service + "._" + proto + "." + name
|
|
|
|
}
|
2010-07-28 22:58:28 -06:00
|
|
|
var r *syscall.DNSRecord
|
2010-11-05 21:08:18 -06:00
|
|
|
e := syscall.DnsQuery(target, syscall.DNS_TYPE_SRV, 0, nil, &r, nil)
|
2011-12-07 18:07:21 -07:00
|
|
|
if e != nil {
|
2011-11-13 20:44:52 -07:00
|
|
|
return "", nil, os.NewSyscallError("LookupSRV", e)
|
2010-07-28 22:58:28 -06:00
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
2014-08-15 00:37:19 -06:00
|
|
|
|
2011-08-04 18:27:51 -06:00
|
|
|
addrs = make([]*SRV, 0, 10)
|
2014-08-15 00:37:19 -06:00
|
|
|
for _, p := range validRecs(r, syscall.DNS_TYPE_SRV, target) {
|
2010-07-28 22:58:28 -06:00
|
|
|
v := (*syscall.DNSSRVData)(unsafe.Pointer(&p.Data[0]))
|
2011-08-04 18:27:51 -06:00
|
|
|
addrs = append(addrs, &SRV{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Target))[:]), v.Port, v.Priority, v.Weight})
|
2010-07-28 22:58:28 -06:00
|
|
|
}
|
2011-08-04 18:27:51 -06:00
|
|
|
byPriorityWeight(addrs).sort()
|
2010-07-28 22:58:28 -06:00
|
|
|
return name, addrs, nil
|
|
|
|
}
|
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func lookupMX(name string) (mx []*MX, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2011-08-04 18:27:51 -06:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(name, syscall.DNS_TYPE_MX, 0, nil, &r, nil)
|
2011-12-07 18:07:21 -07:00
|
|
|
if e != nil {
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil, os.NewSyscallError("LookupMX", e)
|
2011-08-04 18:27:51 -06:00
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
2014-08-15 00:37:19 -06:00
|
|
|
|
2011-08-04 18:27:51 -06:00
|
|
|
mx = make([]*MX, 0, 10)
|
2014-08-15 00:37:19 -06:00
|
|
|
for _, p := range validRecs(r, syscall.DNS_TYPE_MX, name) {
|
2011-08-04 18:27:51 -06:00
|
|
|
v := (*syscall.DNSMXData)(unsafe.Pointer(&p.Data[0]))
|
|
|
|
mx = append(mx, &MX{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.NameExchange))[:]) + ".", v.Preference})
|
|
|
|
}
|
|
|
|
byPref(mx).sort()
|
|
|
|
return mx, nil
|
2011-05-03 21:08:36 -06:00
|
|
|
}
|
|
|
|
|
2012-10-18 00:39:04 -06:00
|
|
|
func lookupNS(name string) (ns []*NS, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2012-10-18 00:39:04 -06:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(name, syscall.DNS_TYPE_NS, 0, nil, &r, nil)
|
|
|
|
if e != nil {
|
|
|
|
return nil, os.NewSyscallError("LookupNS", e)
|
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
2014-08-15 00:37:19 -06:00
|
|
|
|
2012-10-18 00:39:04 -06:00
|
|
|
ns = make([]*NS, 0, 10)
|
2014-08-15 00:37:19 -06:00
|
|
|
for _, p := range validRecs(r, syscall.DNS_TYPE_NS, name) {
|
2012-10-18 00:39:04 -06:00
|
|
|
v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
|
|
|
|
ns = append(ns, &NS{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]) + "."})
|
|
|
|
}
|
|
|
|
return ns, nil
|
|
|
|
}
|
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func lookupTXT(name string) (txt []string, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2011-11-02 00:11:40 -06:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &r, nil)
|
2011-12-07 18:07:21 -07:00
|
|
|
if e != nil {
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil, os.NewSyscallError("LookupTXT", e)
|
2011-11-02 00:11:40 -06:00
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
2014-08-15 00:37:19 -06:00
|
|
|
|
2011-11-02 00:11:40 -06:00
|
|
|
txt = make([]string, 0, 10)
|
2014-08-15 00:37:19 -06:00
|
|
|
for _, p := range validRecs(r, syscall.DNS_TYPE_TEXT, name) {
|
|
|
|
d := (*syscall.DNSTXTData)(unsafe.Pointer(&p.Data[0]))
|
2011-11-02 00:11:40 -06:00
|
|
|
for _, v := range (*[1 << 10]*uint16)(unsafe.Pointer(&(d.StringArray[0])))[:d.StringCount] {
|
|
|
|
s := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(v))[:])
|
|
|
|
txt = append(txt, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2011-09-12 21:05:33 -06:00
|
|
|
}
|
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func lookupAddr(addr string) (name []string, err error) {
|
2013-08-16 20:43:05 -06:00
|
|
|
acquireThread()
|
|
|
|
defer releaseThread()
|
2011-08-04 18:27:51 -06:00
|
|
|
arpa, err := reverseaddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(arpa, syscall.DNS_TYPE_PTR, 0, nil, &r, nil)
|
2011-12-07 18:07:21 -07:00
|
|
|
if e != nil {
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil, os.NewSyscallError("LookupAddr", e)
|
2011-08-04 18:27:51 -06:00
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
2014-08-15 00:37:19 -06:00
|
|
|
|
2011-08-04 18:27:51 -06:00
|
|
|
name = make([]string, 0, 10)
|
2014-08-15 00:37:19 -06:00
|
|
|
for _, p := range validRecs(r, syscall.DNS_TYPE_PTR, arpa) {
|
2011-08-04 18:27:51 -06:00
|
|
|
v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
|
|
|
|
name = append(name, syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]))
|
|
|
|
}
|
|
|
|
return name, nil
|
2011-01-20 01:18:04 -07:00
|
|
|
}
|
2014-08-15 00:37:19 -06:00
|
|
|
|
|
|
|
const dnsSectionMask = 0x0003
|
|
|
|
|
|
|
|
// returns only results applicable to name and resolves CNAME entries
|
|
|
|
func validRecs(r *syscall.DNSRecord, dnstype uint16, name string) []*syscall.DNSRecord {
|
|
|
|
cname := syscall.StringToUTF16Ptr(name)
|
|
|
|
if dnstype != syscall.DNS_TYPE_CNAME {
|
|
|
|
cname = resolveCNAME(cname, r)
|
|
|
|
}
|
|
|
|
rec := make([]*syscall.DNSRecord, 0, 10)
|
|
|
|
for p := r; p != nil; p = p.Next {
|
|
|
|
if p.Dw&dnsSectionMask != syscall.DnsSectionAnswer {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.Type != dnstype {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !syscall.DnsNameCompare(cname, p.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rec = append(rec, p)
|
|
|
|
}
|
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the last CNAME in chain
|
|
|
|
func resolveCNAME(name *uint16, r *syscall.DNSRecord) *uint16 {
|
|
|
|
// limit cname resolving to 10 in case of a infinite CNAME loop
|
|
|
|
Cname:
|
|
|
|
for cnameloop := 0; cnameloop < 10; cnameloop++ {
|
|
|
|
for p := r; p != nil; p = p.Next {
|
|
|
|
if p.Dw&dnsSectionMask != syscall.DnsSectionAnswer {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.Type != syscall.DNS_TYPE_CNAME {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !syscall.DnsNameCompare(name, p.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name = (*syscall.DNSPTRData)(unsafe.Pointer(&r.Data[0])).Host
|
|
|
|
continue Cname
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|