From a01a6d6efea52802f455849cd52ef7f8d049033a Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 10 Nov 2021 01:04:12 +0100 Subject: [PATCH] net: add conversion from AddrPort to TCPAddr to complement existing inverse We already have various member functions of TCPAddr that return an AddrPort, but we don't have a helper function to go from a AddrPort to a TCPAddr. UDP has this, but it was left out of TCP. This commit adds the corresponding function. Updates #49298. Change-Id: I85732cf34f47c792fe13a6b4af64fd4b0e85d06a Reviewed-on: https://go-review.googlesource.com/c/go/+/362596 Trust: Jason A. Donenfeld Run-TryBot: Jason A. Donenfeld TryBot-Result: Go Bot Reviewed-by: Brad Fitzpatrick --- src/net/tcpsock.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/net/tcpsock.go b/src/net/tcpsock.go index fddb018aab..6bad0e8f8b 100644 --- a/src/net/tcpsock.go +++ b/src/net/tcpsock.go @@ -96,6 +96,17 @@ func ResolveTCPAddr(network, address string) (*TCPAddr, error) { return addrs.forResolve(network, address).(*TCPAddr), nil } +// TCPAddrFromAddrPort returns addr as a TCPAddr. If addr.IsValid() is false, +// then the returned TCPAddr will contain a nil IP field, indicating an +// address family-agnostic unspecified address. +func TCPAddrFromAddrPort(addr netip.AddrPort) *TCPAddr { + return &TCPAddr{ + IP: addr.Addr().AsSlice(), + Zone: addr.Addr().Zone(), + Port: int(addr.Port()), + } +} + // TCPConn is an implementation of the Conn interface for TCP network // connections. type TCPConn struct {