| 
									
										
										
										
											2018-02-23 15:02:05 +00:00
										 |  |  | // Package request abstracts a client's request so that all plugins will handle them in an unified way.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | package request
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-06-01 15:12:49 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/edns"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Request contains some connection state and is useful in plugin.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | type Request struct {
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	Req *dns.Msg
 | 
					
						
							|  |  |  | 	W   dns.ResponseWriter
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-08 04:23:16 -07:00
										 |  |  | 	// Optional lowercased zone of this query.
 | 
					
						
							|  |  |  | 	Zone string
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 	Context context.Context
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	// Cache size after first call to Size or Do.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	size int
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | 	do   *bool // nil: nothing, otherwise *do value
 | 
					
						
							| 
									
										
										
										
											2016-10-02 17:23:25 +01:00
										 |  |  | 	// TODO(miek): opt record itself as well?
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 	// Caches
 | 
					
						
							|  |  |  | 	name      string // lowercase qname.
 | 
					
						
							|  |  |  | 	ip        string // client's ip.
 | 
					
						
							|  |  |  | 	port      string // client's port.
 | 
					
						
							|  |  |  | 	family    int    // transport's family.
 | 
					
						
							|  |  |  | 	localPort string // server's port.
 | 
					
						
							| 
									
										
										
										
											2018-07-03 09:47:26 +01:00
										 |  |  | 	localIP   string // server's ip.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | // NewWithQuestion returns a new request based on the old, but with a new question
 | 
					
						
							|  |  |  | // section in the request.
 | 
					
						
							|  |  |  | func (r *Request) NewWithQuestion(name string, typ uint16) Request {
 | 
					
						
							|  |  |  | 	req1 := Request{W: r.W, Req: r.Req.Copy()}
 | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:32 -04:00
										 |  |  | 	req1.Req.Question[0] = dns.Question{Name: dns.Fqdn(name), Qclass: dns.ClassINET, Qtype: typ}
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	return req1
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // IP gets the (remote) IP address of the client making the request.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (r *Request) IP() string {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 	if r.ip != "" {
 | 
					
						
							|  |  |  | 		return r.ip
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	ip, _, err := net.SplitHostPort(r.W.RemoteAddr().String())
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 		r.ip = r.W.RemoteAddr().String()
 | 
					
						
							|  |  |  | 		return r.ip
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	r.ip = ip
 | 
					
						
							|  |  |  | 	return r.ip
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // LocalIP gets the (local) IP address of server handling the request.
 | 
					
						
							|  |  |  | func (r *Request) LocalIP() string {
 | 
					
						
							| 
									
										
										
										
											2018-07-03 09:47:26 +01:00
										 |  |  | 	if r.localIP != "" {
 | 
					
						
							|  |  |  | 		return r.localIP
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	ip, _, err := net.SplitHostPort(r.W.LocalAddr().String())
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-07-03 09:47:26 +01:00
										 |  |  | 		r.localIP = r.W.LocalAddr().String()
 | 
					
						
							|  |  |  | 		return r.localIP
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-07-03 09:47:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	r.localIP = ip
 | 
					
						
							|  |  |  | 	return r.localIP
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 17:38:31 +01:00
										 |  |  | // Port gets the (remote) port of the client making the request.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (r *Request) Port() string {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 	if r.port != "" {
 | 
					
						
							|  |  |  | 		return r.port
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	_, port, err := net.SplitHostPort(r.W.RemoteAddr().String())
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 		r.port = "0"
 | 
					
						
							|  |  |  | 		return r.port
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	r.port = port
 | 
					
						
							|  |  |  | 	return r.port
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 17:38:31 +01:00
										 |  |  | // LocalPort gets the local port of the server handling the request.
 | 
					
						
							|  |  |  | func (r *Request) LocalPort() string {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 	if r.localPort != "" {
 | 
					
						
							|  |  |  | 		return r.localPort
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 17:38:31 +01:00
										 |  |  | 	_, port, err := net.SplitHostPort(r.W.LocalAddr().String())
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 		r.localPort = "0"
 | 
					
						
							|  |  |  | 		return r.localPort
 | 
					
						
							| 
									
										
										
										
											2018-06-29 17:38:31 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	r.localPort = port
 | 
					
						
							|  |  |  | 	return r.localPort
 | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 17:38:31 +01:00
										 |  |  | // RemoteAddr returns the net.Addr of the client that sent the current request.
 | 
					
						
							|  |  |  | func (r *Request) RemoteAddr() string { return r.W.RemoteAddr().String() }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // LocalAddr returns the net.Addr of the server handling the current request.
 | 
					
						
							|  |  |  | func (r *Request) LocalAddr() string { return r.W.LocalAddr().String() }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | // Proto gets the protocol used as the transport. This will be udp or tcp.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (r *Request) Proto() string { return Proto(r.W) }
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Proto gets the protocol used as the transport. This will be udp or tcp.
 | 
					
						
							|  |  |  | func Proto(w dns.ResponseWriter) string {
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | 	// FIXME(miek): why not a method on Request
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	if _, ok := w.RemoteAddr().(*net.UDPAddr); ok {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		return "udp"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	if _, ok := w.RemoteAddr().(*net.TCPAddr); ok {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		return "tcp"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return "udp"
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | // Family returns the family of the transport, 1 for IPv4 and 2 for IPv6.
 | 
					
						
							|  |  |  | func (r *Request) Family() int {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 	if r.family != 0 {
 | 
					
						
							|  |  |  | 		return r.family
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	var a net.IP
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	ip := r.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 {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 		r.family = 1
 | 
					
						
							|  |  |  | 		return r.family
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 	r.family = 2
 | 
					
						
							|  |  |  | 	return r.family
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | // Do returns if the request has the DO (DNSSEC OK) bit set.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (r *Request) Do() bool {
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | 	if r.do != nil {
 | 
					
						
							|  |  |  | 		return *r.do
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | 	r.do = new(bool)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	if o := r.Req.IsEdns0(); o != nil {
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | 		*r.do = o.Do()
 | 
					
						
							|  |  |  | 		return *r.do
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | 	*r.do = false
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	return false
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-29 11:02:43 +00:00
										 |  |  | // Len returns the length in bytes in the request.
 | 
					
						
							|  |  |  | func (r *Request) Len() int { return r.Req.Len() }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Size returns if buffer size *advertised* in the requests OPT record.
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | // Or when the request was over TCP, we return the maximum allowed size of 64K.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (r *Request) Size() int {
 | 
					
						
							|  |  |  | 	if r.size != 0 {
 | 
					
						
							|  |  |  | 		return r.size
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	size := 0
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	if o := r.Req.IsEdns0(); o != nil {
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | 		if r.do == nil {
 | 
					
						
							|  |  |  | 			r.do = new(bool)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | 		*r.do = o.Do()
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 		size = int(o.UDPSize())
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	size = edns.Size(r.Proto(), size)
 | 
					
						
							|  |  |  | 	r.size = size
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	return size
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | // SizeAndDo adds an OPT record that the reflects the intent from request.
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | // The returned bool indicated if an record was found and normalised.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (r *Request) SizeAndDo(m *dns.Msg) bool {
 | 
					
						
							|  |  |  | 	o := r.Req.IsEdns0() // TODO(miek): speed this up
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	if o == nil {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-02 17:23:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	odo := o.Do()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 	if mo := m.IsEdns0(); mo != nil {
 | 
					
						
							|  |  |  | 		mo.Hdr.Name = "."
 | 
					
						
							|  |  |  | 		mo.Hdr.Rrtype = dns.TypeOPT
 | 
					
						
							|  |  |  | 		mo.SetVersion(0)
 | 
					
						
							|  |  |  | 		mo.SetUDPSize(o.UDPSize())
 | 
					
						
							| 
									
										
										
										
											2016-10-02 17:23:25 +01:00
										 |  |  | 		mo.Hdr.Ttl &= 0xff00 // clear flags
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if odo {
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 			mo.SetDo()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return true
 | 
					
						
							| 
									
										
										
										
											2016-03-25 17:23:06 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-02 17:23:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	o.Hdr.Name = "."
 | 
					
						
							|  |  |  | 	o.Hdr.Rrtype = dns.TypeOPT
 | 
					
						
							|  |  |  | 	o.SetVersion(0)
 | 
					
						
							|  |  |  | 	o.Hdr.Ttl &= 0xff00 // clear flags
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if odo {
 | 
					
						
							|  |  |  | 		o.SetDo()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	m.Extra = append(m.Extra, o)
 | 
					
						
							|  |  |  | 	return true
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 12:26:22 +01:00
										 |  |  | // Scrub is a noop function, added for backwards compatibility reasons. The original Scrub is now called
 | 
					
						
							|  |  |  | // automatically by the server on writing the reply. See ScrubWriter.
 | 
					
						
							|  |  |  | func (r *Request) Scrub(reply *dns.Msg) (*dns.Msg, int) { return reply, 0 }
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 12:26:22 +01:00
										 |  |  | // scrub scrubs the reply message so that it will fit the client's buffer. It will first
 | 
					
						
							| 
									
										
										
										
											2018-05-01 21:04:06 +01:00
										 |  |  | // check if the reply fits without compression and then *with* compression.
 | 
					
						
							|  |  |  | // Scrub will then use binary search to find a save cut off point in the additional section.
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | // If even *without* the additional section the reply still doesn't fit we
 | 
					
						
							|  |  |  | // repeat this process for the answer section. If we scrub the answer section
 | 
					
						
							|  |  |  | // we set the TC bit on the reply; indicating the client should retry over TCP.
 | 
					
						
							|  |  |  | // Note, the TC bit will be set regardless of protocol, even TCP message will
 | 
					
						
							|  |  |  | // get the bit, the client should then retry with pigeons.
 | 
					
						
							| 
									
										
										
										
											2018-08-29 12:26:22 +01:00
										 |  |  | func (r *Request) scrub(reply *dns.Msg) *dns.Msg {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	size := r.Size()
 | 
					
						
							| 
									
										
										
										
											2018-05-01 21:04:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	reply.Compress = false
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	rl := reply.Len()
 | 
					
						
							| 
									
										
										
										
											2018-05-01 21:04:06 +01:00
										 |  |  | 	if size >= rl {
 | 
					
						
							| 
									
										
										
										
											2018-08-29 12:26:22 +01:00
										 |  |  | 		return reply
 | 
					
						
							| 
									
										
										
										
											2018-05-01 21:04:06 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 21:04:06 +01:00
										 |  |  | 	reply.Compress = true
 | 
					
						
							|  |  |  | 	rl = reply.Len()
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	if size >= rl {
 | 
					
						
							| 
									
										
										
										
											2018-08-29 12:26:22 +01:00
										 |  |  | 		return reply
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 11:50:55 +00:00
										 |  |  | 	// Account for the OPT record that gets added in SizeAndDo(), subtract that length.
 | 
					
						
							|  |  |  | 	sub := 0
 | 
					
						
							| 
									
										
										
										
											2018-07-28 10:27:38 +01:00
										 |  |  | 	if r.Req.IsEdns0() != nil {
 | 
					
						
							| 
									
										
										
										
											2018-03-24 11:50:55 +00:00
										 |  |  | 		sub = optLen
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-07-28 10:27:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:55:55 +02:00
										 |  |  | 	// subtract to make spaces for re-added EDNS0 OPT RR.
 | 
					
						
							| 
									
										
										
										
											2018-03-24 11:50:55 +00:00
										 |  |  | 	re := len(reply.Extra) - sub
 | 
					
						
							| 
									
										
										
										
											2018-07-28 10:27:38 +01:00
										 |  |  | 	size -= sub
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	l, m := 0, 0
 | 
					
						
							| 
									
										
										
										
											2018-07-28 10:27:38 +01:00
										 |  |  | 	origExtra := reply.Extra
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	for l < re {
 | 
					
						
							|  |  |  | 		m = (l + re) / 2
 | 
					
						
							|  |  |  | 		reply.Extra = origExtra[:m]
 | 
					
						
							|  |  |  | 		rl = reply.Len()
 | 
					
						
							|  |  |  | 		if rl < size {
 | 
					
						
							|  |  |  | 			l = m + 1
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if rl > size {
 | 
					
						
							|  |  |  | 			re = m - 1
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-03-23 17:59:06 +01:00
										 |  |  | 		if rl == size {
 | 
					
						
							|  |  |  | 			break
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-03-24 11:50:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// We may come out of this loop with one rotation too many, m makes it too large, but m-1 works.
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	if rl > size && m > 0 {
 | 
					
						
							|  |  |  | 		reply.Extra = origExtra[:m-1]
 | 
					
						
							|  |  |  | 		rl = reply.Len()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if rl < size {
 | 
					
						
							|  |  |  | 		r.SizeAndDo(reply)
 | 
					
						
							| 
									
										
										
										
											2018-08-29 12:26:22 +01:00
										 |  |  | 		return reply
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-01-24 14:28:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	ra := len(reply.Answer)
 | 
					
						
							|  |  |  | 	l, m = 0, 0
 | 
					
						
							| 
									
										
										
										
											2018-07-28 10:27:38 +01:00
										 |  |  | 	origAnswer := reply.Answer
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	for l < ra {
 | 
					
						
							|  |  |  | 		m = (l + ra) / 2
 | 
					
						
							|  |  |  | 		reply.Answer = origAnswer[:m]
 | 
					
						
							|  |  |  | 		rl = reply.Len()
 | 
					
						
							|  |  |  | 		if rl < size {
 | 
					
						
							|  |  |  | 			l = m + 1
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if rl > size {
 | 
					
						
							|  |  |  | 			ra = m - 1
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-03-23 17:59:06 +01:00
										 |  |  | 		if rl == size {
 | 
					
						
							|  |  |  | 			break
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-03-24 11:50:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// We may come out of this loop with one rotation too many, m makes it too large, but m-1 works.
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:13:41 +00:00
										 |  |  | 	if rl > size && m > 0 {
 | 
					
						
							|  |  |  | 		reply.Answer = origAnswer[:m-1]
 | 
					
						
							|  |  |  | 		// No need to recalc length, as we don't use it. We set truncated anyway. Doing
 | 
					
						
							|  |  |  | 		// this extra m-1 step does make it fit in the client's buffer however.
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 10:27:38 +01:00
										 |  |  | 	r.SizeAndDo(reply)
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | 	reply.Truncated = true
 | 
					
						
							| 
									
										
										
										
											2018-08-29 12:26:22 +01:00
										 |  |  | 	return reply
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 10:27:38 +01:00
										 |  |  | // Type returns the type of the question as a string. If the request is malformed the empty string is returned.
 | 
					
						
							| 
									
										
										
										
											2017-08-31 16:24:11 +02:00
										 |  |  | func (r *Request) Type() string {
 | 
					
						
							|  |  |  | 	if r.Req == nil {
 | 
					
						
							|  |  |  | 		return ""
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(r.Req.Question) == 0 {
 | 
					
						
							|  |  |  | 		return ""
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-25 17:23:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-31 16:24:11 +02:00
										 |  |  | 	return dns.Type(r.Req.Question[0].Qtype).String()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // QType returns the type of the question as an uint16. If the request is malformed
 | 
					
						
							|  |  |  | // 0 is returned.
 | 
					
						
							|  |  |  | func (r *Request) QType() uint16 {
 | 
					
						
							|  |  |  | 	if r.Req == nil {
 | 
					
						
							|  |  |  | 		return 0
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(r.Req.Question) == 0 {
 | 
					
						
							|  |  |  | 		return 0
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return r.Req.Question[0].Qtype
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Name returns the name of the question in the request. Note
 | 
					
						
							| 
									
										
											  
											
												Allow debug queries to etcd middleware (#150)
With this you can retreive the raw data that the etcd middleware
used to create the reply. The debug data is put in TXT records
that are stuffed in the CH classs. This is only enabled if you
specify `debug` in the etcd stanza.
You can retrieve it by prefixing your query with 'o-o.debug.'
For instance:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @localhost -p 1053 SRV o-o.debug.production.*.skydns.local
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47798
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;o-o.debug.production.*.skydns.local. IN	SRV
;; ANSWER SECTION:
production.*.skydns.local. 154	IN	SRV	10 50 8080 service1.example.com.
production.*.skydns.local. 154	IN	SRV	10 50 8080 service2.example.com.
;; ADDITIONAL SECTION:
skydns.local.skydns.east.production.rails.1. 154 CH TXT	"service1.example.com:8080(10,0,,false)[0,]"
skydns.local.skydns.west.production.rails.2. 154 CH TXT	"service2.example.com:8080(10,0,,false)[0,]"
											
										 
											2016-05-22 21:16:26 +01:00
										 |  |  | // this name will always have a closing dot and will be lower cased. After a call Name
 | 
					
						
							|  |  |  | // the value will be cached. To clear this caching call Clear.
 | 
					
						
							| 
									
										
										
										
											2017-08-31 16:24:11 +02:00
										 |  |  | // If the request is malformed the root zone is returned.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (r *Request) Name() string {
 | 
					
						
							|  |  |  | 	if r.name != "" {
 | 
					
						
							|  |  |  | 		return r.name
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-08-31 16:24:11 +02:00
										 |  |  | 	if r.Req == nil {
 | 
					
						
							|  |  |  | 		r.name = "."
 | 
					
						
							|  |  |  | 		return "."
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(r.Req.Question) == 0 {
 | 
					
						
							|  |  |  | 		r.name = "."
 | 
					
						
							|  |  |  | 		return "."
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	r.name = strings.ToLower(dns.Name(r.Req.Question[0].Name).String())
 | 
					
						
							|  |  |  | 	return r.name
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // QName returns the name of the question in the request.
 | 
					
						
							| 
									
										
										
										
											2017-08-31 16:24:11 +02:00
										 |  |  | // If the request is malformed the root zone is returned.
 | 
					
						
							|  |  |  | func (r *Request) QName() string {
 | 
					
						
							|  |  |  | 	if r.Req == nil {
 | 
					
						
							|  |  |  | 		return "."
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(r.Req.Question) == 0 {
 | 
					
						
							|  |  |  | 		return "."
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return dns.Name(r.Req.Question[0].Name).String()
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Class returns the class of the question in the request.
 | 
					
						
							| 
									
										
										
										
											2017-08-31 16:24:11 +02:00
										 |  |  | // If the request is malformed the empty string is returned.
 | 
					
						
							|  |  |  | func (r *Request) Class() string {
 | 
					
						
							|  |  |  | 	if r.Req == nil {
 | 
					
						
							|  |  |  | 		return ""
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(r.Req.Question) == 0 {
 | 
					
						
							|  |  |  | 		return ""
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return dns.Class(r.Req.Question[0].Qclass).String()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // QClass returns the class of the question in the request.
 | 
					
						
							| 
									
										
										
										
											2017-08-31 16:24:11 +02:00
										 |  |  | // If the request is malformed 0 returned.
 | 
					
						
							|  |  |  | func (r *Request) QClass() uint16 {
 | 
					
						
							|  |  |  | 	if r.Req == nil {
 | 
					
						
							|  |  |  | 		return 0
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(r.Req.Question) == 0 {
 | 
					
						
							|  |  |  | 		return 0
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return r.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-09-07 11:10:16 +01:00
										 |  |  | func (r *Request) ErrorMessage(rcode int) *dns.Msg {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	m.SetRcode(r.Req, rcode)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	return m
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | // Clear clears all caching from Request s.
 | 
					
						
							|  |  |  | func (r *Request) Clear() {
 | 
					
						
							|  |  |  | 	r.name = ""
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 	r.ip = ""
 | 
					
						
							| 
									
										
										
										
											2018-07-03 09:47:26 +01:00
										 |  |  | 	r.localIP = ""
 | 
					
						
							| 
									
										
										
										
											2018-07-01 16:34:52 +01:00
										 |  |  | 	r.port = ""
 | 
					
						
							|  |  |  | 	r.localPort = ""
 | 
					
						
							|  |  |  | 	r.family = 0
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-15 08:42:49 +00:00
										 |  |  | // Match checks if the reply matches the qname and qtype from the request, it returns
 | 
					
						
							|  |  |  | // false when they don't match.
 | 
					
						
							|  |  |  | func (r *Request) Match(reply *dns.Msg) bool {
 | 
					
						
							|  |  |  | 	if len(reply.Question) != 1 {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:35:42 +01:00
										 |  |  | 	if reply.Response == false {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-15 08:42:49 +00:00
										 |  |  | 	if strings.ToLower(reply.Question[0].Name) != r.Name() {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if reply.Question[0].Qtype != r.QType() {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 17:22:24 +01:00
										 |  |  | const optLen = 12 // OPT record length.
 |