Skip to content

Commit

Permalink
types/inet: Handle IPv6 address inside brackets (#182)
Browse files Browse the repository at this point in the history
* types/inet: Handle IPv6 address inside brackets

`net.ParseCIDR` fails when IPv6 address given inside brackets:
```
invalid CIDR address: [2000::1]
```

* add test case for parse IPv6 inside brackets
  • Loading branch information
gshirazi authored Mar 12, 2020
1 parent db22b9f commit 1938937
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion types/inet.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ func (i *Inet) Scan(value interface{}) error {

// ParseInet will return the Inet address/netmask represented in the input string
func ParseInet(addr string) (*Inet, error) {
if len(addr) == 0 {
addr_len := len(addr)
if addr_len == 0 {
return nil, nil
}
if addr[0] == '[' && addr[addr_len-1] == ']' {
addr = addr[1 : addr_len-1]
}

ip, cidr, err := net.ParseCIDR(addr)
var mask net.IPMask
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions types/inet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func TestInetParse(t *testing.T) {
if !inet.IP.Equal(net.ParseIP("fe80:3::1ff:fe23:4567:890a")) || !reflect.DeepEqual(inet.Mask, net.CIDRMask(128, 128)) {
t.Errorf("Did not get expected value, got %+v", *inet)
}
// ------
inet, err = ParseInet("[2000::1]")
if err != nil {
t.Error(err)
}
if !inet.IP.Equal(net.ParseIP("2000::1")) || !reflect.DeepEqual(inet.Mask, net.CIDRMask(128, 128)) {
t.Errorf("Did not get expected value, got %+v", *inet)
}
}

func TestString(t *testing.T) {
Expand Down

0 comments on commit 1938937

Please sign in to comment.