Skip to content

Commit

Permalink
fix netiputil
Browse files Browse the repository at this point in the history
Signed-off-by: walnuts1018 <[email protected]>
  • Loading branch information
walnuts1018 committed Aug 29, 2024
1 parent 80cbe71 commit a141c87
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pkg/util/netiputil/netiputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ func FromAddr(addr netip.Addr) net.IP {
}

func ToAddr(ip net.IP) (netip.Addr, bool) {
return netip.AddrFromSlice(ip)
if f := ip.To4(); f != nil {
return netip.AddrFromSlice(f)
} else if f := ip.To16(); f != nil {
return netip.AddrFromSlice(f)
}
return netip.Addr{}, false
}

func IsFamilyMatched(a, b netip.Addr) bool {
Expand Down
15 changes: 14 additions & 1 deletion pkg/util/netiputil/netiputil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,20 @@ func TestFromAddr(t *testing.T) {
args args
want net.IP
}{
// TODO: Add test cases.
{
name: "1",
args: args{
addr: netip.MustParseAddr("10.244.0.1"),
},
want: net.ParseIP("10.244.0.1").To4(),
},
{
name: "2",
args: args{
addr: netip.MustParseAddr("ffcc::1"),
},
want: net.ParseIP("ffcc::1").To16(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit a141c87

Please sign in to comment.