dns/xfr.go

252 lines
5.8 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-26 02:43:47 +11:00
// Xfr is used in communicating with Xfr* functions.
2011-03-22 03:55:14 +11:00
type Xfr struct {
2011-03-26 02:43:47 +11:00
// If Add is true the resource record in RR must be added to
// the zone. If Add is false the resource record must be removed.
// be considered to have failed.
2011-03-25 00:42:35 +11:00
Add bool
2011-03-26 02:43:47 +11:00
// The RR that should be added or removed.
2011-03-22 03:55:14 +11:00
RR
2011-03-26 02:43:47 +11:00
// If err in non nil some error occurred and the transfer must
// be considered to have faild.
2011-03-22 03:55:14 +11:00
Err os.Error
}
2011-03-31 01:34:17 +11:00
// todo maybe add the SOA serial too?
2011-03-22 03:55:14 +11:00
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.
func (d *Conn) XfrRead(q *Msg, m chan *Xfr) {
2011-03-28 23:45:40 +11:00
if d.TCP == nil && d.UDP == nil {
// No connection yet
if err := d.Dial("tcp"); err != nil {
m <- &Xfr{true, nil, err}
2011-03-28 23:45:40 +11:00
close(m)
return
}
}
2011-03-29 01:03:47 +11:00
// Send q now.
2011-03-24 19:24:24 +11:00
err := d.WriteMsg(q)
if err != nil {
m <- &Xfr{true, nil, err}
2011-03-25 19:58:14 +11:00
close(m)
2011-03-24 19:24:24 +11:00
return
}
2011-03-22 02:28:13 +11:00
switch q.Question[0].Qtype {
case TypeAXFR:
d.axfrRead(q, m)
case TypeIXFR:
d.ixfrRead(q, m)
2011-03-25 19:58:14 +11:00
default:
m <- &Xfr{true, nil, &Error{Error: "Xfr Qtype not recognized"}}
2011-03-25 19:58:14 +11:00
close(m)
2011-03-22 02:28:13 +11:00
}
2011-03-22 01:44:51 +11:00
}
2011-03-24 05:37:07 +11: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.
2011-03-31 01:34:17 +11:00
// 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) {
2011-03-22 02:28:13 +11:00
switch q.Question[0].Qtype {
case TypeAXFR:
2011-03-31 01:34:17 +11:00
d.axfrWrite(q, m, e)
2011-03-22 02:28:13 +11:00
case TypeIXFR:
// d.ixfrWrite(q, m)
2011-03-25 19:58:14 +11:00
default:
2011-03-31 04:06:44 +11:00
e <- &Error{Error: "Xfr Qtype not recognized"}
2011-03-25 19:58:14 +11:00
close(m)
2011-03-22 02:28:13 +11:00
}
2011-03-22 01:44:51 +11:00
}
func (d *Conn) axfrRead(q *Msg, m chan *Xfr) {
2011-03-22 01:44:51 +11:00
defer close(m)
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 {
if !checkXfrSOA(in, true) {
m <- &Xfr{true, nil, ErrXfrSoa}
2011-03-22 01:44:51 +11:00
return
}
first = !first
}
2011-03-19 00:13:42 +11:00
2011-03-22 01:44:51 +11:00
if !first {
2011-03-22 03:55:14 +11:00
if d.Tsig != nil {
2011-03-25 04:07:38 +11:00
d.Tsig.TimersOnly = true // Subsequent envelopes use this.
2011-03-22 03:55:14 +11:00
}
2011-03-22 01:44:51 +11:00
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
}
// Just send the zone
2011-03-31 01:34:17 +11:00
func (d *Conn) axfrWrite(q *Msg, m chan *Xfr, e chan os.Error) {
2011-03-22 01:44:51 +11:00
out := new(Msg)
2011-03-22 02:28:13 +11:00
out.Id = q.Id
out.Question = q.Question
2011-03-31 01:34:17 +11:00
out.Answer = make([]RR, 1001) // TODO(mg) look at this number
2011-03-24 19:24:24 +11:00
out.MsgHdr.Response = true
out.MsgHdr.Authoritative = true
2011-03-31 01:34:17 +11:00
first := true
2011-03-22 02:28:13 +11:00
var soa *RR_SOA
i := 0
for r := range m {
out.Answer[i] = r.RR
if soa == nil {
if r.RR.Header().Rrtype != TypeSOA {
2011-03-31 01:34:17 +11:00
e <- ErrXfrSoa
return
2011-03-22 02:28:13 +11:00
} else {
soa = r.RR.(*RR_SOA)
}
}
i++
if i > 1000 {
// Send it
2011-03-23 20:48:21 +11:00
err := d.WriteMsg(out)
2011-03-22 02:28:13 +11:00
if err != nil {
2011-03-31 01:34:17 +11:00
e <- err
return
2011-03-22 02:28:13 +11:00
}
i = 0
2011-03-24 19:24:24 +11:00
// Gaat dit goed?
2011-03-22 02:28:13 +11:00
out.Answer = out.Answer[:0]
2011-03-31 01:34:17 +11:00
if first {
if d.Tsig != nil {
d.Tsig.TimersOnly = true
}
first = !first
}
2011-03-22 02:28:13 +11:00
}
}
2011-03-22 04:37:11 +11:00
// Everything is sent, only the closing soa is left.
2011-03-22 02:28:13 +11:00
out.Answer[i] = soa
2011-03-24 19:24:24 +11:00
out.Answer = out.Answer[:i+1]
2011-03-23 20:48:21 +11:00
err := d.WriteMsg(out)
2011-03-22 02:28:13 +11:00
if err != nil {
2011-03-31 01:34:17 +11:00
e <- err
2011-03-22 02:28:13 +11:00
}
2011-03-22 01:44:51 +11:00
}
func (d *Conn) ixfrRead(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-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
}
// Send the answer section to the channel
func sendMsg(in *Msg, c chan *Xfr, nosoa bool) {
2011-03-22 02:28:13 +11:00
for k, r := range in.Answer {
2011-03-31 04:06:44 +11:00
x := &Xfr{Add: true}
2011-03-22 02:28:13 +11:00
if nosoa && k == len(in.Answer)-1 {
continue
}
x.RR = r
c <- x
}
}