Skip to content

Commit

Permalink
set the truncated header when sending large responses via UDP and ena…
Browse files Browse the repository at this point in the history
…ble compression #205
  • Loading branch information
0xERR0R committed May 8, 2021
1 parent 2abcf42 commit 114171a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,31 @@ func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) {
} else {
response.Res.MsgHdr.RecursionAvailable = request.MsgHdr.RecursionDesired

// truncate if necessary
response.Res.Truncate(getMaxResponseSize(w.LocalAddr().Network(), request))

// enable compression
response.Res.Compress = true

err := w.WriteMsg(response.Res)
util.LogOnError("can't write message: ", err)
}
}

// returns EDNS upd size or if not present, 512 for UDP and 64K for TCP
func getMaxResponseSize(network string, request *dns.Msg) int {
edns := request.IsEdns0()
if edns != nil && edns.UDPSize() > 0 {
return int(edns.UDPSize())
}

if network == "tcp" {
return dns.MaxMsgSize
}

return dns.MinMsgSize
}

// OnHealthCheck Handler for docker health check. Just returns OK code without delegating to resolver chain
func (s *Server) OnHealthCheck(w dns.ResponseWriter, request *dns.Msg) {
resp := new(dns.Msg)
Expand Down

0 comments on commit 114171a

Please sign in to comment.