Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // IP address manipulations |
| 6 | // |
| 7 | // IPv4 addresses are 4 bytes; IPv6 addresses are 16 bytes. |
| 8 | // An IPv4 address can be converted to an IPv6 address by |
| 9 | // adding a canonical prefix (10 zeros, 2 0xFFs). |
| 10 | // This library accepts either size of byte slice but always |
| 11 | // returns 16-byte addresses. |
| 12 | |
| 13 | package net |
| 14 | |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame^] | 15 | import "internal/bytealg" |
Dan Willemsen | c78f714 | 2017-07-26 13:08:14 -0700 | [diff] [blame] | 16 | |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 17 | // IP address lengths (bytes). |
| 18 | const ( |
| 19 | IPv4len = 4 |
| 20 | IPv6len = 16 |
| 21 | ) |
| 22 | |
| 23 | // An IP is a single IP address, a slice of bytes. |
| 24 | // Functions in this package accept either 4-byte (IPv4) |
| 25 | // or 16-byte (IPv6) slices as input. |
| 26 | // |
| 27 | // Note that in this documentation, referring to an |
| 28 | // IP address as an IPv4 address or an IPv6 address |
| 29 | // is a semantic property of the address, not just the |
| 30 | // length of the byte slice: a 16-byte slice can still |
| 31 | // be an IPv4 address. |
| 32 | type IP []byte |
| 33 | |
| 34 | // An IP mask is an IP address. |
| 35 | type IPMask []byte |
| 36 | |
| 37 | // An IPNet represents an IP network. |
| 38 | type IPNet struct { |
| 39 | IP IP // network number |
| 40 | Mask IPMask // network mask |
| 41 | } |
| 42 | |
| 43 | // IPv4 returns the IP address (in 16-byte form) of the |
| 44 | // IPv4 address a.b.c.d. |
| 45 | func IPv4(a, b, c, d byte) IP { |
| 46 | p := make(IP, IPv6len) |
| 47 | copy(p, v4InV6Prefix) |
| 48 | p[12] = a |
| 49 | p[13] = b |
| 50 | p[14] = c |
| 51 | p[15] = d |
| 52 | return p |
| 53 | } |
| 54 | |
| 55 | var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff} |
| 56 | |
| 57 | // IPv4Mask returns the IP mask (in 4-byte form) of the |
| 58 | // IPv4 mask a.b.c.d. |
| 59 | func IPv4Mask(a, b, c, d byte) IPMask { |
| 60 | p := make(IPMask, IPv4len) |
| 61 | p[0] = a |
| 62 | p[1] = b |
| 63 | p[2] = c |
| 64 | p[3] = d |
| 65 | return p |
| 66 | } |
| 67 | |
| 68 | // CIDRMask returns an IPMask consisting of `ones' 1 bits |
| 69 | // followed by 0s up to a total length of `bits' bits. |
| 70 | // For a mask of this form, CIDRMask is the inverse of IPMask.Size. |
| 71 | func CIDRMask(ones, bits int) IPMask { |
| 72 | if bits != 8*IPv4len && bits != 8*IPv6len { |
| 73 | return nil |
| 74 | } |
| 75 | if ones < 0 || ones > bits { |
| 76 | return nil |
| 77 | } |
| 78 | l := bits / 8 |
| 79 | m := make(IPMask, l) |
| 80 | n := uint(ones) |
| 81 | for i := 0; i < l; i++ { |
| 82 | if n >= 8 { |
| 83 | m[i] = 0xff |
| 84 | n -= 8 |
| 85 | continue |
| 86 | } |
| 87 | m[i] = ^byte(0xff >> n) |
| 88 | n = 0 |
| 89 | } |
| 90 | return m |
| 91 | } |
| 92 | |
| 93 | // Well-known IPv4 addresses |
| 94 | var ( |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 95 | IPv4bcast = IPv4(255, 255, 255, 255) // limited broadcast |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 96 | IPv4allsys = IPv4(224, 0, 0, 1) // all systems |
| 97 | IPv4allrouter = IPv4(224, 0, 0, 2) // all routers |
| 98 | IPv4zero = IPv4(0, 0, 0, 0) // all zeros |
| 99 | ) |
| 100 | |
| 101 | // Well-known IPv6 addresses |
| 102 | var ( |
| 103 | IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
| 104 | IPv6unspecified = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
| 105 | IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} |
| 106 | IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01} |
| 107 | IPv6linklocalallnodes = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01} |
| 108 | IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02} |
| 109 | ) |
| 110 | |
Dan Willemsen | c78f714 | 2017-07-26 13:08:14 -0700 | [diff] [blame] | 111 | // IsUnspecified reports whether ip is an unspecified address, either |
| 112 | // the IPv4 address "0.0.0.0" or the IPv6 address "::". |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 113 | func (ip IP) IsUnspecified() bool { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 114 | return ip.Equal(IPv4zero) || ip.Equal(IPv6unspecified) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 117 | // IsLoopback reports whether ip is a loopback address. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 118 | func (ip IP) IsLoopback() bool { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 119 | if ip4 := ip.To4(); ip4 != nil { |
| 120 | return ip4[0] == 127 |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 121 | } |
| 122 | return ip.Equal(IPv6loopback) |
| 123 | } |
| 124 | |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 125 | // IsMulticast reports whether ip is a multicast address. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 126 | func (ip IP) IsMulticast() bool { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 127 | if ip4 := ip.To4(); ip4 != nil { |
| 128 | return ip4[0]&0xf0 == 0xe0 |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 129 | } |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 130 | return len(ip) == IPv6len && ip[0] == 0xff |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 133 | // IsInterfaceLocalMulticast reports whether ip is |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 134 | // an interface-local multicast address. |
| 135 | func (ip IP) IsInterfaceLocalMulticast() bool { |
| 136 | return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x01 |
| 137 | } |
| 138 | |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 139 | // IsLinkLocalMulticast reports whether ip is a link-local |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 140 | // multicast address. |
| 141 | func (ip IP) IsLinkLocalMulticast() bool { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 142 | if ip4 := ip.To4(); ip4 != nil { |
| 143 | return ip4[0] == 224 && ip4[1] == 0 && ip4[2] == 0 |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 144 | } |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 145 | return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x02 |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 148 | // IsLinkLocalUnicast reports whether ip is a link-local |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 149 | // unicast address. |
| 150 | func (ip IP) IsLinkLocalUnicast() bool { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 151 | if ip4 := ip.To4(); ip4 != nil { |
| 152 | return ip4[0] == 169 && ip4[1] == 254 |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 153 | } |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 154 | return len(ip) == IPv6len && ip[0] == 0xfe && ip[1]&0xc0 == 0x80 |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 157 | // IsGlobalUnicast reports whether ip is a global unicast |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 158 | // address. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 159 | // |
| 160 | // The identification of global unicast addresses uses address type |
| 161 | // identification as defined in RFC 1122, RFC 4632 and RFC 4291 with |
| 162 | // the exception of IPv4 directed broadcast addresses. |
| 163 | // It returns true even if ip is in IPv4 private address space or |
| 164 | // local IPv6 unicast address space. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 165 | func (ip IP) IsGlobalUnicast() bool { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 166 | return (len(ip) == IPv4len || len(ip) == IPv6len) && |
| 167 | !ip.Equal(IPv4bcast) && |
| 168 | !ip.IsUnspecified() && |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 169 | !ip.IsLoopback() && |
| 170 | !ip.IsMulticast() && |
| 171 | !ip.IsLinkLocalUnicast() |
| 172 | } |
| 173 | |
| 174 | // Is p all zeros? |
| 175 | func isZeros(p IP) bool { |
| 176 | for i := 0; i < len(p); i++ { |
| 177 | if p[i] != 0 { |
| 178 | return false |
| 179 | } |
| 180 | } |
| 181 | return true |
| 182 | } |
| 183 | |
| 184 | // To4 converts the IPv4 address ip to a 4-byte representation. |
| 185 | // If ip is not an IPv4 address, To4 returns nil. |
| 186 | func (ip IP) To4() IP { |
| 187 | if len(ip) == IPv4len { |
| 188 | return ip |
| 189 | } |
| 190 | if len(ip) == IPv6len && |
| 191 | isZeros(ip[0:10]) && |
| 192 | ip[10] == 0xff && |
| 193 | ip[11] == 0xff { |
| 194 | return ip[12:16] |
| 195 | } |
| 196 | return nil |
| 197 | } |
| 198 | |
| 199 | // To16 converts the IP address ip to a 16-byte representation. |
| 200 | // If ip is not an IP address (it is the wrong length), To16 returns nil. |
| 201 | func (ip IP) To16() IP { |
| 202 | if len(ip) == IPv4len { |
| 203 | return IPv4(ip[0], ip[1], ip[2], ip[3]) |
| 204 | } |
| 205 | if len(ip) == IPv6len { |
| 206 | return ip |
| 207 | } |
| 208 | return nil |
| 209 | } |
| 210 | |
| 211 | // Default route masks for IPv4. |
| 212 | var ( |
| 213 | classAMask = IPv4Mask(0xff, 0, 0, 0) |
| 214 | classBMask = IPv4Mask(0xff, 0xff, 0, 0) |
| 215 | classCMask = IPv4Mask(0xff, 0xff, 0xff, 0) |
| 216 | ) |
| 217 | |
| 218 | // DefaultMask returns the default IP mask for the IP address ip. |
| 219 | // Only IPv4 addresses have default masks; DefaultMask returns |
| 220 | // nil if ip is not a valid IPv4 address. |
| 221 | func (ip IP) DefaultMask() IPMask { |
| 222 | if ip = ip.To4(); ip == nil { |
| 223 | return nil |
| 224 | } |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame^] | 225 | switch { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 226 | case ip[0] < 0x80: |
| 227 | return classAMask |
| 228 | case ip[0] < 0xC0: |
| 229 | return classBMask |
| 230 | default: |
| 231 | return classCMask |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | func allFF(b []byte) bool { |
| 236 | for _, c := range b { |
| 237 | if c != 0xff { |
| 238 | return false |
| 239 | } |
| 240 | } |
| 241 | return true |
| 242 | } |
| 243 | |
| 244 | // Mask returns the result of masking the IP address ip with mask. |
| 245 | func (ip IP) Mask(mask IPMask) IP { |
| 246 | if len(mask) == IPv6len && len(ip) == IPv4len && allFF(mask[:12]) { |
| 247 | mask = mask[12:] |
| 248 | } |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame^] | 249 | if len(mask) == IPv4len && len(ip) == IPv6len && bytealg.Equal(ip[:12], v4InV6Prefix) { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 250 | ip = ip[12:] |
| 251 | } |
| 252 | n := len(ip) |
| 253 | if n != len(mask) { |
| 254 | return nil |
| 255 | } |
| 256 | out := make(IP, n) |
| 257 | for i := 0; i < n; i++ { |
| 258 | out[i] = ip[i] & mask[i] |
| 259 | } |
| 260 | return out |
| 261 | } |
| 262 | |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 263 | // ubtoa encodes the string form of the integer v to dst[start:] and |
| 264 | // returns the number of bytes written to dst. The caller must ensure |
| 265 | // that dst has sufficient length. |
| 266 | func ubtoa(dst []byte, start int, v byte) int { |
| 267 | if v < 10 { |
| 268 | dst[start] = v + '0' |
| 269 | return 1 |
| 270 | } else if v < 100 { |
| 271 | dst[start+1] = v%10 + '0' |
| 272 | dst[start] = v/10 + '0' |
| 273 | return 2 |
| 274 | } |
| 275 | |
| 276 | dst[start+2] = v%10 + '0' |
| 277 | dst[start+1] = (v/10)%10 + '0' |
| 278 | dst[start] = v/100 + '0' |
| 279 | return 3 |
| 280 | } |
| 281 | |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 282 | // String returns the string form of the IP address ip. |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 283 | // It returns one of 4 forms: |
| 284 | // - "<nil>", if ip has length 0 |
| 285 | // - dotted decimal ("192.0.2.1"), if ip is an IPv4 or IP4-mapped IPv6 address |
| 286 | // - IPv6 ("2001:db8::1"), if ip is a valid IPv6 address |
| 287 | // - the hexadecimal form of ip, without punctuation, if no other cases apply |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 288 | func (ip IP) String() string { |
| 289 | p := ip |
| 290 | |
| 291 | if len(ip) == 0 { |
| 292 | return "<nil>" |
| 293 | } |
| 294 | |
| 295 | // If IPv4, use dotted notation. |
| 296 | if p4 := p.To4(); len(p4) == IPv4len { |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 297 | const maxIPv4StringLen = len("255.255.255.255") |
| 298 | b := make([]byte, maxIPv4StringLen) |
| 299 | |
| 300 | n := ubtoa(b, 0, p4[0]) |
| 301 | b[n] = '.' |
| 302 | n++ |
| 303 | |
| 304 | n += ubtoa(b, n, p4[1]) |
| 305 | b[n] = '.' |
| 306 | n++ |
| 307 | |
| 308 | n += ubtoa(b, n, p4[2]) |
| 309 | b[n] = '.' |
| 310 | n++ |
| 311 | |
| 312 | n += ubtoa(b, n, p4[3]) |
| 313 | return string(b[:n]) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 314 | } |
| 315 | if len(p) != IPv6len { |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 316 | return "?" + hexString(ip) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Find longest run of zeros. |
| 320 | e0 := -1 |
| 321 | e1 := -1 |
| 322 | for i := 0; i < IPv6len; i += 2 { |
| 323 | j := i |
| 324 | for j < IPv6len && p[j] == 0 && p[j+1] == 0 { |
| 325 | j += 2 |
| 326 | } |
| 327 | if j > i && j-i > e1-e0 { |
| 328 | e0 = i |
| 329 | e1 = j |
| 330 | i = j |
| 331 | } |
| 332 | } |
| 333 | // The symbol "::" MUST NOT be used to shorten just one 16 bit 0 field. |
| 334 | if e1-e0 <= 2 { |
| 335 | e0 = -1 |
| 336 | e1 = -1 |
| 337 | } |
| 338 | |
| 339 | const maxLen = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") |
| 340 | b := make([]byte, 0, maxLen) |
| 341 | |
| 342 | // Print with possible :: in place of run of zeros |
| 343 | for i := 0; i < IPv6len; i += 2 { |
| 344 | if i == e0 { |
| 345 | b = append(b, ':', ':') |
| 346 | i = e1 |
| 347 | if i >= IPv6len { |
| 348 | break |
| 349 | } |
| 350 | } else if i > 0 { |
| 351 | b = append(b, ':') |
| 352 | } |
| 353 | b = appendHex(b, (uint32(p[i])<<8)|uint32(p[i+1])) |
| 354 | } |
| 355 | return string(b) |
| 356 | } |
| 357 | |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 358 | func hexString(b []byte) string { |
| 359 | s := make([]byte, len(b)*2) |
| 360 | for i, tn := range b { |
| 361 | s[i*2], s[i*2+1] = hexDigit[tn>>4], hexDigit[tn&0xf] |
| 362 | } |
| 363 | return string(s) |
| 364 | } |
| 365 | |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 366 | // ipEmptyString is like ip.String except that it returns |
| 367 | // an empty string when ip is unset. |
| 368 | func ipEmptyString(ip IP) string { |
| 369 | if len(ip) == 0 { |
| 370 | return "" |
| 371 | } |
| 372 | return ip.String() |
| 373 | } |
| 374 | |
| 375 | // MarshalText implements the encoding.TextMarshaler interface. |
Dan Willemsen | c78f714 | 2017-07-26 13:08:14 -0700 | [diff] [blame] | 376 | // The encoding is the same as returned by String, with one exception: |
| 377 | // When len(ip) is zero, it returns an empty slice. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 378 | func (ip IP) MarshalText() ([]byte, error) { |
| 379 | if len(ip) == 0 { |
| 380 | return []byte(""), nil |
| 381 | } |
| 382 | if len(ip) != IPv4len && len(ip) != IPv6len { |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 383 | return nil, &AddrError{Err: "invalid IP address", Addr: hexString(ip)} |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 384 | } |
| 385 | return []byte(ip.String()), nil |
| 386 | } |
| 387 | |
| 388 | // UnmarshalText implements the encoding.TextUnmarshaler interface. |
| 389 | // The IP address is expected in a form accepted by ParseIP. |
| 390 | func (ip *IP) UnmarshalText(text []byte) error { |
| 391 | if len(text) == 0 { |
| 392 | *ip = nil |
| 393 | return nil |
| 394 | } |
| 395 | s := string(text) |
| 396 | x := ParseIP(s) |
| 397 | if x == nil { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 398 | return &ParseError{Type: "IP address", Text: s} |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 399 | } |
| 400 | *ip = x |
| 401 | return nil |
| 402 | } |
| 403 | |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 404 | // Equal reports whether ip and x are the same IP address. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 405 | // An IPv4 address and that same address in IPv6 form are |
| 406 | // considered to be equal. |
| 407 | func (ip IP) Equal(x IP) bool { |
| 408 | if len(ip) == len(x) { |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame^] | 409 | return bytealg.Equal(ip, x) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 410 | } |
| 411 | if len(ip) == IPv4len && len(x) == IPv6len { |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame^] | 412 | return bytealg.Equal(x[0:12], v4InV6Prefix) && bytealg.Equal(ip, x[12:]) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 413 | } |
| 414 | if len(ip) == IPv6len && len(x) == IPv4len { |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame^] | 415 | return bytealg.Equal(ip[0:12], v4InV6Prefix) && bytealg.Equal(ip[12:], x) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 416 | } |
| 417 | return false |
| 418 | } |
| 419 | |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 420 | func (ip IP) matchAddrFamily(x IP) bool { |
| 421 | return ip.To4() != nil && x.To4() != nil || ip.To16() != nil && ip.To4() == nil && x.To16() != nil && x.To4() == nil |
| 422 | } |
| 423 | |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 424 | // If mask is a sequence of 1 bits followed by 0 bits, |
| 425 | // return the number of 1 bits. |
| 426 | func simpleMaskLength(mask IPMask) int { |
| 427 | var n int |
| 428 | for i, v := range mask { |
| 429 | if v == 0xff { |
| 430 | n += 8 |
| 431 | continue |
| 432 | } |
| 433 | // found non-ff byte |
| 434 | // count 1 bits |
| 435 | for v&0x80 != 0 { |
| 436 | n++ |
| 437 | v <<= 1 |
| 438 | } |
| 439 | // rest must be 0 bits |
| 440 | if v != 0 { |
| 441 | return -1 |
| 442 | } |
| 443 | for i++; i < len(mask); i++ { |
| 444 | if mask[i] != 0 { |
| 445 | return -1 |
| 446 | } |
| 447 | } |
| 448 | break |
| 449 | } |
| 450 | return n |
| 451 | } |
| 452 | |
| 453 | // Size returns the number of leading ones and total bits in the mask. |
| 454 | // If the mask is not in the canonical form--ones followed by zeros--then |
| 455 | // Size returns 0, 0. |
| 456 | func (m IPMask) Size() (ones, bits int) { |
| 457 | ones, bits = simpleMaskLength(m), len(m)*8 |
| 458 | if ones == -1 { |
| 459 | return 0, 0 |
| 460 | } |
| 461 | return |
| 462 | } |
| 463 | |
| 464 | // String returns the hexadecimal form of m, with no punctuation. |
| 465 | func (m IPMask) String() string { |
| 466 | if len(m) == 0 { |
| 467 | return "<nil>" |
| 468 | } |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 469 | return hexString(m) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | func networkNumberAndMask(n *IPNet) (ip IP, m IPMask) { |
| 473 | if ip = n.IP.To4(); ip == nil { |
| 474 | ip = n.IP |
| 475 | if len(ip) != IPv6len { |
| 476 | return nil, nil |
| 477 | } |
| 478 | } |
| 479 | m = n.Mask |
| 480 | switch len(m) { |
| 481 | case IPv4len: |
| 482 | if len(ip) != IPv4len { |
| 483 | return nil, nil |
| 484 | } |
| 485 | case IPv6len: |
| 486 | if len(ip) == IPv4len { |
| 487 | m = m[12:] |
| 488 | } |
| 489 | default: |
| 490 | return nil, nil |
| 491 | } |
| 492 | return |
| 493 | } |
| 494 | |
| 495 | // Contains reports whether the network includes ip. |
| 496 | func (n *IPNet) Contains(ip IP) bool { |
| 497 | nn, m := networkNumberAndMask(n) |
| 498 | if x := ip.To4(); x != nil { |
| 499 | ip = x |
| 500 | } |
| 501 | l := len(ip) |
| 502 | if l != len(nn) { |
| 503 | return false |
| 504 | } |
| 505 | for i := 0; i < l; i++ { |
| 506 | if nn[i]&m[i] != ip[i]&m[i] { |
| 507 | return false |
| 508 | } |
| 509 | } |
| 510 | return true |
| 511 | } |
| 512 | |
| 513 | // Network returns the address's network name, "ip+net". |
| 514 | func (n *IPNet) Network() string { return "ip+net" } |
| 515 | |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 516 | // String returns the CIDR notation of n like "192.0.2.1/24" |
| 517 | // or "2001:db8::/48" as defined in RFC 4632 and RFC 4291. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 518 | // If the mask is not in the canonical form, it returns the |
| 519 | // string which consists of an IP address, followed by a slash |
| 520 | // character and a mask expressed as hexadecimal form with no |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 521 | // punctuation like "198.51.100.1/c000ff00". |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 522 | func (n *IPNet) String() string { |
| 523 | nn, m := networkNumberAndMask(n) |
| 524 | if nn == nil || m == nil { |
| 525 | return "<nil>" |
| 526 | } |
| 527 | l := simpleMaskLength(m) |
| 528 | if l == -1 { |
| 529 | return nn.String() + "/" + m.String() |
| 530 | } |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 531 | return nn.String() + "/" + uitoa(uint(l)) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // Parse IPv4 address (d.d.d.d). |
| 535 | func parseIPv4(s string) IP { |
| 536 | var p [IPv4len]byte |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 537 | for i := 0; i < IPv4len; i++ { |
| 538 | if len(s) == 0 { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 539 | // Missing octets. |
| 540 | return nil |
| 541 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 542 | if i > 0 { |
| 543 | if s[0] != '.' { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 544 | return nil |
| 545 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 546 | s = s[1:] |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 547 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 548 | n, c, ok := dtoi(s) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 549 | if !ok || n > 0xFF { |
| 550 | return nil |
| 551 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 552 | s = s[c:] |
| 553 | p[i] = byte(n) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 554 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 555 | if len(s) != 0 { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 556 | return nil |
| 557 | } |
| 558 | return IPv4(p[0], p[1], p[2], p[3]) |
| 559 | } |
| 560 | |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 561 | // parseIPv6Zone parses s as a literal IPv6 address and its associated zone |
| 562 | // identifier which is described in RFC 4007. |
| 563 | func parseIPv6Zone(s string) (IP, string) { |
| 564 | s, zone := splitHostZone(s) |
| 565 | return parseIPv6(s), zone |
| 566 | } |
| 567 | |
| 568 | // parseIPv6Zone parses s as a literal IPv6 address described in RFC 4291 |
| 569 | // and RFC 5952. |
| 570 | func parseIPv6(s string) (ip IP) { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 571 | ip = make(IP, IPv6len) |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 572 | ellipsis := -1 // position of ellipsis in ip |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 573 | |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 574 | // Might have leading ellipsis |
| 575 | if len(s) >= 2 && s[0] == ':' && s[1] == ':' { |
| 576 | ellipsis = 0 |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 577 | s = s[2:] |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 578 | // Might be only ellipsis |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 579 | if len(s) == 0 { |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 580 | return ip |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | // Loop, parsing hex numbers followed by colon. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 585 | i := 0 |
| 586 | for i < IPv6len { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 587 | // Hex number. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 588 | n, c, ok := xtoi(s) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 589 | if !ok || n > 0xFFFF { |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 590 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // If followed by dot, might be in trailing IPv4. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 594 | if c < len(s) && s[c] == '.' { |
| 595 | if ellipsis < 0 && i != IPv6len-IPv4len { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 596 | // Not the right place. |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 597 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 598 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 599 | if i+IPv4len > IPv6len { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 600 | // Not enough room. |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 601 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 602 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 603 | ip4 := parseIPv4(s) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 604 | if ip4 == nil { |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 605 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 606 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 607 | ip[i] = ip4[12] |
| 608 | ip[i+1] = ip4[13] |
| 609 | ip[i+2] = ip4[14] |
| 610 | ip[i+3] = ip4[15] |
| 611 | s = "" |
| 612 | i += IPv4len |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 613 | break |
| 614 | } |
| 615 | |
| 616 | // Save this 16-bit chunk. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 617 | ip[i] = byte(n >> 8) |
| 618 | ip[i+1] = byte(n) |
| 619 | i += 2 |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 620 | |
| 621 | // Stop at end of string. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 622 | s = s[c:] |
| 623 | if len(s) == 0 { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 624 | break |
| 625 | } |
| 626 | |
| 627 | // Otherwise must be followed by colon and more. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 628 | if s[0] != ':' || len(s) == 1 { |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 629 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 630 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 631 | s = s[1:] |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 632 | |
| 633 | // Look for ellipsis. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 634 | if s[0] == ':' { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 635 | if ellipsis >= 0 { // already have one |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 636 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 637 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 638 | ellipsis = i |
| 639 | s = s[1:] |
| 640 | if len(s) == 0 { // can be at end |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 641 | break |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // Must have used entire string. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 647 | if len(s) != 0 { |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 648 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | // If didn't parse enough, expand ellipsis. |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 652 | if i < IPv6len { |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 653 | if ellipsis < 0 { |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 654 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 655 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 656 | n := IPv6len - i |
| 657 | for j := i - 1; j >= ellipsis; j-- { |
| 658 | ip[j+n] = ip[j] |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 659 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 660 | for j := ellipsis + n - 1; j >= ellipsis; j-- { |
| 661 | ip[j] = 0 |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 662 | } |
| 663 | } else if ellipsis >= 0 { |
| 664 | // Ellipsis must represent at least one 0 group. |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 665 | return nil |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 666 | } |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 667 | return ip |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 670 | // ParseIP parses s as an IP address, returning the result. |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 671 | // The string s can be in dotted decimal ("192.0.2.1") |
| 672 | // or IPv6 ("2001:db8::68") form. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 673 | // If s is not a valid textual representation of an IP address, |
| 674 | // ParseIP returns nil. |
| 675 | func ParseIP(s string) IP { |
| 676 | for i := 0; i < len(s); i++ { |
| 677 | switch s[i] { |
| 678 | case '.': |
| 679 | return parseIPv4(s) |
| 680 | case ':': |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 681 | return parseIPv6(s) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | return nil |
| 685 | } |
| 686 | |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 687 | // parseIPZone parses s as an IP address, return it and its associated zone |
| 688 | // identifier (IPv6 only). |
| 689 | func parseIPZone(s string) (IP, string) { |
| 690 | for i := 0; i < len(s); i++ { |
| 691 | switch s[i] { |
| 692 | case '.': |
| 693 | return parseIPv4(s), "" |
| 694 | case ':': |
| 695 | return parseIPv6Zone(s) |
| 696 | } |
| 697 | } |
| 698 | return nil, "" |
| 699 | } |
| 700 | |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 701 | // ParseCIDR parses s as a CIDR notation IP address and prefix length, |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 702 | // like "192.0.2.0/24" or "2001:db8::/32", as defined in |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 703 | // RFC 4632 and RFC 4291. |
| 704 | // |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 705 | // It returns the IP address and the network implied by the IP and |
| 706 | // prefix length. |
| 707 | // For example, ParseCIDR("192.0.2.1/24") returns the IP address |
Dan Willemsen | c78f714 | 2017-07-26 13:08:14 -0700 | [diff] [blame] | 708 | // 192.0.2.1 and the network 192.0.2.0/24. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 709 | func ParseCIDR(s string) (IP, *IPNet, error) { |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame^] | 710 | i := bytealg.IndexByteString(s, '/') |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 711 | if i < 0 { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 712 | return nil, nil, &ParseError{Type: "CIDR address", Text: s} |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 713 | } |
| 714 | addr, mask := s[:i], s[i+1:] |
| 715 | iplen := IPv4len |
| 716 | ip := parseIPv4(addr) |
| 717 | if ip == nil { |
| 718 | iplen = IPv6len |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 719 | ip = parseIPv6(addr) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 720 | } |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 721 | n, i, ok := dtoi(mask) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 722 | if ip == nil || !ok || i != len(mask) || n < 0 || n > 8*iplen { |
Dan Willemsen | 6ff2325 | 2015-09-15 13:49:18 -0700 | [diff] [blame] | 723 | return nil, nil, &ParseError{Type: "CIDR address", Text: s} |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 724 | } |
| 725 | m := CIDRMask(n, 8*iplen) |
| 726 | return ip, &IPNet{IP: ip.Mask(m), Mask: m}, nil |
| 727 | } |