1 | /* $Id: IPv4Pool.h 70836 2018-01-31 14:55:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - a pool of IPv4 addresses
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2018 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef _DHCPD_IPV4_POOL_H_
|
---|
19 | #define _DHCPD_IPV4_POOL_H_
|
---|
20 |
|
---|
21 | #include <iprt/asm.h>
|
---|
22 | #include <iprt/stdint.h>
|
---|
23 | #include <iprt/net.h>
|
---|
24 | #include <set>
|
---|
25 |
|
---|
26 | typedef uint32_t ip_haddr_t; /* in host order */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*
|
---|
30 | * A range of IPv4 addresses (in host order).
|
---|
31 | */
|
---|
32 | struct IPv4Range
|
---|
33 | {
|
---|
34 | ip_haddr_t FirstAddr;
|
---|
35 | ip_haddr_t LastAddr; /* inclusive */
|
---|
36 |
|
---|
37 | IPv4Range()
|
---|
38 | : FirstAddr(), LastAddr() {}
|
---|
39 |
|
---|
40 | explicit IPv4Range(ip_haddr_t aSingleAddr)
|
---|
41 | : FirstAddr(aSingleAddr), LastAddr(aSingleAddr) {}
|
---|
42 |
|
---|
43 | IPv4Range(ip_haddr_t aFirstAddr, ip_haddr_t aLastAddr)
|
---|
44 | : FirstAddr(aFirstAddr), LastAddr(aLastAddr) {}
|
---|
45 |
|
---|
46 | explicit IPv4Range(RTNETADDRIPV4 aSingleAddr)
|
---|
47 | : FirstAddr(RT_N2H_U32(aSingleAddr.u)), LastAddr(RT_N2H_U32(aSingleAddr.u)) {}
|
---|
48 |
|
---|
49 | IPv4Range(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr)
|
---|
50 | : FirstAddr(RT_N2H_U32(aFirstAddr.u)), LastAddr(RT_N2H_U32(aLastAddr.u)) {}
|
---|
51 |
|
---|
52 | bool isValid() const
|
---|
53 | {
|
---|
54 | return FirstAddr <= LastAddr;
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool contains(ip_haddr_t addr) const
|
---|
58 | {
|
---|
59 | return FirstAddr <= addr && addr <= LastAddr;
|
---|
60 | }
|
---|
61 |
|
---|
62 | bool contains(RTNETADDRIPV4 addr) const
|
---|
63 | {
|
---|
64 | return contains(RT_N2H_U32(addr.u));
|
---|
65 | }
|
---|
66 |
|
---|
67 | bool contains(const IPv4Range &range) const
|
---|
68 | {
|
---|
69 | return range.isValid() && FirstAddr <= range.FirstAddr && range.LastAddr <= LastAddr;
|
---|
70 | }
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | inline bool operator==(const IPv4Range &l, const IPv4Range &r)
|
---|
75 | {
|
---|
76 | return l.FirstAddr == r.FirstAddr && l.LastAddr == r.LastAddr;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | inline bool operator<(const IPv4Range &l, const IPv4Range &r)
|
---|
81 | {
|
---|
82 | return l.LastAddr < r.FirstAddr;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | class IPv4Pool
|
---|
87 | {
|
---|
88 | typedef std::set<IPv4Range> set_t;
|
---|
89 | typedef set_t::iterator it_t;
|
---|
90 |
|
---|
91 | IPv4Range m_range;
|
---|
92 | set_t m_pool;
|
---|
93 |
|
---|
94 | public:
|
---|
95 | IPv4Pool() {}
|
---|
96 |
|
---|
97 | int init(const IPv4Range &aRange);
|
---|
98 | int init(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr);
|
---|
99 |
|
---|
100 | bool contains(RTNETADDRIPV4 addr) const
|
---|
101 | { return m_range.contains(addr); }
|
---|
102 |
|
---|
103 | int insert(const IPv4Range &range);
|
---|
104 |
|
---|
105 | #if 0
|
---|
106 | int insert(ip_haddr_t single)
|
---|
107 | { return insert(IPv4Range(single)); }
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | int insert(ip_haddr_t first, ip_haddr_t last)
|
---|
111 | { return insert(IPv4Range(first, last)); }
|
---|
112 |
|
---|
113 | int insert(RTNETADDRIPV4 single)
|
---|
114 | { return insert(IPv4Range(single)); }
|
---|
115 |
|
---|
116 | int insert(RTNETADDRIPV4 first, RTNETADDRIPV4 last)
|
---|
117 | { return insert(IPv4Range(first, last)); }
|
---|
118 |
|
---|
119 | RTNETADDRIPV4 allocate();
|
---|
120 | bool allocate(RTNETADDRIPV4);
|
---|
121 | };
|
---|
122 |
|
---|
123 | #endif /* _DHCPD_IPV4_POOL_H_ */
|
---|