Skip to content

Commit

Permalink
Merge pull request #127 from swordqiu/hotfix/qj-ipv6-zero-padding
Browse files Browse the repository at this point in the history
fix: ipv6 zero padding for less than 1 consecutive zero
  • Loading branch information
swordqiu authored May 23, 2024
2 parents 8203b24 + 5442d00 commit c920e76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion util/netutils/ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (addr IPV6Addr) String() string {
maxZeroLen = maxZeroEnd - maxZeroStart + 1
}
}
if maxZeroLen > 0 {
if maxZeroLen > 1 {
return strings.Join(hexStrs[:maxZeroStart], ":") + "::" + strings.Join(hexStrs[maxZeroEnd+1:], ":")
} else {
return strings.Join(hexStrs, ":")
Expand Down
17 changes: 15 additions & 2 deletions util/netutils/ipv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestNewIPV6Addr(t *testing.T) {
0x2001, 0xdb8, 0x3333, 0x4444, 0xcccc, 0xdddd, 0xeeee, 0xffff,
},
want: "2001:db8:3333:4444:cccc:dddd:eeee:ffff",
stepUp: "2001:db8:3333:4444:cccc:dddd:eeef::",
stepUp: "2001:db8:3333:4444:cccc:dddd:eeef:0",
stepDown: "2001:db8:3333:4444:cccc:dddd:eeee:fffe",

preflen: 96,
Expand Down Expand Up @@ -264,7 +264,20 @@ func TestNewIPV6Addr(t *testing.T) {

preflen: 64,
netAddr: "fc00:0:1:1004::",
broadcastAddr: "fc00::1:1004:ffff:ffff:ffff:ffff",
broadcastAddr: "fc00:0:1:1004:ffff:ffff:ffff:ffff",
},
{
in: "fc00::1:1004:ac1f:68f0:1433:ca53",
wantAddr: IPV6Addr{
0xfc00, 0, 1, 0x1004, 0xac1f, 0x68f0, 0x1433, 0xca53,
},
want: "fc00:0:1:1004:ac1f:68f0:1433:ca53",
stepUp: "fc00:0:1:1004:ac1f:68f0:1433:ca54",
stepDown: "fc00:0:1:1004:ac1f:68f0:1433:ca52",

preflen: 64,
netAddr: "fc00:0:1:1004::",
broadcastAddr: "fc00:0:1:1004:ffff:ffff:ffff:ffff",
},
}
for _, c := range cases {
Expand Down

0 comments on commit c920e76

Please sign in to comment.