Skip to content

Commit

Permalink
fix: truncate dns message in udp response
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Apr 9, 2024
1 parent 72df27b commit bd703b8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions component/resolver/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func RelayDnsConn(ctx context.Context, conn net.Conn, readTimeout time.Duration)
ctx, cancel := context.WithTimeout(ctx, DefaultDnsRelayTimeout)
defer cancel()
inData := buff[:n]
msg, err := RelayDnsPacket(ctx, inData, buff)
msg, err := relayDnsPacket(ctx, inData, buff, 0)
if err != nil {
return err
}
Expand All @@ -69,7 +69,7 @@ func RelayDnsConn(ctx context.Context, conn net.Conn, readTimeout time.Duration)
return nil
}

func RelayDnsPacket(ctx context.Context, payload []byte, target []byte) ([]byte, error) {
func relayDnsPacket(ctx context.Context, payload []byte, target []byte, maxSize int) ([]byte, error) {
msg := &D.Msg{}
if err := msg.Unpack(payload); err != nil {
return nil, err
Expand All @@ -83,6 +83,14 @@ func RelayDnsPacket(ctx context.Context, payload []byte, target []byte) ([]byte,
}

r.SetRcode(msg, r.Rcode)
if maxSize > 0 {
r.Truncate(maxSize)
}
r.Compress = true
return r.PackBuffer(target)
}

// RelayDnsPacket will truncate udp message up to SafeDnsPacketSize
func RelayDnsPacket(ctx context.Context, payload []byte, target []byte) ([]byte, error) {
return relayDnsPacket(ctx, payload, target, SafeDnsPacketSize)
}

0 comments on commit bd703b8

Please sign in to comment.