VirtualBox

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

Last change on this file since 91581 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/* $Id: IPv4Pool.h 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * DHCP server - a pool of IPv4 addresses
4 */
5
6/*
7 * Copyright (C) 2017-2020 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. */
31typedef uint32_t IPV4HADDR;
32
33
34/**
35 * A range of IPv4 addresses (in host order).
36 */
37struct IPv4Range
38{
39 IPV4HADDR FirstAddr; /**< Lowest address. */
40 IPV4HADDR LastAddr; /**< Higest address (inclusive). */
41
42 IPv4Range() RT_NOEXCEPT
43 : FirstAddr(0), LastAddr(0)
44 {}
45
46 explicit IPv4Range(IPV4HADDR aSingleAddr) RT_NOEXCEPT
47 : FirstAddr(aSingleAddr), LastAddr(aSingleAddr)
48 {}
49
50 IPv4Range(IPV4HADDR aFirstAddr, IPV4HADDR aLastAddr) RT_NOEXCEPT
51 : FirstAddr(aFirstAddr), LastAddr(aLastAddr)
52 {}
53
54 explicit IPv4Range(RTNETADDRIPV4 aSingleAddr) RT_NOEXCEPT
55 : FirstAddr(RT_N2H_U32(aSingleAddr.u)), LastAddr(RT_N2H_U32(aSingleAddr.u))
56 {}
57
58 IPv4Range(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr) RT_NOEXCEPT
59 : FirstAddr(RT_N2H_U32(aFirstAddr.u)), LastAddr(RT_N2H_U32(aLastAddr.u))
60 {}
61
62 bool isValid() const RT_NOEXCEPT
63 {
64 return FirstAddr <= LastAddr;
65 }
66
67 bool contains(IPV4HADDR addr) const RT_NOEXCEPT
68 {
69 return FirstAddr <= addr && addr <= LastAddr;
70 }
71
72 bool contains(RTNETADDRIPV4 addr) const RT_NOEXCEPT
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 RT_NOEXCEPT
79 {
80 return a_rRange.isValid()
81 && FirstAddr <= a_rRange.FirstAddr
82 && a_rRange.LastAddr <= LastAddr;
83 }
84};
85
86
87inline bool operator==(const IPv4Range &l, const IPv4Range &r) RT_NOEXCEPT
88{
89 return l.FirstAddr == r.FirstAddr && l.LastAddr == r.LastAddr;
90}
91
92
93inline bool operator<(const IPv4Range &l, const IPv4Range &r) RT_NOEXCEPT
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 */
106class 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
116public:
117 IPv4Pool()
118 {}
119
120 int init(const IPv4Range &aRange) RT_NOEXCEPT;
121 int init(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr) RT_NOEXCEPT;
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 RT_NOEXCEPT
130 {
131 return m_range.contains(addr);
132 }
133
134private:
135 int i_insert(const IPv4Range &range) RT_NOEXCEPT;
136#if 0
137 int i_insert(IPV4HADDR single) RT_NOEXCEPT { return i_insert(IPv4Range(single)); }
138#endif
139 int i_insert(IPV4HADDR first, IPV4HADDR last) RT_NOEXCEPT { return i_insert(IPv4Range(first, last)); }
140 int i_insert(RTNETADDRIPV4 single) RT_NOEXCEPT { return i_insert(IPv4Range(single)); }
141 int i_insert(RTNETADDRIPV4 first, RTNETADDRIPV4 last) RT_NOEXCEPT { return i_insert(IPv4Range(first, last)); }
142};
143
144#endif /* !VBOX_INCLUDED_SRC_Dhcpd_IPv4Pool_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette