Debian Codegraph @

ipparse.go

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