| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | package forward
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2025-08-25 23:06:44 +03:00
										 |  |  | 	"net/netip"
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/dnstap/msg"
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/proxy"
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	"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.
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | func toDnstap(ctx context.Context, f *Forward, host string, state request.Request, opts proxy.Options, reply *dns.Msg, start time.Time) {
 | 
					
						
							| 
									
										
										
										
											2025-08-25 23:06:44 +03:00
										 |  |  | 	ap, _ := netip.ParseAddrPort(host) // this is preparsed and can't err here
 | 
					
						
							|  |  |  | 	ip := net.IP(ap.Addr().AsSlice())
 | 
					
						
							|  |  |  | 	port := int(ap.Port())
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 23:06:44 +03:00
										 |  |  | 	var ta net.Addr = &net.UDPAddr{
 | 
					
						
							|  |  |  | 		IP:   ip,
 | 
					
						
							|  |  |  | 		Port: port,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	t := state.Proto()
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	switch {
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	case opts.ForceTCP:
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 		t = "tcp"
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	case opts.PreferUDP:
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 		t = "udp"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if t == "tcp" {
 | 
					
						
							| 
									
										
										
										
											2025-08-25 23:06:44 +03:00
										 |  |  | 		ta = &net.TCPAddr{IP: ip, Port: port}
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 	for _, t := range f.tapPlugins {
 | 
					
						
							| 
									
										
										
										
											2023-01-31 03:38:15 +08:00
										 |  |  | 		// Query
 | 
					
						
							|  |  |  | 		q := new(tap.Message)
 | 
					
						
							|  |  |  | 		msg.SetQueryTime(q, start)
 | 
					
						
							|  |  |  | 		// 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)
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		if t.IncludeRawMessage {
 | 
					
						
							|  |  |  | 			buf, _ := state.Req.Pack()
 | 
					
						
							|  |  |  | 			q.QueryMessage = buf
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		msg.SetType(q, tap.Message_FORWARDER_QUERY)
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 		t.TapMessageWithMetadata(ctx, q, state)
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Response
 | 
					
						
							|  |  |  | 		if reply != nil {
 | 
					
						
							|  |  |  | 			r := new(tap.Message)
 | 
					
						
							|  |  |  | 			if t.IncludeRawMessage {
 | 
					
						
							|  |  |  | 				buf, _ := reply.Pack()
 | 
					
						
							|  |  |  | 				r.ResponseMessage = buf
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			msg.SetQueryTime(r, start)
 | 
					
						
							|  |  |  | 			msg.SetQueryAddress(r, state.W.RemoteAddr())
 | 
					
						
							|  |  |  | 			msg.SetResponseAddress(r, ta)
 | 
					
						
							|  |  |  | 			msg.SetResponseTime(r, time.Now())
 | 
					
						
							|  |  |  | 			msg.SetType(r, tap.Message_FORWARDER_RESPONSE)
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 			t.TapMessageWithMetadata(ctx, r, state)
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |