2011-03-17 02:21:35 +11:00
|
|
|
package dns
|
|
|
|
|
2012-08-28 16:14:12 +10:00
|
|
|
// XfrToken is used when doing [IA]xfr with a remote server.
|
|
|
|
type XfrToken struct {
|
2012-08-28 04:58:58 +10:00
|
|
|
RR []RR // the set of RRs in the answer section form the message of the server
|
|
|
|
Error error // if something went wrong, this contains the error
|
2012-08-08 17:44:34 +10:00
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:58 +10:00
|
|
|
// XfrReceive performs a [AI]xfr request (depends on the message's Qtype). It returns
|
2012-08-28 16:14:12 +10:00
|
|
|
// a channel of XfrToken on which the replies from the server are sent. At the end of
|
2012-08-28 03:48:58 +10:00
|
|
|
// the transfer the channel is closed.
|
|
|
|
// It panics if the Qtype does not equal TypeAXFR or TypeIXFR. The messages are TSIG checked if
|
|
|
|
// needed, no other post-processing is performed. The caller must dissect the returned
|
|
|
|
// messages.
|
|
|
|
//
|
|
|
|
// Basic use pattern for receiving an AXFR:
|
|
|
|
//
|
2012-08-28 04:58:58 +10:00
|
|
|
// // m contains the AXFR request
|
2012-08-28 03:48:58 +10:00
|
|
|
// t, e := client.XfrReceive(m, "127.0.0.1:53")
|
|
|
|
// for r := range t {
|
2012-08-28 04:58:58 +10:00
|
|
|
// // ... deal with r.RR or r.Error
|
2012-08-28 03:48:58 +10:00
|
|
|
// }
|
2012-08-28 16:14:12 +10:00
|
|
|
func (c *Client) XfrReceive(q *Msg, a string) (chan *XfrToken, 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 {
|
2012-05-18 20:06:31 +10:00
|
|
|
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 {
|
2012-05-18 20:06:31 +10:00
|
|
|
return nil, err
|
2011-04-19 06:08:12 +10:00
|
|
|
}
|
2012-08-28 16:14:12 +10:00
|
|
|
e := make(chan *XfrToken)
|
2011-03-22 02:28:13 +11:00
|
|
|
switch q.Question[0].Qtype {
|
|
|
|
case TypeAXFR:
|
2012-05-18 20:06:31 +10:00
|
|
|
go w.axfrReceive(e)
|
|
|
|
return e, nil
|
2011-03-22 02:28:13 +11:00
|
|
|
case TypeIXFR:
|
2012-05-18 20:06:31 +10:00
|
|
|
go w.ixfrReceive(e)
|
|
|
|
return e, nil
|
2011-09-12 06:10:04 +10:00
|
|
|
default:
|
2012-05-18 20:06:31 +10:00
|
|
|
return nil, ErrXfrType
|
2011-03-22 02:28:13 +11:00
|
|
|
}
|
2012-05-18 20:06:31 +10:00
|
|
|
panic("not reached")
|
2011-03-22 01:44:51 +11:00
|
|
|
}
|
|
|
|
|
2012-08-28 16:14:12 +10:00
|
|
|
func (w *reply) axfrReceive(c chan *XfrToken) {
|
2011-04-19 06:08:12 +10:00
|
|
|
first := true
|
2012-08-23 18:33:33 +10:00
|
|
|
defer w.conn.Close()
|
2012-05-18 20:06:31 +10:00
|
|
|
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 {
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, 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 {
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, ErrId}
|
2011-09-12 01:01:55 +10:00
|
|
|
return
|
|
|
|
}
|
2011-03-22 01:44:51 +11:00
|
|
|
if first {
|
|
|
|
if !checkXfrSOA(in, true) {
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, 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) {
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, nil}
|
2011-09-10 22:48:22 +10:00
|
|
|
return
|
2011-03-22 01:44:51 +11:00
|
|
|
}
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, nil}
|
2011-03-22 01:44:51 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("not reached")
|
2011-04-18 17:58:15 +10:00
|
|
|
}
|
|
|
|
|
2012-08-28 16:14:12 +10:00
|
|
|
func (w *reply) ixfrReceive(c chan *XfrToken) {
|
2011-03-22 01:44:51 +11:00
|
|
|
var serial uint32 // The first serial seen is the current server serial
|
|
|
|
first := true
|
2012-08-23 18:33:33 +10:00
|
|
|
defer w.conn.Close()
|
2012-05-18 20:06:31 +10:00
|
|
|
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 {
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, err}
|
2011-09-12 06:10:04 +10:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if w.req.Id != in.Id {
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, 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-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, nil}
|
2011-09-12 06:10:04 +10:00
|
|
|
return
|
2011-03-22 01:44:51 +11:00
|
|
|
}
|
|
|
|
|
2011-09-11 00:49:22 +10:00
|
|
|
// Check if the returned answer is ok
|
2011-03-22 01:44:51 +11:00
|
|
|
if !checkXfrSOA(in, true) {
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, 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-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, nil}
|
2011-09-12 06:10:04 +10:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 16:14:12 +10:00
|
|
|
c <- &XfrToken{in.Answer, nil}
|
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-08-28 16:14:12 +10: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
|
|
|
|
}
|
|
|
|
|
2012-08-28 04:58:58 +10:00
|
|
|
// XfrSend performs an outgoing [IX]xfr depending on the request message. As
|
|
|
|
// long as the channel c is open ... TODO(mg): docs
|
|
|
|
// tsig is done, enveloping is done, voor de rest niks... TODO
|
2012-08-28 16:24:42 +10:00
|
|
|
func XfrSend(w ResponseWriter, q *Msg) (chan *XfrToken, error) {
|
|
|
|
c := make(chan *XfrToken)
|
2012-08-28 04:58:58 +10:00
|
|
|
switch req.Question[0].Qtype {
|
2011-11-03 09:06:54 +11:00
|
|
|
case TypeAXFR, TypeIXFR:
|
2012-08-28 16:24:42 +10:00
|
|
|
go axfrSend(w, q, c)
|
|
|
|
return c, nil
|
2011-09-12 06:10:04 +10:00
|
|
|
default:
|
|
|
|
return ErrXfrType
|
2012-08-28 16:24:42 +10:00
|
|
|
return nil, ErrXfrType
|
2011-09-10 22:59:21 +10:00
|
|
|
}
|
2012-08-28 16:24:42 +10:00
|
|
|
panic("not reached")
|
2011-09-10 22:59:21 +10:00
|
|
|
}
|
|
|
|
|
2012-08-28 05:28:02 +10:00
|
|
|
// TODO(mg): count the RRs and the resulting size.
|
2012-08-28 16:14:12 +10:00
|
|
|
func axfrSend(w ResponseWriter, req *Msg, c chan *XfrToken) {
|
2012-08-28 04:58:58 +10:00
|
|
|
rep := new(Msg)
|
|
|
|
rep.SetReply(req)
|
|
|
|
rep.MsgHdr.Authoritative = true
|
|
|
|
|
2012-08-28 05:28:02 +10:00
|
|
|
first := true
|
|
|
|
w.TsigTimersOnly(false)
|
2012-08-28 04:58:58 +10:00
|
|
|
for x := range c {
|
2012-08-28 16:14:12 +10:00
|
|
|
// assume it fits
|
2012-08-28 04:58:58 +10:00
|
|
|
rep.Answer = append(rep.Answer, x.RR...)
|
|
|
|
w.Write(rep)
|
2012-08-28 05:28:02 +10:00
|
|
|
if first {
|
|
|
|
first = !first
|
|
|
|
w.TsigTimersOnly(first)
|
2012-08-28 04:58:58 +10:00
|
|
|
}
|
|
|
|
rep.Answer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|