fix(dnstap): store IPv4-mapped IPv6 addresses as 4 octets with SocketFamily INET (#8186)

Signed-off-by: Jaime Hablutzel <hablutzel1@gmail.com>
This commit is contained in:
Jaime Hablutzel
2026-06-25 17:49:32 -05:00
committed by GitHub
parent d0e0d92fa4
commit 1936a2bb40
3 changed files with 111 additions and 20 deletions

View File

@@ -15,31 +15,37 @@ var (
familyINET6 = tap.SocketFamily_INET6
)
// socketFamilyAndAddress maps an IP to the dnstap SocketFamily and the address
// bytes to store for it. IPv4, including IPv4-mapped IPv6, is returned as a
// 4-octet INET address; anything else as a 16-octet INET6 address, matching
// https://github.com/dnstap/dnstap.pb/blob/main/dnstap.proto.
func socketFamilyAndAddress(ip net.IP) (*tap.SocketFamily, []byte) {
if ip4 := ip.To4(); ip4 != nil {
return &familyINET, ip4
}
return &familyINET6, ip
}
// TODO: SetQueryAddress and SetResponseAddress each set the message-level SocketFamily (and SocketProtocol) from their own address. A dnstap Message describes a single socket whose two endpoints share one family, but calling both setters with addresses of different families leaves SocketFamily reflecting only the last call and inconsistent with the other stored address. Evaluate replacing the two setters with a single SetAddresses(t, query, response) that derives SocketFamily/SocketProtocol once for the whole socket and rejects a family mismatch.
// SetQueryAddress adds the query address to the message. This also sets the SocketFamily and SocketProtocol.
func SetQueryAddress(t *tap.Message, addr net.Addr) error {
t.SocketFamily = &familyINET
switch a := addr.(type) {
case *net.TCPAddr:
t.SocketProtocol = &protoTCP
t.QueryAddress = a.IP
p := uint32(a.Port) // #nosec G115 -- Port is inherently bounded (1-65535)
t.QueryPort = &p
if a.IP.To4() == nil {
t.SocketFamily = &familyINET6
}
t.SocketFamily, t.QueryAddress = socketFamilyAndAddress(a.IP)
return nil
case *net.UDPAddr:
t.SocketProtocol = &protoUDP
t.QueryAddress = a.IP
p := uint32(a.Port) // #nosec G115 -- Port is inherently bounded (1-65535)
t.QueryPort = &p
if a.IP.To4() == nil {
t.SocketFamily = &familyINET6
}
t.SocketFamily, t.QueryAddress = socketFamilyAndAddress(a.IP)
return nil
default:
return fmt.Errorf("unknown address type: %T", a)
@@ -48,29 +54,22 @@ func SetQueryAddress(t *tap.Message, addr net.Addr) error {
// SetResponseAddress the response address to the message. This also sets the SocketFamily and SocketProtocol.
func SetResponseAddress(t *tap.Message, addr net.Addr) error {
t.SocketFamily = &familyINET
switch a := addr.(type) {
case *net.TCPAddr:
t.SocketProtocol = &protoTCP
t.ResponseAddress = a.IP
p := uint32(a.Port) // #nosec G115 -- Port is inherently bounded (1-65535)
t.ResponsePort = &p
if a.IP.To4() == nil {
t.SocketFamily = &familyINET6
}
t.SocketFamily, t.ResponseAddress = socketFamilyAndAddress(a.IP)
return nil
case *net.UDPAddr:
t.SocketProtocol = &protoUDP
t.ResponseAddress = a.IP
p := uint32(a.Port) // #nosec G115 -- Port is inherently bounded (1-65535)
t.ResponsePort = &p
if a.IP.To4() == nil {
t.SocketFamily = &familyINET6
}
t.SocketFamily, t.ResponseAddress = socketFamilyAndAddress(a.IP)
return nil
default:
return fmt.Errorf("unknown address type: %T", a)