dns/xfr.go

210 lines
4.7 KiB
Go
Raw Normal View History

2011-03-17 02:21:35 +11:00
package dns
2011-03-22 03:55:14 +11:00
import (
"os"
)
2011-03-24 05:37:07 +11:00
// Perform an incoming Ixfr or Axfr. If the message q's question
// section contains an AXFR type an Axfr is performed. If q's question
// section contains an IXFR type an Ixfr is performed.
2011-09-10 22:48:22 +10:00
// Each message will be send along the Client's reply channel as it
// is received.
func (c *Client) XfrReceive(q *Msg, a string) os.Error {
2011-04-19 06:08:12 +10:00
w := new(reply)
w.client = c
w.addr = a
2011-09-10 22:48:22 +10:00
w.req = q
2011-03-22 01:44:51 +11:00
2011-04-18 17:58:15 +10:00
if err := w.Send(q); err != nil {
2011-09-10 22:48:22 +10:00
return err
2011-04-19 06:08:12 +10:00
}
// conn should be set now
2011-03-22 02:28:13 +11:00
switch q.Question[0].Qtype {
case TypeAXFR:
2011-09-10 22:48:22 +10:00
go w.axfrReceive()
2011-03-22 02:28:13 +11:00
case TypeIXFR:
2011-09-10 22:48:22 +10:00
// go w.ixfrReceive()
2011-03-22 02:28:13 +11:00
}
2011-09-10 22:48:22 +10:00
return nil
2011-03-22 01:44:51 +11:00
}
2011-09-10 22:48:22 +10:00
func (w *reply) axfrReceive() {
2011-04-19 06:08:12 +10:00
first := true
2011-03-22 01:44:51 +11:00
for {
2011-04-18 17:58:15 +10:00
in, err := w.Receive()
2011-03-22 01:44:51 +11:00
if err != nil {
2011-09-10 22:48:22 +10:00
w.Client().ChannelReply <- &Exchange{Request: w.req, Reply: in, Error: err}
return
2011-03-22 01:44:51 +11:00
}
if first {
if !checkXfrSOA(in, true) {
2011-09-10 22:48:22 +10:00
w.Client().ChannelReply <- &Exchange{Request: w.req, Reply: in, Error: ErrXfrSoa}
return
2011-03-22 01:44:51 +11:00
}
first = !first
}
2011-03-19 00:13:42 +11:00
2011-03-22 01:44:51 +11:00
if !first {
2011-04-19 06:08:12 +10:00
w.tsigTimersOnly = true // Subsequent envelopes use this.
2011-09-10 22:48:22 +10:00
w.Client().ChannelReply <- &Exchange{Request: w.req, Reply: in}
if checkXfrSOA(in, false) {
return
2011-03-22 01:44:51 +11:00
}
}
}
panic("not reached")
2011-09-10 22:48:22 +10:00
return
2011-04-18 17:58:15 +10:00
}
/*
func (d *Conn) ixfrReceive(q *Msg, m chan *Xfr) {
2011-03-22 01:44:51 +11:00
defer close(m)
var serial uint32 // The first serial seen is the current server serial
var x *Xfr
2011-03-22 01:44:51 +11:00
first := true
in := new(Msg)
for {
2011-03-24 19:24:24 +11:00
err := d.ReadMsg(in)
2011-03-22 01:44:51 +11:00
if err != nil {
m <- &Xfr{true, nil, err}
2011-03-22 01:44:51 +11:00
return
}
if in.Id != q.Id {
m <- &Xfr{true, nil, ErrId}
2011-03-22 01:44:51 +11:00
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) {
m <- &Xfr{true, nil, ErrXfrSoa}
2011-03-22 01:44:51 +11:00
return
}
// 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 {
2011-03-22 03:55:14 +11:00
if d.Tsig != nil {
d.Tsig.TimersOnly = true
}
2011-03-22 01:44:51 +11:00
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
}
}
}
panic("not reached")
return
2011-03-19 00:13:42 +11:00
}
2011-09-10 22:59:21 +10:00
// Perform an outgoing Ixfr or Axfr. If the message q's question
// section contains an AXFR type an Axfr is performed. If q's question
// section contains an IXFR type an Ixfr is performed.
// The actual records to send are given on the channel m. And errors
// during transport are return on channel e.
func (d *Conn) XfrWrite(q *Msg, m chan *Xfr, e chan os.Error) {
switch q.Question[0].Qtype {
case TypeAXFR:
d.axfrWrite(q, m, e)
case TypeIXFR:
// d.ixfrWrite(q, m)
default:
e <- &Error{Error: "Xfr Qtype not recognized"}
close(m)
}
}
// Just send the zone
func (d *Conn) axfrWrite(q *Msg, m chan *Xfr, e chan os.Error) {
out := new(Msg)
out.Id = q.Id
out.Question = q.Question
out.Answer = make([]RR, 1001) // TODO(mg) look at this number
out.MsgHdr.Response = true
out.MsgHdr.Authoritative = true
first := true
var soa *RR_SOA
i := 0
for r := range m {
out.Answer[i] = r.RR
if soa == nil {
if r.RR.Header().Rrtype != TypeSOA {
e <- ErrXfrSoa
return
} else {
soa = r.RR.(*RR_SOA)
}
}
i++
if i > 1000 {
// Send it
err := d.WriteMsg(out)
if err != nil {
e <- err
return
}
i = 0
// Gaat dit goed?
out.Answer = out.Answer[:0]
if first {
if d.Tsig != nil {
d.Tsig.TimersOnly = true
}
first = !first
}
}
}
// Everything is sent, only the closing soa is left.
out.Answer[i] = soa
out.Answer = out.Answer[:i+1]
err := d.WriteMsg(out)
if err != nil {
e <- err
}
}
2011-04-18 17:58:15 +10:00
*/
2011-03-22 02:28:13 +11:00
// 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
}