| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | package dnstap
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"io"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/dnstap/msg"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/dnstap/taprw"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tap "github.com/dnstap/golang-dnstap"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | // Dnstap is the dnstap handler.
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | type Dnstap struct {
 | 
					
						
							|  |  |  | 	Next middleware.Handler
 | 
					
						
							|  |  |  | 	Out  io.Writer
 | 
					
						
							|  |  |  | 	Pack bool
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | type (
 | 
					
						
							|  |  |  | 	// Tapper is implemented by the Context passed by the dnstap handler.
 | 
					
						
							|  |  |  | 	Tapper interface {
 | 
					
						
							|  |  |  | 		TapMessage(*tap.Message) error
 | 
					
						
							|  |  |  | 		TapBuilder() msg.Builder
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	tapContext struct {
 | 
					
						
							|  |  |  | 		context.Context
 | 
					
						
							|  |  |  | 		Dnstap
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TapperFromContext will return a Tapper if the dnstap middleware is enabled.
 | 
					
						
							|  |  |  | func TapperFromContext(ctx context.Context) (t Tapper) {
 | 
					
						
							|  |  |  | 	t, _ = ctx.(Tapper)
 | 
					
						
							|  |  |  | 	return
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | func tapMessageTo(w io.Writer, m *tap.Message) error {
 | 
					
						
							|  |  |  | 	frame, err := msg.Marshal(m)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return fmt.Errorf("marshal: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	_, err = w.Write(frame)
 | 
					
						
							|  |  |  | 	return err
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | // TapMessage implements Tapper.
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | func (h Dnstap) TapMessage(m *tap.Message) error {
 | 
					
						
							|  |  |  | 	return tapMessageTo(h.Out, m)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | // TapBuilder implements Tapper.
 | 
					
						
							|  |  |  | func (h Dnstap) TapBuilder() msg.Builder {
 | 
					
						
							|  |  |  | 	return msg.Builder{Full: h.Pack}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServeDNS logs the client query and response to dnstap and passes the dnstap Context.
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	rw := &taprw.ResponseWriter{ResponseWriter: w, Tapper: &h, Query: r}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	rw.QueryEpoch()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	code, err := middleware.NextOrFailure(h.Name(), h.Next, tapContext{ctx, h}, rw, r)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		// ignore dnstap errors
 | 
					
						
							|  |  |  | 		return code, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := rw.DnstapError(); err != nil {
 | 
					
						
							|  |  |  | 		return code, middleware.Error("dnstap", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return code, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Name returns dnstap.
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | func (h Dnstap) Name() string { return "dnstap" }
 |