Commit 2eb7c6ec authored by Mikio Hara's avatar Mikio Hara

net: simplify non-cgo DNS exchange

Also does less buffer allocation in case of TCP fallback.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/12925043
parent 3a93626b
...@@ -26,13 +26,7 @@ import ( ...@@ -26,13 +26,7 @@ import (
// Send a request on the connection and hope for a reply. // Send a request on the connection and hope for a reply.
// Up to cfg.attempts attempts. // Up to cfg.attempts attempts.
func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, error) { func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, error) {
var useTCP bool _, useTCP := c.(*TCPConn)
switch c.(type) {
case *UDPConn:
useTCP = false
case *TCPConn:
useTCP = true
}
if len(name) >= 256 { if len(name) >= 256 {
return nil, &DNSError{Err: "name too long", Name: name} return nil, &DNSError{Err: "name too long", Name: name}
} }
...@@ -69,8 +63,11 @@ func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, error ...@@ -69,8 +63,11 @@ func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, error
continue continue
} }
} }
buf = make([]byte, uint16(buf[0])<<8+uint16(buf[1])) mlen := int(buf[0])<<8 | int(buf[1])
n, err = io.ReadFull(c, buf) if mlen > len(buf) {
buf = make([]byte, mlen)
}
n, err = io.ReadFull(c, buf[:mlen])
} else { } else {
n, err = c.Read(buf) n, err = c.Read(buf)
} }
...@@ -80,7 +77,7 @@ func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, error ...@@ -80,7 +77,7 @@ func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, error
} }
return nil, err return nil, err
} }
buf = buf[0:n] buf = buf[:n]
in := new(dnsMsg) in := new(dnsMsg)
if !in.Unpack(buf) || in.id != out.id { if !in.Unpack(buf) || in.id != out.id {
continue continue
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment