Source file src/pkg/net/ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13 package net
14
15
16 const (
17 IPv4len = 4
18 IPv6len = 16
19 )
20
21
22
23
24
25
26
27
28
29
30 type IP []byte
31
32
33 type IPMask []byte
34
35
36 type IPNet struct {
37 IP IP
38 Mask IPMask
39 }
40
41
42
43 func IPv4(a, b, c, d byte) IP {
44 p := make(IP, IPv6len)
45 copy(p, v4InV6Prefix)
46 p[12] = a
47 p[13] = b
48 p[14] = c
49 p[15] = d
50 return p
51 }
52
53 var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
54
55
56
57 func IPv4Mask(a, b, c, d byte) IPMask {
58 p := make(IPMask, IPv4len)
59 p[0] = a
60 p[1] = b
61 p[2] = c
62 p[3] = d
63 return p
64 }
65
66
67
68
69 func CIDRMask(ones, bits int) IPMask {
70 if bits != 8*IPv4len && bits != 8*IPv6len {
71 return nil
72 }
73 if ones < 0 || ones > bits {
74 return nil
75 }
76 l := bits / 8
77 m := make(IPMask, l)
78 n := uint(ones)
79 for i := 0; i < l; i++ {
80 if n >= 8 {
81 m[i] = 0xff
82 n -= 8
83 continue
84 }
85 m[i] = ^byte(0xff >> n)
86 n = 0
87 }
88 return m
89 }
90
91
92 var (
93 IPv4bcast = IPv4(255, 255, 255, 255)
94 IPv4allsys = IPv4(224, 0, 0, 1)
95 IPv4allrouter = IPv4(224, 0, 0, 2)
96 IPv4zero = IPv4(0, 0, 0, 0)
97 )
98
99
100 var (
101 IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
102 IPv6unspecified = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
103 IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
104 IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
105 IPv6linklocalallnodes = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
106 IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}
107 )
108
109
110 func (ip IP) IsUnspecified() bool {
111 if ip.Equal(IPv4zero) || ip.Equal(IPv6unspecified) {
112 return true
113 }
114 return false
115 }
116
117
118 func (ip IP) IsLoopback() bool {
119 if ip4 := ip.To4(); ip4 != nil && ip4[0] == 127 {
120 return true
121 }
122 return ip.Equal(IPv6loopback)
123 }
124
125
126 func (ip IP) IsMulticast() bool {
127 if ip4 := ip.To4(); ip4 != nil && ip4[0]&0xf0 == 0xe0 {
128 return true
129 }
130 return ip[0] == 0xff
131 }
132
133
134
135 func (ip IP) IsInterfaceLocalMulticast() bool {
136 return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x01
137 }
138
139
140
141 func (ip IP) IsLinkLocalMulticast() bool {
142 if ip4 := ip.To4(); ip4 != nil && ip4[0] == 224 && ip4[1] == 0 && ip4[2] == 0 {
143 return true
144 }
145 return ip[0] == 0xff && ip[1]&0x0f == 0x02
146 }
147
148
149
150 func (ip IP) IsLinkLocalUnicast() bool {
151 if ip4 := ip.To4(); ip4 != nil && ip4[0] == 169 && ip4[1] == 254 {
152 return true
153 }
154 return ip[0] == 0xfe && ip[1]&0xc0 == 0x80
155 }
156
157
158
159 func (ip IP) IsGlobalUnicast() bool {
160 return !ip.IsUnspecified() &&
161 !ip.IsLoopback() &&
162 !ip.IsMulticast() &&
163 !ip.IsLinkLocalUnicast()
164 }
165
166
167 func isZeros(p IP) bool {
168 for i := 0; i < len(p); i++ {
169 if p[i] != 0 {
170 return false
171 }
172 }
173 return true
174 }
175
176
177
178 func (ip IP) To4() IP {
179 if len(ip) == IPv4len {
180 return ip
181 }
182 if len(ip) == IPv6len &&
183 isZeros(ip[0:10]) &&
184 ip[10] == 0xff &&
185 ip[11] == 0xff {
186 return ip[12:16]
187 }
188 return nil
189 }
190
191
192
193 func (ip IP) To16() IP {
194 if len(ip) == IPv4len {
195 return IPv4(ip[0], ip[1], ip[2], ip[3])
196 }
197 if len(ip) == IPv6len {
198 return ip
199 }
200 return nil
201 }
202
203
204 var (
205 classAMask = IPv4Mask(0xff, 0, 0, 0)
206 classBMask = IPv4Mask(0xff, 0xff, 0, 0)
207 classCMask = IPv4Mask(0xff, 0xff, 0xff, 0)
208 )
209
210
211
212
213 func (ip IP) DefaultMask() IPMask {
214 if ip = ip.To4(); ip == nil {
215 return nil
216 }
217 switch true {
218 case ip[0] < 0x80:
219 return classAMask
220 case ip[0] < 0xC0:
221 return classBMask
222 default:
223 return classCMask
224 }
225 }
226
227 func allFF(b []byte) bool {
228 for _, c := range b {
229 if c != 0xff {
230 return false
231 }
232 }
233 return true
234 }
235
236
237 func (ip IP) Mask(mask IPMask) IP {
238 if len(mask) == IPv6len && len(ip) == IPv4len && allFF(mask[:12]) {
239 mask = mask[12:]
240 }
241 if len(mask) == IPv4len && len(ip) == IPv6len && bytesEqual(ip[:12], v4InV6Prefix) {
242 ip = ip[12:]
243 }
244 n := len(ip)
245 if n != len(mask) {
246 return nil
247 }
248 out := make(IP, n)
249 for i := 0; i < n; i++ {
250 out[i] = ip[i] & mask[i]
251 }
252 return out
253 }
254
255
256
257
258
259 func (ip IP) String() string {
260 p := ip
261
262 if len(ip) == 0 {
263 return "<nil>"
264 }
265
266
267 if p4 := p.To4(); len(p4) == IPv4len {
268 return itod(uint(p4[0])) + "." +
269 itod(uint(p4[1])) + "." +
270 itod(uint(p4[2])) + "." +
271 itod(uint(p4[3]))
272 }
273 if len(p) != IPv6len {
274 return "?"
275 }
276
277
278 e0 := -1
279 e1 := -1
280 for i := 0; i < IPv6len; i += 2 {
281 j := i
282 for j < IPv6len && p[j] == 0 && p[j+1] == 0 {
283 j += 2
284 }
285 if j > i && j-i > e1-e0 {
286 e0 = i
287 e1 = j
288 }
289 }
290
291 if e1-e0 <= 2 {
292 e0 = -1
293 e1 = -1
294 }
295
296
297 var s string
298 for i := 0; i < IPv6len; i += 2 {
299 if i == e0 {
300 s += "::"
301 i = e1
302 if i >= IPv6len {
303 break
304 }
305 } else if i > 0 {
306 s += ":"
307 }
308 s += itox((uint(p[i])<<8)|uint(p[i+1]), 1)
309 }
310 return s
311 }
312
313
314
315
316 func (ip IP) Equal(x IP) bool {
317 if len(ip) == len(x) {
318 return bytesEqual(ip, x)
319 }
320 if len(ip) == IPv4len && len(x) == IPv6len {
321 return bytesEqual(x[0:12], v4InV6Prefix) && bytesEqual(ip, x[12:])
322 }
323 if len(ip) == IPv6len && len(x) == IPv4len {
324 return bytesEqual(ip[0:12], v4InV6Prefix) && bytesEqual(ip[12:], x)
325 }
326 return false
327 }
328
329 func bytesEqual(x, y []byte) bool {
330 if len(x) != len(y) {
331 return false
332 }
333 for i, b := range x {
334 if y[i] != b {
335 return false
336 }
337 }
338 return true
339 }
340
341
342
343 func simpleMaskLength(mask IPMask) int {
344 var n int
345 for i, v := range mask {
346 if v == 0xff {
347 n += 8
348 continue
349 }
350
351
352 for v&0x80 != 0 {
353 n++
354 v <<= 1
355 }
356
357 if v != 0 {
358 return -1
359 }
360 for i++; i < len(mask); i++ {
361 if mask[i] != 0 {
362 return -1
363 }
364 }
365 break
366 }
367 return n
368 }
369
370
371
372
373 func (m IPMask) Size() (ones, bits int) {
374 ones, bits = simpleMaskLength(m), len(m)*8
375 if ones == -1 {
376 return 0, 0
377 }
378 return
379 }
380
381
382 func (m IPMask) String() string {
383 s := ""
384 for _, b := range m {
385 s += itox(uint(b), 2)
386 }
387 if len(s) == 0 {
388 return "<nil>"
389 }
390 return s
391 }
392
393 func networkNumberAndMask(n *IPNet) (ip IP, m IPMask) {
394 if ip = n.IP.To4(); ip == nil {
395 ip = n.IP
396 if len(ip) != IPv6len {
397 return nil, nil
398 }
399 }
400 m = n.Mask
401 switch len(m) {
402 case IPv4len:
403 if len(ip) != IPv4len {
404 return nil, nil
405 }
406 case IPv6len:
407 if len(ip) == IPv4len {
408 m = m[12:]
409 }
410 default:
411 return nil, nil
412 }
413 return
414 }
415
416
417 func (n *IPNet) Contains(ip IP) bool {
418 nn, m := networkNumberAndMask(n)
419 if x := ip.To4(); x != nil {
420 ip = x
421 }
422 l := len(ip)
423 if l != len(nn) {
424 return false
425 }
426 for i := 0; i < l; i++ {
427 if nn[i]&m[i] != ip[i]&m[i] {
428 return false
429 }
430 }
431 return true
432 }
433
434
435 func (n *IPNet) Network() string { return "ip+net" }
436
437
438
439
440
441
442
443 func (n *IPNet) String() string {
444 nn, m := networkNumberAndMask(n)
445 if nn == nil || m == nil {
446 return "<nil>"
447 }
448 l := simpleMaskLength(m)
449 if l == -1 {
450 return nn.String() + "/" + m.String()
451 }
452 return nn.String() + "/" + itod(uint(l))
453 }
454
455
456 func parseIPv4(s string) IP {
457 var p [IPv4len]byte
458 i := 0
459 for j := 0; j < IPv4len; j++ {
460 if i >= len(s) {
461
462 return nil
463 }
464 if j > 0 {
465 if s[i] != '.' {
466 return nil
467 }
468 i++
469 }
470 var (
471 n int
472 ok bool
473 )
474 n, i, ok = dtoi(s, i)
475 if !ok || n > 0xFF {
476 return nil
477 }
478 p[j] = byte(n)
479 }
480 if i != len(s) {
481 return nil
482 }
483 return IPv4(p[0], p[1], p[2], p[3])
484 }
485
486
487
488
489
490 func parseIPv6(s string, zoneAllowed bool) (ip IP, zone string) {
491 ip = make(IP, IPv6len)
492 ellipsis := -1
493 i := 0
494
495 if zoneAllowed {
496 s, zone = splitHostZone(s)
497 }
498
499
500 if len(s) >= 2 && s[0] == ':' && s[1] == ':' {
501 ellipsis = 0
502 i = 2
503
504 if i == len(s) {
505 return ip, zone
506 }
507 }
508
509
510 j := 0
511 for j < IPv6len {
512
513 n, i1, ok := xtoi(s, i)
514 if !ok || n > 0xFFFF {
515 return nil, zone
516 }
517
518
519 if i1 < len(s) && s[i1] == '.' {
520 if ellipsis < 0 && j != IPv6len-IPv4len {
521
522 return nil, zone
523 }
524 if j+IPv4len > IPv6len {
525
526 return nil, zone
527 }
528 ip4 := parseIPv4(s[i:])
529 if ip4 == nil {
530 return nil, zone
531 }
532 ip[j] = ip4[12]
533 ip[j+1] = ip4[13]
534 ip[j+2] = ip4[14]
535 ip[j+3] = ip4[15]
536 i = len(s)
537 j += IPv4len
538 break
539 }
540
541
542 ip[j] = byte(n >> 8)
543 ip[j+1] = byte(n)
544 j += 2
545
546
547 i = i1
548 if i == len(s) {
549 break
550 }
551
552
553 if s[i] != ':' || i+1 == len(s) {
554 return nil, zone
555 }
556 i++
557
558
559 if s[i] == ':' {
560 if ellipsis >= 0 {
561 return nil, zone
562 }
563 ellipsis = j
564 if i++; i == len(s) {
565 break
566 }
567 }
568 }
569
570
571 if i != len(s) {
572 return nil, zone
573 }
574
575
576 if j < IPv6len {
577 if ellipsis < 0 {
578 return nil, zone
579 }
580 n := IPv6len - j
581 for k := j - 1; k >= ellipsis; k-- {
582 ip[k+n] = ip[k]
583 }
584 for k := ellipsis + n - 1; k >= ellipsis; k-- {
585 ip[k] = 0
586 }
587 }
588 return ip, zone
589 }
590
591
592 type ParseError struct {
593 Type string
594 Text string
595 }
596
597 func (e *ParseError) Error() string {
598 return "invalid " + e.Type + ": " + e.Text
599 }
600
601
602
603
604
605
606 func ParseIP(s string) IP {
607 if ip := parseIPv4(s); ip != nil {
608 return ip
609 }
610 ip, _ := parseIPv6(s, false)
611 return ip
612 }
613
614
615
616
617
618
619
620
621 func ParseCIDR(s string) (IP, *IPNet, error) {
622 i := byteIndex(s, '/')
623 if i < 0 {
624 return nil, nil, &ParseError{"CIDR address", s}
625 }
626 addr, mask := s[:i], s[i+1:]
627 iplen := IPv4len
628 ip := parseIPv4(addr)
629 if ip == nil {
630 iplen = IPv6len
631 ip, _ = parseIPv6(addr, false)
632 }
633 n, i, ok := dtoi(mask, 0)
634 if ip == nil || !ok || i != len(mask) || n < 0 || n > 8*iplen {
635 return nil, nil, &ParseError{"CIDR address", s}
636 }
637 m := CIDRMask(n, 8*iplen)
638 return ip, &IPNet{IP: ip.Mask(m), Mask: m}, nil
639 }
View as plain text