| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package middleware
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | // This file contains the state functions available for use in the middlewares.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | // State contains some connection state and is useful in middleware.
 | 
					
						
							|  |  |  | type State struct {
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	Req *dns.Msg
 | 
					
						
							|  |  |  | 	W   dns.ResponseWriter
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Cache size after first call to Size or Do
 | 
					
						
							|  |  |  | 	size int
 | 
					
						
							|  |  |  | 	do   int // 0: not, 1: true: 2: false
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Now returns the current timestamp in the specified format.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Now(format string) string { return time.Now().Format(format) }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 17:23:06 +00:00
										 |  |  | // NowDate returns the current date/time that can be used in other time functions.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) NowDate() time.Time { return time.Now() }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 17:23:06 +00:00
										 |  |  | // Header gets the heaser of the request in State.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Header() *dns.RR_Header {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	// TODO(miek)
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // IP gets the (remote) IP address of the client making the request.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) IP() string {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	ip, _, err := net.SplitHostPort(s.W.RemoteAddr().String())
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 		return s.W.RemoteAddr().String()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return ip
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Post gets the (remote) Port of the client making the request.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Port() (string, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	_, port, err := net.SplitHostPort(s.W.RemoteAddr().String())
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return "0", err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return port, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | // RemoteAddr returns the net.Addr of the client that sent the current request.
 | 
					
						
							|  |  |  | func (s *State) RemoteAddr() string {
 | 
					
						
							|  |  |  | 	return s.W.RemoteAddr().String()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // Proto gets the protocol used as the transport. This
 | 
					
						
							|  |  |  | // will be udp or tcp.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Proto() string {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	if _, ok := s.W.RemoteAddr().(*net.UDPAddr); ok {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		return "udp"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	if _, ok := s.W.RemoteAddr().(*net.TCPAddr); ok {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		return "tcp"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return "udp"
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Family returns the family of the transport.
 | 
					
						
							|  |  |  | // 1 for IPv4 and 2 for IPv6.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Family() int {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	var a net.IP
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	ip := s.W.RemoteAddr()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	if i, ok := ip.(*net.UDPAddr); ok {
 | 
					
						
							|  |  |  | 		a = i.IP
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if i, ok := ip.(*net.TCPAddr); ok {
 | 
					
						
							|  |  |  | 		a = i.IP
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if a.To4() != nil {
 | 
					
						
							|  |  |  | 		return 1
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return 2
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | // Do returns if the request has the DO (DNSSEC OK) bit set.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Do() bool {
 | 
					
						
							|  |  |  | 	if s.do != 0 {
 | 
					
						
							|  |  |  | 		return s.do == doTrue
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	if o := s.Req.IsEdns0(); o != nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 		if o.Do() {
 | 
					
						
							|  |  |  | 			s.do = doTrue
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			s.do = doFalse
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 		return o.Do()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	s.do = doFalse
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	return false
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // UDPSize returns if UDP buffer size advertised in the requests OPT record.
 | 
					
						
							|  |  |  | // Or when the request was over TCP, we return the maximum allowed size of 64K.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Size() int {
 | 
					
						
							|  |  |  | 	if s.size != 0 {
 | 
					
						
							|  |  |  | 		return s.size
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	if s.Proto() == "tcp" {
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 		s.size = dns.MaxMsgSize
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 		return dns.MaxMsgSize
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if o := s.Req.IsEdns0(); o != nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 		if o.Do() == true {
 | 
					
						
							|  |  |  | 			s.do = doTrue
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			s.do = doFalse
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		size := o.UDPSize()
 | 
					
						
							|  |  |  | 		if size < dns.MinMsgSize {
 | 
					
						
							|  |  |  | 			size = dns.MinMsgSize
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		s.size = int(size)
 | 
					
						
							|  |  |  | 		return int(size)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	s.size = dns.MinMsgSize
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	return dns.MinMsgSize
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | // SizeAndDo returns a ready made OPT record that the reflects the intent from
 | 
					
						
							|  |  |  | // state. This can be added to upstream requests that will then hopefully
 | 
					
						
							|  |  |  | // return a message that is fits the buffer in the client.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) SizeAndDo() *dns.OPT {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 17:23:06 +00:00
										 |  |  | 	size := s.Size()
 | 
					
						
							|  |  |  | 	Do := s.Do()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	o := new(dns.OPT)
 | 
					
						
							|  |  |  | 	o.Hdr.Name = "."
 | 
					
						
							|  |  |  | 	o.Hdr.Rrtype = dns.TypeOPT
 | 
					
						
							|  |  |  | 	o.SetUDPSize(uint16(size))
 | 
					
						
							|  |  |  | 	if Do {
 | 
					
						
							|  |  |  | 		o.SetDo()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return o
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 09:10:07 +00:00
										 |  |  | // Result is the result of Scrub.
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | type Result int
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	// ScrubIgnored is returned when Scrub did nothing to the message.
 | 
					
						
							|  |  |  | 	ScrubIgnored Result = iota
 | 
					
						
							|  |  |  | 	// ScrubDone is returned when the reply has been scrubbed.
 | 
					
						
							|  |  |  | 	ScrubDone
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Scrub scrubs the reply message so that it will fit the client's buffer. If even after dropping
 | 
					
						
							|  |  |  | // the additional section, it still does not fit the TC bit will be set on the message. Note,
 | 
					
						
							|  |  |  | // the TC bit will be set regardless of protocol, even TCP message will get the bit, the client
 | 
					
						
							|  |  |  | // should then retry with pigeons.
 | 
					
						
							|  |  |  | // TODO(referral).
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Scrub(reply *dns.Msg) (*dns.Msg, Result) {
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | 	size := s.Size()
 | 
					
						
							|  |  |  | 	l := reply.Len()
 | 
					
						
							|  |  |  | 	if size >= l {
 | 
					
						
							|  |  |  | 		return reply, ScrubIgnored
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	// If not delegation, drop additional section.
 | 
					
						
							|  |  |  | 	// TODO(miek): check for delegation
 | 
					
						
							|  |  |  | 	reply.Extra = nil
 | 
					
						
							|  |  |  | 	l = reply.Len()
 | 
					
						
							|  |  |  | 	if size >= l {
 | 
					
						
							|  |  |  | 		return reply, ScrubDone
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	// Still?!! does not fit.
 | 
					
						
							|  |  |  | 	reply.Truncated = true
 | 
					
						
							|  |  |  | 	return reply, ScrubDone
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 17:23:06 +00:00
										 |  |  | // Type returns the type of the question as a string.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Type() string { return dns.Type(s.Req.Question[0].Qtype).String() }
 | 
					
						
							| 
									
										
										
										
											2016-03-25 17:23:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 09:10:07 +00:00
										 |  |  | // QType returns the type of the question as an uint16.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) QType() uint16 { return s.Req.Question[0].Qtype }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Name returns the name of the question in the request. Note
 | 
					
						
							|  |  |  | // this name will always have a closing dot and will be lower cased.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Name() string { return strings.ToLower(dns.Name(s.Req.Question[0].Name).String()) }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // QName returns the name of the question in the request.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) QName() string { return dns.Name(s.Req.Question[0].Name).String() }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Class returns the class of the question in the request.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) Class() string { return dns.Class(s.Req.Question[0].Qclass).String() }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // QClass returns the class of the question in the request.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) QClass() uint16 { return s.Req.Question[0].Qclass }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // ErrorMessage returns an error message suitable for sending
 | 
					
						
							|  |  |  | // back to the client.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (s *State) ErrorMessage(rcode int) *dns.Msg {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	m.SetRcode(s.Req, rcode)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	return m
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	doTrue  = 1
 | 
					
						
							|  |  |  | 	doFalse = 2
 | 
					
						
							|  |  |  | )
 |