Skip to content

Commit

Permalink
return errors when translating DNSRR->dns.RR (#15)
Browse files Browse the repository at this point in the history
* add new method that returns error
* log failures to translate records
* deprecate old method

fixes #13
  • Loading branch information
fardog authored Nov 19, 2017
1 parent 5a9f7bf commit a98d2fe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
34 changes: 18 additions & 16 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,9 @@ func (h *Handler) Handle(w dns.ResponseWriter, r *dns.Msg) {
}

// Parse google RRs to DNS RRs
answers := []dns.RR{}
for _, a := range dnsResp.Answer {
answers = append(answers, a.RR())
}

// Parse google RRs to DNS RRs
authorities := []dns.RR{}
for _, ns := range dnsResp.Authority {
authorities = append(authorities, ns.RR())
}

// Parse google RRs to DNS RRs
extras := []dns.RR{}
for _, extra := range dnsResp.Extra {
authorities = append(authorities, extra.RR())
}
answers := transformRR(dnsResp.Answer, "answer")
authorities := transformRR(dnsResp.Authority, "authority")
extras := transformRR(dnsResp.Extra, "extra")

resp := dns.Msg{
MsgHdr: dns.MsgHdr{
Expand Down Expand Up @@ -87,3 +74,18 @@ func (h *Handler) Handle(w dns.ResponseWriter, r *dns.Msg) {
log.Errorln("Error writing DNS response:", err)
}
}

// for a given []DNSRR, transform to dns.RR, logging if any errors occur
func transformRR(rrs []DNSRR, logType string) []dns.RR {
var t []dns.RR

for _, r := range rrs {
if rr, err := r.DNSRR(); err != nil {
log.Errorln("unable to translate record rr", logType, r, err)
} else {
t = append(t, rr)
}
}

return t
}
15 changes: 14 additions & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@ type DNSRR struct {
Data string `json:"data,omitempty"`
}

// RR transforms a DNSRR to a dns.RR.
// RR is deprecated as of 2.2.0, use DNSRR instead
func (r DNSRR) RR() dns.RR {
hdr := dns.RR_Header{Name: r.Name, Rrtype: r.Type, Class: dns.ClassINET, Ttl: r.TTL}
str := hdr.String() + r.Data
rr, _ := dns.NewRR(str)
return rr
}

// DNSRR transforms a DNSRR to a dns.RR; returns `nil` if an RR could not be
// created from the record.
func (r DNSRR) DNSRR() (dns.RR, error) {
hdr := dns.RR_Header{Name: r.Name, Rrtype: r.Type, Class: dns.ClassINET, Ttl: r.TTL}
str := hdr.String() + r.Data
return dns.NewRR(str)
}

func (r DNSRR) String() string {
hdr := dns.RR_Header{Name: r.Name, Rrtype: r.Type, Class: dns.ClassINET, Ttl: r.TTL}
return hdr.String()
}

// DNSResponse represents a complete DNS server response, to be served by the
// DNS server handler.
type DNSResponse struct {
Expand Down
20 changes: 16 additions & 4 deletions provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ func TestDNSRRTypeA(t *testing.T) {
Data: "10.10.10.1",
}

rr = r.RR()
rr, err := r.DNSRR()
if err != nil {
t.Fatal(err)
}

v, ok := rr.(*dns.A)
if !ok {
Expand Down Expand Up @@ -46,7 +49,10 @@ func TestDNSRRTypeMX(t *testing.T) {
Data: "10 mail.who.wut.co.jp",
}

rr = r.RR()
rr, err := r.DNSRR()
if err != nil {
t.Fatal(err)
}

v, ok := rr.(*dns.MX)
if !ok {
Expand Down Expand Up @@ -78,7 +84,10 @@ func TestDNSRRTypeCNAME(t *testing.T) {
Data: "omg.wtf.bbq",
}

rr = r.RR()
rr, err := r.DNSRR()
if err != nil {
t.Fatal(err)
}

v, ok := rr.(*dns.CNAME)
if !ok {
Expand Down Expand Up @@ -107,7 +116,10 @@ func TestDNSRRTypeAAAA(t *testing.T) {
Data: "::1",
}

rr = r.RR()
rr, err := r.DNSRR()
if err != nil {
t.Fatal(err)
}

v, ok := rr.(*dns.AAAA)
if !ok {
Expand Down

0 comments on commit a98d2fe

Please sign in to comment.