dns/xfr.go

199 lines
5.3 KiB
Go
Raw Normal View History

2011-03-17 02:21:35 +11:00
package dns
2011-09-12 01:24:52 +10:00
// XfrReceives requests an incoming Ixfr or Axfr. If the message q's question
// section has type TypeAXFR an Axfr is performed, if it is TypeIXFR it does an Ixfr.
2012-05-19 03:41:20 +10:00
// The [AI]xfr's records are returned on the channel. Note that with an IXFR the client
2012-05-21 22:56:14 +10:00
// needs to determine if records are to be removed or added.
// The returned channel is closed when the transfer is terminated.
//
// Basic use pattern for setting up a transfer:
//
2012-05-19 03:41:20 +10:00
// // m contains the [AI]xfr request
// t, _ := client.XfrReceive(m, "127.0.0.1:53")
// for r := range t {
2012-05-19 03:41:20 +10:00
// // ... deal with r.Reply or r.Error
// }
func (c *Client) XfrReceive(q *Msg, a string) (chan *Exchange, 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
2012-08-07 04:34:09 +10:00
if err := w.dial(); err != nil {
return nil, err
2011-09-12 06:10:04 +10:00
}
2012-08-07 04:34:09 +10:00
if err := w.send(q); err != nil {
return nil, err
2011-04-19 06:08:12 +10:00
}
e := make(chan *Exchange)
2011-03-22 02:28:13 +11:00
switch q.Question[0].Qtype {
case TypeAXFR:
go w.axfrReceive(e)
return e, nil
2011-03-22 02:28:13 +11:00
case TypeIXFR:
go w.ixfrReceive(e)
return e, nil
2011-09-12 06:10:04 +10:00
default:
return nil, ErrXfrType
2011-03-22 02:28:13 +11:00
}
panic("not reached")
2011-03-22 01:44:51 +11:00
}
func (w *reply) axfrReceive(c chan *Exchange) {
2011-04-19 06:08:12 +10:00
first := true
2011-09-12 06:10:04 +10:00
defer w.Close()
defer close(c)
2011-03-22 01:44:51 +11:00
for {
2012-08-07 04:34:09 +10:00
in, err := w.receive()
2011-03-22 01:44:51 +11:00
if err != nil {
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: err}
2011-09-10 22:48:22 +10:00
return
2011-03-22 01:44:51 +11:00
}
2011-09-12 06:10:04 +10:00
if w.req.Id != in.Id {
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: ErrId}
2011-09-12 01:01:55 +10:00
return
}
2011-03-22 01:44:51 +11:00
if first {
if !checkXfrSOA(in, true) {
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: ErrXfrSoa}
2011-09-10 22:48:22 +10:00
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
if checkXfrSOA(in, false) {
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: nil}
2011-09-10 22:48:22 +10:00
return
2011-03-22 01:44:51 +11:00
}
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: nil}
2011-03-22 01:44:51 +11:00
}
}
panic("not reached")
2011-04-18 17:58:15 +10:00
}
func (w *reply) ixfrReceive(c chan *Exchange) {
2011-03-22 01:44:51 +11:00
var serial uint32 // The first serial seen is the current server serial
first := true
2011-09-12 06:10:04 +10:00
defer w.Close()
defer close(c)
2011-03-22 01:44:51 +11:00
for {
2012-08-07 04:34:09 +10:00
in, err := w.receive()
2011-09-12 06:10:04 +10:00
if err != nil {
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: err}
2011-09-12 06:10:04 +10:00
return
}
if w.req.Id != in.Id {
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: 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) {
2012-06-21 04:16:36 +10:00
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: nil}
2011-09-12 06:10:04 +10:00
return
2011-03-22 01:44:51 +11:00
}
// Check if the returned answer is ok
2011-03-22 01:44:51 +11:00
if !checkXfrSOA(in, true) {
2012-06-21 04:16:36 +10:00
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: 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
if !first {
2011-09-12 06:10:04 +10:00
w.tsigTimersOnly = true
// If the last record in the IXFR contains the servers' SOA, we should quit
if v, ok := in.Answer[len(in.Answer)-1].(*RR_SOA); ok {
if v.Serial == serial {
2012-06-21 04:16:36 +10:00
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr(), Error: nil}
2011-09-12 06:10:04 +10:00
return
}
}
c <- &Exchange{Request: w.req, Reply: in, Rtt: w.rtt, RemoteAddr: w.conn.RemoteAddr()}
2011-03-22 01:44:51 +11:00
}
}
panic("not reached")
2011-03-19 00:13:42 +11:00
}
2011-09-12 01:24:52 +10:00
2012-05-18 20:29:01 +10:00
// XfrSend performs an outgoing Ixfr or Axfr. The function is [AI]xfr agnostic, it is
2011-09-12 06:10:04 +10:00
// up to the caller to correctly send the sequence of messages.
2011-11-03 09:06:54 +11:00
func XfrSend(w ResponseWriter, q *Msg, a string) error {
2011-09-10 22:59:21 +10:00
switch q.Question[0].Qtype {
2011-11-03 09:06:54 +11:00
case TypeAXFR, TypeIXFR:
// go d.xfrWrite(q, m, e)
2011-09-12 06:10:04 +10:00
default:
return ErrXfrType
2011-09-10 22:59:21 +10:00
}
2011-09-12 06:10:04 +10:00
return nil
2011-09-10 22:59:21 +10:00
}
2011-09-12 01:24:52 +10:00
/*
2011-09-10 22:59:21 +10:00
// 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
2011-03-22 02:28:13 +11:00
// 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
}