| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | package dnstap
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"io"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/dnstap/msg"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/dnstap/taprw"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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 {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	Next plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | 	IO   IORoutine
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	Pack bool
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | type (
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | 	// IORoutine is the dnstap I/O thread as defined by: <http://dnstap.info/Architecture>.
 | 
					
						
							|  |  |  | 	IORoutine interface {
 | 
					
						
							|  |  |  | 		Dnstap(tap.Dnstap)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	// 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
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 13:38:01 -07:00
										 |  |  | // ContextKey defines the type of key that is used to save data into the context
 | 
					
						
							|  |  |  | type ContextKey string
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	// DnstapSendOption specifies the Dnstap message to be send.  Default is sent all.
 | 
					
						
							|  |  |  | 	DnstapSendOption ContextKey = "dnstap-send-option"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // TapperFromContext will return a Tapper if the dnstap plugin is enabled.
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 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 {
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | 	h.IO.Dnstap(msg.Wrap(m))
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-29 13:38:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Add send option into context so other plugin can decide on which DNSTap
 | 
					
						
							|  |  |  | 	// message to be sent out
 | 
					
						
							|  |  |  | 	sendOption := taprw.SendOption{Cq: true, Cr: true}
 | 
					
						
							|  |  |  | 	newCtx := context.WithValue(ctx, DnstapSendOption, &sendOption)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rw := &taprw.ResponseWriter{ResponseWriter: w, Tapper: &h, Query: r, Send: &sendOption}
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:46:41 +01:00
										 |  |  | 	rw.SetQueryEpoch()
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 13:38:01 -07:00
										 |  |  | 	code, err := plugin.NextOrFailure(h.Name(), h.Next, tapContext{newCtx, 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 {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return code, plugin.Error("dnstap", err)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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" }
 |