VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/Dhcpd/IPv4Pool.h

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1/* $Id: IPv4Pool.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * DHCP server - a pool of IPv4 addresses
4 */
5
6/*
7 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef VBOX_INCLUDED_SRC_Dhcpd_IPv4Pool_h
29#define VBOX_INCLUDED_SRC_Dhcpd_IPv4Pool_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include <iprt/asm.h>
35#include <iprt/stdint.h>
36#include <iprt/net.h>
37#include <set>
38
39
40/** Host order IPv4 address. */
41typedef uint32_t IPV4HADDR;
42
43
44/**
45 * A range of IPv4 addresses (in host order).
46 */
47struct IPv4Range
48{
49 IPV4HADDR FirstAddr; /**< Lowest address. */
50 IPV4HADDR LastAddr; /**< Higest address (inclusive). */
51
52 IPv4Range() RT_NOEXCEPT
53 : FirstAddr(0), LastAddr(0)
54 {}
55
56 explicit IPv4Range(IPV4HADDR aSingleAddr) RT_NOEXCEPT
57 : FirstAddr(aSingleAddr), LastAddr(aSingleAddr)
58 {}
59
60 IPv4Range(IPV4HADDR aFirstAddr, IPV4HADDR aLastAddr) RT_NOEXCEPT
61 : FirstAddr(aFirstAddr), LastAddr(aLastAddr)
62 {}
63
64 explicit IPv4Range(RTNETADDRIPV4 aSingleAddr) RT_NOEXCEPT
65 : FirstAddr(RT_N2H_U32(aSingleAddr.u)), LastAddr(RT_N2H_U32(aSingleAddr.u))
66 {}
67
68 IPv4Range(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr) RT_NOEXCEPT
69 : FirstAddr(RT_N2H_U32(aFirstAddr.u)), LastAddr(RT_N2H_U32(aLastAddr.u))
70 {}
71
72 bool isValid() const RT_NOEXCEPT
73 {
74 return FirstAddr <= LastAddr;
75 }
76
77 bool contains(IPV4HADDR addr) const RT_NOEXCEPT
78 {
79 return FirstAddr <= addr && addr <= LastAddr;
80 }
81
82 bool contains(RTNETADDRIPV4 addr) const RT_NOEXCEPT
83 {
84 return contains(RT_N2H_U32(addr.u));
85 }
86
87 /** Checks if this range includes the @a a_rRange. */
88 bool contains(const IPv4Range &a_rRange) const RT_NOEXCEPT
89 {
90 return a_rRange.isValid()
91 && FirstAddr <= a_rRange.FirstAddr
92 && a_rRange.LastAddr <= LastAddr;
93 }
94};
95
96
97inline bool operator==(const IPv4Range &l, const IPv4Range &r) RT_NOEXCEPT
98{
99 return l.FirstAddr == r.FirstAddr && l.LastAddr == r.LastAddr;
100}
101
102
103inline bool operator<(const IPv4Range &l, const IPv4Range &r) RT_NOEXCEPT
104{
105 return l.LastAddr < r.FirstAddr;
106}
107
108
109/**
110 * IPv4 address pool.
111 *
112 * This manages a single range of IPv4 addresses (m_range). Unallocated
113 * addresses are tracked as a set of sub-ranges in the m_pool set.
114 *
115 */
116class IPv4Pool
117{
118 typedef std::set<IPv4Range> set_t;
119 typedef set_t::iterator it_t;
120
121 /** The IPv4 range of this pool. */
122 IPv4Range m_range;
123 /** Pool of available IPv4 ranges. */
124 set_t m_pool;
125
126public:
127 IPv4Pool()
128 {}
129
130 int init(const IPv4Range &aRange) RT_NOEXCEPT;
131 int init(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr) RT_NOEXCEPT;
132
133 RTNETADDRIPV4 allocate();
134 bool allocate(RTNETADDRIPV4);
135
136 /**
137 * Checks if the pool range includes @a a_Addr (allocation status not considered).
138 */
139 bool contains(RTNETADDRIPV4 a_Addr) const RT_NOEXCEPT
140 {
141 return m_range.contains(a_Addr);
142 }
143
144private:
145 int i_insert(const IPv4Range &range) RT_NOEXCEPT;
146#if 0
147 int i_insert(IPV4HADDR a_Single) RT_NOEXCEPT { return i_insert(IPv4Range(a_Single)); }
148#endif
149 int i_insert(IPV4HADDR a_First, IPV4HADDR a_Last) RT_NOEXCEPT { return i_insert(IPv4Range(a_First, a_Last)); }
150 int i_insert(RTNETADDRIPV4 a_Single) RT_NOEXCEPT { return i_insert(IPv4Range(a_Single)); }
151 int i_insert(RTNETADDRIPV4 a_First, RTNETADDRIPV4 a_Last) RT_NOEXCEPT { return i_insert(IPv4Range(a_First, a_Last)); }
152};
153
154#endif /* !VBOX_INCLUDED_SRC_Dhcpd_IPv4Pool_h */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use