it works very nicely
This commit is contained in:
parent
d67c427953
commit
e47ebb2e4c
2
Makefile
2
Makefile
|
@ -7,7 +7,6 @@ include $(GOROOT)/src/Make.inc
|
||||||
|
|
||||||
TARG=dns
|
TARG=dns
|
||||||
GOFILES=\
|
GOFILES=\
|
||||||
xfr.go\
|
|
||||||
config.go\
|
config.go\
|
||||||
defaults.go\
|
defaults.go\
|
||||||
dns.go\
|
dns.go\
|
||||||
|
@ -21,6 +20,7 @@ GOFILES=\
|
||||||
string.go\
|
string.go\
|
||||||
tsig.go\
|
tsig.go\
|
||||||
types.go\
|
types.go\
|
||||||
|
xfr.go\
|
||||||
# y.go\
|
# y.go\
|
||||||
|
|
||||||
include $(GOROOT)/src/Make.pkg
|
include $(GOROOT)/src/Make.pkg
|
||||||
|
|
2
TODO
2
TODO
|
@ -5,8 +5,6 @@ o clean, small API
|
||||||
o fast data structures (rb-tree, when they come available)
|
o fast data structures (rb-tree, when they come available)
|
||||||
o api-use should lead to self documenting code
|
o api-use should lead to self documenting code
|
||||||
o compression (only ownernames?)
|
o compression (only ownernames?)
|
||||||
o Xfr, part of (d *Conn).Xfr(m, channel)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Todo:
|
Todo:
|
||||||
|
|
205
resolver.go
205
resolver.go
|
@ -77,7 +77,7 @@ func (res *Resolver) Query(q *Msg, tsig *Tsig) (d *Msg, err os.Error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
in.Unpack(inb) // Discard error.
|
in.Unpack(inb) // Discard error.
|
||||||
res.Rtt[server] = time.Nanoseconds() - t
|
res.Rtt[server] = time.Nanoseconds() - t
|
||||||
c.Close()
|
c.Close()
|
||||||
break
|
break
|
||||||
|
@ -97,33 +97,10 @@ type Xfr struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (res *Resolver) Xfr(q *Msg, t *Tsig, m chan Xfr) {
|
func (res *Resolver) Xfr(q *Msg, t *Tsig, m chan Xfr) {
|
||||||
switch q.Question[0].Qtype {
|
|
||||||
case TypeAXFR:
|
|
||||||
res.axfr(q, t, m)
|
|
||||||
case TypeIXFR:
|
|
||||||
res.ixfr(q, t, m)
|
|
||||||
default:
|
|
||||||
// wrong request
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start an IXFR, q should contain a *Msg with the question
|
|
||||||
// for an IXFR: "miek.nl" ANY IXFR. RRs that should be added
|
|
||||||
// have Xfr.Add set to true otherwise it is false.
|
|
||||||
// Channel m is closed when the IXFR ends.
|
|
||||||
func (res *Resolver) ixfr(q *Msg, t *Tsig, m chan Xfr) {
|
|
||||||
var (
|
|
||||||
x Xfr
|
|
||||||
inb []byte
|
|
||||||
)
|
|
||||||
in := new(Msg)
|
|
||||||
port, err := check(res, q)
|
port, err := check(res, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer close(m)
|
|
||||||
sending, ok := q.Pack()
|
sending, ok := q.Pack()
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
|
@ -136,182 +113,20 @@ Server:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue Server
|
continue Server
|
||||||
}
|
}
|
||||||
var serial uint32 // The first serial seen is the current server serial
|
d := new(Conn)
|
||||||
d := new(Conn)
|
d.TCP = c.(*net.TCPConn)
|
||||||
d.TCP = c.(*net.TCPConn)
|
d.Addr = d.TCP.RemoteAddr()
|
||||||
d.Addr = d.TCP.RemoteAddr()
|
d.Tsig = t
|
||||||
|
|
||||||
first := true
|
_, err = d.Write(sending)
|
||||||
defer c.Close()
|
if err != nil {
|
||||||
for {
|
println(err.String())
|
||||||
if first {
|
}
|
||||||
inb, err = d.Exchange(sending, false)
|
d.XfrRead(q, m) // check
|
||||||
} else {
|
|
||||||
inb, err = d.Exchange(sending, true)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
c.Close()
|
|
||||||
continue Server
|
|
||||||
}
|
|
||||||
|
|
||||||
in.Unpack(inb)
|
|
||||||
if in.Id != q.Id {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if first {
|
|
||||||
// A single SOA RR signals "no changes"
|
|
||||||
if len(in.Answer) == 1 && checkXfrSOA(in, true) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// But still check if the returned answer is ok
|
|
||||||
if !checkXfrSOA(in, true) {
|
|
||||||
c.Close()
|
|
||||||
continue Server
|
|
||||||
}
|
|
||||||
// This serial is important
|
|
||||||
serial = in.Answer[0].(*RR_SOA).Serial
|
|
||||||
first = !first
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now we need to check each message for SOA records, to see what we need to do
|
|
||||||
x.Add = true
|
|
||||||
if !first {
|
|
||||||
for k, r := range in.Answer {
|
|
||||||
// If the last record in the IXFR contains the servers' SOA, we should quit
|
|
||||||
if r.Header().Rrtype == TypeSOA {
|
|
||||||
switch {
|
|
||||||
case r.(*RR_SOA).Serial == serial:
|
|
||||||
if k == len(in.Answer)-1 {
|
|
||||||
// last rr is SOA with correct serial
|
|
||||||
//m <- r dont' send it
|
|
||||||
return
|
|
||||||
}
|
|
||||||
x.Add = true
|
|
||||||
if k != 0 {
|
|
||||||
// Intermediate SOA
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
case r.(*RR_SOA).Serial != serial:
|
|
||||||
x.Add = false
|
|
||||||
continue // Don't need to see this SOA
|
|
||||||
}
|
|
||||||
}
|
|
||||||
x.RR = r
|
|
||||||
m <- x
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
panic("not reached")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start an AXFR, q should contain a message with the question
|
|
||||||
// for an AXFR: "miek.nl" ANY AXFR. The closing SOA isn't
|
|
||||||
// returned over the channel, so the caller will receive
|
|
||||||
// the zone as-is. Xfr.Add is always true.
|
|
||||||
// The channel is closed to signal the end of the AXFR.
|
|
||||||
func (res *Resolver) axfr(q *Msg, t *Tsig, m chan Xfr) {
|
|
||||||
var inb []byte
|
|
||||||
in := new(Msg)
|
|
||||||
port, err := check(res, q)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer close(m)
|
|
||||||
sending, ok := q.Pack()
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Server:
|
|
||||||
for i := 0; i < len(res.Servers); i++ {
|
|
||||||
server := res.Servers[i] + ":" + port
|
|
||||||
c, err := net.Dial("tcp", "", server)
|
|
||||||
if err != nil {
|
|
||||||
continue Server
|
|
||||||
}
|
|
||||||
d := new(Conn)
|
|
||||||
d.TCP = c.(*net.TCPConn)
|
|
||||||
d.Addr = d.TCP.RemoteAddr()
|
|
||||||
d.Tsig = t
|
|
||||||
|
|
||||||
first := true
|
|
||||||
defer c.Close() // TODO(mg): if not open?
|
|
||||||
for {
|
|
||||||
if first {
|
|
||||||
inb, err = d.Exchange(sending, false)
|
|
||||||
} else {
|
|
||||||
inb, err = d.Exchange(sending, true)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
c.Close()
|
|
||||||
continue Server
|
|
||||||
}
|
|
||||||
if !in.Unpack(inb) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if in.Id != q.Id {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if first {
|
|
||||||
if !checkXfrSOA(in, true) {
|
|
||||||
c.Close()
|
|
||||||
continue Server
|
|
||||||
}
|
|
||||||
first = !first
|
|
||||||
}
|
|
||||||
|
|
||||||
if !first {
|
|
||||||
d.Tsig.TimersOnly = true
|
|
||||||
if !checkXfrSOA(in, false) {
|
|
||||||
// Soa record not the last one
|
|
||||||
sendMsg(in, m, false)
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
sendMsg(in, m, true)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
panic("not reached")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if he SOA record exists in the Answer section of
|
|
||||||
// the packet. If first is true the first RR must be a soa
|
|
||||||
// if false, the last one should be a SOA
|
|
||||||
func checkXfrSOA(in *Msg, first bool) bool {
|
|
||||||
if len(in.Answer) > 0 {
|
|
||||||
if first {
|
|
||||||
return in.Answer[0].Header().Rrtype == TypeSOA
|
|
||||||
} else {
|
|
||||||
return in.Answer[len(in.Answer)-1].Header().Rrtype == TypeSOA
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send the answer section to the channel
|
|
||||||
func sendMsg(in *Msg, c chan Xfr, nosoa bool) {
|
|
||||||
x := Xfr{Add: true}
|
|
||||||
for k, r := range in.Answer {
|
|
||||||
if nosoa && k == len(in.Answer)-1 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
x.RR = r
|
|
||||||
c <- x
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Some assorted checks on the resolver
|
// Some assorted checks on the resolver
|
||||||
func check(res *Resolver, q *Msg) (port string, err os.Error) {
|
func check(res *Resolver, q *Msg) (port string, err os.Error) {
|
||||||
if res.Port == "" {
|
if res.Port == "" {
|
||||||
|
|
125
xfr.go
125
xfr.go
|
@ -5,21 +5,21 @@ package dns
|
||||||
|
|
||||||
// Msg tells use what to do
|
// Msg tells use what to do
|
||||||
func (d *Conn) XfrRead(q *Msg, m chan Xfr) {
|
func (d *Conn) XfrRead(q *Msg, m chan Xfr) {
|
||||||
switch q.Question[0].Qtype {
|
switch q.Question[0].Qtype {
|
||||||
case TypeAXFR:
|
case TypeAXFR:
|
||||||
d.axfrRead(q, m)
|
d.axfrRead(q, m)
|
||||||
case TypeIXFR:
|
case TypeIXFR:
|
||||||
d.ixfrRead(q, m)
|
d.ixfrRead(q, m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Conn) XfrWrite(q *Msg, m chan Xfr) {
|
func (d *Conn) XfrWrite(q *Msg, m chan Xfr) {
|
||||||
switch q.Question[0].Qtype {
|
switch q.Question[0].Qtype {
|
||||||
case TypeAXFR:
|
case TypeAXFR:
|
||||||
d.axfrWrite(q, m)
|
d.axfrWrite(q, m)
|
||||||
case TypeIXFR:
|
case TypeIXFR:
|
||||||
// d.ixfrWrite(q, m)
|
// d.ixfrWrite(q, m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Conn) axfrRead(q *Msg, m chan Xfr) {
|
func (d *Conn) axfrRead(q *Msg, m chan Xfr) {
|
||||||
|
@ -49,6 +49,9 @@ func (d *Conn) axfrRead(q *Msg, m chan Xfr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !first {
|
if !first {
|
||||||
|
if d.Tsig != nil {
|
||||||
|
d.Tsig.TimersOnly = true // Subsequent envelopes use this
|
||||||
|
}
|
||||||
if !checkXfrSOA(in, false) {
|
if !checkXfrSOA(in, false) {
|
||||||
// Soa record not the last one
|
// Soa record not the last one
|
||||||
sendMsg(in, m, false)
|
sendMsg(in, m, false)
|
||||||
|
@ -57,7 +60,6 @@ func (d *Conn) axfrRead(q *Msg, m chan Xfr) {
|
||||||
sendMsg(in, m, true)
|
sendMsg(in, m, true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d.Tsig.TimersOnly = true // Subsequent envelopes use this
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic("not reached")
|
panic("not reached")
|
||||||
|
@ -67,39 +69,40 @@ func (d *Conn) axfrRead(q *Msg, m chan Xfr) {
|
||||||
// Just send the zone
|
// Just send the zone
|
||||||
func (d *Conn) axfrWrite(q *Msg, m chan Xfr) {
|
func (d *Conn) axfrWrite(q *Msg, m chan Xfr) {
|
||||||
out := new(Msg)
|
out := new(Msg)
|
||||||
out.Id = q.Id
|
out.Id = q.Id
|
||||||
out.Question = q.Question
|
out.Question = q.Question
|
||||||
out.Answer = make([]RR, 1000)
|
out.Answer = make([]RR, 1000)
|
||||||
var soa *RR_SOA;
|
var soa *RR_SOA
|
||||||
i := 0
|
i := 0
|
||||||
for r := range m {
|
for r := range m {
|
||||||
out.Answer[i] = r.RR
|
out.Answer[i] = r.RR
|
||||||
if soa == nil {
|
if soa == nil {
|
||||||
if r.RR.Header().Rrtype != TypeSOA {
|
if r.RR.Header().Rrtype != TypeSOA {
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
soa = r.RR.(*RR_SOA)
|
soa = r.RR.(*RR_SOA)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
if i > 1000 {
|
if i > 1000 {
|
||||||
// Send it
|
// Send it
|
||||||
send, _ := out.Pack()
|
send, _ := out.Pack()
|
||||||
_, err := d.Write(send)
|
_, err := d.Write(send)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
/* ... */
|
/* ... */
|
||||||
}
|
}
|
||||||
i = 0
|
i = 0
|
||||||
out.Answer = out.Answer[:0]
|
out.Answer = out.Answer[:0]
|
||||||
}
|
}
|
||||||
}
|
// TimersOnly foo
|
||||||
// Everything is send, only the closing soa is left.
|
}
|
||||||
out.Answer[i] = soa
|
// Everything is send, only the closing soa is left.
|
||||||
send, _ := out.Pack()
|
out.Answer[i] = soa
|
||||||
_, err := d.Write(send)
|
send, _ := out.Pack()
|
||||||
if err != nil {
|
_, err := d.Write(send)
|
||||||
/* ... */
|
if err != nil {
|
||||||
}
|
/* ... */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Conn) ixfrRead(q *Msg, m chan Xfr) {
|
func (d *Conn) ixfrRead(q *Msg, m chan Xfr) {
|
||||||
|
@ -146,7 +149,9 @@ func (d *Conn) ixfrRead(q *Msg, m chan Xfr) {
|
||||||
// Now we need to check each message for SOA records, to see what we need to do
|
// Now we need to check each message for SOA records, to see what we need to do
|
||||||
x.Add = true
|
x.Add = true
|
||||||
if !first {
|
if !first {
|
||||||
d.Tsig.TimersOnly = true
|
if d.Tsig != nil {
|
||||||
|
d.Tsig.TimersOnly = true
|
||||||
|
}
|
||||||
for k, r := range in.Answer {
|
for k, r := range in.Answer {
|
||||||
// If the last record in the IXFR contains the servers' SOA, we should quit
|
// If the last record in the IXFR contains the servers' SOA, we should quit
|
||||||
if r.Header().Rrtype == TypeSOA {
|
if r.Header().Rrtype == TypeSOA {
|
||||||
|
@ -175,3 +180,29 @@ func (d *Conn) ixfrRead(q *Msg, m chan Xfr) {
|
||||||
panic("not reached")
|
panic("not reached")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if he SOA record exists in the Answer section of
|
||||||
|
// the packet. If first is true the first RR must be a soa
|
||||||
|
// if false, the last one should be a SOA
|
||||||
|
func checkXfrSOA(in *Msg, first bool) bool {
|
||||||
|
if len(in.Answer) > 0 {
|
||||||
|
if first {
|
||||||
|
return in.Answer[0].Header().Rrtype == TypeSOA
|
||||||
|
} else {
|
||||||
|
return in.Answer[len(in.Answer)-1].Header().Rrtype == TypeSOA
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the answer section to the channel
|
||||||
|
func sendMsg(in *Msg, c chan Xfr, nosoa bool) {
|
||||||
|
x := Xfr{Add: true}
|
||||||
|
for k, r := range in.Answer {
|
||||||
|
if nosoa && k == len(in.Answer)-1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
x.RR = r
|
||||||
|
c <- x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue