2011-09-16 17:47:21 +02:00
|
|
|
// Copyright 2011 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.
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
2011-10-27 01:57:58 +02:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
var onceReadProtocols sync.Once
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// readProtocols loads contents of /etc/protocols into protocols map
|
|
|
|
// for quick access.
|
|
|
|
func readProtocols() {
|
|
|
|
if file, err := open("/etc/protocols"); err == nil {
|
|
|
|
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
|
|
|
|
// tcp 6 TCP # transmission control protocol
|
|
|
|
if i := byteIndex(line, '#'); i >= 0 {
|
|
|
|
line = line[0:i]
|
|
|
|
}
|
|
|
|
f := getFields(line)
|
|
|
|
if len(f) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if proto, _, ok := dtoi(f[1], 0); ok {
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, ok := protocols[f[0]]; !ok {
|
|
|
|
protocols[f[0]] = proto
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
for _, alias := range f[2:] {
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, ok := protocols[alias]; !ok {
|
|
|
|
protocols[alias] = proto
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupProtocol looks up IP protocol name in /etc/protocols 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
|
|
|
onceReadProtocols.Do(readProtocols)
|
|
|
|
proto, found := protocols[name]
|
|
|
|
if !found {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, errors.New("unknown IP protocol specified: " + name)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupHost(host string) (addrs []string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
addrs, err, ok := cgoLookupHost(host)
|
|
|
|
if !ok {
|
|
|
|
addrs, err = goLookupHost(host)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupIP(host string) (addrs []IP, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
addrs, err, ok := cgoLookupIP(host)
|
|
|
|
if !ok {
|
|
|
|
addrs, err = goLookupIP(host)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
port, err, ok := cgoLookupPort(network, service)
|
|
|
|
if !ok {
|
|
|
|
port, err = goLookupPort(network, service)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupCNAME(name string) (cname string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
cname, err, ok := cgoLookupCNAME(name)
|
|
|
|
if !ok {
|
|
|
|
cname, err = goLookupCNAME(name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
var records []dnsRR
|
|
|
|
cname, records, err = lookup(target, dnsTypeSRV)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addrs = make([]*SRV, len(records))
|
|
|
|
for i, rr := range records {
|
|
|
|
r := rr.(*dnsRR_SRV)
|
|
|
|
addrs[i] = &SRV{r.Target, r.Port, r.Priority, r.Weight}
|
|
|
|
}
|
|
|
|
byPriorityWeight(addrs).sort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupMX(name string) (mx []*MX, err error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
_, records, err := lookup(name, dnsTypeMX)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
mx = make([]*MX, len(records))
|
|
|
|
for i, rr := range records {
|
|
|
|
r := rr.(*dnsRR_MX)
|
2011-09-16 17:47:21 +02:00
|
|
|
mx[i] = &MX{r.Mx, r.Pref}
|
|
|
|
}
|
|
|
|
byPref(mx).sort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
func lookupNS(name string) (ns []*NS, err error) {
|
|
|
|
_, records, err := lookup(name, dnsTypeNS)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ns = make([]*NS, len(records))
|
|
|
|
for i, r := range records {
|
|
|
|
r := r.(*dnsRR_NS)
|
|
|
|
ns[i] = &NS{r.Ns}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupTXT(name string) (txt []string, err error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
_, records, err := lookup(name, dnsTypeTXT)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
txt = make([]string, len(records))
|
|
|
|
for i, r := range records {
|
|
|
|
txt[i] = r.(*dnsRR_TXT).Txt
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupAddr(addr string) (name []string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
name = lookupStaticAddr(addr)
|
|
|
|
if len(name) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var arpa string
|
|
|
|
arpa, err = reverseaddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var records []dnsRR
|
|
|
|
_, records, err = lookup(arpa, dnsTypePTR)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name = make([]string, len(records))
|
|
|
|
for i := range records {
|
|
|
|
r := records[i].(*dnsRR_PTR)
|
|
|
|
name[i] = r.Ptr
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|