diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-04-06 15:14:11 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-04-06 15:14:11 +0200 |
commit | 505c19580e0f43fe5224431459cacb7c21edd93d (patch) | |
tree | 79e2634c253d60afc0cc0b2f510dc7dcbb48497b /src/pkg/net/dnsclient.go | |
parent | 1336a7c91e596c423a49d1194ea42d98bca0d958 (diff) | |
download | golang-505c19580e0f43fe5224431459cacb7c21edd93d.tar.gz |
Imported Upstream version 1upstream/1
Diffstat (limited to 'src/pkg/net/dnsclient.go')
-rw-r--r-- | src/pkg/net/dnsclient.go | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/src/pkg/net/dnsclient.go b/src/pkg/net/dnsclient.go index 93c04f6b5..e69cb3188 100644 --- a/src/pkg/net/dnsclient.go +++ b/src/pkg/net/dnsclient.go @@ -5,22 +5,19 @@ package net import ( - "bytes" - "fmt" - "os" - "rand" + "math/rand" "sort" ) // DNSError represents a DNS lookup error. type DNSError struct { - Error string // description of the error + Err string // description of the error Name string // name looked for Server string // server used IsTimeout bool } -func (e *DNSError) String() string { +func (e *DNSError) Error() string { if e == nil { return "<nil>" } @@ -28,7 +25,7 @@ func (e *DNSError) String() string { if e.Server != "" { s += " on " + e.Server } - s += ": " + e.Error + s += ": " + e.Err return s } @@ -40,42 +37,44 @@ const noSuchHost = "no such host" // reverseaddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP // address addr suitable for rDNS (PTR) record lookup or an error if it fails // to parse the IP address. -func reverseaddr(addr string) (arpa string, err os.Error) { +func reverseaddr(addr string) (arpa string, err error) { ip := ParseIP(addr) if ip == nil { - return "", &DNSError{Error: "unrecognized address", Name: addr} + return "", &DNSError{Err: "unrecognized address", Name: addr} } if ip.To4() != nil { - return fmt.Sprintf("%d.%d.%d.%d.in-addr.arpa.", ip[15], ip[14], ip[13], ip[12]), nil + return itoa(int(ip[15])) + "." + itoa(int(ip[14])) + "." + itoa(int(ip[13])) + "." + + itoa(int(ip[12])) + ".in-addr.arpa.", nil } // Must be IPv6 - var buf bytes.Buffer + buf := make([]byte, 0, len(ip)*4+len("ip6.arpa.")) // Add it, in reverse, to the buffer for i := len(ip) - 1; i >= 0; i-- { - s := fmt.Sprintf("%02x", ip[i]) - buf.WriteByte(s[1]) - buf.WriteByte('.') - buf.WriteByte(s[0]) - buf.WriteByte('.') + v := ip[i] + buf = append(buf, hexDigit[v&0xF]) + buf = append(buf, '.') + buf = append(buf, hexDigit[v>>4]) + buf = append(buf, '.') } // Append "ip6.arpa." and return (buf already has the final .) - return buf.String() + "ip6.arpa.", nil + buf = append(buf, "ip6.arpa."...) + return string(buf), nil } // Find answer for name in dns message. // On return, if err == nil, addrs != nil. -func answer(name, server string, dns *dnsMsg, qtype uint16) (cname string, addrs []dnsRR, err os.Error) { +func answer(name, server string, dns *dnsMsg, qtype uint16) (cname string, addrs []dnsRR, err error) { addrs = make([]dnsRR, 0, len(dns.answer)) if dns.rcode == dnsRcodeNameError && dns.recursion_available { - return "", nil, &DNSError{Error: noSuchHost, Name: name} + return "", nil, &DNSError{Err: noSuchHost, Name: name} } if dns.rcode != dnsRcodeSuccess { // None of the error codes make sense // for the query we sent. If we didn't get // a name error and we didn't get success, // the server is behaving incorrectly. - return "", nil, &DNSError{Error: "server misbehaving", Name: name, Server: server} + return "", nil, &DNSError{Err: "server misbehaving", Name: name, Server: server} } // Look for the name. @@ -107,12 +106,12 @@ Cname: } } if len(addrs) == 0 { - return "", nil, &DNSError{Error: noSuchHost, Name: name, Server: server} + return "", nil, &DNSError{Err: noSuchHost, Name: name, Server: server} } return name, addrs, nil } - return "", nil, &DNSError{Error: "too many redirects", Name: name, Server: server} + return "", nil, &DNSError{Err: "too many redirects", Name: name, Server: server} } func isDomainName(s string) bool { |