-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ipnet.go
91 lines (77 loc) · 2.08 KB
/
ipnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Package ipnet wraps net.IPNet to get CIDR serialization.
package ipnet
import (
"encoding/json"
"net"
"reflect"
"github.com/pkg/errors"
)
var nullString = "null"
var nullBytes = []byte(nullString)
var emptyIPNet = net.IPNet{}
// IPNet wraps net.IPNet to get CIDR serialization.
type IPNet struct {
net.IPNet
}
// String returns a CIDR serialization of the subnet, or an empty
// string if the subnet is nil.
func (ipnet *IPNet) String() string {
if ipnet == nil {
return ""
}
return ipnet.IPNet.String()
}
// MarshalJSON interface for an IPNet
func (ipnet IPNet) MarshalJSON() (data []byte, err error) {
if reflect.DeepEqual(ipnet.IPNet, emptyIPNet) {
return nullBytes, nil
}
return json.Marshal(ipnet.String())
}
// UnmarshalJSON interface for an IPNet
func (ipnet *IPNet) UnmarshalJSON(b []byte) (err error) {
if string(b) == nullString {
ipnet.IP = net.IP{}
ipnet.Mask = net.IPMask{}
return nil
}
var cidr string
err = json.Unmarshal(b, &cidr)
if err != nil {
return errors.Wrap(err, "failed to Unmarshal string")
}
ip, net, err := net.ParseCIDR(cidr)
if err != nil {
return errors.Wrap(err, "failed to Parse cidr string to net.IPNet")
}
// This check is needed in order to work around a strange quirk in the Go
// standard library. All of the addresses returned by net.ParseCIDR() are
// 16-byte addresses. This does _not_ imply that they are IPv6 addresses,
// which is what some libraries (e.g. github.com/apparentlymart/go-cidr)
// assume. By forcing the address to be the expected length, we can work
// around these bugs.
if ip.To4() != nil {
ipnet.IP = ip.To4()
} else {
ipnet.IP = ip
}
ipnet.Mask = net.Mask
return nil
}
// ParseCIDR parses a CIDR from its string representation.
func ParseCIDR(s string) (*IPNet, error) {
_, cidr, err := net.ParseCIDR(s)
if err != nil {
return nil, err
}
return &IPNet{IPNet: *cidr}, nil
}
// MustParseCIDR parses a CIDR from its string representation. If the parse fails,
// the function will panic.
func MustParseCIDR(s string) *IPNet {
cidr, err := ParseCIDR(s)
if err != nil {
panic(err)
}
return cidr
}