1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/* $Id$ */
/** @file
* DHCP server - a pool of IPv4 addresses
*/
/*
* Copyright (C) 2017-2018 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <iprt/err.h>
#include <iprt/stream.h>
#include "IPv4Pool.h"
int IPv4Pool::init(const IPv4Range &aRange)
{
if (!aRange.isValid())
return VERR_INVALID_PARAMETER;
m_range = aRange;
m_pool.insert(m_range);
return VINF_SUCCESS;
}
int IPv4Pool::init(RTNETADDRIPV4 aFirstAddr, RTNETADDRIPV4 aLastAddr)
{
IPv4Range range(aFirstAddr, aLastAddr);
if (!range.isValid())
return VERR_INVALID_PARAMETER;
m_range = range;
m_pool.insert(m_range);
return VINF_SUCCESS;
}
int IPv4Pool::insert(const IPv4Range &range)
{
if (!m_range.isValid())
return VERR_INVALID_PARAMETER;
if (!m_range.contains(range))
return VERR_INVALID_PARAMETER;
it_t it = m_pool.upper_bound(IPv4Range(range.LastAddr)); /* successor */
if (it != m_pool.begin())
{
it_t prev(it);
--prev;
if (range.FirstAddr <= prev->LastAddr) {
#if 1 /* XXX */
RTPrintf("%08x-%08x conflicts with %08x-%08x\n",
range.FirstAddr, range.LastAddr,
prev->FirstAddr, prev->LastAddr);
#endif
return VERR_INVALID_PARAMETER;
}
}
m_pool.insert(it, range);
return VINF_SUCCESS;
}
RTNETADDRIPV4 IPv4Pool::allocate()
{
if (m_pool.empty())
{
RTNETADDRIPV4 res = { 0 };
return res;
}
it_t beg = m_pool.begin();
ip_haddr_t addr = beg->FirstAddr;
if (beg->FirstAddr == beg->LastAddr)
{
m_pool.erase(beg);
}
else
{
IPv4Range trimmed = *beg;
++trimmed.FirstAddr;
m_pool.erase(beg);
m_pool.insert(trimmed);
}
RTNETADDRIPV4 res = { RT_H2N_U32(addr) };
return res;
}
bool IPv4Pool::allocate(RTNETADDRIPV4 addr)
{
it_t it = m_pool.lower_bound(IPv4Range(addr)); /* candidate range */
if (it == m_pool.end())
return false;
Assert(RT_N2H_U32(addr.u) <= it->LastAddr); /* by definition of < and lower_bound */
if (!it->contains(addr))
return false;
const ip_haddr_t haddr = RT_N2H_U32(addr.u);
ip_haddr_t first = it->FirstAddr;
ip_haddr_t last = it->LastAddr;
m_pool.erase(it);
if (first != last)
{
if (haddr == first)
{
insert(++first, last);
}
else if (haddr == last)
{
insert(first, --last);
}
else
{
insert(first, haddr - 1);
insert(haddr + 1, last);
}
}
return true;
}
|