-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddr.ml
55 lines (46 loc) · 1.3 KB
/
addr.ml
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
module V4 = struct
let next i = Ipaddr.V4.of_int32 (Int32.add (Ipaddr.V4.to_int32 i) 1l)
let init = Ipaddr.V4.Prefix.network
end
module V6 = struct
let next i =
let (i3, i2, i1, i0) = Ipaddr.V6.to_int32 i in
let i' =
if i0 == Int32.max_int then
if i1 == Int32.max_int then
if i2 == Int32.max_int then
(Int32.add i3 1l, i2, i1, i0)
else
(i3, Int32.add i2 1l, i1, i0)
else
(i3, i2, Int32.add i1 1l, i0)
else
(i3, i2, i1, Int32.add i0 1l)
in
Ipaddr.V6.of_int32 i'
let init n = next (Ipaddr.V6.Prefix.network n)
end
let next = function
| Ipaddr.V4 i -> Ipaddr.V4 (V4.next i)
| Ipaddr.V6 i -> Ipaddr.V6 (V6.next i)
let init = function
| Ipaddr.V4 i -> Ipaddr.V4 (V4.init i)
| Ipaddr.V6 i -> Ipaddr.V6 (V6.init i)
let is_in = Ipaddr.Prefix.mem
let network_of_string s =
match Ipaddr.Prefix.of_string s with
| Ok n -> Some n
| Error _ -> (
match Ipaddr.of_string s with
| Ok i -> Some (Ipaddr.Prefix.of_addr i)
| Error _ -> None )
let to_unix = Ipaddr_unix.to_inet_addr
let network_gen network =
let ipaddr = ref (init network) in
fun () ->
let value = to_unix !ipaddr in
if is_in !ipaddr network then (
ipaddr := next !ipaddr;
Some value
) else
None