You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In configuration and other places, IPv4 addresses are usually specified unmapped, like 1.1.1.1:53. When dealing with ReadFromUDPAddrPort and ReadMsgUDPAddrPort, the returned netip.AddrPort is likely to be an IPv4-mapped IPv6 address, like [::ffff:1.1.1.1]:53, depending on how the UDP socket was opened.
To check whether these 2 addresses point to the same endpoint, we would have to do something like:
// AddrPortMappedEqual returns whether the two addresses point to the same endpoint.// An IPv4 address and an IPv4-mapped IPv6 address pointing to the same endpoint are considered equal.// For example, 1.1.1.1:53 and [::ffff:1.1.1.1]:53 are considered equal.funcAddrPortMappedEqual(l, r netip.AddrPort) bool {
ifl==r {
returntrue
}
returnl.Port() ==r.Port() &&l.Addr().Unmap() ==r.Addr().Unmap()
}
It'd be nice if we have a method on netip.AddrPort to directly compare the unexported addr field in netip.Addr. I propose we add something like:
I don't think we want to add API for this. We're trying to fix the real problem (ReadMsgUDPAddrPort etc returning bad netip.Addr/AddrPort values) instead in #54234
I wouldn't call this "the real problem" or "bad values". It is natural for ReadMsgUDPAddrPort calls on IPv6 UDP sockets to return IPv4-mapped IPv6 addresses, because that's how the underlying socket works. A method like UnmappedEquals allows us to conveniently compare the returned address without changing the address family.
The following benchmark uses AddrPortMappedEqualUnsafe to simulate the proposed UnmappedEquals.
// AddrPortMappedEqual returns whether the two addresses point to the same endpoint.// An IPv4 address and an IPv4-mapped IPv6 address pointing to the same endpoint are considered equal.// For example, 1.1.1.1:53 and [::ffff:1.1.1.1]:53 are considered equal.funcAddrPortMappedEqual(l, r netip.AddrPort) bool {
ifl==r {
returntrue
}
returnl.Port() ==r.Port() &&l.Addr().Unmap() ==r.Addr().Unmap()
}
typeaddrPortHeaderstruct {
ip [16]bytez unsafe.Pointerportuint16
}
funcAddrPortMappedEqualUnsafe(l, r netip.AddrPort) bool {
lp:= (*addrPortHeader)(unsafe.Pointer(&l))
rp:= (*addrPortHeader)(unsafe.Pointer(&r))
returnlp.ip==rp.ip&&lp.port==rp.port
}
In configuration and other places, IPv4 addresses are usually specified unmapped, like
1.1.1.1:53
. When dealing withReadFromUDPAddrPort
andReadMsgUDPAddrPort
, the returnednetip.AddrPort
is likely to be an IPv4-mapped IPv6 address, like[::ffff:1.1.1.1]:53
, depending on how the UDP socket was opened.To check whether these 2 addresses point to the same endpoint, we would have to do something like:
It'd be nice if we have a method on
netip.AddrPort
to directly compare the unexportedaddr
field innetip.Addr
. I propose we add something like:The method can also be called
MappedEquals
if #54365 is accepted./cc @bradfitz
The text was updated successfully, but these errors were encountered: