slow_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2020 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. package netip_test
  5. import (
  6. "fmt"
  7. . "net/netip"
  8. "strconv"
  9. "strings"
  10. )
  11. // zeros is a slice of eight stringified zeros. It's used in
  12. // parseIPSlow to construct slices of specific amounts of zero fields,
  13. // from 1 to 8.
  14. var zeros = []string{"0", "0", "0", "0", "0", "0", "0", "0"}
  15. // parseIPSlow is like ParseIP, but aims for readability above
  16. // speed. It's the reference implementation for correctness checking
  17. // and against which we measure optimized parsers.
  18. //
  19. // parseIPSlow understands the following forms of IP addresses:
  20. // - Regular IPv4: 1.2.3.4
  21. // - IPv4 with many leading zeros: 0000001.0000002.0000003.0000004
  22. // - Regular IPv6: 1111:2222:3333:4444:5555:6666:7777:8888
  23. // - IPv6 with many leading zeros: 00000001:0000002:0000003:0000004:0000005:0000006:0000007:0000008
  24. // - IPv6 with zero blocks elided: 1111:2222::7777:8888
  25. // - IPv6 with trailing 32 bits expressed as IPv4: 1111:2222:3333:4444:5555:6666:77.77.88.88
  26. //
  27. // It does not process the following IP address forms, which have been
  28. // varyingly accepted by some programs due to an under-specification
  29. // of the shapes of IPv4 addresses:
  30. //
  31. // - IPv4 as a single 32-bit uint: 4660 (same as "1.2.3.4")
  32. // - IPv4 with octal numbers: 0300.0250.0.01 (same as "192.168.0.1")
  33. // - IPv4 with hex numbers: 0xc0.0xa8.0x0.0x1 (same as "192.168.0.1")
  34. // - IPv4 in "class-B style": 1.2.52 (same as "1.2.3.4")
  35. // - IPv4 in "class-A style": 1.564 (same as "1.2.3.4")
  36. func parseIPSlow(s string) (Addr, error) {
  37. // Identify and strip out the zone, if any. There should be 0 or 1
  38. // '%' in the string.
  39. var zone string
  40. fs := strings.Split(s, "%")
  41. switch len(fs) {
  42. case 1:
  43. // No zone, that's fine.
  44. case 2:
  45. s, zone = fs[0], fs[1]
  46. if zone == "" {
  47. return Addr{}, fmt.Errorf("netaddr.ParseIP(%q): no zone after zone specifier", s)
  48. }
  49. default:
  50. return Addr{}, fmt.Errorf("netaddr.ParseIP(%q): too many zone specifiers", s) // TODO: less specific?
  51. }
  52. // IPv4 by itself is easy to do in a helper.
  53. if strings.Count(s, ":") == 0 {
  54. if zone != "" {
  55. return Addr{}, fmt.Errorf("netaddr.ParseIP(%q): IPv4 addresses cannot have a zone", s)
  56. }
  57. return parseIPv4Slow(s)
  58. }
  59. normal, err := normalizeIPv6Slow(s)
  60. if err != nil {
  61. return Addr{}, err
  62. }
  63. // At this point, we've normalized the address back into 8 hex
  64. // fields of 16 bits each. Parse that.
  65. fs = strings.Split(normal, ":")
  66. if len(fs) != 8 {
  67. return Addr{}, fmt.Errorf("netaddr.ParseIP(%q): wrong size address", s)
  68. }
  69. var ret [16]byte
  70. for i, f := range fs {
  71. a, b, err := parseWord(f)
  72. if err != nil {
  73. return Addr{}, err
  74. }
  75. ret[i*2] = a
  76. ret[i*2+1] = b
  77. }
  78. return AddrFrom16(ret).WithZone(zone), nil
  79. }
  80. // normalizeIPv6Slow expands s, which is assumed to be an IPv6
  81. // address, to its canonical text form.
  82. //
  83. // The canonical form of an IPv6 address is 8 colon-separated fields,
  84. // where each field should be a hex value from 0 to ffff. This
  85. // function does not verify the contents of each field.
  86. //
  87. // This function performs two transformations:
  88. // - The last 32 bits of an IPv6 address may be represented in
  89. // IPv4-style dotted quad form, as in 1:2:3:4:5:6:7.8.9.10. That
  90. // address is transformed to its hex equivalent,
  91. // e.g. 1:2:3:4:5:6:708:90a.
  92. // - An address may contain one "::", which expands into as many
  93. // 16-bit blocks of zeros as needed to make the address its correct
  94. // full size. For example, fe80::1:2 expands to fe80:0:0:0:0:0:1:2.
  95. //
  96. // Both short forms may be present in a single address,
  97. // e.g. fe80::1.2.3.4.
  98. func normalizeIPv6Slow(orig string) (string, error) {
  99. s := orig
  100. // Find and convert an IPv4 address in the final field, if any.
  101. i := strings.LastIndex(s, ":")
  102. if i == -1 {
  103. return "", fmt.Errorf("netaddr.ParseIP(%q): invalid IP address", orig)
  104. }
  105. if strings.Contains(s[i+1:], ".") {
  106. ip, err := parseIPv4Slow(s[i+1:])
  107. if err != nil {
  108. return "", err
  109. }
  110. a4 := ip.As4()
  111. s = fmt.Sprintf("%s:%02x%02x:%02x%02x", s[:i], a4[0], a4[1], a4[2], a4[3])
  112. }
  113. // Find and expand a ::, if any.
  114. fs := strings.Split(s, "::")
  115. switch len(fs) {
  116. case 1:
  117. // No ::, nothing to do.
  118. case 2:
  119. lhs, rhs := fs[0], fs[1]
  120. // Found a ::, figure out how many zero blocks need to be
  121. // inserted.
  122. nblocks := strings.Count(lhs, ":") + strings.Count(rhs, ":")
  123. if lhs != "" {
  124. nblocks++
  125. }
  126. if rhs != "" {
  127. nblocks++
  128. }
  129. if nblocks > 7 {
  130. return "", fmt.Errorf("netaddr.ParseIP(%q): address too long", orig)
  131. }
  132. fs = nil
  133. // Either side of the :: can be empty. We don't want empty
  134. // fields to feature in the final normalized address.
  135. if lhs != "" {
  136. fs = append(fs, lhs)
  137. }
  138. fs = append(fs, zeros[:8-nblocks]...)
  139. if rhs != "" {
  140. fs = append(fs, rhs)
  141. }
  142. s = strings.Join(fs, ":")
  143. default:
  144. // Too many ::
  145. return "", fmt.Errorf("netaddr.ParseIP(%q): invalid IP address", orig)
  146. }
  147. return s, nil
  148. }
  149. // parseIPv4Slow parses and returns an IPv4 address in dotted quad
  150. // form, e.g. "192.168.0.1". It is slow but easy to read, and the
  151. // reference implementation against which we compare faster
  152. // implementations for correctness.
  153. func parseIPv4Slow(s string) (Addr, error) {
  154. fs := strings.Split(s, ".")
  155. if len(fs) != 4 {
  156. return Addr{}, fmt.Errorf("netaddr.ParseIP(%q): invalid IP address", s)
  157. }
  158. var ret [4]byte
  159. for i := range ret {
  160. val, err := strconv.ParseUint(fs[i], 10, 8)
  161. if err != nil {
  162. return Addr{}, err
  163. }
  164. ret[i] = uint8(val)
  165. }
  166. return AddrFrom4([4]byte{ret[0], ret[1], ret[2], ret[3]}), nil
  167. }
  168. // parseWord converts a 16-bit hex string into its corresponding
  169. // two-byte value.
  170. func parseWord(s string) (byte, byte, error) {
  171. ret, err := strconv.ParseUint(s, 16, 16)
  172. if err != nil {
  173. return 0, 0, err
  174. }
  175. return uint8(ret >> 8), uint8(ret), nil
  176. }