1 | /* $Id: Config.cpp 70836 2018-01-31 14:55:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - server configuration
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2018 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 <iprt/types.h>
|
---|
19 | #include <iprt/net.h> /* NB: must come before getopt.h */
|
---|
20 | #include <iprt/getopt.h>
|
---|
21 | #include <iprt/path.h>
|
---|
22 | #include <iprt/string.h>
|
---|
23 |
|
---|
24 | #include "Config.h"
|
---|
25 |
|
---|
26 |
|
---|
27 | bool Config::isSane()
|
---|
28 | {
|
---|
29 | int rc;
|
---|
30 |
|
---|
31 | /* unicast MAC address */
|
---|
32 | if (m_MacAddress.au8[0] & 0x01)
|
---|
33 | return false;
|
---|
34 |
|
---|
35 | /* unicast IP address */
|
---|
36 | if ((m_IPv4Address.au8[0] & 0xe0) == 0xe0)
|
---|
37 | return false;
|
---|
38 |
|
---|
39 | /* valid netmask */
|
---|
40 | int iPrefixLengh;
|
---|
41 | rc = RTNetMaskToPrefixIPv4(&m_IPv4Netmask, &iPrefixLengh);
|
---|
42 | if (RT_FAILURE(rc) || iPrefixLengh == 0)
|
---|
43 | return false;
|
---|
44 |
|
---|
45 | /* first IP is from the same network */
|
---|
46 | if ((m_IPv4PoolFirst.u & m_IPv4Netmask.u) != (m_IPv4Address.u & m_IPv4Netmask.u))
|
---|
47 | return false;
|
---|
48 |
|
---|
49 | /* last IP is from the same network */
|
---|
50 | if ((m_IPv4PoolLast.u & m_IPv4Netmask.u) != (m_IPv4Address.u & m_IPv4Netmask.u))
|
---|
51 | return false;
|
---|
52 |
|
---|
53 | /* the pool is valid */
|
---|
54 | if (RT_N2H_U32(m_IPv4PoolLast.u) < RT_N2H_U32(m_IPv4PoolFirst.u))
|
---|
55 | return false;
|
---|
56 |
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | Config *Config::hardcoded()
|
---|
62 | {
|
---|
63 | std::unique_ptr<Config> config(new Config());
|
---|
64 |
|
---|
65 | config->m_strNetwork.assign("HostInterfaceNetworking-vboxnet0");
|
---|
66 | config->sanitizeBaseName(); /* nop, but be explicit */
|
---|
67 |
|
---|
68 | config->m_strTrunk.assign("vboxnet0");
|
---|
69 | config->m_enmTrunkType = kIntNetTrunkType_NetFlt;
|
---|
70 |
|
---|
71 | config->m_MacAddress.au8[0] = 0x08;
|
---|
72 | config->m_MacAddress.au8[1] = 0x00;
|
---|
73 | config->m_MacAddress.au8[2] = 0x27;
|
---|
74 | config->m_MacAddress.au8[3] = 0xa9;
|
---|
75 | config->m_MacAddress.au8[4] = 0xcf;
|
---|
76 | config->m_MacAddress.au8[5] = 0xef;
|
---|
77 |
|
---|
78 |
|
---|
79 | config->m_IPv4Address.u = RT_H2N_U32_C(0xc0a838fe); /* 192.168.56.254 */
|
---|
80 | config->m_IPv4Netmask.u = RT_H2N_U32_C(0xffffff00); /* 255.255.255.0 */
|
---|
81 |
|
---|
82 | /* flip to test naks */
|
---|
83 | #if 1
|
---|
84 | config->m_IPv4PoolFirst.u = RT_H2N_U32_C(0xc0a8385a); /* 192.168.56.90 */
|
---|
85 | config->m_IPv4PoolLast.u = RT_H2N_U32_C(0xc0a83863); /* 192.168.56.99 */
|
---|
86 | #else
|
---|
87 | config->m_IPv4PoolFirst.u = RT_H2N_U32_C(0xc0a838c9); /* 192.168.56.201 */
|
---|
88 | config->m_IPv4PoolLast.u = RT_H2N_U32_C(0xc0a838dc); /* 192.168.56.220 */
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | AssertReturn(config->isSane(), NULL);
|
---|
92 | return config.release();
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /* compatibility with old VBoxNetDHCP */
|
---|
97 | static RTGETOPTDEF g_aCompatOptions[] =
|
---|
98 | {
|
---|
99 | { "--ip-address", 'i', RTGETOPT_REQ_IPV4ADDR },
|
---|
100 | { "--lower-ip", 'l', RTGETOPT_REQ_IPV4ADDR },
|
---|
101 | { "--mac-address", 'a', RTGETOPT_REQ_MACADDR },
|
---|
102 | { "--netmask", 'm', RTGETOPT_REQ_IPV4ADDR },
|
---|
103 | { "--network", 'n', RTGETOPT_REQ_STRING },
|
---|
104 | { "--trunk-name", 't', RTGETOPT_REQ_STRING },
|
---|
105 | { "--trunk-type", 'T', RTGETOPT_REQ_STRING },
|
---|
106 | { "--upper-ip", 'u', RTGETOPT_REQ_IPV4ADDR },
|
---|
107 | };
|
---|
108 |
|
---|
109 |
|
---|
110 | Config *Config::compat(int argc, char **argv)
|
---|
111 | {
|
---|
112 | RTGETOPTSTATE State;
|
---|
113 | int rc;
|
---|
114 |
|
---|
115 | rc = RTGetOptInit(&State, argc, argv,
|
---|
116 | g_aCompatOptions, RT_ELEMENTS(g_aCompatOptions), 0,
|
---|
117 | RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
118 |
|
---|
119 | std::unique_ptr<Config> config(new Config());
|
---|
120 | for (;;)
|
---|
121 | {
|
---|
122 | RTGETOPTUNION Val;
|
---|
123 |
|
---|
124 | rc = RTGetOpt(&State, &Val);
|
---|
125 | if (rc == 0) /* done */
|
---|
126 | break;
|
---|
127 |
|
---|
128 | switch (rc)
|
---|
129 | {
|
---|
130 | case 'a': /* --mac-address */
|
---|
131 | config->m_MacAddress = Val.MacAddr;
|
---|
132 | break;
|
---|
133 |
|
---|
134 | case 'i': /* --ip-address */
|
---|
135 | config->m_IPv4Address = Val.IPv4Addr;
|
---|
136 | break;
|
---|
137 |
|
---|
138 | case 'l': /* --lower-ip */
|
---|
139 | config->m_IPv4PoolFirst = Val.IPv4Addr;
|
---|
140 | break;
|
---|
141 |
|
---|
142 | case 'm': /* --netmask */
|
---|
143 | config->m_IPv4Netmask = Val.IPv4Addr;
|
---|
144 | break;
|
---|
145 |
|
---|
146 | case 'n': /* --network */
|
---|
147 | config->m_strNetwork.assign(Val.psz);
|
---|
148 | break;
|
---|
149 |
|
---|
150 | case 't': /* --trunk-name */
|
---|
151 | config->m_strTrunk.assign(Val.psz);
|
---|
152 | break;
|
---|
153 |
|
---|
154 | case 'T': /* --trunk-type */
|
---|
155 | if (strcmp(Val.psz, "none") == 0)
|
---|
156 | config->m_enmTrunkType = kIntNetTrunkType_None;
|
---|
157 | else if (strcmp(Val.psz, "whatever") == 0)
|
---|
158 | config->m_enmTrunkType = kIntNetTrunkType_WhateverNone;
|
---|
159 | else if (strcmp(Val.psz, "netflt") == 0)
|
---|
160 | config->m_enmTrunkType = kIntNetTrunkType_NetFlt;
|
---|
161 | else if (strcmp(Val.psz, "netadp") == 0)
|
---|
162 | config->m_enmTrunkType = kIntNetTrunkType_NetAdp;
|
---|
163 | else
|
---|
164 | return NULL;
|
---|
165 | break;
|
---|
166 |
|
---|
167 | case 'u': /* --upper-ip */
|
---|
168 | config->m_IPv4PoolLast = Val.IPv4Addr;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | config->sanitizeBaseName();
|
---|
174 |
|
---|
175 | if (!config->isSane())
|
---|
176 | return NULL;
|
---|
177 |
|
---|
178 | return config.release();
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | Config *Config::read(const char *pszFileName)
|
---|
183 | {
|
---|
184 | RT_NOREF(pszFileName);
|
---|
185 | return NULL; /* not yet */
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Set m_strBaseName to sanitized version of m_strNetwork that can be
|
---|
191 | * used in a path component.
|
---|
192 | */
|
---|
193 | void Config::sanitizeBaseName()
|
---|
194 | {
|
---|
195 | int rc;
|
---|
196 |
|
---|
197 | char szBaseName[RTPATH_MAX];
|
---|
198 | rc = RTStrCopy(szBaseName, sizeof(szBaseName), m_strNetwork.c_str());
|
---|
199 | if (RT_FAILURE(rc))
|
---|
200 | return;
|
---|
201 |
|
---|
202 | for (char *p = szBaseName; *p != '\0'; ++p)
|
---|
203 | {
|
---|
204 | if (RTPATH_IS_SEP(*p))
|
---|
205 | {
|
---|
206 | *p = '_';
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | m_strBaseName.assign(szBaseName);
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | optmap_t Config::getOptions(const OptParameterRequest &reqOpts,
|
---|
215 | const ClientId &id,
|
---|
216 | const OptVendorClassId &vendor) const
|
---|
217 | {
|
---|
218 | optmap_t optmap;
|
---|
219 |
|
---|
220 | fillDefaultOptions(optmap, reqOpts);
|
---|
221 | fillVendorOptions(optmap, reqOpts, vendor);
|
---|
222 | fillHostOptions(optmap, reqOpts, id);
|
---|
223 |
|
---|
224 | return optmap;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | void Config::fillDefaultOptions(optmap_t &optmap,
|
---|
229 | const OptParameterRequest &reqOpts) const
|
---|
230 | {
|
---|
231 | RT_NOREF(reqOpts);
|
---|
232 |
|
---|
233 | optmap << new OptSubnetMask(m_IPv4Netmask);
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | void Config::fillHostOptions(optmap_t &optmap,
|
---|
238 | const OptParameterRequest &reqOpts,
|
---|
239 | const ClientId &id) const
|
---|
240 | {
|
---|
241 | /* not yet */
|
---|
242 | RT_NOREF(optmap, reqOpts, id);
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | void Config::fillVendorOptions(optmap_t &optmap,
|
---|
247 | const OptParameterRequest &reqOpts,
|
---|
248 | const OptVendorClassId &vendor) const
|
---|
249 | {
|
---|
250 | if (!vendor.present())
|
---|
251 | return;
|
---|
252 |
|
---|
253 | /* may be some day... */
|
---|
254 | RT_NOREF(optmap, reqOpts);
|
---|
255 | }
|
---|