| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | package msg
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tap "github.com/dnstap/golang-dnstap"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | var (
 | 
					
						
							|  |  |  | 	protoUDP    = tap.SocketProtocol_UDP
 | 
					
						
							|  |  |  | 	protoTCP    = tap.SocketProtocol_TCP
 | 
					
						
							|  |  |  | 	familyINET  = tap.SocketFamily_INET
 | 
					
						
							|  |  |  | 	familyINET6 = tap.SocketFamily_INET6
 | 
					
						
							|  |  |  | )
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | // SetQueryAddress adds the query address to the message. This also sets the SocketFamily and SocketProtocol.
 | 
					
						
							|  |  |  | func SetQueryAddress(t *tap.Message, addr net.Addr) error {
 | 
					
						
							|  |  |  | 	t.SocketFamily = &familyINET
 | 
					
						
							|  |  |  | 	switch a := addr.(type) {
 | 
					
						
							|  |  |  | 	case *net.TCPAddr:
 | 
					
						
							|  |  |  | 		t.SocketProtocol = &protoTCP
 | 
					
						
							|  |  |  | 		t.QueryAddress = a.IP
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		p := uint32(a.Port)
 | 
					
						
							|  |  |  | 		t.QueryPort = &p
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		if a.IP.To4() == nil {
 | 
					
						
							|  |  |  | 			t.SocketFamily = &familyINET6
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	case *net.UDPAddr:
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		t.SocketProtocol = &protoUDP
 | 
					
						
							|  |  |  | 		t.QueryAddress = a.IP
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		p := uint32(a.Port)
 | 
					
						
							|  |  |  | 		t.QueryPort = &p
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		if a.IP.To4() == nil {
 | 
					
						
							|  |  |  | 			t.SocketFamily = &familyINET6
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		return fmt.Errorf("unknown address type: %T", a)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | // SetResponseAddress the response address to the message. This also sets the SocketFamily and SocketProtocol.
 | 
					
						
							|  |  |  | func SetResponseAddress(t *tap.Message, addr net.Addr) error {
 | 
					
						
							|  |  |  | 	t.SocketFamily = &familyINET
 | 
					
						
							|  |  |  | 	switch a := addr.(type) {
 | 
					
						
							|  |  |  | 	case *net.TCPAddr:
 | 
					
						
							|  |  |  | 		t.SocketProtocol = &protoTCP
 | 
					
						
							|  |  |  | 		t.ResponseAddress = a.IP
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		p := uint32(a.Port)
 | 
					
						
							|  |  |  | 		t.ResponsePort = &p
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		if a.IP.To4() == nil {
 | 
					
						
							|  |  |  | 			t.SocketFamily = &familyINET6
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	case *net.UDPAddr:
 | 
					
						
							|  |  |  | 		t.SocketProtocol = &protoUDP
 | 
					
						
							|  |  |  | 		t.ResponseAddress = a.IP
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		p := uint32(a.Port)
 | 
					
						
							|  |  |  | 		t.ResponsePort = &p
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		if a.IP.To4() == nil {
 | 
					
						
							|  |  |  | 			t.SocketFamily = &familyINET6
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		return fmt.Errorf("unknown address type: %T", a)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | // SetQueryTime sets the time of the query in t.
 | 
					
						
							|  |  |  | func SetQueryTime(t *tap.Message, ti time.Time) {
 | 
					
						
							|  |  |  | 	qts := uint64(ti.Unix())
 | 
					
						
							|  |  |  | 	qtn := uint32(ti.Nanosecond())
 | 
					
						
							|  |  |  | 	t.QueryTimeSec = &qts
 | 
					
						
							|  |  |  | 	t.QueryTimeNsec = &qtn
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | // SetResponseTime sets the time of the response in t.
 | 
					
						
							|  |  |  | func SetResponseTime(t *tap.Message, ti time.Time) {
 | 
					
						
							|  |  |  | 	rts := uint64(ti.Unix())
 | 
					
						
							|  |  |  | 	rtn := uint32(ti.Nanosecond())
 | 
					
						
							|  |  |  | 	t.ResponseTimeSec = &rts
 | 
					
						
							|  |  |  | 	t.ResponseTimeNsec = &rtn
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | // SetType sets the type in t.
 | 
					
						
							|  |  |  | func SetType(t *tap.Message, typ tap.Message_Type) { t.Type = &typ }
 |