diff --git a/dnsrocks/dnsdata/data.go b/dnsrocks/dnsdata/data.go index bd4a367..90cffd4 100644 --- a/dnsrocks/dnsdata/data.go +++ b/dnsrocks/dnsdata/data.go @@ -287,7 +287,7 @@ const ( func (m Lmap) String() string { var builder strings.Builder - putlmaptext(&builder, m) + Putlmaptext(&builder, m) return builder.String() } @@ -1639,7 +1639,8 @@ func putloc(w io.Writer, lo Loc) error { return err } -func putloctext(w io.Writer, lo Loc) { +// Putloctext escapes the location ID and writes it to the writer +func Putloctext(w io.Writer, lo Loc) { if len(lo) == 2 { for _, b := range lo { fmt.Fprintf(w, "\\%03o", b) @@ -1670,7 +1671,8 @@ func putlmap(w io.Writer, m Lmap) { } } -func putlmaptext(w io.Writer, m Lmap) { +// Putlmaptext escapes the map ID and writes it to the writer +func Putlmaptext(w io.Writer, m Lmap) { if len(m) == 0 { m = []byte{0, 0} } diff --git a/dnsrocks/dnsdata/data_marshaltext.go b/dnsrocks/dnsdata/data_marshaltext.go index 3317484..ffd8d1e 100644 --- a/dnsrocks/dnsdata/data_marshaltext.go +++ b/dnsrocks/dnsdata/data_marshaltext.go @@ -44,7 +44,7 @@ func (r *Rsoa) MarshalText() (text []byte, err error) { // skip one unused field w.Write(NSEP) w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -53,11 +53,11 @@ func (r *Rsoa) MarshalText() (text []byte, err error) { func (r *Rnet) MarshalText() (text []byte, err error) { w := new(bytes.Buffer) w.WriteString(string(prefixNet)) - putloctext(w, r.lo) + Putloctext(w, r.lo) w.Write(NSEP) w.Write([]byte(r.ipnet.String())) w.Write(NSEP) - putlmaptext(w, r.lmap) + Putlmaptext(w, r.lmap) return w.Bytes(), nil } @@ -79,7 +79,7 @@ func (r *Rns) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.Rns1.lo) + Putloctext(w, r.Rns1.lo) return w.Bytes(), nil } @@ -97,7 +97,7 @@ func (r *Rns1) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -120,7 +120,7 @@ func (r *Raddr) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) w.Write(NSEP) fmt.Fprintf(w, "%d", r.weight) return w.Bytes(), nil @@ -145,7 +145,7 @@ func (r *Rpaddr) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -169,7 +169,7 @@ func (r *Rmx) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.Rmx1.lo) + Putloctext(w, r.Rmx1.lo) return w.Bytes(), nil } @@ -189,7 +189,7 @@ func (r *Rmx1) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -217,7 +217,7 @@ func (r *Rsrv) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.Rsrv1.lo) + Putloctext(w, r.Rsrv1.lo) return w.Bytes(), nil } @@ -241,7 +241,7 @@ func (r *Rsrv1) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -260,7 +260,7 @@ func (r *Rcname) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -276,7 +276,7 @@ func (r *Rptr) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -295,7 +295,7 @@ func (r *Rtxt) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -313,7 +313,7 @@ func (r *Raux) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.lo) + Putloctext(w, r.lo) return w.Bytes(), nil } @@ -335,7 +335,7 @@ func (r *Rdot) MarshalText() (text []byte, err error) { w.Write(NSEP) // skip one unused field w.Write(NSEP) - putloctext(w, r.Rns1.lo) + Putloctext(w, r.Rns1.lo) return w.Bytes(), nil } @@ -345,7 +345,7 @@ func (r *Ripmap) MarshalText() (text []byte, err error) { w.WriteString(string(prefixIPMap)) putdomtext(w, r.dom) w.Write(NSEP) - putlmaptext(w, r.lmap) + Putlmaptext(w, r.lmap) return w.Bytes(), nil } @@ -355,7 +355,7 @@ func (r *Rcsmap) MarshalText() (text []byte, err error) { w.WriteString(string(prefixCSMap)) putdomtext(w, r.dom) w.Write(NSEP) - putlmaptext(w, r.lmap) + Putlmaptext(w, r.lmap) return w.Bytes(), nil } @@ -381,7 +381,7 @@ func (r *Rsvcb) MarshalText() (text []byte, err error) { buf.Write(NSEP) fmt.Fprintf(buf, "%d", r.ttl) buf.Write(NSEP) - putloctext(buf, r.lo) + Putloctext(buf, r.lo) buf.Write(NSEP) fmt.Fprintf(buf, "%d", r.priority) buf.Write(NSEP) diff --git a/dnsrocks/dnsdata/rearranger.go b/dnsrocks/dnsdata/rearranger.go index 6a2360d..b253211 100644 --- a/dnsrocks/dnsdata/rearranger.go +++ b/dnsrocks/dnsdata/rearranger.go @@ -75,7 +75,7 @@ func (r RangePoints) String() string { func (p *RangePoint) MarshalTextForLmap(lmap Lmap) (text []byte, err error) { w := new(bytes.Buffer) w.WriteString(string(prefixRangePoint)) - putlmaptext(w, lmap) + Putlmaptext(w, lmap) w.Write(NSEP) ip := p.To16() b, err := ip.MarshalText() @@ -92,7 +92,7 @@ func (p *RangePoint) MarshalTextForLmap(lmap Lmap) (text []byte, err error) { fmt.Fprint(w, mlen) w.Write(NSEP) lo := Loc(p.LocID()) - putloctext(w, lo) + Putloctext(w, lo) } return w.Bytes(), nil } diff --git a/dnsrocks/dnsserver/handler.go b/dnsrocks/dnsserver/handler.go index 1cccea7..850ad6c 100644 --- a/dnsrocks/dnsserver/handler.go +++ b/dnsrocks/dnsserver/handler.go @@ -107,7 +107,7 @@ func MakeOPTWithECS(s string) (*dns.OPT, error) { } // writeAndLog writes the response to the network as well as log and bump stats -func (h *FBDNSDB) writeAndLog(state request.Request, resp *dns.Msg, ecs *dns.EDNS0_SUBNET) (int, error) { +func (h *FBDNSDB) writeAndLog(state request.Request, resp *dns.Msg, ecs *dns.EDNS0_SUBNET, loc *db.Location) (int, error) { rcode := resp.Rcode state.SizeAndDo(resp) @@ -123,7 +123,7 @@ func (h *FBDNSDB) writeAndLog(state request.Request, resp *dns.Msg, ecs *dns.EDN if err != nil { return dns.RcodeServerFailure, err } - h.logger.Log(state, resp, ecs) + h.logger.Log(state, resp, ecs, loc) if !resp.Authoritative { h.stats.IncrementCounter("DNS_queries_notauthoritative") } @@ -181,7 +181,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r // Check if this is a supported edns version if a, err := edns.Version(r); err != nil { // Wrong EDNS version, return at once. - return h.writeAndLog(state, a, ecs) + return h.writeAndLog(state, a, ecs, loc) } offset, err := dns.PackDomainName(state.Name(), packedQName, 0, nil, false) @@ -189,7 +189,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r h.stats.IncrementCounter("DNS_error.pack_domain_fail") glog.Errorf("could not pack domain %s", state.Name()) dns.HandleFailed(w, r) - h.logger.LogFailed(state, r, ecs) + h.logger.LogFailed(state, r, ecs, loc) // nolint: nilerr return dns.RcodeServerFailure, nil } @@ -199,7 +199,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r if ecs, loc, err = reader.FindLocation(packedQName, r, state.IP()); err != nil { glog.Errorf("%s: failed to find location: %v", state.Name(), err) //dns.HandleFailed(w, r) - h.logger.LogFailed(state, r, ecs) + h.logger.LogFailed(state, r, ecs, loc) return dns.RcodeServerFailure, nil } @@ -207,7 +207,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r // We could not find a location, not even the default one... potentially a bogus DB. // dns.HandleFailed(w, r) glog.Errorf("%s: nil location", state.Name()) - h.logger.LogFailed(state, r, ecs) + h.logger.LogFailed(state, r, ecs, loc) return dns.RcodeServerFailure, nil } @@ -257,7 +257,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r resp.Extra = append([]dns.RR{o}, resp.Extra...) } - return h.writeAndLog(state, resp, ecs) + return h.writeAndLog(state, resp, ecs, loc) } } else { h.stats.IncrementCounter("DNS_cache.missed") @@ -293,7 +293,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r m.IsEdns0().Option = append(m.IsEdns0().Option, &ede) } // does not matter if this write fails - return h.writeAndLog(state, m, ecs) + return h.writeAndLog(state, m, ecs, loc) } // Not authoritative but we have NS (implicit or we would not have passed the @@ -337,7 +337,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r if err != nil { glog.Errorf("Failed to unpack control domain name %s", err) dns.HandleFailed(w, r) - h.logger.Log(state, r, ecs) + h.logger.Log(state, r, ecs, loc) return dns.RcodeServerFailure, nil } @@ -387,7 +387,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r a.Extra = append([]dns.RR{o}, a.Extra...) } - return h.writeAndLog(state, a, ecs) + return h.writeAndLog(state, a, ecs, loc) } // ServeDNS implements the plugin.Handler interface. diff --git a/dnsrocks/dnsserver/logger.go b/dnsrocks/dnsserver/logger.go index 84674f7..01da8f6 100644 --- a/dnsrocks/dnsserver/logger.go +++ b/dnsrocks/dnsserver/logger.go @@ -19,15 +19,16 @@ import ( "strings" "github.com/coredns/coredns/request" + "github.com/facebook/dns/dnsrocks/db" "github.com/miekg/dns" ) // Logger is an interface for logging messages type Logger interface { // LogFailed logs a message when we could not construct an answer - LogFailed(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET) + LogFailed(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET, loc *db.Location) // Log logs a DNS response - Log(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET) + Log(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET, loc *db.Location) } // TextLogger logs to an io.Writer @@ -36,24 +37,25 @@ type TextLogger struct { } // Log is used to log to an ioWriter. -func (l *TextLogger) Log(state request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET) { +func (l *TextLogger) Log(state request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET, _ *db.Location) { fmt.Fprintf(l.IoWriter, "[%s] %s %s %s\n", state.IP(), strings.ToUpper(state.Proto()), state.Name(), state.Type()) } // LogFailed is used to log failures -func (l *TextLogger) LogFailed(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET) { +func (l *TextLogger) LogFailed(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET, loc *db.Location) { m := new(dns.Msg) m.SetRcode(r, dns.RcodeServerFailure) - l.Log(state, m, ecs) + l.Log(state, m, ecs, loc) } // DummyLogger logs nothing type DummyLogger struct{} // Log is used to log to an ioWriter. -func (l *DummyLogger) Log(_ request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET) {} +func (l *DummyLogger) Log(_ request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET, _ *db.Location) {} // LogFailed is used to log failures -func (l *DummyLogger) LogFailed(_ request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET) {} +func (l *DummyLogger) LogFailed(_ request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET, _ *db.Location) { +}