2010-12-03 05:34:57 +01: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"
|
|
|
|
"sync"
|
2011-12-07 02:11:29 +01:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
var (
|
|
|
|
protoentLock sync.Mutex
|
|
|
|
hostentLock sync.Mutex
|
|
|
|
serventLock sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// lookupProtocol looks up IP protocol name and returns correspondent protocol number.
|
2011-12-03 03:17:34 +01:00
|
|
|
func lookupProtocol(name string) (proto int, err error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
protoentLock.Lock()
|
|
|
|
defer protoentLock.Unlock()
|
|
|
|
p, e := syscall.GetProtoByName(name)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-10-27 01:57:58 +02:00
|
|
|
return 0, os.NewSyscallError("GetProtoByName", e)
|
|
|
|
}
|
|
|
|
return int(p.Proto), nil
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupHost(name string) (addrs []string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
ips, err := LookupIP(name)
|
2011-04-07 19:09:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addrs = make([]string, 0, len(ips))
|
|
|
|
for _, ip := range ips {
|
|
|
|
addrs = append(addrs, ip.String())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupIP(name string) (addrs []IP, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
hostentLock.Lock()
|
|
|
|
defer hostentLock.Unlock()
|
|
|
|
h, e := syscall.GetHostByName(name)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-04-07 19:09:10 +02:00
|
|
|
return nil, os.NewSyscallError("GetHostByName", e)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
switch h.AddrType {
|
|
|
|
case syscall.AF_INET:
|
|
|
|
i := 0
|
2011-04-07 19:09:10 +02:00
|
|
|
addrs = make([]IP, 100) // plenty of room to grow
|
2010-12-03 05:34:57 +01:00
|
|
|
for p := (*[100](*[4]byte))(unsafe.Pointer(h.AddrList)); i < cap(addrs) && p[i] != nil; i++ {
|
2011-04-07 19:09:10 +02:00
|
|
|
addrs[i] = IPv4(p[i][0], p[i][1], p[i][2], p[i][3])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
addrs = addrs[0:i]
|
|
|
|
default: // TODO(vcc): Implement non IPv4 address lookups.
|
2011-04-07 19:09:10 +02:00
|
|
|
return nil, os.NewSyscallError("LookupHost", syscall.EWINDOWS)
|
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupPort(network, service string) (port int, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
switch network {
|
|
|
|
case "tcp4", "tcp6":
|
|
|
|
network = "tcp"
|
|
|
|
case "udp4", "udp6":
|
|
|
|
network = "udp"
|
|
|
|
}
|
|
|
|
serventLock.Lock()
|
|
|
|
defer serventLock.Unlock()
|
|
|
|
s, e := syscall.GetServByName(service, network)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return 0, os.NewSyscallError("GetServByName", e)
|
|
|
|
}
|
|
|
|
return int(syscall.Ntohs(s.Port)), nil
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupCNAME(name string) (cname string, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(name, syscall.DNS_TYPE_CNAME, 0, nil, &r, nil)
|
2012-01-12 02:31:45 +01:00
|
|
|
if e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return "", os.NewSyscallError("LookupCNAME", e)
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
|
|
|
if r != nil && r.Type == syscall.DNS_TYPE_CNAME {
|
|
|
|
v := (*syscall.DNSPTRData)(unsafe.Pointer(&r.Data[0]))
|
|
|
|
cname = syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]) + "."
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
var target string
|
|
|
|
if service == "" && proto == "" {
|
|
|
|
target = name
|
|
|
|
} else {
|
|
|
|
target = "_" + service + "._" + proto + "." + name
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(target, syscall.DNS_TYPE_SRV, 0, nil, &r, nil)
|
2012-01-12 02:31:45 +01:00
|
|
|
if e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return "", nil, os.NewSyscallError("LookupSRV", e)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
2011-09-16 17:47:21 +02:00
|
|
|
addrs = make([]*SRV, 0, 10)
|
2010-12-03 05:34:57 +01:00
|
|
|
for p := r; p != nil && p.Type == syscall.DNS_TYPE_SRV; p = p.Next {
|
|
|
|
v := (*syscall.DNSSRVData)(unsafe.Pointer(&p.Data[0]))
|
2011-09-16 17:47:21 +02:00
|
|
|
addrs = append(addrs, &SRV{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Target))[:]), v.Port, v.Priority, v.Weight})
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
byPriorityWeight(addrs).sort()
|
2010-12-03 05:34:57 +01:00
|
|
|
return name, addrs, nil
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupMX(name string) (mx []*MX, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(name, syscall.DNS_TYPE_MX, 0, nil, &r, nil)
|
2012-01-12 02:31:45 +01:00
|
|
|
if e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, os.NewSyscallError("LookupMX", e)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
|
|
|
mx = make([]*MX, 0, 10)
|
|
|
|
for p := r; p != nil && p.Type == syscall.DNS_TYPE_MX; p = p.Next {
|
|
|
|
v := (*syscall.DNSMXData)(unsafe.Pointer(&p.Data[0]))
|
|
|
|
mx = append(mx, &MX{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.NameExchange))[:]) + ".", v.Preference})
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
byPref(mx).sort()
|
|
|
|
return mx, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupTXT(name string) (txt []string, err error) {
|
2011-12-07 02:11:29 +01:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &r, nil)
|
2012-01-12 02:31:45 +01:00
|
|
|
if e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, os.NewSyscallError("LookupTXT", e)
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
|
|
|
txt = make([]string, 0, 10)
|
|
|
|
if r != nil && r.Type == syscall.DNS_TYPE_TEXT {
|
|
|
|
d := (*syscall.DNSTXTData)(unsafe.Pointer(&r.Data[0]))
|
|
|
|
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-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupAddr(addr string) (name []string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
arpa, err := reverseaddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
var r *syscall.DNSRecord
|
|
|
|
e := syscall.DnsQuery(arpa, syscall.DNS_TYPE_PTR, 0, nil, &r, nil)
|
2012-01-12 02:31:45 +01:00
|
|
|
if e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, os.NewSyscallError("LookupAddr", e)
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
defer syscall.DnsRecordListFree(r, 1)
|
|
|
|
name = make([]string, 0, 10)
|
|
|
|
for p := r; p != nil && p.Type == syscall.DNS_TYPE_PTR; p = p.Next {
|
|
|
|
v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
|
|
|
|
name = append(name, syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]))
|
|
|
|
}
|
|
|
|
return name, nil
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|