Make the resolver a goroutine
Fits more nicely with DNS, async. for free Renamed the files: dropped the dns prefix
This commit is contained in:
parent
3c745019dd
commit
0e00511c31
9
Makefile
9
Makefile
|
@ -2,15 +2,16 @@
|
|||
# Use of this source code is governed by a BSD-style
|
||||
# license that can be found in the LICENSE file.
|
||||
|
||||
include $(GOROOT)/src/Make.$(GOARCH)
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=dns
|
||||
GOFILES=\
|
||||
parse.go\
|
||||
dns.go\
|
||||
dnsmsg.go\
|
||||
dnsconfig.go\
|
||||
dnstypes.go\
|
||||
msg.go\
|
||||
resolver.go \
|
||||
config.go\
|
||||
types.go\
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
||||
|
||||
|
|
|
@ -0,0 +1,179 @@
|
|||
// Find answer for name in dns message.
|
||||
// On return, if err == nil, addrs != nil.
|
||||
func answer(name, server string, dns *Msg, qtype uint16) (addrs []RR, err os.Error) {
|
||||
addrs = make([]RR, 0, len(dns.Answer))
|
||||
|
||||
if dns.rcode == RcodeNameError && dns.recursion_available {
|
||||
return nil, &DnsError{Error: noSuchHost, Name: name}
|
||||
}
|
||||
if dns.rcode != RcodeSuccess {
|
||||
// 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}
|
||||
}
|
||||
|
||||
// Look for the name.
|
||||
// Presotto says it's okay to assume that servers listed in
|
||||
// /etc/resolv.conf are recursive resolvers.
|
||||
// We asked for recursion, so it should have included
|
||||
// all the answers we need in this one packet.
|
||||
Cname:
|
||||
for cnameloop := 0; cnameloop < 10; cnameloop++ {
|
||||
addrs = addrs[0:0]
|
||||
for i := 0; i < len(dns.Answer); i++ {
|
||||
rr := dns.Answer[i]
|
||||
h := rr.Header()
|
||||
if h.Class == ClassINET && h.Name == name {
|
||||
switch h.Rrtype {
|
||||
case qtype:
|
||||
n := len(addrs)
|
||||
addrs = addrs[0 : n+1]
|
||||
addrs[n] = rr
|
||||
case TypeCNAME:
|
||||
// redirect to cname
|
||||
name = rr.(*RR_CNAME).Cname
|
||||
continue Cname
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(addrs) == 0 {
|
||||
return nil, &DnsError{Error: noSuchHost, Name: name, Server: server}
|
||||
}
|
||||
return addrs, nil
|
||||
}
|
||||
|
||||
return nil, &DnsError{Error: "too many redirects", Name: name, Server: server}
|
||||
}
|
||||
|
||||
// Do a lookup for a single name, which must be rooted
|
||||
// (otherwise answer will not find the answers).
|
||||
func (res *Resolver) TryOneName(name string, qtype uint16) (addrs []RR, err os.Error) {
|
||||
if len(res.Servers) == 0 {
|
||||
return nil, &DnsError{Error: "no DNS servers", Name: name}
|
||||
}
|
||||
for i := 0; i < len(res.Servers); i++ {
|
||||
// Calling Dial here is scary -- we have to be sure
|
||||
// not to dial a name that will require a DNS lookup,
|
||||
// or Dial will call back here to translate it.
|
||||
// The DNS config parser has already checked that
|
||||
// all the res.Servers[i] are IP addresses, which
|
||||
// Dial will use without a DNS lookup.
|
||||
server := res.Servers[i] + ":53"
|
||||
c, cerr := net.Dial("udp", "", server)
|
||||
if cerr != nil {
|
||||
err = cerr
|
||||
continue
|
||||
}
|
||||
msg, merr := Exchange(res, c, name, qtype, ClassINET)
|
||||
c.Close()
|
||||
if merr != nil {
|
||||
err = merr
|
||||
continue
|
||||
}
|
||||
addrs, err = answer(name, server, msg, qtype)
|
||||
if err == nil || err.(*DnsError).Error == noSuchHost {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func isDomainName(s string) bool {
|
||||
// Requirements on DNS name:
|
||||
// * must not be empty.
|
||||
// * must be alphanumeric plus - and .
|
||||
// * each of the dot-separated elements must begin
|
||||
// and end with a letter or digit.
|
||||
// RFC 1035 required the element to begin with a letter,
|
||||
// but RFC 3696 says this has been relaxed to allow digits too.
|
||||
// still, there must be a letter somewhere in the entire name.
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
if s[len(s)-1] != '.' { // simplify checking loop: make name end in dot
|
||||
s += "."
|
||||
}
|
||||
|
||||
last := byte('.')
|
||||
ok := false // ok once we've seen a letter
|
||||
for i := 0; i < len(s); i++ {
|
||||
c := s[i]
|
||||
switch {
|
||||
default:
|
||||
return false
|
||||
case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z':
|
||||
ok = true
|
||||
case '0' <= c && c <= '9':
|
||||
// fine
|
||||
case c == '-':
|
||||
// byte before dash cannot be dot
|
||||
if last == '.' {
|
||||
return false
|
||||
}
|
||||
case c == '.':
|
||||
// byte before dot cannot be dot, dash
|
||||
if last == '.' || last == '-' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
last = c
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func lookup(name string, qtype uint16) (cname string, addrs []RR, err os.Error) {
|
||||
if !isDomainName(name) {
|
||||
return name, nil, &DnsError{Error: "invalid domain name", Name: name}
|
||||
}
|
||||
|
||||
if dnserr != nil || res == nil {
|
||||
err = dnserr
|
||||
return
|
||||
}
|
||||
// If name is rooted (trailing dot) or has enough dots,
|
||||
// try it by itself first.
|
||||
rooted := len(name) > 0 && name[len(name)-1] == '.'
|
||||
if rooted || count(name, '.') >= res.Ndots {
|
||||
rname := name
|
||||
if !rooted {
|
||||
rname += "."
|
||||
}
|
||||
// Can try as ordinary name.
|
||||
addrs, err = res.TryOneName(rname, qtype)
|
||||
if err == nil {
|
||||
cname = rname
|
||||
return
|
||||
}
|
||||
}
|
||||
if rooted {
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, try suffixes.
|
||||
for i := 0; i < len(res.Search); i++ {
|
||||
rname := name + "." + res.Search[i]
|
||||
if rname[len(rname)-1] != '.' {
|
||||
rname += "."
|
||||
}
|
||||
addrs, err = res.TryOneName(rname, qtype)
|
||||
if err == nil {
|
||||
cname = rname
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Last ditch effort: try unsuffixed.
|
||||
rname := name
|
||||
if !rooted {
|
||||
rname += "."
|
||||
}
|
||||
addrs, err = res.TryOneName(rname, qtype)
|
||||
if err == nil {
|
||||
cname = rname
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
274
dns.go
274
dns.go
|
@ -14,13 +14,6 @@
|
|||
|
||||
package dns
|
||||
|
||||
import (
|
||||
"os"
|
||||
"rand"
|
||||
"time"
|
||||
"net"
|
||||
)
|
||||
|
||||
// DnsError represents a DNS lookup error.
|
||||
type DnsError struct {
|
||||
Error string // description of the error
|
||||
|
@ -42,270 +35,3 @@ func (e *DnsError) Timeout() bool { return e.IsTimeout }
|
|||
func (e *DnsError) Temporary() bool { return e.IsTimeout }
|
||||
|
||||
const noSuchHost = "no such host"
|
||||
|
||||
type Resolver struct {
|
||||
Servers []string // servers to use
|
||||
Search []string // suffixes to append to local name
|
||||
Ndots int // number of dots in name to trigger absolute lookup
|
||||
Timeout int // seconds before giving up on packet
|
||||
Attempts int // lost packets before giving up on server
|
||||
Rotate bool // round robin among servers
|
||||
}
|
||||
|
||||
// Send a request on the connection and hope for a reply.
|
||||
// Up to res.Attempts attempts.
|
||||
func Exchange(res *Resolver, c net.Conn, name string, qtype uint16, qclass uint16) (*Msg, os.Error) {
|
||||
if len(name) >= 256 {
|
||||
return nil, &DnsError{Error: "name too long", Name: name}
|
||||
}
|
||||
out := new(Msg)
|
||||
out.id = uint16(rand.Int()) ^ uint16(time.Nanoseconds())
|
||||
out.Question = []Question{
|
||||
Question{name, qtype, qclass},
|
||||
}
|
||||
out.recursion_desired = true
|
||||
msg, ok := out.Pack()
|
||||
if !ok {
|
||||
return nil, &DnsError{Error: "internal error - cannot pack message", Name: name}
|
||||
}
|
||||
|
||||
for attempt := 0; attempt < res.Attempts; attempt++ {
|
||||
n, err := c.Write(msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.SetReadTimeout(int64(res.Timeout) * 1e9) // nanoseconds
|
||||
// EDNS
|
||||
buf := make([]byte, 2000) // More than enough.
|
||||
n, err = c.Read(buf)
|
||||
if err != nil {
|
||||
// if e, ok := err.(Error); ok && e.Timeout() {
|
||||
// continue
|
||||
// }
|
||||
return nil, err
|
||||
}
|
||||
buf = buf[0:n]
|
||||
in := new(Msg)
|
||||
if !in.Unpack(buf) || in.id != out.id {
|
||||
continue
|
||||
}
|
||||
return in, nil
|
||||
}
|
||||
var server string
|
||||
if a := c.RemoteAddr(); a != nil {
|
||||
server = a.String()
|
||||
}
|
||||
return nil, &DnsError{Error: "no answer from server", Name: name, Server: server, IsTimeout: true}
|
||||
}
|
||||
|
||||
// Find answer for name in dns message.
|
||||
// On return, if err == nil, addrs != nil.
|
||||
func answer(name, server string, dns *Msg, qtype uint16) (addrs []RR, err os.Error) {
|
||||
addrs = make([]RR, 0, len(dns.Answer))
|
||||
|
||||
if dns.rcode == RcodeNameError && dns.recursion_available {
|
||||
return nil, &DnsError{Error: noSuchHost, Name: name}
|
||||
}
|
||||
if dns.rcode != RcodeSuccess {
|
||||
// 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}
|
||||
}
|
||||
|
||||
// Look for the name.
|
||||
// Presotto says it's okay to assume that servers listed in
|
||||
// /etc/resolv.conf are recursive resolvers.
|
||||
// We asked for recursion, so it should have included
|
||||
// all the answers we need in this one packet.
|
||||
Cname:
|
||||
for cnameloop := 0; cnameloop < 10; cnameloop++ {
|
||||
addrs = addrs[0:0]
|
||||
for i := 0; i < len(dns.Answer); i++ {
|
||||
rr := dns.Answer[i]
|
||||
h := rr.Header()
|
||||
if h.Class == ClassINET && h.Name == name {
|
||||
switch h.Rrtype {
|
||||
case qtype:
|
||||
n := len(addrs)
|
||||
addrs = addrs[0 : n+1]
|
||||
addrs[n] = rr
|
||||
case TypeCNAME:
|
||||
// redirect to cname
|
||||
name = rr.(*RR_CNAME).Cname
|
||||
continue Cname
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(addrs) == 0 {
|
||||
return nil, &DnsError{Error: noSuchHost, Name: name, Server: server}
|
||||
}
|
||||
return addrs, nil
|
||||
}
|
||||
|
||||
return nil, &DnsError{Error: "too many redirects", Name: name, Server: server}
|
||||
}
|
||||
|
||||
// Look up a single name
|
||||
|
||||
func (res *Resolver) Query(name string, qtype uint16, qclass uint16) (msg *Msg, err os.Error) {
|
||||
if len(res.Servers) == 0 {
|
||||
return nil, &DnsError{Error: "no DNS servers", Name: name}
|
||||
}
|
||||
for i := 0; i < len(res.Servers); i++ {
|
||||
// Calling Dial here is scary -- we have to be sure
|
||||
// not to dial a name that will require a DNS lookup,
|
||||
// or Dial will call back here to translate it.
|
||||
// The DNS config parser has already checked that
|
||||
// all the res.Servers[i] are IP addresses, which
|
||||
// Dial will use without a DNS lookup.
|
||||
server := res.Servers[i] + ":53"
|
||||
c, cerr := net.Dial("udp", "", server)
|
||||
if cerr != nil {
|
||||
err = cerr
|
||||
continue
|
||||
}
|
||||
msg, err = Exchange(res, c, name, qtype, qclass)
|
||||
c.Close()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Do a lookup for a single name, which must be rooted
|
||||
// (otherwise answer will not find the answers).
|
||||
func (res *Resolver) TryOneName(name string, qtype uint16) (addrs []RR, err os.Error) {
|
||||
if len(res.Servers) == 0 {
|
||||
return nil, &DnsError{Error: "no DNS servers", Name: name}
|
||||
}
|
||||
for i := 0; i < len(res.Servers); i++ {
|
||||
// Calling Dial here is scary -- we have to be sure
|
||||
// not to dial a name that will require a DNS lookup,
|
||||
// or Dial will call back here to translate it.
|
||||
// The DNS config parser has already checked that
|
||||
// all the res.Servers[i] are IP addresses, which
|
||||
// Dial will use without a DNS lookup.
|
||||
server := res.Servers[i] + ":53"
|
||||
c, cerr := net.Dial("udp", "", server)
|
||||
if cerr != nil {
|
||||
err = cerr
|
||||
continue
|
||||
}
|
||||
msg, merr := Exchange(res, c, name, qtype, ClassINET)
|
||||
c.Close()
|
||||
if merr != nil {
|
||||
err = merr
|
||||
continue
|
||||
}
|
||||
addrs, err = answer(name, server, msg, qtype)
|
||||
if err == nil || err.(*DnsError).Error == noSuchHost {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var res *Resolver
|
||||
var dnserr os.Error
|
||||
|
||||
func isDomainName(s string) bool {
|
||||
// Requirements on DNS name:
|
||||
// * must not be empty.
|
||||
// * must be alphanumeric plus - and .
|
||||
// * each of the dot-separated elements must begin
|
||||
// and end with a letter or digit.
|
||||
// RFC 1035 required the element to begin with a letter,
|
||||
// but RFC 3696 says this has been relaxed to allow digits too.
|
||||
// still, there must be a letter somewhere in the entire name.
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
if s[len(s)-1] != '.' { // simplify checking loop: make name end in dot
|
||||
s += "."
|
||||
}
|
||||
|
||||
last := byte('.')
|
||||
ok := false // ok once we've seen a letter
|
||||
for i := 0; i < len(s); i++ {
|
||||
c := s[i]
|
||||
switch {
|
||||
default:
|
||||
return false
|
||||
case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z':
|
||||
ok = true
|
||||
case '0' <= c && c <= '9':
|
||||
// fine
|
||||
case c == '-':
|
||||
// byte before dash cannot be dot
|
||||
if last == '.' {
|
||||
return false
|
||||
}
|
||||
case c == '.':
|
||||
// byte before dot cannot be dot, dash
|
||||
if last == '.' || last == '-' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
last = c
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func lookup(name string, qtype uint16) (cname string, addrs []RR, err os.Error) {
|
||||
if !isDomainName(name) {
|
||||
return name, nil, &DnsError{Error: "invalid domain name", Name: name}
|
||||
}
|
||||
|
||||
if dnserr != nil || res == nil {
|
||||
err = dnserr
|
||||
return
|
||||
}
|
||||
// If name is rooted (trailing dot) or has enough dots,
|
||||
// try it by itself first.
|
||||
rooted := len(name) > 0 && name[len(name)-1] == '.'
|
||||
if rooted || count(name, '.') >= res.Ndots {
|
||||
rname := name
|
||||
if !rooted {
|
||||
rname += "."
|
||||
}
|
||||
// Can try as ordinary name.
|
||||
addrs, err = res.TryOneName(rname, qtype)
|
||||
if err == nil {
|
||||
cname = rname
|
||||
return
|
||||
}
|
||||
}
|
||||
if rooted {
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, try suffixes.
|
||||
for i := 0; i < len(res.Search); i++ {
|
||||
rname := name + "." + res.Search[i]
|
||||
if rname[len(rname)-1] != '.' {
|
||||
rname += "."
|
||||
}
|
||||
addrs, err = res.TryOneName(rname, qtype)
|
||||
if err == nil {
|
||||
cname = rname
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Last ditch effort: try unsuffixed.
|
||||
rname := name
|
||||
if !rooted {
|
||||
rname += "."
|
||||
}
|
||||
addrs, err = res.TryOneName(rname, qtype)
|
||||
if err == nil {
|
||||
cname = rname
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -185,7 +185,7 @@ func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
|
|||
if fv.Len() > net.IPv4len || off+fv.Len() > len(msg) {
|
||||
return len(msg), false
|
||||
}
|
||||
msg[off] = byte(fv.Elem(0).(*reflect.UintValue).Get())
|
||||
msg[off] = byte(fv.Elem(0).(*reflect.UintValue).Get())
|
||||
msg[off+1] = byte(fv.Elem(1).(*reflect.UintValue).Get())
|
||||
msg[off+2] = byte(fv.Elem(2).(*reflect.UintValue).Get())
|
||||
msg[off+3] = byte(fv.Elem(3).(*reflect.UintValue).Get())
|
||||
|
@ -194,7 +194,7 @@ func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
|
|||
if fv.Len() > net.IPv6len || off+fv.Len() > len(msg) {
|
||||
return len(msg), false
|
||||
}
|
||||
for j:=0; j<net.IPv6len; j++ {
|
||||
for j := 0; j < net.IPv6len; j++ {
|
||||
msg[off] = byte(fv.Elem(j).(*reflect.UintValue).Get())
|
||||
off++
|
||||
}
|
||||
|
@ -345,8 +345,8 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
|
|||
default:
|
||||
consumed = 0 // TODO
|
||||
}
|
||||
s = hex.EncodeToString(msg[off:off+rdlength-consumed])
|
||||
off += rdlength-consumed
|
||||
s = hex.EncodeToString(msg[off : off+rdlength-consumed])
|
||||
off += rdlength - consumed
|
||||
case "base64":
|
||||
// Rest of the RR is base64 encoded value
|
||||
rdlength := int(val.FieldByName("Hdr").(*reflect.StructValue).FieldByName("Rdlength").(*reflect.UintValue).Get())
|
||||
|
@ -364,7 +364,7 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
|
|||
b64 := make([]byte, base64.StdEncoding.EncodedLen(len(msg[off:off+rdlength-consumed])))
|
||||
base64.StdEncoding.Encode(b64, msg[off:off+rdlength-consumed])
|
||||
s = string(b64)
|
||||
off += rdlength-consumed
|
||||
off += rdlength - consumed
|
||||
case "domain-name":
|
||||
s, off, ok = unpackDomainName(msg, off)
|
||||
if !ok {
|
||||
|
@ -467,14 +467,14 @@ func unpackRR(msg []byte, off int) (rr RR, off1 int, ok bool) {
|
|||
// A manually-unpacked version of (id, bits).
|
||||
// This is in its own struct for easy printing.
|
||||
type MsgHdr struct {
|
||||
id uint16
|
||||
response bool
|
||||
opcode int
|
||||
authoritative bool
|
||||
truncated bool
|
||||
recursion_desired bool
|
||||
recursion_available bool
|
||||
rcode int
|
||||
Id uint16
|
||||
Response bool
|
||||
Opcode int
|
||||
Authoritative bool
|
||||
Truncated bool
|
||||
Recursion_desired bool
|
||||
Recursion_available bool
|
||||
Rcode int
|
||||
}
|
||||
|
||||
//;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48404
|
||||
|
@ -484,21 +484,21 @@ func (h *MsgHdr) String() string {
|
|||
return "<nil> MsgHdr"
|
||||
}
|
||||
|
||||
s := ";; ->>HEADER<<- opcode: " + opcode_str[h.opcode]
|
||||
s += ", status: " + rcode_str[h.rcode]
|
||||
s += ", id: " + strconv.Itoa(int(h.id)) + "\n"
|
||||
s := ";; ->>HEADER<<- opcode: " + opcode_str[h.Opcode]
|
||||
s += ", status: " + rcode_str[h.Rcode]
|
||||
s += ", id: " + strconv.Itoa(int(h.Id)) + "\n"
|
||||
|
||||
s += ";; flags: "
|
||||
if h.authoritative {
|
||||
if h.Authoritative {
|
||||
s += "aa "
|
||||
}
|
||||
if h.truncated {
|
||||
if h.Truncated {
|
||||
s += "tc "
|
||||
}
|
||||
if h.recursion_desired {
|
||||
if h.Recursion_desired {
|
||||
s += "rd "
|
||||
}
|
||||
if h.recursion_available {
|
||||
if h.Recursion_available {
|
||||
s += "ra "
|
||||
}
|
||||
s += ";"
|
||||
|
@ -519,21 +519,21 @@ func (dns *Msg) Pack() (msg []byte, ok bool) {
|
|||
var dh Header
|
||||
|
||||
// Convert convenient Msg into wire-like Header.
|
||||
dh.Id = dns.id
|
||||
dh.Bits = uint16(dns.opcode)<<11 | uint16(dns.rcode)
|
||||
if dns.recursion_available {
|
||||
dh.Id = dns.Id
|
||||
dh.Bits = uint16(dns.Opcode)<<11 | uint16(dns.Rcode)
|
||||
if dns.Recursion_available {
|
||||
dh.Bits |= _RA
|
||||
}
|
||||
if dns.recursion_desired {
|
||||
if dns.Recursion_desired {
|
||||
dh.Bits |= _RD
|
||||
}
|
||||
if dns.truncated {
|
||||
if dns.Truncated {
|
||||
dh.Bits |= _TC
|
||||
}
|
||||
if dns.authoritative {
|
||||
if dns.Authoritative {
|
||||
dh.Bits |= _AA
|
||||
}
|
||||
if dns.response {
|
||||
if dns.Response {
|
||||
dh.Bits |= _QR
|
||||
}
|
||||
|
||||
|
@ -582,14 +582,14 @@ func (dns *Msg) Unpack(msg []byte) bool {
|
|||
if off, ok = unpackStruct(&dh, msg, off); !ok {
|
||||
return false
|
||||
}
|
||||
dns.id = dh.Id
|
||||
dns.response = (dh.Bits & _QR) != 0
|
||||
dns.opcode = int(dh.Bits>>11) & 0xF
|
||||
dns.authoritative = (dh.Bits & _AA) != 0
|
||||
dns.truncated = (dh.Bits & _TC) != 0
|
||||
dns.recursion_desired = (dh.Bits & _RD) != 0
|
||||
dns.recursion_available = (dh.Bits & _RA) != 0
|
||||
dns.rcode = int(dh.Bits & 0xF)
|
||||
dns.Id = dh.Id
|
||||
dns.Response = (dh.Bits & _QR) != 0
|
||||
dns.Opcode = int(dh.Bits>>11) & 0xF
|
||||
dns.Authoritative = (dh.Bits & _AA) != 0
|
||||
dns.Truncated = (dh.Bits & _TC) != 0
|
||||
dns.Recursion_desired = (dh.Bits & _RD) != 0
|
||||
dns.Recursion_available = (dh.Bits & _RA) != 0
|
||||
dns.Rcode = int(dh.Bits & 0xF)
|
||||
|
||||
// Arrays.
|
||||
dns.Question = make([]Question, dh.Qdcount)
|
|
@ -0,0 +1,123 @@
|
|||
// Copyright 2009 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.
|
||||
|
||||
// DNS resolver client: see RFC 1035.
|
||||
// A dns resolver is to be run as a seperate goroutine.
|
||||
// For every reply the resolver answers by sending the
|
||||
// received packet back on the channel.
|
||||
|
||||
package dns
|
||||
|
||||
import (
|
||||
"os"
|
||||
"rand"
|
||||
"time"
|
||||
"net"
|
||||
)
|
||||
|
||||
type MsgErr struct {
|
||||
M *Msg
|
||||
E os.Error
|
||||
}
|
||||
|
||||
type Resolver struct {
|
||||
Servers []string // servers to use
|
||||
rtt []int // round trip times for each NS (TODO)
|
||||
Search []string // suffixes to append to local name
|
||||
Port string // what port to use
|
||||
Ndots int // number of dots in name to trigger absolute lookup
|
||||
Timeout int // seconds before giving up on packet
|
||||
Attempts int // lost packets before giving up on server
|
||||
Rotate bool // round robin among servers
|
||||
}
|
||||
|
||||
// do it
|
||||
func (res *Resolver) Query(msg chan MsgErr, quit chan bool) {
|
||||
var c net.Conn
|
||||
var err os.Error
|
||||
var in *Msg
|
||||
select {
|
||||
case <-quit: // quit signal recevied
|
||||
println("Quiting")
|
||||
// send something back on the channel?
|
||||
return
|
||||
case out := <-msg: //msg received
|
||||
var cerr os.Error
|
||||
println("Getting a message")
|
||||
// Set an id
|
||||
//if len(name) >= 256 {
|
||||
out.M.Id = uint16(rand.Int()) ^ uint16(time.Nanoseconds())
|
||||
println("Setting the id", out.M.Id)
|
||||
sending, ok := out.M.Pack()
|
||||
if !ok {
|
||||
println("error converting")
|
||||
msg <- MsgErr{nil, nil} // todo error
|
||||
}
|
||||
println("here")
|
||||
|
||||
for i := 0; i < len(res.Servers); i++ {
|
||||
println("here", i)
|
||||
server := res.Servers[i] + ":53"
|
||||
println(server)
|
||||
|
||||
println("before dial")
|
||||
c, cerr = net.Dial("udp", "", server)
|
||||
println("after dial")
|
||||
if cerr != nil {
|
||||
println("error sending")
|
||||
err = cerr
|
||||
continue
|
||||
}
|
||||
println("exchange")
|
||||
in, err = exchange(c, sending, res.Attempts, res.Timeout)
|
||||
// Check id in.id != out.id
|
||||
|
||||
c.Close()
|
||||
if err != nil {
|
||||
println("Err not nil")
|
||||
continue
|
||||
}
|
||||
}
|
||||
println("komt ik hier dan")
|
||||
if err != nil {
|
||||
msg <- MsgErr{nil, err}
|
||||
} else {
|
||||
msg <- MsgErr{in, nil}
|
||||
}
|
||||
}
|
||||
println("Mag nooit hier komen")
|
||||
return
|
||||
}
|
||||
|
||||
// Use Pack to create a DNS question, from a msg
|
||||
|
||||
// Send a request on the connection and hope for a reply.
|
||||
// Up to res.Attempts attempts.
|
||||
func exchange(c net.Conn, m []byte, attempts, timeout int) (*Msg, os.Error) {
|
||||
for attempt := 0; attempt < attempts; attempt++ {
|
||||
n, err := c.Write(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.SetReadTimeout(int64(timeout) * 1e9) // nanoseconds
|
||||
// EDNS TODO
|
||||
buf := make([]byte, 2000) // More than enough.
|
||||
n, err = c.Read(buf)
|
||||
if err != nil {
|
||||
// More Go foo needed
|
||||
//if e, ok := err.(Error); ok && e.Timeout() {
|
||||
// continue
|
||||
//}
|
||||
return nil, err
|
||||
}
|
||||
buf = buf[0:n]
|
||||
in := new(Msg)
|
||||
if !in.Unpack(buf) {
|
||||
continue
|
||||
}
|
||||
return in, nil
|
||||
}
|
||||
return nil, nil // todo error
|
||||
}
|
59
restest.go
59
restest.go
|
@ -3,33 +3,56 @@ package main
|
|||
import (
|
||||
"dns"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
res := new(dns.Resolver)
|
||||
res := new(dns.Resolver) // create a new resolver
|
||||
res.Servers = []string{"192.168.1.2"}
|
||||
res.Timeout = 2
|
||||
res.Attempts = 1
|
||||
|
||||
a := new(dns.RR_A)
|
||||
a.A = net.ParseIP("192.168.1.2").To4()
|
||||
// Create a new message
|
||||
m := new(dns.Msg)
|
||||
m.MsgHdr.Recursion_desired = true //only set this bit
|
||||
m.Question = make([]dns.Question, 1)
|
||||
m.Question[0] = dns.Question{"miek.nl", dns.TypeSOA, dns.ClassINET}
|
||||
|
||||
aaaa := new(dns.RR_AAAA)
|
||||
aaaa.AAAA = net.ParseIP("2003::53").To16()
|
||||
msgch := make(chan dns.MsgErr)
|
||||
qch := make(chan bool)
|
||||
|
||||
fmt.Printf("%v\n", a)
|
||||
fmt.Printf("%v\n", aaaa)
|
||||
// start the resolver
|
||||
go res.Query(msgch, qch)
|
||||
|
||||
// msg, _ := res.Query("miek.nl.", dns.TypeTXT, dns.ClassINET)
|
||||
// fmt.Printf("%v\n", msg)
|
||||
//
|
||||
// msg, _ = res.Query("www.nlnetlabs.nl", dns.TypeAAAA, dns.ClassINET)
|
||||
// fmt.Printf("%v\n", msg)
|
||||
//
|
||||
msg, _ := res.Query("nlnetlabs.nl", dns.TypeDNSKEY, dns.ClassINET)
|
||||
fmt.Printf("%v\n", msg)
|
||||
// ask something
|
||||
msgch <- dns.MsgErr{m, nil}
|
||||
|
||||
msg, _ = res.Query("jelte.nlnetlabs.nl", dns.TypeDS, dns.ClassINET)
|
||||
fmt.Printf("%v\n", msg)
|
||||
// wait for an reply
|
||||
in := <-msgch
|
||||
fmt.Printf("%v\n", in.M)
|
||||
// kill resolver
|
||||
// qch <- true does not work yet
|
||||
time.Sleep(2.0e9)
|
||||
|
||||
/*
|
||||
a := new(dns.RR_A)
|
||||
a.A = net.ParseIP("192.168.1.2").To4()
|
||||
|
||||
aaaa := new(dns.RR_AAAA)
|
||||
aaaa.AAAA = net.ParseIP("2003::53").To16()
|
||||
|
||||
fmt.Printf("%v\n", a)
|
||||
fmt.Printf("%v\n", aaaa)
|
||||
|
||||
// msg, _ := res.Query("miek.nl.", dns.TypeTXT, dns.ClassINET)
|
||||
//
|
||||
// msg, _ = res.Query("www.nlnetlabs.nl", dns.TypeAAAA, dns.ClassINET)
|
||||
// fmt.Printf("%v\n", msg)
|
||||
//
|
||||
msg, _ := res.Query("nlnetlabs.nl", dns.TypeDNSKEY, dns.ClassINET)
|
||||
fmt.Printf("%v\n", msg)
|
||||
|
||||
msg, _ = res.Query("jelte.nlnetlabs.nl", dns.TypeDS, dns.ClassINET)
|
||||
fmt.Printf("%v\n", msg)
|
||||
*/
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue