-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: extract netipx package from dnscore (#52)
This diff extracts the netipx package from dnscore and exports the AddrToAddrPort function.
- Loading branch information
1 parent
30adea1
commit 4cc2756
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Package netipx contains [net/netip] extensions. | ||
package netipx | ||
|
||
import ( | ||
"net" | ||
"net/netip" | ||
) | ||
|
||
// AddrToAddrPort converts a [net.Addr] to a [netip.AddrPort]. | ||
// | ||
// If the input is nil or neither a [*net.TCPAddr] nor [*net.UDPAddr], | ||
// returns an unspecified IPv6 address with port 0. | ||
// | ||
// For [*net.TCPAddr] and [*net.UDPAddr] addresses, returns their | ||
// corresponding [netip.AddrPort] representation. | ||
func AddrToAddrPort(addr net.Addr) netip.AddrPort { | ||
if addr == nil { | ||
return netip.AddrPortFrom(netip.IPv6Unspecified(), 0) | ||
} | ||
if tcp, ok := addr.(*net.TCPAddr); ok { | ||
return tcp.AddrPort() | ||
} | ||
if udp, ok := addr.(*net.UDPAddr); ok { | ||
return udp.AddrPort() | ||
} | ||
return netip.AddrPortFrom(netip.IPv6Unspecified(), 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package netipx_test | ||
|
||
import ( | ||
"net" | ||
"net/netip" | ||
"testing" | ||
|
||
"github.com/rbmk-project/x/netipx" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAddrToAddrPort(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
addr net.Addr | ||
want netip.AddrPort | ||
}{ | ||
{ | ||
name: "nil address", | ||
addr: nil, | ||
want: netip.AddrPortFrom(netip.IPv6Unspecified(), 0), | ||
}, | ||
|
||
{ | ||
name: "TCP address", | ||
addr: &net.TCPAddr{ | ||
IP: net.ParseIP("2001:db8::1"), | ||
Port: 1234, | ||
}, | ||
want: netip.MustParseAddrPort("[2001:db8::1]:1234"), | ||
}, | ||
|
||
{ | ||
name: "UDP address", | ||
addr: &net.UDPAddr{ | ||
IP: net.ParseIP("2001:db8::2"), | ||
Port: 5678, | ||
}, | ||
want: netip.MustParseAddrPort("[2001:db8::2]:5678"), | ||
}, | ||
|
||
{ | ||
name: "other address type", | ||
addr: &net.UnixAddr{}, | ||
want: netip.AddrPortFrom(netip.IPv6Unspecified(), 0), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := netipx.AddrToAddrPort(tt.addr) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |