common/helpers: rename NetIPTo6 to AddrTo6

This commit is contained in:
Vincent Bernat
2025-11-04 07:43:19 +01:00
parent 23518c3e2e
commit ffe696e0e1
11 changed files with 24 additions and 24 deletions

View File

@@ -15,7 +15,7 @@ import (
func expectCacheGet(t *testing.T, c *cache.Cache[netip.Addr, string], key string, expectedResult string, expectedOk bool) {
t.Helper()
ip := netip.MustParseAddr(key)
ip = helpers.NetIPTo6(ip)
ip = helpers.AddrTo6(ip)
result, ok := c.Get(time.Time{}, ip)
got := struct {
Result string

View File

@@ -13,7 +13,7 @@ var (
z6noz = *(*uint64)(unsafe.Add(unsafe.Pointer(&someIPv6), 16))
)
// NetIPTo6 maps an IPv4 to an IPv4-mapped IPv6 and returns an IPv6 unmodified.
// AddrTo6 maps an IPv4 to an IPv4-mapped IPv6 and returns an IPv6 unmodified.
// This is unsafe, but there is a test to ensure netip.Addr is like we expect.
// Copying a unique.Handle bypass reference count, but z6noz is "static".
//
@@ -25,7 +25,7 @@ var (
// }
// return ip
// }
func NetIPTo6(ip netip.Addr) netip.Addr {
func AddrTo6(ip netip.Addr) netip.Addr {
if ip.Is4() {
p := (*uint64)(unsafe.Add(unsafe.Pointer(&ip), 16))
*p = z6noz

View File

@@ -13,7 +13,7 @@ import (
"akvorado/common/helpers"
)
func TestNetIPTo6(t *testing.T) {
func TestAddrTo6(t *testing.T) {
cases := []struct {
input netip.Addr
output netip.Addr
@@ -23,9 +23,9 @@ func TestNetIPTo6(t *testing.T) {
{netip.MustParseAddr("2a01:db8::1"), netip.MustParseAddr("2a01:db8::1")},
}
for _, tc := range cases {
got := helpers.NetIPTo6(tc.input)
got := helpers.AddrTo6(tc.input)
if diff := helpers.Diff(got, tc.output); diff != "" {
t.Errorf("NetIPTo6(%s) (-got, +want):\n%s", tc.input, diff)
t.Errorf("AddrTo6(%s) (-got, +want):\n%s", tc.input, diff)
}
}
}
@@ -78,18 +78,18 @@ func TestNetIPAddrStructure(t *testing.T) {
field0.Type.Size(), field1.Type.Size())
}
func netIPTo6Safe(ip netip.Addr) netip.Addr {
func addrTo6Safe(ip netip.Addr) netip.Addr {
if ip.Is4() {
return netip.AddrFrom16(ip.As16())
}
return ip
}
func netIPTo6SafeNocheck(ip netip.Addr) netip.Addr {
func addrTo6SafeNocheck(ip netip.Addr) netip.Addr {
return netip.AddrFrom16(ip.As16())
}
func BenchmarkNetIPTo6(b *testing.B) {
func BenchmarkAddrTo6(b *testing.B) {
ipv4 := netip.MustParseAddr("192.168.1.1")
ipv6 := netip.MustParseAddr("2a01:db8::1")
for _, ip := range []netip.Addr{ipv4, ipv6} {
@@ -99,17 +99,17 @@ func BenchmarkNetIPTo6(b *testing.B) {
}
b.Run(fmt.Sprintf("safe %s", version), func(b *testing.B) {
for b.Loop() {
_ = netIPTo6Safe(ip)
_ = addrTo6Safe(ip)
}
})
b.Run(fmt.Sprintf("safe nocheck %s", version), func(b *testing.B) {
for b.Loop() {
_ = netIPTo6SafeNocheck(ip)
_ = addrTo6SafeNocheck(ip)
}
})
b.Run(fmt.Sprintf("unsafe %s", version), func(b *testing.B) {
for b.Loop() {
_ = helpers.NetIPTo6(ip)
_ = helpers.AddrTo6(ip)
}
})
}

View File

@@ -228,7 +228,7 @@ func PrefixTo16(prefix netip.Prefix) netip.Prefix {
return prefix
}
// Convert IPv4 to IPv4-mapped IPv6
return netip.PrefixFrom(NetIPTo6(prefix.Addr()), prefix.Bits()+96)
return netip.PrefixFrom(AddrTo6(prefix.Addr()), prefix.Bits()+96)
}
// SubnetMapParseKey parses a prefix or an IP address into a netip.Prefix that