-
Notifications
You must be signed in to change notification settings - Fork 6
/
ip.ex
532 lines (453 loc) · 13.7 KB
/
ip.ex
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
defmodule IP do
@moduledoc """
Elixir IP value tool suite.
Erlang provides a structured IP type, but the tooling around it is
very squirelly, spread out over several disjoint modules, and not
ergonomic with respect to structured programming.
Elixir gives us some better tools (like protocols and guards).
This module, and its related modules `IP.Range` and `IP.Subnet`
provide a better view into operator-friendly, composable programming
that interfaces well with erlang IP primitives.
"""
@typedoc "ipv4 representation in erlang."
@type v4 :: {byte, byte, byte, byte}
@typedoc "16-bit integers"
@type short :: 0x0..0xFFFF
@typedoc "ipv6 representation in erlang."
@type v6 :: {short, short, short, short, short, short, short, short}
@typedoc "any ip address, either v4 or v6"
@type addr :: v4 | v6
import Bitwise
####################################################################
## guards
@doc false
defguard is_byte(n) when is_integer(n) and n >= 0 and n <= 255
@doc false
defguard is_short(n) when is_integer(n) and n >= 0 and n <= 0xFFFF
@doc """
true if the argument is an ipv4 datatype
usable in guards.
```elixir
iex> IP.is_ipv4({10, 0, 0, 1})
true
iex> IP.is_ipv4(:foo)
false
iex> IP.is_ipv4({256, 0, 0, 0})
false
```
"""
defguard is_ipv4(v4) when is_tuple(v4) and tuple_size(v4) == 4 and
is_byte(elem(v4, 0)) and is_byte(elem(v4, 1)) and
is_byte(elem(v4, 2)) and is_byte(elem(v4, 3))
@doc """
true if the argument is an ipv6 datatype
usable in guards.
```elixir
iex> IP.is_ipv6({0, 0, 0, 0, 0, 0, 0, 1})
true
iex> IP.is_ipv6(:foo)
false
iex> IP.is_ipv6({0x10000, 0, 0, 0, 0, 0, 0, 1})
false
```
"""
defguard is_ipv6(v6) when is_tuple(v6) and tuple_size(v6) == 8 and
is_short(elem(v6, 0)) and is_short(elem(v6, 1)) and
is_short(elem(v6, 2)) and is_short(elem(v6, 3)) and
is_short(elem(v6, 4)) and is_short(elem(v6, 5)) and
is_short(elem(v6, 6)) and is_short(elem(v6, 7))
@doc """
true if the argument is either ipv6 or ipv4 datatype
usable in guards.
```elixir
iex> IP.is_ip({0, 0, 0, 0, 0, 0, 0, 1})
true
iex> IP.is_ip({127, 0, 0, 1})
true
```
"""
defguard is_ip(ip) when is_ipv4(ip) or is_ipv6(ip)
####################################################################
## conversions
@doc """
Converts an ip address to a string.
For some situations, like converting an ip address to a hostname
you might want hyphens as delimiters instead, in which case you
should pass :hyphens as the `:style` term.
Also takes, nil, in which case it emits an empty string.
```elixir
iex> IP.to_string({255, 255, 255, 255})
"255.255.255.255"
iex> IP.to_string({255, 255, 255, 255}, :hyphens)
"255-255-255-255"
iex> IP.to_string({0, 0, 0, 0, 0, 0, 0, 1})
"::1"
```
"""
@spec to_string(addr | nil, :dots | :hyphens) :: String.t
def to_string(ip_addr, style \\ :dots)
def to_string(nil, _), do: ""
def to_string({a, b, c, d}, :dots) do
"#{a}.#{b}.#{c}.#{d}"
end
def to_string({a, b, c, d}, :hyphens) do
"#{a}-#{b}-#{c}-#{d}"
end
def to_string(v6, _) when is_ipv6(v6) do
# cheat by using the erlang builtin function.
v6
|> :inet.ntoa
|> List.to_string
end
@doc """
Converts an ip address from a string.
```elixir
iex> IP.from_string!("255.255.255.255")
{255, 255, 255, 255}
```
"""
@spec from_string!(String.t) :: addr
def from_string!(str) do
case from_string(str) do
{:ok, v} -> v
_ -> raise ArgumentError, "malformed ip address string #{str}"
end
end
@doc """
Finds an ip address in a string, returning an ok or error tuple on failure.
"""
@spec from_string(String.t) :: {:ok, addr} | {:error, :einval}
def from_string(str) do
str
|> String.to_charlist
|> :inet.parse_address()
end
@spec next(v4) :: v4
@spec next(v6) :: v6
@doc """
returns the next ip address.
```elixir
iex> IP.next({10, 0, 0, 255})
{10, 0, 1, 0}
iex> IP.next({0, 0, 0, 0, 0, 0, 0, 0xFFFF})
{0, 0, 0, 0, 0, 0, 1, 0}
```
"""
def next(ip) when is_ipv4(ip), do: from_integer(to_integer(ip) + 1, :v4)
def next(ip) when is_ipv6(ip), do: from_integer(to_integer(ip) + 1, :v6)
@spec prev(v4) :: v4
@spec prev(v6) :: v6
@doc """
returns the previous ip address.
```elixir
iex> IP.prev({10, 0, 1, 0})
{10, 0, 0, 255}
iex> IP.prev({0, 0, 0, 0, 0, 0, 1, 0})
{0, 0, 0, 0, 0, 0, 0, 0xFFFF}
```
"""
def prev(ip) when is_ipv4(ip), do: from_integer(to_integer(ip) - 1, :v4)
def prev(ip) when is_ipv6(ip), do: from_integer(to_integer(ip) - 1, :v6)
@spec mask(0..32, :v4_int) :: 0..0xFFFF_FFFF
@spec mask(0..32, :v4_bin) :: <<_::32>>
@spec mask(0..32, :v4) :: v4
@spec mask(0..32, :v6_int) :: 0..0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF
@spec mask(0..32, :v6_bin) :: <<_::128>>
@spec mask(0..32, :v6) :: v6
@doc """
generates an ip mask with specified bit_length.
Can also generate raw integer or raw binary forms.
Defaults to IPv4.
```elixir
iex> IP.mask(32, :v4_int)
0xFFFF_FFFF
iex> IP.mask(32, :v4_bin)
<<255, 255, 255, 255>>
iex> IP.mask(32, :v4)
{255, 255, 255, 255}
iex> IP.mask(24, :v4)
{255, 255, 255, 0}
iex> IP.mask(0)
{0, 0, 0, 0}
iex> IP.mask(128, :v6)
{0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}
iex> IP.mask(0, :v6)
{0, 0, 0, 0, 0, 0, 0, 0}
"""
def mask(bit_length, mode \\ :v4)
def mask(0, :v4_int), do: 0
def mask(bit_length, :v4_int) do
<<i::unsigned-integer-size(32)>> = <<(-1 <<< (32-bit_length))::32>>
i
end
def mask(bit_length, :v4_bin) do
<<mask(bit_length, :v4_int)::unsigned-integer-size(32)>>
end
def mask(bit_length, :v4) do
bit_length
|> mask(:v4_int)
|> from_integer(:v4)
end
def mask(0, :v6_int), do: 0
def mask(bit_length, :v6_int) do
<<i::unsigned-integer-size(128)>> = <<(-1 <<< (128-bit_length))::128>>
i
end
def mask(bit_length, :v6) do
bit_length
|> mask(:v6_int)
|> from_integer(:v6)
end
@spec prefix(v4, 0..32) :: v4
@spec prefix(v6, 0..128) :: v6
@doc """
generates an the subnet prefix for a given ip address.
```elixir
iex> IP.prefix({10, 0, 1, 23}, 24)
{10, 0, 1, 0}
```
"""
def prefix(ip, bit_length) when is_ipv4(ip) do
ip
|> to_integer
|> Bitwise.&&&(mask(bit_length, :v4_int))
|> from_integer(:v4)
end
def prefix(ip, bit_length) when is_ipv6(ip) do
ip
|> to_integer
|> Bitwise.&&&(mask(bit_length, :v6_int))
|> from_integer(:v6)
end
@spec localhost(:v4) :: v4
@spec localhost(:v6) :: v6
@doc """
returns the canonical ip address of localhost. Defaults to ipv4.
```elixir
iex> IP.localhost()
{127, 0, 0, 1}
iex> IP.localhost(:v6)
{0, 0, 0, 0, 0, 0, 0, 1}
```
"""
def localhost(mode \\ :v4)
def localhost(:v4), do: {127, 0, 0, 1}
def localhost(:v6), do: {0, 0, 0, 0, 0, 0, 0, 1}
####################################################################
## random
@doc """
picks a random ip address from a range or a subnet.
You may exclude individual ip addresses, ranges, or subnets to the
`excludes` parameter and they won't be picked.
Note that subnets will pick the prefix and broadcast addresses, if
you would like to exclude those, you must add them explicitly to
the `excludes` parameter.
Warning: this algorithm is rather bad, so use only with blocks of
less than about 1024.
"""
def random(range_or_subnet, excludes \\ [])
def random(range_or_subnet, excludes) do
range_or_subnet
|> Enum.map(&(&1))
|> Kernel.--(expand(excludes))
|> Enum.random
end
defp expand(excludes) do
Enum.flat_map(excludes, fn
exclude when is_ip(exclude) -> [exclude]
exclude = %type{} when type in [IP.Range, IP.Subnet]->
Enum.map(exclude, &(&1))
end)
end
####################################################################
## ETC
@spec sigil_i(Macro.t, [byte]) :: Macro.t
@doc """
allows you to use the convenient ~i sigil to declare an IP address
in its normal string form, instead of using the tuple form.
```elixir
iex> import IP
iex> ~i"10.0.0.1"
{10, 0, 0, 1}
```
also works for cidr-form subnet blocks
```elixir
iex> import IP
iex> ~i"10.0.0.0/24"
%IP.Subnet{routing_prefix: {10, 0, 0, 0}, bit_length: 24}
```
and generic IP ranges
```
iex> import IP
iex> ~i"10.0.0.3..10.0.0.7"
%IP.Range{first: {10, 0, 0, 3}, last: {10, 0, 0, 7}}
```
and socket addresses:
```
iex> import IP
iex> ~i"10.0.0.1:1234"
%IP.SockAddr{family: :inet, port: 1234, addr: {10, 0, 0, 1}}
```
and ip/subnet combinations for configuration:
```
iex> import IP
iex> ~i"10.0.0.4/24"config
{{10, 0, 0, 4}, %IP.Subnet{routing_prefix: {10, 0, 0, 0}, bit_length: 24}}
```
You can also use `~i` for ip addresses and subnets with the `m` suffix
in the context of matches.
```
iex> import IP
iex> fn -> ~i"10.0.x.3"m = {10, 0, 1, 3}; x end.()
1
```
"""
defmacro sigil_i({:<<>>, meta, [definition]}, 'm') do
caller_meta = Keyword.merge([file: __CALLER__.file, line: __CALLER__.line], meta)
unless __CALLER__.context == :match do
s(caller_meta, "~s/#{definition}/m must be used inside of a match")
end
# perform matching
case String.split(definition, ".") do
ip = [_, _, _, _] ->
{:{}, meta, Enum.map(ip, &token_to_matchv(&1, caller_meta))}
_ ->
s(caller_meta, "invalid ip match #{definition}")
end
end
defmacro sigil_i({:<<>>, meta, [definition]}, 'config') do
caller_meta = Keyword.merge([file: __CALLER__.file, line: __CALLER__.line], meta)
case String.split(definition, "/") do
[ip_str, bit_size] ->
ip = IP.from_string!(ip_str)
subnet = IP.Subnet.of(ip, String.to_integer(bit_size))
{ip, subnet}
_ ->
s(caller_meta, "invalid configuration definition #{definition}")
end
|> Macro.escape()
end
defmacro sigil_i({:<<>>, _meta, [definition]}, []) do
# check to see if it has a slash, in which case it's an ip range
cond do
String.contains?(definition, "/") ->
IP.Subnet.from_string!(definition)
String.contains?(definition, "..") ->
IP.Range.from_string!(definition)
# only one colon is the sign of an ipv4 socket address
match?([_ , _], String.split(definition, ":")) ->
IP.SockAddr.from_string!(definition)
true ->
IP.from_string!(definition)
end
|> Macro.escape()
end
defp s(caller, msg), do: raise(SyntaxError, caller ++ [description: msg])
@ctx [context: Elixir, import: Kernel]
defp token_to_matchv(<<x>> <> _ = int_str, meta) when x in ?0..?9 do
int_val = String.to_integer(int_str)
unless int_val in 0..255 do
s(meta, "#{int_val} is out of the range for ipv4 addresses")
end
int_val
end
defp token_to_matchv(<<?^>> <> var, meta) do
{:^, meta, [token_to_matchv(var, meta)]}
end
defp token_to_matchv(var, meta) do
{:var!, @ctx, [{String.to_atom(var), meta, Elixir}]}
end
####################################################################
## PRIVATE API
# to_integer and from_integer are strictly private calls, only to be
# called from within the IP.* family of modules
@doc false
@spec to_integer(addr) :: integer
def to_integer(ip = {a, b, c, d}) when is_ipv4(ip) do
<<i::unsigned-integer-size(32)>> = <<a, b, c, d>>
i
end
def to_integer(ip = {a, b, c, d, e, f, g, h}) when is_ipv6(ip) do
<<i::unsigned-integer-size(128)>> =
<<a::unsigned-integer-size(16),
b::unsigned-integer-size(16),
c::unsigned-integer-size(16),
d::unsigned-integer-size(16),
e::unsigned-integer-size(16),
f::unsigned-integer-size(16),
g::unsigned-integer-size(16),
h::unsigned-integer-size(16)>>
i
end
@doc false
@spec from_integer(integer, :v4) :: v4
@spec from_integer(integer, :v6) :: v6
def from_integer(i, :v4) when is_integer(i) and i >= 0 and i <= 0xFFFF_FFFF do
<<a, b, c, d>> = <<i::unsigned-integer-size(32)>>
{a, b, c, d}
end
def from_integer(i, :v6) when is_integer(i) do
<<a::unsigned-integer-size(16),
b::unsigned-integer-size(16),
c::unsigned-integer-size(16),
d::unsigned-integer-size(16),
e::unsigned-integer-size(16),
f::unsigned-integer-size(16),
g::unsigned-integer-size(16),
h::unsigned-integer-size(16)>> = <<i::128>>
{a, b, c, d, e, f, g, h}
end
@doc false
@spec type(v4) :: :v4
@spec type(v6) :: :v6
def type(addr) when is_ipv4(addr), do: :v4
def type(addr) when is_ipv6(addr), do: :v6
# useful for some guards
@doc false
defmacro octet_4(ip) do
quote do elem(unquote(ip), 3) end
end
@doc false
defmacro octet_34(ip) do
quote do
Bitwise.<<<(elem(unquote(ip), 2), 8) +
elem(unquote(ip), 3)
end
end
@doc false
defmacro octet_24(ip) do
quote do
Bitwise.<<<(elem(unquote(ip), 1), 16) +
Bitwise.<<<(elem(unquote(ip), 2), 8) +
elem(unquote(ip), 3)
end
end
@doc false
defmacro octet_14(ip) do
quote do
Bitwise.<<<(elem(unquote(ip), 0), 24) +
Bitwise.<<<(elem(unquote(ip), 1), 16) +
Bitwise.<<<(elem(unquote(ip), 2), 8) +
elem(unquote(ip), 3)
end
end
@doc false
defmacro octet_1(ip) do
quote do Bitwise.<<<(elem(unquote(ip), 0), 24) end
end
@doc false
defmacro octet_12(ip) do
quote do
Bitwise.<<<(elem(unquote(ip), 0), 24) +
Bitwise.<<<(elem(unquote(ip), 1), 16)
end
end
@doc false
defmacro octet_13(ip) do
quote do
Bitwise.<<<(elem(unquote(ip), 0), 24) +
Bitwise.<<<(elem(unquote(ip), 1), 16) +
Bitwise.<<<(elem(unquote(ip), 2), 8)
end
end
end