1 | /* $Id: IPv4Pool.h 79531 2019-07-04 18:27:21Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - a pool of IPv4 addresses
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2019 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 VBOX_INCLUDED_SRC_Dhcpd_IPv4Pool_h
|
---|
19 | #define VBOX_INCLUDED_SRC_Dhcpd_IPv4Pool_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <iprt/asm.h>
|
---|
25 | #include <iprt/stdint.h>
|
---|
26 | #include <iprt/net.h>
|
---|
27 | #include <set>
|
---|
28 |
|
---|
29 |
|
---|
30 | /** Host order IPv4 address. */
|
---|
31 | typedef uint32_t IPV4HADDR;
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * A range of IPv4 addresses (in host order).
|
---|
36 | */
|
---|
37 | struct IPv4Range
|
---|
38 | {
|
---|
39 | IPV4HADDR FirstAddr; /**< Lowest address. */
|
---|
40 | IPV4HADDR LastAddr; /**< Higest address (inclusive). */
|
---|
41 |
|
---|
42 | IPv4Range()
|
---|
43 | : FirstAddr(0), LastAddr(0)
|
---|
44 | {}
|
---|
45 |
|
---|
46 | explicit IPv4Range(IPV4HADDR aSingleAddr)
|
---|
47 | : FirstAddr(aSingleAddr), LastAddr(aSingleAddr)
|
---|
48 | {}
|
---|
49 |
|
---|
50 | IPv4Range(IPV4HADDR aFirstAddr, IPV4HADDR aLastAddr)
|
---|
51 | : FirstAddr(aFirstAddr), LastAddr(aLastAddr)
|
---|
52 | {}
|
---|
53 |
|
---|
54 | explicit IPv4Range(RTNETADDRIPV4 aSingleAddr)
|
---|
55 | : FirstAddr(RT_N2H_U32(aSingleAddr.u)), LastAddr(RT_N2H_U32(aSingleAddr.u))
|
---|
56 | {}
|
---|
57 |
|
---|
58 | IPv4Range(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr)
|
---|
59 | : FirstAddr(RT_N2H_U32(aFirstAddr.u)), LastAddr(RT_N2H_U32(aLastAddr.u))
|
---|
60 | {}
|
---|
61 |
|
---|
62 | bool isValid() const
|
---|
63 | {
|
---|
64 | return FirstAddr <= LastAddr;
|
---|
65 | }
|
---|
66 |
|
---|
67 | bool contains(IPV4HADDR addr) const
|
---|
68 | {
|
---|
69 | return FirstAddr <= addr && addr <= LastAddr;
|
---|
70 | }
|
---|
71 |
|
---|
72 | bool contains(RTNETADDRIPV4 addr) const
|
---|
73 | {
|
---|
74 | return contains(RT_N2H_U32(addr.u));
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** Checks if this range includes the @a a_rRange. */
|
---|
78 | bool contains(const IPv4Range &a_rRange) const
|
---|
79 | {
|
---|
80 | return a_rRange.isValid()
|
---|
81 | && FirstAddr <= a_rRange.FirstAddr
|
---|
82 | && a_rRange.LastAddr <= LastAddr;
|
---|
83 | }
|
---|
84 | };
|
---|
85 |
|
---|
86 |
|
---|
87 | inline bool operator==(const IPv4Range &l, const IPv4Range &r)
|
---|
88 | {
|
---|
89 | return l.FirstAddr == r.FirstAddr && l.LastAddr == r.LastAddr;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | inline bool operator<(const IPv4Range &l, const IPv4Range &r)
|
---|
94 | {
|
---|
95 | return l.LastAddr < r.FirstAddr;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * IPv4 address pool.
|
---|
101 | *
|
---|
102 | * This manages a single range of IPv4 addresses (m_range). Unallocated
|
---|
103 | * addresses are tracked as a set of sub-ranges in the m_pool set.
|
---|
104 | *
|
---|
105 | */
|
---|
106 | class IPv4Pool
|
---|
107 | {
|
---|
108 | typedef std::set<IPv4Range> set_t;
|
---|
109 | typedef set_t::iterator it_t;
|
---|
110 |
|
---|
111 | /** The IPv4 range of this pool. */
|
---|
112 | IPv4Range m_range;
|
---|
113 | /** Pool of available IPv4 ranges. */
|
---|
114 | set_t m_pool;
|
---|
115 |
|
---|
116 | public:
|
---|
117 | IPv4Pool()
|
---|
118 | {}
|
---|
119 |
|
---|
120 | int init(const IPv4Range &aRange);
|
---|
121 | int init(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr);
|
---|
122 |
|
---|
123 | RTNETADDRIPV4 allocate();
|
---|
124 | bool allocate(RTNETADDRIPV4);
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Checks if the pool range includes @a addr (allocation status not considered).
|
---|
128 | */
|
---|
129 | bool contains(RTNETADDRIPV4 addr) const
|
---|
130 | {
|
---|
131 | return m_range.contains(addr);
|
---|
132 | }
|
---|
133 |
|
---|
134 | private:
|
---|
135 | int i_insert(const IPv4Range &range);
|
---|
136 | #if 0
|
---|
137 | int i_insert(IPV4HADDR single) { return i_insert(IPv4Range(single)); }
|
---|
138 | #endif
|
---|
139 | int i_insert(IPV4HADDR first, IPV4HADDR last) { return i_insert(IPv4Range(first, last)); }
|
---|
140 | int i_insert(RTNETADDRIPV4 single) { return i_insert(IPv4Range(single)); }
|
---|
141 | int i_insert(RTNETADDRIPV4 first, RTNETADDRIPV4 last) { return i_insert(IPv4Range(first, last)); }
|
---|
142 | };
|
---|
143 |
|
---|
144 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_IPv4Pool_h */
|
---|