| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | ) |
| 6 | |
| 7 | var hexTable = [256]byte{ |
| 8 | '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, |
| 9 | 'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15, |
| 10 | 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, |
| 11 | } |
| 12 | |
| 13 | // IsIPv4 works the same way as net.ParseIP, |
| 14 | // but without check for IPv6 case and without returning net.IP slice, whereby IsIPv4 makes no allocations. |
| 15 | func IsIPv4(s string) bool { |
| 16 | for i := range net.IPv4len { |
| 17 | if len(s) == 0 { |
| 18 | return false |
| 19 | } |
| 20 | |
| 21 | if i > 0 { |
| 22 | if s[0] != '.' { |
| 23 | return false |
| 24 | } |
| 25 | s = s[1:] |
| 26 | } |
| 27 | |
| 28 | n, ci := 0, 0 |
| 29 | |
| 30 | for ci = 0; ci < len(s) && '0' <= s[ci] && s[ci] <= '9'; ci++ { |
| 31 | n = n*10 + int(s[ci]-'0') |
| 32 | if n > 0xFF { |
| 33 | return false |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | if ci == 0 || (ci > 1 && s[0] == '0') { |
| 38 | return false |
| 39 | } |
| 40 | |
| 41 | s = s[ci:] |
| 42 | } |
| 43 | |
| 44 | return len(s) == 0 |
| 45 | } |
| 46 | |
| 47 | // IsIPv6 works the same way as net.ParseIP, |
| 48 | // but without check for IPv4 case and without returning net.IP slice, whereby IsIPv6 makes no allocations. |
| 49 | func IsIPv6(s string) bool { |
| 50 | ellipsis := -1 // position of ellipsis in ip |
| 51 | |
| 52 | // Might have leading ellipsis |
| 53 | if len(s) >= 2 && s[0] == ':' && s[1] == ':' { |
| 54 | ellipsis = 0 |
| 55 | s = s[2:] |
| 56 | // Might be only ellipsis |
| 57 | if len(s) == 0 { |
| 58 | return true |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Loop, parsing hex numbers followed by colon. |
| 63 | i := 0 |
| 64 | for i < net.IPv6len { |
| 65 | // Hex number. |
| 66 | n, ci := 0, 0 |
| 67 | |
| 68 | for ci = 0; ci < len(s); ci++ { |
| 69 | if (s[ci] < '0' || s[ci] > '9') && (s[ci] < 'a' || s[ci] > 'f') && (s[ci] < 'A' || s[ci] > 'F') { |
| 70 | break |
| 71 | } |
| 72 | n *= 16 |
| 73 | n += int(hexTable[s[ci]]) |
| 74 | |
| 75 | if n > 0xFFFF { |
| 76 | return false |
| 77 | } |
| 78 | } |
| 79 | if ci == 0 || n > 0xFFFF { |
| 80 | return false |
| 81 | } |
| 82 | |
| 83 | if ci < len(s) && s[ci] == '.' { |
| 84 | if ellipsis < 0 && i != net.IPv6len-net.IPv4len { |
| 85 | return false |
| 86 | } |
| 87 | if i+net.IPv4len > net.IPv6len { |
| 88 | return false |
| 89 | } |
| 90 | |
| 91 | if !IsIPv4(s) { |
| 92 | return false |
| 93 | } |
| 94 | |
| 95 | s = "" |
| 96 | i += net.IPv4len |
| 97 | break |
| 98 | } |
| 99 | |
| 100 | // Save this 16-bit chunk. |
| 101 | i += 2 |
| 102 | |
| 103 | // Stop at end of string. |
| 104 | s = s[ci:] |
| 105 | if len(s) == 0 { |
| 106 | break |
| 107 | } |
| 108 | |
| 109 | // Otherwise must be followed by colon and more. |
| 110 | if s[0] != ':' || len(s) == 1 { |
| 111 | return false |
| 112 | } |
| 113 | s = s[1:] |
| 114 | |
| 115 | // Look for ellipsis. |
| 116 | if s[0] == ':' { |
| 117 | if ellipsis >= 0 { // already have one |
| 118 | return false |
| 119 | } |
| 120 | ellipsis = i |
| 121 | s = s[1:] |
| 122 | if len(s) == 0 { // can be at end |
| 123 | break |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Must have used entire string. |
| 129 | if len(s) != 0 { |
| 130 | return false |
| 131 | } |
| 132 | |
| 133 | // If didn't parse enough, expand ellipsis. |
| 134 | if i < net.IPv6len { |
| 135 | if ellipsis < 0 { |
| 136 | return false |
| 137 | } |
| 138 | } else if ellipsis >= 0 { |
| 139 | // Ellipsis must represent at least one 0 group. |
| 140 | return false |
| 141 | } |
| 142 | return true |
| 143 | } |