From df43b8f228b705b0a97de31a271133b0cb1d5c48 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Sun, 24 Mar 2024 22:31:04 +0800 Subject: [PATCH] fix: fail to parse ipv6 address of fc00::1:1004:ffff:ffff:ffff:ffff --- util/netutils/ipv6.go | 6 +++++- util/netutils/ipv6_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/util/netutils/ipv6.go b/util/netutils/ipv6.go index 4b390c7..1dbc067 100644 --- a/util/netutils/ipv6.go +++ b/util/netutils/ipv6.go @@ -155,8 +155,12 @@ func normalizeIpv6Addr(addrStr string) ([8]uint16, error) { } } else { for i := 0; i < len(parts); i++ { + partStr := strings.TrimSpace(parts[i]) + if len(partStr) == 0 { + continue + } var err error - addr[i], err = hex2Number(strings.TrimSpace(parts[i])) + addr[i], err = hex2Number(partStr) if err != nil { return addr, errors.Wrapf(err, "hex2Number %s", addrStr) } diff --git a/util/netutils/ipv6_test.go b/util/netutils/ipv6_test.go index 0e75e9b..55b79b0 100644 --- a/util/netutils/ipv6_test.go +++ b/util/netutils/ipv6_test.go @@ -253,6 +253,19 @@ func TestNewIPV6Addr(t *testing.T) { netAddr: "::1200:0:0:0", broadcastAddr: "::12ff:ffff:ffff:ffff", }, + { + in: "fc00:0:1:1004::10", + wantAddr: IPV6Addr{ + 0xfc00, 0, 1, 0x1004, 0, 0, 0, 0x10, + }, + want: "fc00:0:1:1004::10", + stepUp: "fc00:0:1:1004::11", + stepDown: "fc00:0:1:1004::f", + + preflen: 64, + netAddr: "fc00:0:1:1004::", + broadcastAddr: "fc00::1:1004:ffff:ffff:ffff:ffff", + }, } for _, c := range cases { addr6, err := NewIPV6Addr(c.in) @@ -391,6 +404,10 @@ func TestRandomAddress(t *testing.T) { addr1: "::1234:1", addr2: "::1238:ffff", }, + { + addr1: "fc00:0:1:1004::10", + addr2: "fc00::1:1004:ffff:ffff:ffff:ffff", + }, } for _, c := range cases { addr1, err := NewIPV6Addr(c.addr1)