diff options
Diffstat (limited to 'src/pkg/net/lookup_plan9.go')
-rw-r--r-- | src/pkg/net/lookup_plan9.go | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/src/pkg/net/lookup_plan9.go b/src/pkg/net/lookup_plan9.go index f1204a99f..b80ac10e0 100644 --- a/src/pkg/net/lookup_plan9.go +++ b/src/pkg/net/lookup_plan9.go @@ -16,6 +16,10 @@ func query(filename, query string, bufSize int) (res []string, err error) { } defer file.Close() + _, err = file.Seek(0, 0) + if err != nil { + return + } _, err = file.WriteString(query) if err != nil { return @@ -45,7 +49,7 @@ func queryCS(net, host, service string) (res []string, err error) { if host == "" { host = "*" } - return query("/net/cs", net+"!"+host+"!"+service, 128) + return query(netdir+"/cs", net+"!"+host+"!"+service, 128) } func queryCS1(net string, ip IP, port int) (clone, dest string, err error) { @@ -59,20 +63,41 @@ func queryCS1(net string, ip IP, port int) (clone, dest string, err error) { } f := getFields(lines[0]) if len(f) < 2 { - return "", "", errors.New("net: bad response from ndb/cs") + return "", "", errors.New("bad response from ndb/cs") } clone, dest = f[0], f[1] return } func queryDNS(addr string, typ string) (res []string, err error) { - return query("/net/dns", addr+" "+typ, 1024) + return query(netdir+"/dns", addr+" "+typ, 1024) +} + +// toLower returns a lower-case version of in. Restricting us to +// ASCII is sufficient to handle the IP protocol names and allow +// us to not depend on the strings and unicode packages. +func toLower(in string) string { + for _, c := range in { + if 'A' <= c && c <= 'Z' { + // Has upper case; need to fix. + out := []byte(in) + for i := 0; i < len(in); i++ { + c := in[i] + if 'A' <= c && c <= 'Z' { + c += 'a' - 'A' + } + out[i] = c + } + return string(out) + } + } + return in } // lookupProtocol looks up IP protocol name and returns // the corresponding protocol number. func lookupProtocol(name string) (proto int, err error) { - lines, err := query("/net/cs", "!protocol="+name, 128) + lines, err := query(netdir+"/cs", "!protocol="+toLower(name), 128) if err != nil { return 0, err } @@ -92,12 +117,13 @@ func lookupProtocol(name string) (proto int, err error) { } func lookupHost(host string) (addrs []string, err error) { - // Use /net/cs instead of /net/dns because cs knows about + // Use netdir/cs instead of netdir/dns because cs knows about // host names in local network (e.g. from /lib/ndb/local) - lines, err := queryCS("tcp", host, "1") + lines, err := queryCS("net", host, "1") if err != nil { return } +loop: for _, line := range lines { f := getFields(line) if len(f) < 2 { @@ -110,6 +136,12 @@ func lookupHost(host string) (addrs []string, err error) { if ParseIP(addr) == nil { continue } + // only return unique addresses + for _, a := range addrs { + if a == addr { + continue loop + } + } addrs = append(addrs, addr) } return @@ -167,7 +199,7 @@ func lookupCNAME(name string) (cname string, err error) { return f[2] + ".", nil } } - return "", errors.New("net: bad response from ndb/dns") + return "", errors.New("bad response from ndb/dns") } func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) { |