diff --git a/types/ip.go b/types/ip.go index 8756d0c..1844d15 100644 --- a/types/ip.go +++ b/types/ip.go @@ -23,10 +23,10 @@ func (s *Subnet) UnmarshalJSON(in []byte) error { ip, ipnet, err := net.ParseCIDR(string(in)) if err != nil { - return fmt.Errorf("not a valid subnet") + return fmt.Errorf("not a valid subnet: %#v", err) } if !ipnet.IP.Equal(ip) { - return fmt.Errorf("provided IP is not a subnet") + return fmt.Errorf("provided IP '%s' is not network address '%s' of declared subnet", ip.String(), ipnet.IP.String()) } *s = Subnet(*ipnet) return nil @@ -40,6 +40,17 @@ func (s *Subnet) String() string { return (*net.IPNet)(s).String() } +func (i IP) Is4() bool { + if a := net.IP(i).To4(); a == nil { + return false + } + return true +} + +func (i IP) Is6() bool { + return !i.Is4() +} + // Value implements the database Value interface. // // This function is needed so that a subnet can be inserted into