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 (
|
|
|
|
"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
|
|
|
|
|
2019-10-04 09:44:58 +01:00
|
|
|
// Cache size after first call to Size or Do. If size is zero nothing has been cached yet.
|
|
|
|
// Both Size and Do set these values (and cache them).
|
|
|
|
size uint16 // UDP buffer size, or 64K in case of TCP.
|
|
|
|
do bool // DNSSEC OK value
|
2016-04-26 17:57:11 +01:00
|
|
|
|
2018-07-01 16:34:52 +01:00
|
|
|
// Caches
|
2019-10-04 09:44:58 +01:00
|
|
|
family int8 // transport's family.
|
2018-07-01 16:34:52 +01:00
|
|
|
name string // lowercase qname.
|
|
|
|
ip string // client's ip.
|
|
|
|
port string // client's port.
|
|
|
|
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.
|
2019-12-06 19:21:34 +08:00
|
|
|
func (r *Request) Proto() string {
|
|
|
|
if _, ok := r.W.RemoteAddr().(*net.UDPAddr); ok {
|
2016-03-18 20:57:35 +00:00
|
|
|
return "udp"
|
|
|
|
}
|
2019-12-06 19:21:34 +08:00
|
|
|
if _, ok := r.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 {
|
2019-10-04 09:44:58 +01:00
|
|
|
return int(r.family)
|
2018-07-01 16:34:52 +01:00
|
|
|
}
|
|
|
|
|
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
|
2019-10-04 09:44:58 +01:00
|
|
|
return 1
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
2018-07-01 16:34:52 +01:00
|
|
|
r.family = 2
|
2019-10-04 09:44:58 +01:00
|
|
|
return 2
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 16:28:43 +02:00
|
|
|
// Do returns true if the request has the DO (DNSSEC OK) bit set.
|
2016-09-07 11:10:16 +01:00
|
|
|
func (r *Request) Do() bool {
|
2019-10-04 09:44:58 +01:00
|
|
|
if r.size != 0 {
|
|
|
|
return r.do
|
2016-04-04 08:19:06 +01:00
|
|
|
}
|
|
|
|
|
2019-10-04 09:44:58 +01:00
|
|
|
r.Size()
|
|
|
|
return r.do
|
2016-03-20 21:36:55 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-10-04 09:44:58 +01:00
|
|
|
return int(r.size)
|
2016-04-04 08:19:06 +01:00
|
|
|
}
|
|
|
|
|
2019-10-04 09:44:58 +01:00
|
|
|
size := uint16(0)
|
2016-09-07 11:10:16 +01:00
|
|
|
if o := r.Req.IsEdns0(); o != nil {
|
2019-10-04 09:44:58 +01:00
|
|
|
r.do = o.Do()
|
|
|
|
size = o.UDPSize()
|
2016-03-20 21:36:55 +00:00
|
|
|
}
|
2018-03-31 17:22:24 +01:00
|
|
|
|
2019-10-04 09:44:58 +01:00
|
|
|
// normalize size
|
2016-09-07 11:10:16 +01:00
|
|
|
size = edns.Size(r.Proto(), size)
|
|
|
|
r.size = size
|
2019-10-04 09:44:58 +01:00
|
|
|
return int(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.
|
2018-12-10 19:43:46 +00:00
|
|
|
// The returned bool indicates if an record was found and normalised.
|
2016-09-07 11:10:16 +01:00
|
|
|
func (r *Request) SizeAndDo(m *dns.Msg) bool {
|
2018-12-06 21:18:11 +00:00
|
|
|
o := r.Req.IsEdns0()
|
2016-04-09 16:17:53 +01:00
|
|
|
if o == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-10-02 17:23:25 +01:00
|
|
|
|
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
|
|
|
|
|
2018-12-10 19:43:46 +00:00
|
|
|
// Assume if the message m has options set, they are OK and represent what an upstream can do.
|
2018-12-06 21:18:11 +00:00
|
|
|
|
2018-12-10 19:43:46 +00:00
|
|
|
if o.Do() {
|
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
|
|
|
|
2018-12-10 19:43:46 +00:00
|
|
|
// Reuse the request's OPT record and tack it to m.
|
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
|
|
|
|
|
2018-12-06 21:18:11 +00:00
|
|
|
if len(o.Option) > 0 {
|
|
|
|
o.Option = supportedOptions(o.Option)
|
|
|
|
}
|
|
|
|
|
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-10-23 17:55:40 +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.
|
2018-02-21 21:13:41 +00:00
|
|
|
// 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-10-23 17:55:40 +01:00
|
|
|
func (r *Request) Scrub(reply *dns.Msg) *dns.Msg {
|
2019-07-11 12:54:47 +00:00
|
|
|
reply.Truncate(r.Size())
|
2018-05-01 21:04:06 +01:00
|
|
|
|
2019-07-11 12:54:47 +00:00
|
|
|
if reply.Compress {
|
|
|
|
return reply
|
|
|
|
}
|
2018-12-04 20:15:57 +00:00
|
|
|
|
2019-07-11 12:54:47 +00:00
|
|
|
if r.Proto() == "udp" {
|
|
|
|
rl := reply.Len()
|
2018-12-04 20:15:57 +00:00
|
|
|
// Last ditch attempt to avoid fragmentation, if the size is bigger than the v4/v6 UDP fragmentation
|
|
|
|
// limit and sent via UDP compress it (in the hope we go under that limit). Limits taken from NSD:
|
|
|
|
//
|
2019-08-27 21:01:17 +08:00
|
|
|
// .., 1480 (EDNS/IPv4), 1220 (EDNS/IPv6), or the advertised EDNS buffer size if that is
|
2018-12-04 20:15:57 +00:00
|
|
|
// smaller than the EDNS default.
|
|
|
|
// See: https://open.nlnetlabs.nl/pipermail/nsd-users/2011-November/001278.html
|
|
|
|
if rl > 1480 && r.Family() == 1 {
|
|
|
|
reply.Compress = true
|
|
|
|
}
|
|
|
|
if rl > 1220 && r.Family() == 2 {
|
|
|
|
reply.Compress = true
|
|
|
|
}
|
2018-02-21 21:13:41 +00:00
|
|
|
}
|
2018-03-24 11:50:55 +00:00
|
|
|
|
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
|
|
|
|
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
|
2021-02-17 11:42:37 -08:00
|
|
|
r.size = 0
|
|
|
|
r.do = false
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-27 18:10:34 +08:00
|
|
|
if !reply.Response {
|
2018-05-09 12:35:42 +01:00
|
|
|
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
|
|
|
|
}
|