Commit e47c383
authored
⭐ ip.inRange (#5271)
After: #5265
This adds a new function to the IP type to check if it is in range of a subnet or two IP addresses.
This is how to use it with IPv4 subnets:
```coffee
> ip('192.2.3.4').inRange('192.2.3.0/24')
true
# We can also auto-detect the subnet if you don't specify it. For example, this is a class C address, so:
> ip('192.2.3.4').inRange('192.2.3.0')
true
> ip('192.2.3.4').inRange('192.1.1.0/24')
false
```
Here is how you use it with 2 IP addresses:
```coffee
> ip('192.2.3.4').inRange('192.2.3.1', '192.2.3.5')
true
> ip('192.2.3.4').inRange('192.2.3.5', '192.2.3.255')
false
> ip('2001:db8:3c4d:15::1a2f:1a2b').inRange('2001:db8:3c4d::', '2001:db8:3c4e::')
true
```
This PR also improves the auto-detection of netmasks. In IPv4 we can now auto-detect class A, B, and C networks and is able to print the subnet mask via the `subnet` field:
```coffee
> ip('1.2.3.4').subnet
"255.0.0.0"
> ip('172.0.0.1').subnet
"255.255.0.0"
> ip('192.168.0.1').subnet
"255.255.255.0"
```
For IPv6 the `subnet` works differently. From my current understanding it is the subnet-ID that most people care about. The concept of subnet masks doesn't exist here [[1](https://en.wikipedia.org/wiki/IP_address#Subnetworks)]. The subnet is the part of the 64-bit network prefix that is not used by the routing prefix. For example:
```coffee
# 64-bit, default IPv6 prefix:
> ip("2001:db8:3c4d:15::1a2f:1a2b").prefixLength
64
> ip("2001:db8:3c4d:15::1a2f:1a2b").prefix
"2001:db8:3c4d:15::"
> ip("2001:db8:3c4d:15::1a2f:1a2b").subnet
""
# 48-bit prefix:
> ip("2001:db8:3c4d:15::1a2f:1a2b/48").prefixLength
48
> ip("2001:db8:3c4d:15::1a2f:1a2b/48").prefix
"2001:db8:3c4d::"
> ip("2001:db8:3c4d:15::1a2f:1a2b/48").subnet
"15"
```
As you can see, the `prefix` remains in full IPv6 notation, even if the lower bits are appreviated with `"::"`. The subnet, however, is only the identifier since it is not commonly represented as a full IPv6 address, unlike the prefix and host.
Other small fixes:
- compare IP, string and dict types (e.g. `ip("1.2.3.4") == "1.2.3.4"`)
- support the subnet mask of `"/0"` (it was trying to detect the default subnet before, even if users specified that they explicitly wanted this subnet)1 parent 1943c3d commit e47c383
File tree
6 files changed
+231
-14
lines changed- llx
- mqlc
- providers/core/resources
6 files changed
+231
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
| 272 | + | |
| 273 | + | |
272 | 274 | | |
273 | 275 | | |
274 | 276 | | |
| |||
463 | 465 | | |
464 | 466 | | |
465 | 467 | | |
| 468 | + | |
| 469 | + | |
466 | 470 | | |
467 | 471 | | |
468 | 472 | | |
| |||
591 | 595 | | |
592 | 596 | | |
593 | 597 | | |
| 598 | + | |
594 | 599 | | |
595 | 600 | | |
596 | 601 | | |
597 | 602 | | |
598 | 603 | | |
599 | 604 | | |
| 605 | + | |
600 | 606 | | |
601 | 607 | | |
602 | 608 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
41 | 46 | | |
42 | 47 | | |
43 | | - | |
| 48 | + | |
44 | 49 | | |
45 | 50 | | |
46 | 51 | | |
| |||
54 | 59 | | |
55 | 60 | | |
56 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
57 | 83 | | |
58 | 84 | | |
59 | 85 | | |
| |||
117 | 143 | | |
118 | 144 | | |
119 | 145 | | |
120 | | - | |
| 146 | + | |
121 | 147 | | |
122 | 148 | | |
123 | 149 | | |
| |||
150 | 176 | | |
151 | 177 | | |
152 | 178 | | |
153 | | - | |
| 179 | + | |
154 | 180 | | |
155 | 181 | | |
156 | 182 | | |
157 | 183 | | |
158 | 184 | | |
159 | 185 | | |
160 | 186 | | |
161 | | - | |
| 187 | + | |
162 | 188 | | |
163 | 189 | | |
164 | | - | |
165 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
166 | 195 | | |
167 | 196 | | |
168 | 197 | | |
| |||
172 | 201 | | |
173 | 202 | | |
174 | 203 | | |
175 | | - | |
| 204 | + | |
176 | 205 | | |
177 | 206 | | |
178 | 207 | | |
| |||
191 | 220 | | |
192 | 221 | | |
193 | 222 | | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
194 | 250 | | |
195 | 251 | | |
196 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
197 | 263 | | |
198 | 264 | | |
199 | 265 | | |
| |||
212 | 278 | | |
213 | 279 | | |
214 | 280 | | |
215 | | - | |
| 281 | + | |
216 | 282 | | |
217 | 283 | | |
218 | 284 | | |
| |||
221 | 287 | | |
222 | 288 | | |
223 | 289 | | |
224 | | - | |
| 290 | + | |
225 | 291 | | |
226 | 292 | | |
227 | 293 | | |
| |||
239 | 305 | | |
240 | 306 | | |
241 | 307 | | |
242 | | - | |
| 308 | + | |
243 | 309 | | |
244 | 310 | | |
245 | 311 | | |
| |||
250 | 316 | | |
251 | 317 | | |
252 | 318 | | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
248 | | - | |
| 248 | + | |
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
252 | | - | |
| 252 | + | |
253 | 253 | | |
254 | 254 | | |
255 | 255 | | |
| |||
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
0 commit comments