| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "net/netip" |
| 7 | |
| 8 | "github.com/gofiber/utils/v2/internal/unsafeconv" |
| 9 | ) |
| 10 | |
| 11 | // ParseIPv4 parses a dotted-decimal IPv4 address into a netip.Addr. It |
| 12 | // accepts exactly the IPv4 strings netip.ParseAddr does — four octets 0-255, |
| 13 | // no leading zeros, nothing before or after — and reports ok=false for |
| 14 | // everything else, including IPv6 forms. It never allocates, so []byte |
| 15 | // callers skip both the string conversion and netip's error construction. |
| 16 | func ParseIPv4[S byteSeq](s S) (netip.Addr, bool) { |
| 17 | var b [4]byte |
| 18 | n := len(s) |
| 19 | i := 0 |
| 20 | for oct := range 4 { |
| 21 | if oct > 0 { |
| 22 | if i >= n || s[i] != '.' { |
| 23 | return netip.Addr{}, false |
| 24 | } |
| 25 | i++ |
| 26 | } |
| 27 | if i >= n { |
| 28 | return netip.Addr{}, false |
| 29 | } |
| 30 | c := s[i] - '0' |
| 31 | if c > 9 { |
| 32 | return netip.Addr{}, false |
| 33 | } |
| 34 | v := uint32(c) |
| 35 | i++ |
| 36 | digits := 1 |
| 37 | for i < n && digits < 3 { |
| 38 | c = s[i] - '0' |
| 39 | if c > 9 { |
| 40 | break |
| 41 | } |
| 42 | v = v*10 + uint32(c) |
| 43 | i++ |
| 44 | digits++ |
| 45 | } |
| 46 | // Reject values above 255 and, like netip, leading zeros. |
| 47 | if v > 255 || (digits > 1 && s[i-digits] == '0') { |
| 48 | return netip.Addr{}, false |
| 49 | } |
| 50 | b[oct] = byte(v) |
| 51 | } |
| 52 | if i != n { |
| 53 | return netip.Addr{}, false |
| 54 | } |
| 55 | return netip.AddrFrom4(b), true |
| 56 | } |
| 57 | |
| 58 | // ParseIPv6 parses an IPv6 address into a netip.Addr. It accepts exactly |
| 59 | // the IPv6 strings netip.ParseAddr does — 16-bit hex fields, one optional |
| 60 | // "::", an optional embedded dotted-decimal IPv4 tail, and an optional |
| 61 | // non-empty "%zone" suffix — and reports ok=false for everything else, |
| 62 | // including plain IPv4 forms (use ParseIPv4 for those). The parse itself |
| 63 | // never allocates; only a present zone is materialized as a string because |
| 64 | // netip.Addr stores zones as strings. |
| 65 | func ParseIPv6[S byteSeq](s S) (netip.Addr, bool) { |
| 66 | n := len(s) |
| 67 | zone := bytes.IndexByte(unsafeconv.Bytes(s), '%') |
| 68 | end := n |
| 69 | if zone >= 0 { |
| 70 | if zone == n-1 { |
| 71 | // Empty zone. |
| 72 | return netip.Addr{}, false |
| 73 | } |
| 74 | end = zone |
| 75 | } |
| 76 | |
| 77 | var ip [16]byte |
| 78 | off := 0 |
| 79 | ellipsis := -1 |
| 80 | i := 0 |
| 81 | if end >= 2 && s[0] == ':' && s[1] == ':' { |
| 82 | ellipsis = 0 |
| 83 | i = 2 |
| 84 | } else if end > 0 && s[0] == ':' { |
| 85 | // A single leading colon starts no field. |
| 86 | return netip.Addr{}, false |
| 87 | } |
| 88 | |
| 89 | for off < 16 && i < end { |
| 90 | // Parse up to four hex digits; a fifth is an error. |
| 91 | fieldStart := i |
| 92 | val := uint32(0) |
| 93 | lim := min(i+4, end) |
| 94 | for i < lim { |
| 95 | d := unhexTable[s[i]] |
| 96 | if d == 0xFF { |
| 97 | break |
| 98 | } |
| 99 | val = val<<4 | uint32(d) |
| 100 | i++ |
| 101 | } |
| 102 | if i == lim && i < end && unhexTable[s[i]] != 0xFF { |
| 103 | return netip.Addr{}, false |
| 104 | } |
| 105 | if i < end && s[i] == '.' { |
| 106 | // Embedded IPv4 tail filling the final four bytes. |
| 107 | if off > 12 { |
| 108 | return netip.Addr{}, false |
| 109 | } |
| 110 | v4, ok := ParseIPv4(s[fieldStart:end]) |
| 111 | if !ok { |
| 112 | return netip.Addr{}, false |
| 113 | } |
| 114 | b4 := v4.As4() |
| 115 | copy(ip[off:], b4[:]) |
| 116 | off += 4 |
| 117 | i = end |
| 118 | break |
| 119 | } |
| 120 | if i == fieldStart { |
| 121 | // Every colon-separated field needs at least one digit. |
| 122 | return netip.Addr{}, false |
| 123 | } |
| 124 | // off is always even and < 16 here, so the two-byte store fits. |
| 125 | binary.BigEndian.PutUint16(ip[off:], uint16(val)) |
| 126 | off += 2 |
| 127 | if i == end { |
| 128 | break |
| 129 | } |
| 130 | if s[i] != ':' { |
| 131 | return netip.Addr{}, false |
| 132 | } |
| 133 | i++ |
| 134 | if i < end && s[i] == ':' { |
| 135 | if ellipsis >= 0 { |
| 136 | // At most one "::". |
| 137 | return netip.Addr{}, false |
| 138 | } |
| 139 | ellipsis = off |
| 140 | i++ |
| 141 | continue |
| 142 | } |
| 143 | if i == end { |
| 144 | // A trailing single colon is invalid. |
| 145 | return netip.Addr{}, false |
| 146 | } |
| 147 | } |
| 148 | if i != end { |
| 149 | return netip.Addr{}, false |
| 150 | } |
| 151 | if off < 16 { |
| 152 | if ellipsis < 0 { |
| 153 | return netip.Addr{}, false |
| 154 | } |
| 155 | // Expand "::": shift the fields after it to the end, zero the gap. |
| 156 | gap := 16 - off |
| 157 | for j := off - 1; j >= ellipsis; j-- { |
| 158 | ip[j+gap] = ip[j] |
| 159 | ip[j] = 0 |
| 160 | } |
| 161 | } else if ellipsis >= 0 { |
| 162 | // "::" must stand for at least one field of zeros. |
| 163 | return netip.Addr{}, false |
| 164 | } |
| 165 | |
| 166 | addr := netip.AddrFrom16(ip) |
| 167 | if zone >= 0 { |
| 168 | addr = addr.WithZone(string(s[zone+1:])) |
| 169 | } |
| 170 | return addr, true |
| 171 | } |