This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip.go
173 lines (146 loc) · 3.13 KB
/
ip.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ip
import (
"bytes"
"errors"
"fmt"
"net"
"strings"
)
// conforms to interface net.Addr
type IP struct {
IP net.IP
Mask net.IPMask
Zone string // used with IPv4 and IPv6 to indicate interface name
}
const (
NoZone = ""
ZoneSep = "%"
)
func ParseZone(s string) (ip, zone string, err error) {
parts := strings.Split(s, ZoneSep)
switch len(parts) {
case 1:
ip, zone = s, NoZone
case 2:
ip, zone = parts[0], parts[1]
default:
err = errors.New("IP may contain only one '%': <ip>%<zone>")
}
return
}
/* Parses:
IPv4 addr w/o zone 4.5.6.7
IPv4 addr w/ zone 1.2.3.4%lo0
IPv4 net w/o zone 192.168.0.0/16
IPv4 net w/ zone 192.168.0.0/16%eth0
IPv6 addr w/o zone ::1
IPv6 addr w/ zone ::1%eth0
IPv6 net w/o zone 2001:DB8::/48
IPv6 net w/ zone 2001:DB8::/48%eth0
*/
func Parse(s string) (r IP, err error) {
s, r.Zone, err = ParseZone(s)
if err != nil {
return
}
_, ipnet, _ := net.ParseCIDR(s)
if ipnet != nil { // we're done
r.IP = ipnet.IP
r.Mask = ipnet.Mask
return
}
r.IP = net.ParseIP(s)
if r.IP == nil {
err = errors.New("Bad IPv4/v6 IP addr or network")
return
}
return
}
func (n IP) Equal(n2 IP) bool {
return n.EqualZone(n2.Zone) &&
n.IP.Equal(n2.IP) &&
bytes.Compare(n.Mask, n2.Mask) == 0
}
func (n IP) IsIPv6() bool {
return !n.IsIPv4()
}
func (n IP) IsIPv4() bool {
return n.IP.To4() != nil
}
func (n IP) IsNetwork() bool {
for _, v := range n.Mask {
if v != 0xff {
return true
}
}
return false
}
func (n IP) HasZone() bool {
return n.Zone != NoZone
}
func (n IP) EqualZone(zone string) bool {
return zone == n.Zone
}
func (n IP) EqualInterface(iface *net.Interface) bool {
return iface == nil || n.EqualZone(iface.Name)
}
// no zone = all interfaces
func (n IP) Interfaces() (ifaces []net.Interface, err error) {
if !n.HasZone() {
ifaces, err = net.Interfaces()
return
}
iface, err := n.Interface()
if err != nil {
return
}
ifaces = append(ifaces, *iface)
return
}
// convert Zone to net.Interface
func (n IP) Interface() (*net.Interface, error) {
return net.InterfaceByName(n.Zone)
}
// convert ip.IP to net.IPNet
func (n IP) IPNet() *net.IPNet {
if n.Mask == nil {
return nil
}
return &net.IPNet{IP: n.IP, Mask: n.Mask}
}
// convert ip.IP to net.IPAddr
func (n IP) IPAddr() net.IPAddr {
return net.IPAddr{IP: n.IP, Zone: n.Zone}
}
// iface: nil = any interface
func (n IP) ContainsWithInterface(ip net.IP, iface *net.Interface) bool {
if !n.EqualInterface(iface) {
return false
}
ipnet := n.IPNet()
if ipnet == nil {
return n.IP.Equal(ip)
}
return ipnet.Contains(ip)
}
func (n IP) Contains(ip net.IP) bool {
return n.ContainsWithInterface(ip, nil /* any interface */)
}
func (n IP) Network() string {
return "ip+net+zone"
}
func (n IP) String() (s string) {
ipnet := n.IPNet()
if ipnet != nil {
s = ipnet.String()
} else {
s = n.IP.String()
}
if n.HasZone() {
s += ZoneSep + n.Zone
}
return
}
func (n IP) Inspect() string {
return fmt.Sprint("ip.IP(IP: ", n.IP, ", Mask: ", n.Mask, ", Zone: ", n.Zone)
}