VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/Dhcpd/IPv4Pool.cpp@ 79524

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

Dhcpd: s/Defs.h/DhcpdInternal.h/, s/TimeStamp/Timestamp/g, started adding comments and stuff. bugref:9288

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1/* $Id: IPv4Pool.cpp 79524 2019-07-04 10:14:02Z 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#include "DhcpdInternal.h"
19#include <iprt/errcore.h>
20#include <iprt/stream.h>
21
22#include "IPv4Pool.h"
23
24
25int IPv4Pool::init(const IPv4Range &aRange)
26{
27 if (!aRange.isValid())
28 return VERR_INVALID_PARAMETER;
29
30 m_range = aRange;
31 m_pool.insert(m_range);
32 return VINF_SUCCESS;
33}
34
35
36int IPv4Pool::init(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr)
37{
38 IPv4Range range(aFirstAddr, aLastAddr);
39
40 if (!range.isValid())
41 return VERR_INVALID_PARAMETER;
42
43 m_range = range;
44 m_pool.insert(m_range);
45 return VINF_SUCCESS;
46}
47
48
49int IPv4Pool::insert(const IPv4Range &range)
50{
51 if (!m_range.isValid())
52 return VERR_INVALID_PARAMETER;
53
54 if (!m_range.contains(range))
55 return VERR_INVALID_PARAMETER;
56
57 it_t it = m_pool.upper_bound(IPv4Range(range.LastAddr)); /* successor */
58 if (it != m_pool.begin())
59 {
60 it_t prev(it);
61 --prev;
62 if (range.FirstAddr <= prev->LastAddr) {
63#if 1 /* XXX */
64 RTPrintf("%08x-%08x conflicts with %08x-%08x\n",
65 range.FirstAddr, range.LastAddr,
66 prev->FirstAddr, prev->LastAddr);
67#endif
68 return VERR_INVALID_PARAMETER;
69 }
70 }
71
72 m_pool.insert(it, range);
73 return VINF_SUCCESS;
74}
75
76
77RTNETADDRIPV4 IPv4Pool::allocate()
78{
79 if (m_pool.empty())
80 {
81 RTNETADDRIPV4 res = { 0 };
82 return res;
83 }
84
85 it_t beg = m_pool.begin();
86 ip_haddr_t addr = beg->FirstAddr;
87
88 if (beg->FirstAddr == beg->LastAddr)
89 {
90 m_pool.erase(beg);
91 }
92 else
93 {
94 IPv4Range trimmed = *beg;
95 ++trimmed.FirstAddr;
96 m_pool.erase(beg);
97 m_pool.insert(trimmed);
98 }
99
100 RTNETADDRIPV4 res = { RT_H2N_U32(addr) };
101 return res;
102}
103
104
105bool IPv4Pool::allocate(RTNETADDRIPV4 addr)
106{
107 it_t it = m_pool.lower_bound(IPv4Range(addr)); /* candidate range */
108 if (it == m_pool.end())
109 return false;
110
111 Assert(RT_N2H_U32(addr.u) <= it->LastAddr); /* by definition of < and lower_bound */
112
113 if (!it->contains(addr))
114 return false;
115
116 const ip_haddr_t haddr = RT_N2H_U32(addr.u);
117 ip_haddr_t first = it->FirstAddr;
118 ip_haddr_t last = it->LastAddr;
119
120 m_pool.erase(it);
121 if (first != last)
122 {
123 if (haddr == first)
124 {
125 insert(++first, last);
126 }
127 else if (haddr == last)
128 {
129 insert(first, --last);
130 }
131 else
132 {
133 insert(first, haddr - 1);
134 insert(haddr + 1, last);
135 }
136 }
137
138 return true;
139}
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