/
ip-addresses.go
271 lines (235 loc) · 7.66 KB
/
ip-addresses.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package util
import (
"fmt"
"net"
"net/netip"
"strconv"
"strings"
)
// Returns true if the IP address string representation is IPv6.
//
// Note that this function does no validation and assumes the argument is already
// a valid IPv6 or IPv4 address.
func IsIPv6(address string) bool {
// See: https://stackoverflow.com/questions/22751035/golang-distinguish-ipv4-ipv6
return strings.Contains(address, ":")
}
// Returns "[address]:port" for IPv6 and "address:port" for IPv4.
//
// Meant to satisfy the unfortunate requirement of many APIs to provide
// an address (or hostname) and port with a single string argument.
func JoinIPAddressPort(address string, port int) string {
if IsIPv6(address) {
return "[" + address + "]:" + strconv.FormatInt(int64(port), 10)
} else {
return address + ":" + strconv.FormatInt(int64(port), 10)
}
}
func SplitIPAddressPort(addressPort string) (string, int, bool) {
if p := strings.LastIndex(addressPort, ":"); p != -1 {
address := addressPort[:p]
port := addressPort[p+1:]
if strings.HasPrefix(address, "[") {
// IPv6
var ok bool
if address, _, ok = strings.Cut(address[1:], "]"); ok {
if port_, err := strconv.Atoi(port); err == nil {
return address, port_, true
}
}
} else {
// IPv4
if port_, err := strconv.Atoi(port); err == nil {
return address, port_, true
}
}
}
return "", 0, false
}
// If the zone is not empty returns "address%zone".
// It is expected that the argument does not already have a zone.
//
// For IPv6 address string representations only, see:
// https://en.wikipedia.org/wiki/IPv6_address#Scoped_literal_IPv6_addresses_(with_zone_index)
func JoinIPAddressZone(address string, zone string) string {
if zone != "" {
return address + "%" + zone
} else {
return address
}
}
// Returns true if the two UDP addresses are equal.
func IsUDPAddrEqual(a *net.UDPAddr, b *net.UDPAddr) bool {
return a.IP.Equal(b.IP) && (a.Port == b.Port) && (a.Zone == b.Zone)
}
// Always returns a specified address. If the argument is already a specified address,
// returns it as is. Otherwise (when it's "::" or "0.0.0.0") will attempt to find a specified
// address by enumerating the active local interfaces, chosing one arbitrarily, with a
// preference for a global unicast address.
//
// The IP version of the returned address will match that of the argument, IPv6 for "::"
// and IPv4 for "0.0.0.0".
//
// Note that a returned IPv6 address may include a zone (when not a global unicast).
func ToReachableIPAddress(address string) (string, error) {
// (Note: net.ParseIP can't parse IPv6 with zone, but netip.ParseAddr can)
if addr, err := netip.ParseAddr(address); err == nil {
if addr.IsUnspecified() {
// Try to find a global unicast first
collector := IPAddressCollector{
IPv6: IsIPv6(address),
WithZone: true,
FilterInterface: func(interface_ net.Interface) bool {
return (interface_.Flags&net.FlagLoopback == 0) && (interface_.Flags&net.FlagUp != 0)
},
FilterIP: func(ip net.IP) bool {
return ip.IsGlobalUnicast()
},
}
if addresses, err := collector.Collect(); err == nil {
if len(addresses) > 0 {
return addresses[0], nil
}
} else {
return "", err
}
// Otherwise, just use the first address
collector.FilterIP = nil
if addresses, err := collector.Collect(); err == nil {
if len(addresses) > 0 {
return addresses[0], nil
}
} else {
return "", err
}
return "", fmt.Errorf("cannot find an equivalent reachable address for: %s", address)
}
} else {
return "", err
}
return address, nil
}
// The argument is validated as being a multicast address, e.g. "ff02::1" (IPv6) or
// "239.0.0.1" (IPv4). For IPv6, if it does not include a zone, a valid zone will be
// added by enumerating the active local interfaces, chosing one arbitrarily.
func ToBroadcastIPAddress(address string) (string, error) {
// (Note: net.ParseIP can't parse IPv6 with zone, but netip.ParseAddr can)
if addr, err := netip.ParseAddr(address); err == nil {
if !addr.IsMulticast() {
return "", fmt.Errorf("not a multicast address: %s", address)
}
if IsIPv6(address) && (addr.Zone() == "") {
if interfaces, err := net.Interfaces(); err == nil {
for _, interface_ := range interfaces {
if (interface_.Flags&net.FlagLoopback == 0) && (interface_.Flags&net.FlagUp != 0) &&
(interface_.Flags&net.FlagBroadcast != 0) && (interface_.Flags&net.FlagMulticast != 0) {
// The IPv6 zone is usually the interface name
return JoinIPAddressZone(address, interface_.Name), nil
}
}
} else {
return "", err
}
return "", fmt.Errorf("cannot find IPv6 zone for: %s", address)
}
return address, nil
} else {
return "", err
}
}
func IPAddressPortWithoutZone(address string) string {
if strings.Contains(address, "%") {
var port string
if colon := strings.LastIndex(address, ":"); colon != -1 {
port = address[colon+1:]
address = address[:colon]
}
var ipv6 bool
if strings.HasPrefix(address, "[") {
// This should always be the case
ipv6 = true
address = address[1 : len(address)-1]
}
address, _, _ = strings.Cut(address, "%")
if ipv6 {
address = "[" + address + "]"
}
if port == "" {
return address
} else {
return address + ":" + port
}
} else {
return address
}
}
func DumpIPAddress(address any) {
// Note: net.ParseIP can't parse IPv6 zone
ip := netip.MustParseAddr(ToString(address))
fmt.Printf("address: %s\n", ip)
fmt.Printf(" global unicast: %t\n", ip.IsGlobalUnicast())
fmt.Printf(" interface local multicast: %t\n", ip.IsInterfaceLocalMulticast())
fmt.Printf(" link local multicast: %t\n", ip.IsLinkLocalMulticast())
fmt.Printf(" link local unicast: %t\n", ip.IsLinkLocalUnicast())
fmt.Printf(" loopback: %t\n", ip.IsLoopback())
fmt.Printf(" multicast: %t\n", ip.IsMulticast())
fmt.Printf(" private: %t\n", ip.IsPrivate())
fmt.Printf(" unspecified: %t\n", ip.IsUnspecified())
}
//
// IPAddressCollector
//
type FilterInterfaceFunc func(interface_ net.Interface) bool
type FilterIPFunc func(ip net.IP) bool
type IPAddressCollector struct {
// If nil, will call net.Interfaces().
Interfaces []net.Interface
// Which IP version to accept.
IPv6 bool
// Include IPv6 zone in returned addresses.
WithZone bool
// Return true to accept an interface (can be nil).
FilterInterface FilterInterfaceFunc
// Return true to accept an IP (can be nil).
// Note that the argument's address (ip.String()) does not include the IPv6 zone.
FilterIP FilterIPFunc
}
func (self *IPAddressCollector) Collect() ([]string, error) {
var addresses []string
if interfaces, err := self.interfaces(); err == nil {
for _, interface_ := range interfaces {
if (self.FilterInterface == nil) || self.FilterInterface(interface_) {
if addrs, err := interface_.Addrs(); err == nil {
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok {
ip := ipNet.IP
address := ip.String()
isIpV6 := IsIPv6(address)
if (isIpV6 == self.IPv6) && ((self.FilterIP == nil) || self.FilterIP(ip)) {
if self.WithZone && isIpV6 {
// The IPv6 zone is usually the interface name
address = JoinIPAddressZone(address, interface_.Name)
}
addresses = append(addresses, address)
}
}
}
} else {
return nil, err
}
}
}
} else {
return nil, err
}
return addresses, nil
}
func (self *IPAddressCollector) interfaces() ([]net.Interface, error) {
if self.Interfaces == nil {
var err error
if self.Interfaces, err = net.Interfaces(); err != nil {
return nil, err
}
}
return self.Interfaces, nil
}