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.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
2011-09-16 17:47:21 +02:00
|
|
|
"os"
|
2012-03-02 21:01:37 +01:00
|
|
|
"syscall"
|
2011-09-16 17:47:21 +02:00
|
|
|
)
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func query(filename, query string, bufSize int) (res []string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
_, err = file.WriteString(query)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = file.Seek(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf := make([]byte, bufSize)
|
|
|
|
for {
|
|
|
|
n, _ := file.Read(buf)
|
|
|
|
if n <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res = append(res, string(buf[:n]))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func queryCS(net, host, service string) (res []string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
switch net {
|
|
|
|
case "tcp4", "tcp6":
|
|
|
|
net = "tcp"
|
|
|
|
case "udp4", "udp6":
|
|
|
|
net = "udp"
|
|
|
|
}
|
|
|
|
if host == "" {
|
|
|
|
host = "*"
|
|
|
|
}
|
|
|
|
return query("/net/cs", net+"!"+host+"!"+service, 128)
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func queryCS1(net string, ip IP, port int) (clone, dest string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
ips := "*"
|
2011-12-02 20:34:41 +01:00
|
|
|
if len(ip) != 0 && !ip.IsUnspecified() {
|
2011-09-16 17:47:21 +02:00
|
|
|
ips = ip.String()
|
|
|
|
}
|
|
|
|
lines, err := queryCS(net, ips, itoa(port))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f := getFields(lines[0])
|
|
|
|
if len(f) < 2 {
|
2011-12-03 03:17:34 +01:00
|
|
|
return "", "", errors.New("net: bad response from ndb/cs")
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
clone, dest = f[0], f[1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func queryDNS(addr string, typ string) (res []string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
return query("/net/dns", addr+" "+typ, 1024)
|
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
func lookupProtocol(name string) (proto int, err error) {
|
|
|
|
// TODO: Implement this
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, syscall.EPLAN9
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupHost(host string) (addrs []string, err error) {
|
2012-03-06 18:57:23 +01:00
|
|
|
// Use /net/cs instead of /net/dns because cs knows about
|
2011-09-16 17:47:21 +02:00
|
|
|
// host names in local network (e.g. from /lib/ndb/local)
|
|
|
|
lines, err := queryCS("tcp", host, "1")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
f := getFields(line)
|
|
|
|
if len(f) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := f[1]
|
|
|
|
if i := byteIndex(addr, '!'); i >= 0 {
|
|
|
|
addr = addr[:i] // remove port
|
|
|
|
}
|
|
|
|
if ParseIP(addr) == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupIP(host string) (ips []IP, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
addrs, err := LookupHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if ip := ParseIP(addr); ip != nil {
|
|
|
|
ips = append(ips, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
switch network {
|
|
|
|
case "tcp4", "tcp6":
|
|
|
|
network = "tcp"
|
|
|
|
case "udp4", "udp6":
|
|
|
|
network = "udp"
|
|
|
|
}
|
|
|
|
lines, err := queryCS(network, "127.0.0.1", service)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
unknownPortError := &AddrError{"unknown port", network + "/" + service}
|
|
|
|
if len(lines) == 0 {
|
|
|
|
return 0, unknownPortError
|
|
|
|
}
|
|
|
|
f := getFields(lines[0])
|
|
|
|
if len(f) < 2 {
|
|
|
|
return 0, unknownPortError
|
|
|
|
}
|
|
|
|
s := f[1]
|
|
|
|
if i := byteIndex(s, '!'); i >= 0 {
|
|
|
|
s = s[i+1:] // remove address
|
|
|
|
}
|
|
|
|
if n, _, ok := dtoi(s, 0); ok {
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
return 0, unknownPortError
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupCNAME(name string) (cname string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
lines, err := queryDNS(name, "cname")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(lines) > 0 {
|
|
|
|
if f := getFields(lines[0]); len(f) >= 3 {
|
|
|
|
return f[2] + ".", nil
|
|
|
|
}
|
|
|
|
}
|
2011-12-03 03:17:34 +01:00
|
|
|
return "", errors.New("net: bad response from ndb/dns")
|
2011-09-16 17:47:21 +02: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
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
lines, err := queryDNS(target, "srv")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
f := getFields(line)
|
|
|
|
if len(f) < 6 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
port, _, portOk := dtoi(f[2], 0)
|
|
|
|
priority, _, priorityOk := dtoi(f[3], 0)
|
|
|
|
weight, _, weightOk := dtoi(f[4], 0)
|
|
|
|
if !(portOk && priorityOk && weightOk) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs = append(addrs, &SRV{f[5], uint16(port), uint16(priority), uint16(weight)})
|
|
|
|
cname = f[0]
|
|
|
|
}
|
|
|
|
byPriorityWeight(addrs).sort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupMX(name string) (mx []*MX, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
lines, err := queryDNS(name, "mx")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
f := getFields(line)
|
|
|
|
if len(f) < 4 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pref, _, ok := dtoi(f[2], 0); ok {
|
|
|
|
mx = append(mx, &MX{f[3], uint16(pref)})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
byPref(mx).sort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func lookupTXT(name string) (txt []string, err error) {
|
2011-12-02 20:34:41 +01:00
|
|
|
lines, err := queryDNS(name, "txt")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
if i := byteIndex(line, '\t'); i >= 0 {
|
|
|
|
txt = append(txt, line[i+1:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
lines, err := queryDNS(arpa, "ptr")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
f := getFields(line)
|
|
|
|
if len(f) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name = append(name, f[2])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|