Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix host.ip 0.0.0.0 treated as invalid #323

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions model/modelpb/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func MustParseIP(s string) *IP {
return ip
}

// Addr2IP converts a valid netip.Addr to IP.
func Addr2IP(addr netip.Addr) *IP {
if addr.Is4() {
ip := IPFromVTPool()
Expand All @@ -53,16 +54,17 @@ func Addr2IP(addr netip.Addr) *IP {
return ip
}

// IP2Addr converts a nil IP to a zero netip.Addr and a valid IP to a valid netip.Addr.
func IP2Addr(i *IP) netip.Addr {
carsonip marked this conversation as resolved.
Show resolved Hide resolved
if i == nil {
return netip.Addr{}
}
if addr := i.GetV6(); len(addr) == 16 {
return netip.AddrFrom16([16]byte(addr))
}
if i.GetV4() != 0 {
var addr [4]byte
binary.BigEndian.PutUint32(addr[:], i.V4)
return netip.AddrFrom4(addr)
}
return netip.Addr{}
var addr [4]byte
binary.BigEndian.PutUint32(addr[:], i.V4)
return netip.AddrFrom4(addr)
}

func IP2String(i *IP) string {
Expand Down
32 changes: 28 additions & 4 deletions model/modelpb/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,29 @@ func TestAddr2IP(t *testing.T) {
expectedIP *IP
}{
{
name: "with an IPv4 address",
name: "IPv4 address 127.0.0.1",
address: netip.MustParseAddr("127.0.0.1"),

expectedIP: MustParseIP("127.0.0.1"),
},
{
name: "with an IPv6 address",
name: "IPv6 address 0.0.0.0",
address: netip.MustParseAddr("0.0.0.0"),

expectedIP: MustParseIP("0.0.0.0"),
},
{
name: "IPv6 address ::1",
address: netip.MustParseAddr("::1"),

expectedIP: MustParseIP("::1"),
},
{
name: "IPv6 address ::",
address: netip.MustParseAddr("::"),

expectedIP: MustParseIP("::"),
},
} {
t.Run(tt.name, func(t *testing.T) {
ip := Addr2IP(tt.address)
Expand All @@ -152,17 +164,29 @@ func TestIP2Addr(t *testing.T) {
expectedAddr netip.Addr
}{
{
name: "with an IPv4 address",
name: "IPv4 address 127.0.0.1",
ip: MustParseIP("127.0.0.1"),

expectedAddr: netip.MustParseAddr("127.0.0.1"),
},
{
name: "with an IPv6 address",
name: "IPv4 address 0.0.0.0",
ip: MustParseIP("0.0.0.0"),

expectedAddr: netip.MustParseAddr("0.0.0.0"),
},
{
name: "IPv6 address ::1",
ip: MustParseIP("::1"),

expectedAddr: netip.MustParseAddr("::1"),
},
{
name: "IPv6 address ::",
ip: MustParseIP("::"),

expectedAddr: netip.MustParseAddr("::"),
},
} {
t.Run(tt.name, func(t *testing.T) {
addr := IP2Addr(tt.ip)
Expand Down