aboutsummaryrefslogtreecommitdiff
path: root/request
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2018-07-01 16:34:52 +0100
committerGravatar Yong Tang <yong.tang.github@outlook.com> 2018-07-01 08:34:52 -0700
commit0b326e26865af91474703322d5d9b1eb1e45a8ff (patch)
treea9a0a9f9a95c0ff31d1cdf29ca143c6a881ef14b /request
parent37cdbff203011a03bc743fe54950441a19301560 (diff)
downloadcoredns-0b326e26865af91474703322d5d9b1eb1e45a8ff.tar.gz
coredns-0b326e26865af91474703322d5d9b1eb1e45a8ff.tar.zst
coredns-0b326e26865af91474703322d5d9b1eb1e45a8ff.zip
request.Request: cache a few more value (#1921)
Cache IP's and ports as well. Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'request')
-rw-r--r--request/request.go56
1 files changed, 46 insertions, 10 deletions
diff --git a/request/request.go b/request/request.go
index f5e035a6e..88a92405f 100644
--- a/request/request.go
+++ b/request/request.go
@@ -26,8 +26,13 @@ type Request struct {
do *bool // nil: nothing, otherwise *do value
// TODO(miek): opt record itself as well?
- // Cache lowercase qname.
- name string
+ // 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.
+ // TODO(miek): localIP once that is merged.
}
// NewWithQuestion returns a new request based on the old, but with a new question
@@ -40,29 +45,50 @@ func (r *Request) NewWithQuestion(name string, typ uint16) Request {
// IP gets the (remote) IP address of the client making the request.
func (r *Request) IP() string {
+ if r.ip != "" {
+ return r.ip
+ }
+
ip, _, err := net.SplitHostPort(r.W.RemoteAddr().String())
if err != nil {
- return r.W.RemoteAddr().String()
+ r.ip = r.W.RemoteAddr().String()
+ return r.ip
}
- return ip
+
+ r.ip = ip
+ return r.ip
}
// Port gets the (remote) port of the client making the request.
func (r *Request) Port() string {
+ if r.port != "" {
+ return r.port
+ }
+
_, port, err := net.SplitHostPort(r.W.RemoteAddr().String())
if err != nil {
- return "0"
+ r.port = "0"
+ return r.port
}
- return port
+
+ r.port = port
+ return r.port
}
// LocalPort gets the local port of the server handling the request.
func (r *Request) LocalPort() string {
+ if r.localPort != "" {
+ return r.localPort
+ }
+
_, port, err := net.SplitHostPort(r.W.LocalAddr().String())
if err != nil {
- return "0"
+ r.localPort = "0"
+ return r.localPort
}
- return port
+
+ r.localPort = port
+ return r.localPort
}
// RemoteAddr returns the net.Addr of the client that sent the current request.
@@ -88,6 +114,10 @@ func Proto(w dns.ResponseWriter) string {
// Family returns the family of the transport, 1 for IPv4 and 2 for IPv6.
func (r *Request) Family() int {
+ if r.family != 0 {
+ return r.family
+ }
+
var a net.IP
ip := r.W.RemoteAddr()
if i, ok := ip.(*net.UDPAddr); ok {
@@ -98,9 +128,11 @@ func (r *Request) Family() int {
}
if a.To4() != nil {
- return 1
+ r.family = 1
+ return r.family
}
- return 2
+ r.family = 2
+ return r.family
}
// Do returns if the request has the DO (DNSSEC OK) bit set.
@@ -381,6 +413,10 @@ func (r *Request) ErrorMessage(rcode int) *dns.Msg {
// Clear clears all caching from Request s.
func (r *Request) Clear() {
r.name = ""
+ r.ip = ""
+ r.port = ""
+ r.localPort = ""
+ r.family = 0
}
// Match checks if the reply matches the qname and qtype from the request, it returns