| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | package forward
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/dnstap/msg"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tap "github.com/dnstap/golang-dnstap"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | // toDnstap will send the forward and received message to the dnstap plugin.
 | 
					
						
							|  |  |  | func toDnstap(f *Forward, host string, state request.Request, opts options, reply *dns.Msg, start time.Time) {
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	// Query
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	q := new(tap.Message)
 | 
					
						
							|  |  |  | 	msg.SetQueryTime(q, start)
 | 
					
						
							|  |  |  | 	h, p, _ := net.SplitHostPort(host)      // this is preparsed and can't err here
 | 
					
						
							|  |  |  | 	port, _ := strconv.ParseUint(p, 10, 32) // same here
 | 
					
						
							|  |  |  | 	ip := net.ParseIP(h)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ta net.Addr = &net.UDPAddr{IP: ip, Port: int(port)}
 | 
					
						
							|  |  |  | 	t := state.Proto()
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	switch {
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	case opts.forceTCP:
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 		t = "tcp"
 | 
					
						
							|  |  |  | 	case opts.preferUDP:
 | 
					
						
							|  |  |  | 		t = "udp"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if t == "tcp" {
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		ta = &net.TCPAddr{IP: ip, Port: int(port)}
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-21 02:00:27 -07:00
										 |  |  | 	// Forwarder dnstap messages are from the perspective of the downstream server
 | 
					
						
							|  |  |  | 	// (upstream is the forward server)
 | 
					
						
							|  |  |  | 	msg.SetQueryAddress(q, state.W.RemoteAddr())
 | 
					
						
							|  |  |  | 	msg.SetResponseAddress(q, ta)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if f.tapPlugin.IncludeRawMessage {
 | 
					
						
							|  |  |  | 		buf, _ := state.Req.Pack()
 | 
					
						
							|  |  |  | 		q.QueryMessage = buf
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	msg.SetType(q, tap.Message_FORWARDER_QUERY)
 | 
					
						
							|  |  |  | 	f.tapPlugin.TapMessage(q)
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Response
 | 
					
						
							|  |  |  | 	if reply != nil {
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		r := new(tap.Message)
 | 
					
						
							|  |  |  | 		if f.tapPlugin.IncludeRawMessage {
 | 
					
						
							|  |  |  | 			buf, _ := reply.Pack()
 | 
					
						
							|  |  |  | 			r.ResponseMessage = buf
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		msg.SetQueryTime(r, start)
 | 
					
						
							| 
									
										
										
										
											2021-01-21 02:00:27 -07:00
										 |  |  | 		msg.SetQueryAddress(r, state.W.RemoteAddr())
 | 
					
						
							|  |  |  | 		msg.SetResponseAddress(r, ta)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		msg.SetResponseTime(r, time.Now())
 | 
					
						
							|  |  |  | 		msg.SetType(r, tap.Message_FORWARDER_RESPONSE)
 | 
					
						
							|  |  |  | 		f.tapPlugin.TapMessage(r)
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |