From 6aab87f857d65e05217005bdee2cd8d5590dee79 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 16 Apr 2026 11:51:37 +0200 Subject: [PATCH] udp: add UDPAddrFromAddrPort from Big Go implementation Signed-off-by: deadprogram --- udpsock.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/udpsock.go b/udpsock.go index 99cff8c..ef683b7 100644 --- a/udpsock.go +++ b/udpsock.go @@ -113,6 +113,17 @@ func ResolveUDPAddr(network, address string) (*UDPAddr, error) { return &UDPAddr{IP: ip.AsSlice(), Port: port}, nil } +// UDPAddrFromAddrPort returns addr as a [UDPAddr]. If addr.IsValid() is false, +// then the returned UDPAddr will contain a nil IP field, indicating an +// address family-agnostic unspecified address. +func UDPAddrFromAddrPort(addr netip.AddrPort) *UDPAddr { + return &UDPAddr{ + IP: addr.Addr().AsSlice(), + Zone: addr.Addr().Zone(), + Port: int(addr.Port()), + } +} + // UDPConn is the implementation of the Conn and PacketConn interfaces // for UDP network connections. type UDPConn struct {