mirror of
https://github.com/coredns/coredns.git
synced 2025-10-28 16:54:15 -04:00
request.Request: cache a few more value (#1921)
Cache IP's and ports as well. Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
@@ -26,8 +26,13 @@ type Request struct {
|
|||||||
do *bool // nil: nothing, otherwise *do value
|
do *bool // nil: nothing, otherwise *do value
|
||||||
// TODO(miek): opt record itself as well?
|
// TODO(miek): opt record itself as well?
|
||||||
|
|
||||||
// Cache lowercase qname.
|
// Caches
|
||||||
name string
|
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
|
// 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.
|
// IP gets the (remote) IP address of the client making the request.
|
||||||
func (r *Request) IP() string {
|
func (r *Request) IP() string {
|
||||||
|
if r.ip != "" {
|
||||||
|
return r.ip
|
||||||
|
}
|
||||||
|
|
||||||
ip, _, err := net.SplitHostPort(r.W.RemoteAddr().String())
|
ip, _, err := net.SplitHostPort(r.W.RemoteAddr().String())
|
||||||
if err != nil {
|
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.
|
// Port gets the (remote) port of the client making the request.
|
||||||
func (r *Request) Port() string {
|
func (r *Request) Port() string {
|
||||||
|
if r.port != "" {
|
||||||
|
return r.port
|
||||||
|
}
|
||||||
|
|
||||||
_, port, err := net.SplitHostPort(r.W.RemoteAddr().String())
|
_, port, err := net.SplitHostPort(r.W.RemoteAddr().String())
|
||||||
if err != nil {
|
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.
|
// LocalPort gets the local port of the server handling the request.
|
||||||
func (r *Request) LocalPort() string {
|
func (r *Request) LocalPort() string {
|
||||||
|
if r.localPort != "" {
|
||||||
|
return r.localPort
|
||||||
|
}
|
||||||
|
|
||||||
_, port, err := net.SplitHostPort(r.W.LocalAddr().String())
|
_, port, err := net.SplitHostPort(r.W.LocalAddr().String())
|
||||||
if err != nil {
|
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.
|
// 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.
|
// Family returns the family of the transport, 1 for IPv4 and 2 for IPv6.
|
||||||
func (r *Request) Family() int {
|
func (r *Request) Family() int {
|
||||||
|
if r.family != 0 {
|
||||||
|
return r.family
|
||||||
|
}
|
||||||
|
|
||||||
var a net.IP
|
var a net.IP
|
||||||
ip := r.W.RemoteAddr()
|
ip := r.W.RemoteAddr()
|
||||||
if i, ok := ip.(*net.UDPAddr); ok {
|
if i, ok := ip.(*net.UDPAddr); ok {
|
||||||
@@ -98,9 +128,11 @@ func (r *Request) Family() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if a.To4() != nil {
|
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.
|
// 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.
|
// Clear clears all caching from Request s.
|
||||||
func (r *Request) Clear() {
|
func (r *Request) Clear() {
|
||||||
r.name = ""
|
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
|
// Match checks if the reply matches the qname and qtype from the request, it returns
|
||||||
|
|||||||
Reference in New Issue
Block a user