Envelope is the correct term in the context of transfers

This commit is contained in:
Miek Gieben 2012-12-02 08:47:32 +01:00
parent a115d2230a
commit 724a156794
1 changed files with 23 additions and 23 deletions

46
xfr.go
View File

@ -1,13 +1,13 @@
package dns package dns
// XfrMsg is used when doing [IA]xfr with a remote server. // Envelope is used when doing [IA]xfr with a remote server.
type XfrMsg struct { type Envelope struct {
RR []RR // The set of RRs in the answer section of the AXFR reply message. RR []RR // The set of RRs in the answer section of the AXFR reply message.
Error error // If something went wrong, this contains the error. Error error // If something went wrong, this contains the error.
} }
// XfrReceive performs a [AI]xfr request (depends on the message's Qtype). It returns // XfrReceive performs a [AI]xfr request (depends on the message's Qtype). It returns
// a channel of *XfrMsg on which the replies from the server are sent. At the end of // a channel of *Envelope on which the replies from the server are sent. At the end of
// the transfer the channel is closed. // the transfer the channel is closed.
// The messages are TSIG checked if // The messages are TSIG checked if
// needed, no other post-processing is performed. The caller must dissect the returned // needed, no other post-processing is performed. The caller must dissect the returned
@ -20,7 +20,7 @@ type XfrMsg struct {
// for r := range t { // for r := range t {
// // ... deal with r.RR or r.Error // // ... deal with r.RR or r.Error
// } // }
func (c *Client) XfrReceive(q *Msg, a string) (chan *XfrMsg, error) { func (c *Client) XfrReceive(q *Msg, a string) (chan *Envelope, error) {
w := new(reply) w := new(reply)
w.client = c w.client = c
w.addr = a w.addr = a
@ -31,7 +31,7 @@ func (c *Client) XfrReceive(q *Msg, a string) (chan *XfrMsg, error) {
if err := w.send(q); err != nil { if err := w.send(q); err != nil {
return nil, err return nil, err
} }
e := make(chan *XfrMsg) e := make(chan *Envelope)
switch q.Question[0].Qtype { switch q.Question[0].Qtype {
case TypeAXFR: case TypeAXFR:
go w.axfrReceive(q, e) go w.axfrReceive(q, e)
@ -45,23 +45,23 @@ func (c *Client) XfrReceive(q *Msg, a string) (chan *XfrMsg, error) {
panic("dns: not reached") panic("dns: not reached")
} }
func (w *reply) axfrReceive(q *Msg, c chan *XfrMsg) { func (w *reply) axfrReceive(q *Msg, c chan *Envelope) {
first := true first := true
defer w.conn.Close() defer w.conn.Close()
defer close(c) defer close(c)
for { for {
in, err := w.receive() in, err := w.receive()
if err != nil { if err != nil {
c <- &XfrMsg{nil, err} c <- &Envelope{nil, err}
return return
} }
if in.Id != q.Id { if in.Id != q.Id {
c <- &XfrMsg{in.Answer, ErrId} c <- &Envelope{in.Answer, ErrId}
return return
} }
if first { if first {
if !checkXfrSOA(in, true) { if !checkXfrSOA(in, true) {
c <- &XfrMsg{in.Answer, ErrSoa} c <- &Envelope{in.Answer, ErrSoa}
return return
} }
first = !first first = !first
@ -70,16 +70,16 @@ func (w *reply) axfrReceive(q *Msg, c chan *XfrMsg) {
if !first { if !first {
w.tsigTimersOnly = true // Subsequent envelopes use this. w.tsigTimersOnly = true // Subsequent envelopes use this.
if checkXfrSOA(in, false) { if checkXfrSOA(in, false) {
c <- &XfrMsg{in.Answer, nil} c <- &Envelope{in.Answer, nil}
return return
} }
c <- &XfrMsg{in.Answer, nil} c <- &Envelope{in.Answer, nil}
} }
} }
panic("dns: not reached") panic("dns: not reached")
} }
func (w *reply) ixfrReceive(q *Msg, c chan *XfrMsg) { func (w *reply) ixfrReceive(q *Msg, c chan *Envelope) {
var serial uint32 // The first serial seen is the current server serial var serial uint32 // The first serial seen is the current server serial
first := true first := true
defer w.conn.Close() defer w.conn.Close()
@ -87,23 +87,23 @@ func (w *reply) ixfrReceive(q *Msg, c chan *XfrMsg) {
for { for {
in, err := w.receive() in, err := w.receive()
if err != nil { if err != nil {
c <- &XfrMsg{in.Answer, err} c <- &Envelope{in.Answer, err}
return return
} }
if q.Id != in.Id { if q.Id != in.Id {
c <- &XfrMsg{in.Answer, ErrId} c <- &Envelope{in.Answer, ErrId}
return return
} }
if first { if first {
// A single SOA RR signals "no changes" // A single SOA RR signals "no changes"
if len(in.Answer) == 1 && checkXfrSOA(in, true) { if len(in.Answer) == 1 && checkXfrSOA(in, true) {
c <- &XfrMsg{in.Answer, nil} c <- &Envelope{in.Answer, nil}
return return
} }
// Check if the returned answer is ok // Check if the returned answer is ok
if !checkXfrSOA(in, true) { if !checkXfrSOA(in, true) {
c <- &XfrMsg{in.Answer, ErrSoa} c <- &Envelope{in.Answer, ErrSoa}
return return
} }
// This serial is important // This serial is important
@ -117,11 +117,11 @@ func (w *reply) ixfrReceive(q *Msg, c chan *XfrMsg) {
// 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 v, ok := in.Answer[len(in.Answer)-1].(*RR_SOA); ok { if v, ok := in.Answer[len(in.Answer)-1].(*RR_SOA); ok {
if v.Serial == serial { if v.Serial == serial {
c <- &XfrMsg{in.Answer, nil} c <- &Envelope{in.Answer, nil}
return return
} }
} }
c <- &XfrMsg{in.Answer, nil} c <- &Envelope{in.Answer, nil}
} }
} }
panic("dns: not reached") panic("dns: not reached")
@ -143,7 +143,7 @@ func checkXfrSOA(in *Msg, first bool) bool {
// XfrSend performs an outgoing [AI]xfr depending on the request message. The // XfrSend performs an outgoing [AI]xfr depending on the request message. The
// caller is responsible for sending the correct sequence of RR sets through // caller is responsible for sending the correct sequence of RR sets through
// the channel c. For reasons of symmetry XfrMsg is re-used. // the channel c. For reasons of symmetry Envelope is re-used.
// Errors are signaled via the error pointer, when an error occurs the function // Errors are signaled via the error pointer, when an error occurs the function
// sets the error and returns (it does not close the channel). // sets the error and returns (it does not close the channel).
// TSIG and enveloping is handled by XfrSend. // TSIG and enveloping is handled by XfrSend.
@ -151,19 +151,19 @@ func checkXfrSOA(in *Msg, first bool) bool {
// Basic use pattern for sending an AXFR: // Basic use pattern for sending an AXFR:
// //
// // q contains the AXFR request // // q contains the AXFR request
// c := make(chan *XfrMsg) // c := make(chan *Envelope)
// var e *error // var e *error
// err := XfrSend(w, q, c, e) // err := XfrSend(w, q, c, e)
// w.Hijack() // hijack the connection so that the library doesn't close it // w.Hijack() // hijack the connection so that the library doesn't close it
// for _, rrset := range rrsets { // rrset is a []RR // for _, rrset := range rrsets { // rrset is a []RR
// c <- &{XfrMsg{RR: rrset} // c <- &{Envelope{RR: rrset}
// if e != nil { // if e != nil {
// close(c) // close(c)
// break // break
// } // }
// } // }
// // w.Close() // Don't! Let the client close the connection // // w.Close() // Don't! Let the client close the connection
func XfrSend(w ResponseWriter, q *Msg, c chan *XfrMsg, e *error) error { func XfrSend(w ResponseWriter, q *Msg, c chan *Envelope, e *error) error {
switch q.Question[0].Qtype { switch q.Question[0].Qtype {
case TypeAXFR, TypeIXFR: case TypeAXFR, TypeIXFR:
go axfrSend(w, q, c, e) go axfrSend(w, q, c, e)
@ -175,7 +175,7 @@ func XfrSend(w ResponseWriter, q *Msg, c chan *XfrMsg, e *error) error {
} }
// TODO(mg): count the RRs and the resulting size. // TODO(mg): count the RRs and the resulting size.
func axfrSend(w ResponseWriter, req *Msg, c chan *XfrMsg, e *error) { func axfrSend(w ResponseWriter, req *Msg, c chan *Envelope, e *error) {
rep := new(Msg) rep := new(Msg)
rep.SetReply(req) rep.SetReply(req)
rep.Authoritative = true rep.Authoritative = true