Skip to content

Commit 83ede8b

Browse files
committed
only truncate replies over UDP
Signed-off-by: Nino Kodabande <[email protected]>
1 parent f215cb9 commit 83ede8b

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

cmd/limactl/debug.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func debugDNSAction(cmd *cobra.Command, args []string) error {
5050
srvOpts := dns.ServerOptions{
5151
UDPPort: udpLocalPort,
5252
TCPPort: tcpLocalPort,
53+
Address: "127.0.0.1",
5354
HandlerOptions: dns.HandlerOptions{
5455
IPv6: ipv6,
5556
StaticHosts: map[string]string{},

pkg/hostagent/dns/dns.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type HandlerOptions struct {
2323
IPv6 bool
2424
StaticHosts map[string]string
2525
UpstreamServers []string
26+
TruncateReply bool
2627
}
2728

2829
type ServerOptions struct {
@@ -33,9 +34,10 @@ type ServerOptions struct {
3334
}
3435

3536
type Handler struct {
37+
truncate bool
3638
clientConfig *dns.ClientConfig
3739
clients []*dns.Client
38-
IPv6 bool
40+
ipv6 bool
3941
cnameToHost map[string]string
4042
hostToIP map[string]net.IP
4143
}
@@ -98,7 +100,7 @@ func NewHandler(opts HandlerOptions) (dns.Handler, error) {
98100
h := &Handler{
99101
clientConfig: cc,
100102
clients: clients,
101-
IPv6: opts.IPv6,
103+
ipv6: opts.IPv6,
102104
cnameToHost: make(map[string]string),
103105
hostToIP: make(map[string]net.IP),
104106
}
@@ -128,7 +130,7 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
128130
qtype := q.Qtype
129131
switch qtype {
130132
case dns.TypeAAAA:
131-
if !h.IPv6 {
133+
if !h.ipv6 {
132134
// A "correct" answer would be to set `handled = true` and return a NODATA response.
133135
// Unfortunately some older resolvers use a slow random source to set the transaction id.
134136
// This creates a problem on M1 computers, which are too fast for that implementation:
@@ -268,8 +270,12 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
268270
}
269271
}
270272
if handled {
271-
reply.Truncate(truncateSize)
272-
_ = w.WriteMsg(&reply)
273+
if h.truncate {
274+
reply.Truncate(truncateSize)
275+
}
276+
if err := w.WriteMsg(&reply); err != nil {
277+
logrus.Debugf("handleQuery failed writing DNS reply: %v", err)
278+
}
273279
return
274280
}
275281
h.handleDefault(w, req)
@@ -280,17 +286,29 @@ func (h *Handler) handleDefault(w dns.ResponseWriter, req *dns.Msg) {
280286
for _, srv := range h.clientConfig.Servers {
281287
addr := fmt.Sprintf("%s:%s", srv, h.clientConfig.Port)
282288
reply, _, err := client.Exchange(req, addr)
283-
if err == nil {
289+
if err != nil {
290+
logrus.Debugf("handleDefault failed to perform a synchronous query with upstream [%v]: %v", addr, err)
291+
continue
292+
}
293+
if h.truncate {
294+
logrus.Debugf("handleDefault truncating reply: %v", reply)
284295
reply.Truncate(truncateSize)
285-
_ = w.WriteMsg(reply)
286-
return
287296
}
297+
if err = w.WriteMsg(reply); err != nil {
298+
logrus.Debugf("handleDefault failed writing DNS reply to [%v]: %v", addr, err)
299+
}
300+
return
288301
}
289302
}
290303
var reply dns.Msg
291304
reply.SetReply(req)
292-
reply.Truncate(truncateSize)
293-
_ = w.WriteMsg(&reply)
305+
if h.truncate {
306+
logrus.Debugf("handleDefault truncating reply: %v", reply)
307+
reply.Truncate(truncateSize)
308+
}
309+
if err := w.WriteMsg(&reply); err != nil {
310+
logrus.Debugf("handleDefault failed writing DNS reply: %v", err)
311+
}
294312
}
295313

296314
func (h *Handler) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
@@ -303,13 +321,16 @@ func (h *Handler) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
303321
}
304322

305323
func Start(opts ServerOptions) (*Server, error) {
306-
h, err := NewHandler(opts.HandlerOptions)
307-
if err != nil {
308-
return nil, err
309-
}
310324
server := &Server{}
311325
if opts.UDPPort > 0 {
312-
addr := fmt.Sprintf("127.0.0.1:%d", opts.UDPPort)
326+
udpOpts := opts
327+
// always enable reply truncate for UDP
328+
udpOpts.TruncateReply = true
329+
h, err := NewHandler(udpOpts.HandlerOptions)
330+
if err != nil {
331+
return nil, err
332+
}
333+
addr := fmt.Sprintf("%s:%d", opts.Address, opts.UDPPort)
313334
s := &dns.Server{Net: "udp", Addr: addr, Handler: h}
314335
server.udp = s
315336
go func() {
@@ -319,7 +340,13 @@ func Start(opts ServerOptions) (*Server, error) {
319340
}()
320341
}
321342
if opts.TCPPort > 0 {
322-
addr := fmt.Sprintf("127.0.0.1:%d", opts.TCPPort)
343+
tcpOpts := opts
344+
tcpOpts.TruncateReply = false
345+
h, err := NewHandler(tcpOpts.HandlerOptions)
346+
if err != nil {
347+
return nil, err
348+
}
349+
addr := fmt.Sprintf("%s:%d", opts.Address, opts.TCPPort)
323350
s := &dns.Server{Net: "tcp", Addr: addr, Handler: h}
324351
server.tcp = s
325352
go func() {

pkg/hostagent/hostagent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ func (a *HostAgent) Run(ctx context.Context) error {
258258
srvOpts := dns.ServerOptions{
259259
UDPPort: a.udpDNSLocalPort,
260260
TCPPort: a.tcpDNSLocalPort,
261+
Address: "127.0.0.1",
261262
HandlerOptions: dns.HandlerOptions{
262263
IPv6: *a.y.HostResolver.IPv6,
263264
StaticHosts: hosts,

0 commit comments

Comments
 (0)