diff options
Diffstat (limited to 'middleware/state.go')
-rw-r--r-- | middleware/state.go | 85 |
1 files changed, 57 insertions, 28 deletions
diff --git a/middleware/state.go b/middleware/state.go index 7ae89ef1c..406fa4ac3 100644 --- a/middleware/state.go +++ b/middleware/state.go @@ -2,36 +2,38 @@ package middleware import ( "net" - "net/http" "strings" "time" "github.com/miekg/dns" ) -// This file contains the state nd functions available for use in the templates. +// This file contains the state functions available for use in the middlewares. // State contains some connection state and is useful in middleware. type State struct { - Root http.FileSystem // TODO(miek): needed? - Req *dns.Msg - W dns.ResponseWriter + 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 } // Now returns the current timestamp in the specified format. -func (s State) Now(format string) string { return time.Now().Format(format) } +func (s *State) Now(format string) string { return time.Now().Format(format) } // NowDate returns the current date/time that can be used in other time functions. -func (s State) NowDate() time.Time { return time.Now() } +func (s *State) NowDate() time.Time { return time.Now() } // Header gets the heaser of the request in State. -func (s State) Header() *dns.RR_Header { +func (s *State) Header() *dns.RR_Header { // TODO(miek) return nil } // IP gets the (remote) IP address of the client making the request. -func (s State) IP() string { +func (s *State) IP() string { ip, _, err := net.SplitHostPort(s.W.RemoteAddr().String()) if err != nil { return s.W.RemoteAddr().String() @@ -40,7 +42,7 @@ func (s State) IP() string { } // Post gets the (remote) Port of the client making the request. -func (s State) Port() (string, error) { +func (s *State) Port() (string, error) { _, port, err := net.SplitHostPort(s.W.RemoteAddr().String()) if err != nil { return "0", err @@ -50,7 +52,7 @@ func (s State) Port() (string, error) { // Proto gets the protocol used as the transport. This // will be udp or tcp. -func (s State) Proto() string { +func (s *State) Proto() string { if _, ok := s.W.RemoteAddr().(*net.UDPAddr); ok { return "udp" } @@ -62,7 +64,7 @@ func (s State) Proto() string { // Family returns the family of the transport. // 1 for IPv4 and 2 for IPv6. -func (s State) Family() int { +func (s *State) Family() int { var a net.IP ip := s.W.RemoteAddr() if i, ok := ip.(*net.UDPAddr); ok { @@ -79,33 +81,56 @@ func (s State) Family() int { } // Do returns if the request has the DO (DNSSEC OK) bit set. -func (s State) Do() bool { +func (s *State) Do() bool { + if s.do != 0 { + return s.do == doTrue + } + if o := s.Req.IsEdns0(); o != nil { + if o.Do() { + s.do = doTrue + } else { + s.do = doFalse + } return o.Do() } + s.do = doFalse 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. -func (s State) Size() int { +func (s *State) Size() int { + if s.size != 0 { + return s.size + } + if s.Proto() == "tcp" { + s.size = dns.MaxMsgSize return dns.MaxMsgSize } if o := s.Req.IsEdns0(); o != nil { - s := o.UDPSize() - if s < dns.MinMsgSize { - s = dns.MinMsgSize + if o.Do() == true { + s.do = doTrue + } else { + s.do = doFalse } - return int(s) + + size := o.UDPSize() + if size < dns.MinMsgSize { + size = dns.MinMsgSize + } + s.size = int(size) + return int(size) } + s.size = dns.MinMsgSize return dns.MinMsgSize } // 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. -func (s State) SizeAndDo() *dns.OPT { +func (s *State) SizeAndDo() *dns.OPT { size := s.Size() Do := s.Do() @@ -134,7 +159,7 @@ const ( // 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). -func (s State) Scrub(reply *dns.Msg) (*dns.Msg, Result) { +func (s *State) Scrub(reply *dns.Msg) (*dns.Msg, Result) { size := s.Size() l := reply.Len() if size >= l { @@ -150,32 +175,36 @@ func (s State) Scrub(reply *dns.Msg) (*dns.Msg, Result) { // Still?!! does not fit. reply.Truncated = true return reply, ScrubDone - } // Type returns the type of the question as a string. -func (s State) Type() string { return dns.Type(s.Req.Question[0].Qtype).String() } +func (s *State) Type() string { return dns.Type(s.Req.Question[0].Qtype).String() } // QType returns the type of the question as a uint16. -func (s State) QType() uint16 { return s.Req.Question[0].Qtype } +func (s *State) QType() uint16 { return s.Req.Question[0].Qtype } // Name returns the name of the question in the request. Note // this name will always have a closing dot and will be lower cased. -func (s State) Name() string { return strings.ToLower(dns.Name(s.Req.Question[0].Name).String()) } +func (s *State) Name() string { return strings.ToLower(dns.Name(s.Req.Question[0].Name).String()) } // QName returns the name of the question in the request. -func (s State) QName() string { return dns.Name(s.Req.Question[0].Name).String() } +func (s *State) QName() string { return dns.Name(s.Req.Question[0].Name).String() } // Class returns the class of the question in the request. -func (s State) Class() string { return dns.Class(s.Req.Question[0].Qclass).String() } +func (s *State) Class() string { return dns.Class(s.Req.Question[0].Qclass).String() } // QClass returns the class of the question in the request. -func (s State) QClass() uint16 { return s.Req.Question[0].Qclass } +func (s *State) QClass() uint16 { return s.Req.Question[0].Qclass } // ErrorMessage returns an error message suitable for sending // back to the client. -func (s State) ErrorMessage(rcode int) *dns.Msg { +func (s *State) ErrorMessage(rcode int) *dns.Msg { m := new(dns.Msg) m.SetRcode(s.Req, rcode) return m } + +const ( + doTrue = 1 + doFalse = 2 +) |