1 | /* $Id: NATNetworkImpl.cpp 48522 2013-09-18 15:25:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * INATNetwork implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013 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 "NetworkServiceRunner.h"
|
---|
19 | #include "DHCPServerImpl.h"
|
---|
20 | #include "NATNetworkImpl.h"
|
---|
21 | #include "AutoCaller.h"
|
---|
22 | #include "Logging.h"
|
---|
23 |
|
---|
24 | #include <iprt/asm.h>
|
---|
25 | #include <iprt/cpp/utils.h>
|
---|
26 | #include <iprt/cidr.h>
|
---|
27 | #include <iprt/net.h>
|
---|
28 | #include <VBox/com/array.h>
|
---|
29 | #include <VBox/com/ptr.h>
|
---|
30 | #include <VBox/settings.h>
|
---|
31 |
|
---|
32 | #include "EventImpl.h"
|
---|
33 | #include "VBoxEvents.h"
|
---|
34 |
|
---|
35 | #include "VirtualBoxImpl.h"
|
---|
36 | #include <algorithm>
|
---|
37 | #include <list>
|
---|
38 |
|
---|
39 | #ifndef RT_OS_WINDOWS
|
---|
40 | # include <netinet/in.h>
|
---|
41 | #else
|
---|
42 | # define IN_LOOPBACKNET 127
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 | // constructor / destructor
|
---|
47 | /////////////////////////////////////////////////////////////////////////////
|
---|
48 |
|
---|
49 | struct NATNetwork::Data
|
---|
50 | {
|
---|
51 | Data()
|
---|
52 | : fEnabled(FALSE)
|
---|
53 | , fIPv6Enabled(FALSE)
|
---|
54 | , fAdvertiseDefaultIPv6Route(FALSE)
|
---|
55 | , fNeedDhcpServer(FALSE)
|
---|
56 | {
|
---|
57 | IPv4Gateway.setNull();
|
---|
58 | IPv4NetworkCidr.setNull();
|
---|
59 | IPv6Prefix.setNull();
|
---|
60 | IPv4DhcpServer.setNull();
|
---|
61 | IPv4NetworkMask.setNull();
|
---|
62 | IPv4DhcpServerLowerIp.setNull();
|
---|
63 | IPv4DhcpServerUpperIp.setNull();
|
---|
64 | }
|
---|
65 | virtual ~Data(){}
|
---|
66 | const ComObjPtr<EventSource> pEventSource;
|
---|
67 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
68 | NATNetworkServiceRunner NATRunner;
|
---|
69 | ComObjPtr<IDHCPServer> dhcpServer;
|
---|
70 | #endif
|
---|
71 | Bstr IPv4Gateway;
|
---|
72 | Bstr IPv4NetworkCidr;
|
---|
73 | Bstr IPv4NetworkMask;
|
---|
74 | Bstr IPv4DhcpServer;
|
---|
75 | Bstr IPv4DhcpServerLowerIp;
|
---|
76 | Bstr IPv4DhcpServerUpperIp;
|
---|
77 | BOOL fEnabled;
|
---|
78 | BOOL fIPv6Enabled;
|
---|
79 | Bstr IPv6Prefix;
|
---|
80 | BOOL fAdvertiseDefaultIPv6Route;
|
---|
81 | BOOL fNeedDhcpServer;
|
---|
82 | NATRuleMap mapName2PortForwardRule4;
|
---|
83 | NATRuleMap mapName2PortForwardRule6;
|
---|
84 | settings::NATLoopbackOffsetList llNATLoopbackOffsetList;
|
---|
85 | uint32_t u32LoopbackIp6;
|
---|
86 | uint32_t offGateway;
|
---|
87 | uint32_t offDhcp;
|
---|
88 | };
|
---|
89 |
|
---|
90 | NATNetwork::NATNetwork()
|
---|
91 | : mVirtualBox(NULL)
|
---|
92 | {
|
---|
93 | }
|
---|
94 |
|
---|
95 | NATNetwork::~NATNetwork()
|
---|
96 | {
|
---|
97 | }
|
---|
98 |
|
---|
99 | HRESULT NATNetwork::FinalConstruct()
|
---|
100 | {
|
---|
101 | return BaseFinalConstruct();
|
---|
102 | }
|
---|
103 |
|
---|
104 | void NATNetwork::FinalRelease()
|
---|
105 | {
|
---|
106 | uninit ();
|
---|
107 |
|
---|
108 | BaseFinalRelease();
|
---|
109 | }
|
---|
110 |
|
---|
111 | void NATNetwork::uninit()
|
---|
112 | {
|
---|
113 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
114 | AutoUninitSpan autoUninitSpan(this);
|
---|
115 | if (autoUninitSpan.uninitDone())
|
---|
116 | return;
|
---|
117 | delete m;
|
---|
118 | m = NULL;
|
---|
119 | unconst(mVirtualBox) = NULL;
|
---|
120 | }
|
---|
121 |
|
---|
122 | HRESULT NATNetwork::init(VirtualBox *aVirtualBox, IN_BSTR aName)
|
---|
123 | {
|
---|
124 | AssertReturn(aName != NULL, E_INVALIDARG);
|
---|
125 |
|
---|
126 | AutoInitSpan autoInitSpan(this);
|
---|
127 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
128 |
|
---|
129 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
130 | unconst(mVirtualBox) = aVirtualBox;
|
---|
131 | unconst(mName) = aName;
|
---|
132 | m = new Data();
|
---|
133 | m->offGateway = 1;
|
---|
134 | m->IPv4NetworkCidr = "10.0.2.0/24";
|
---|
135 | m->IPv6Prefix = "fe80::/64";
|
---|
136 | m->fEnabled = FALSE;
|
---|
137 |
|
---|
138 | settings::NATHostLoopbackOffset off;
|
---|
139 | off.strLoopbackHostAddress = "127.0.0.1";
|
---|
140 | off.u32Offset = (uint32_t)2;
|
---|
141 | m->llNATLoopbackOffsetList.push_back(off);
|
---|
142 |
|
---|
143 | recalculateIpv4AddressAssignments();
|
---|
144 |
|
---|
145 | HRESULT hrc = unconst(m->pEventSource).createObject();
|
---|
146 | if (FAILED(hrc)) throw hrc;
|
---|
147 |
|
---|
148 | hrc = m->pEventSource->init(static_cast<INATNetwork *>(this));
|
---|
149 | if (FAILED(hrc)) throw hrc;
|
---|
150 |
|
---|
151 | /* Confirm a successful initialization */
|
---|
152 | autoInitSpan.setSucceeded();
|
---|
153 |
|
---|
154 | return S_OK;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | HRESULT NATNetwork::init(VirtualBox *aVirtualBox,
|
---|
159 | const settings::NATNetwork &data)
|
---|
160 | {
|
---|
161 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
162 | AutoInitSpan autoInitSpan(this);
|
---|
163 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
164 |
|
---|
165 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
166 | unconst(mVirtualBox) = aVirtualBox;
|
---|
167 |
|
---|
168 | unconst(mName) = data.strNetworkName;
|
---|
169 | m = new Data();
|
---|
170 | m->IPv4NetworkCidr = data.strNetwork;
|
---|
171 | m->fEnabled = data.fEnabled;
|
---|
172 | m->fAdvertiseDefaultIPv6Route = data.fAdvertiseDefaultIPv6Route;
|
---|
173 | m->fNeedDhcpServer = data.fNeedDhcpServer;
|
---|
174 | m->fIPv6Enabled = data.fIPv6;
|
---|
175 |
|
---|
176 | m->u32LoopbackIp6 = data.u32HostLoopback6Offset;
|
---|
177 |
|
---|
178 | m->llNATLoopbackOffsetList.clear();
|
---|
179 | m->llNATLoopbackOffsetList.assign(data.llHostLoopbackOffsetList.begin(),
|
---|
180 | data.llHostLoopbackOffsetList.end());
|
---|
181 |
|
---|
182 | recalculateIpv4AddressAssignments();
|
---|
183 |
|
---|
184 | /* IPv4 port-forward rules */
|
---|
185 | m->mapName2PortForwardRule4.clear();
|
---|
186 | for (settings::NATRuleList::const_iterator it = data.llPortForwardRules4.begin();
|
---|
187 | it != data.llPortForwardRules4.end(); ++it)
|
---|
188 | {
|
---|
189 | m->mapName2PortForwardRule4.insert(std::make_pair(it->strName.c_str(), *it));
|
---|
190 | }
|
---|
191 |
|
---|
192 | /* IPv6 port-forward rules */
|
---|
193 | m->mapName2PortForwardRule6.clear();
|
---|
194 | for (settings::NATRuleList::const_iterator it = data.llPortForwardRules6.begin();
|
---|
195 | it != data.llPortForwardRules6.end(); ++it)
|
---|
196 | {
|
---|
197 | m->mapName2PortForwardRule6.insert(std::make_pair(it->strName, *it));
|
---|
198 | }
|
---|
199 |
|
---|
200 | HRESULT hrc = unconst(m->pEventSource).createObject();
|
---|
201 | if (FAILED(hrc)) throw hrc;
|
---|
202 |
|
---|
203 | hrc = m->pEventSource->init(static_cast<INATNetwork *>(this));
|
---|
204 | if (FAILED(hrc)) throw hrc;
|
---|
205 |
|
---|
206 | autoInitSpan.setSucceeded();
|
---|
207 |
|
---|
208 | return S_OK;
|
---|
209 | }
|
---|
210 |
|
---|
211 | #ifdef NAT_XML_SERIALIZATION
|
---|
212 | HRESULT NATNetwork::saveSettings(settings::NATNetwork &data)
|
---|
213 | {
|
---|
214 | AutoCaller autoCaller(this);
|
---|
215 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
216 |
|
---|
217 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
218 |
|
---|
219 | data.strNetworkName = mName;
|
---|
220 | data.strNetwork = m->IPv4NetworkCidr;
|
---|
221 | data.fEnabled = RT_BOOL(m->fEnabled);
|
---|
222 | data.fAdvertiseDefaultIPv6Route = RT_BOOL(m->fAdvertiseDefaultIPv6Route);
|
---|
223 | data.fNeedDhcpServer = RT_BOOL(m->fNeedDhcpServer);
|
---|
224 | data.fIPv6 = RT_BOOL(m->fIPv6Enabled);
|
---|
225 | data.strIPv6Prefix = m->IPv6Prefix;
|
---|
226 |
|
---|
227 | /* saving ipv4 port-forward Rules*/
|
---|
228 | data.llPortForwardRules4.clear();
|
---|
229 | for (NATRuleMap::iterator it = m->mapName2PortForwardRule4.begin();
|
---|
230 | it != m->mapName2PortForwardRule4.end(); ++it)
|
---|
231 | data.llPortForwardRules4.push_back(it->second);
|
---|
232 |
|
---|
233 | /* saving ipv6 port-forward Rules*/
|
---|
234 | data.llPortForwardRules6.clear();
|
---|
235 | for (NATRuleMap::iterator it = m->mapName2PortForwardRule6.begin();
|
---|
236 | it != m->mapName2PortForwardRule6.end(); ++it)
|
---|
237 | data.llPortForwardRules4.push_back(it->second);
|
---|
238 |
|
---|
239 | data.u32HostLoopback6Offset = m->u32LoopbackIp6;
|
---|
240 |
|
---|
241 | data.llHostLoopbackOffsetList.clear();
|
---|
242 | data.llHostLoopbackOffsetList.assign(m->llNATLoopbackOffsetList.begin(),
|
---|
243 | m->llNATLoopbackOffsetList.end());
|
---|
244 |
|
---|
245 | mVirtualBox->onNATNetworkSetting(mName.raw(),
|
---|
246 | data.fEnabled ? TRUE : FALSE,
|
---|
247 | m->IPv4NetworkCidr.raw(),
|
---|
248 | m->IPv4Gateway.raw(),
|
---|
249 | data.fAdvertiseDefaultIPv6Route ? TRUE : FALSE,
|
---|
250 | data.fNeedDhcpServer ? TRUE : FALSE);
|
---|
251 |
|
---|
252 | return S_OK;
|
---|
253 | }
|
---|
254 | #endif /* NAT_XML_SERIALIZATION */
|
---|
255 |
|
---|
256 | STDMETHODIMP NATNetwork::COMGETTER(EventSource)(IEventSource ** aEventSource)
|
---|
257 | {
|
---|
258 | CheckComArgOutPointerValid(aEventSource);
|
---|
259 |
|
---|
260 | AutoCaller autoCaller(this);
|
---|
261 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
262 |
|
---|
263 | /* event source is const, no need to lock */
|
---|
264 | m->pEventSource.queryInterfaceTo(aEventSource);
|
---|
265 |
|
---|
266 | return S_OK;
|
---|
267 | }
|
---|
268 |
|
---|
269 | STDMETHODIMP NATNetwork::COMGETTER(NetworkName)(BSTR *aName)
|
---|
270 | {
|
---|
271 | CheckComArgOutPointerValid(aName);
|
---|
272 |
|
---|
273 | AutoCaller autoCaller(this);
|
---|
274 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
275 |
|
---|
276 | mName.cloneTo(aName);
|
---|
277 |
|
---|
278 | return S_OK;
|
---|
279 | }
|
---|
280 |
|
---|
281 | STDMETHODIMP NATNetwork::COMSETTER(NetworkName)(IN_BSTR aName)
|
---|
282 | {
|
---|
283 | CheckComArgOutPointerValid(aName);
|
---|
284 |
|
---|
285 | HRESULT rc = S_OK;
|
---|
286 | AutoCaller autoCaller(this);
|
---|
287 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
288 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
289 | unconst(mName) = aName;
|
---|
290 |
|
---|
291 | alock.release();
|
---|
292 |
|
---|
293 | #ifdef NAT_XML_SERIALIZATION
|
---|
294 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
295 | rc = mVirtualBox->saveSettings();
|
---|
296 | #endif
|
---|
297 | return rc;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | STDMETHODIMP NATNetwork::COMGETTER(Enabled)(BOOL *aEnabled)
|
---|
302 | {
|
---|
303 | CheckComArgOutPointerValid(aEnabled);
|
---|
304 |
|
---|
305 | AutoCaller autoCaller(this);
|
---|
306 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
307 |
|
---|
308 | *aEnabled = m->fEnabled;
|
---|
309 | recalculateIpv4AddressAssignments();
|
---|
310 |
|
---|
311 | return S_OK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | STDMETHODIMP NATNetwork::COMSETTER(Enabled)(BOOL aEnabled)
|
---|
315 | {
|
---|
316 | AutoCaller autoCaller(this);
|
---|
317 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
318 | HRESULT rc = S_OK;
|
---|
319 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
320 | m->fEnabled = aEnabled;
|
---|
321 |
|
---|
322 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
323 | alock.release();
|
---|
324 | #ifdef NAT_XML_SERIALIZATION
|
---|
325 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
326 | rc = mVirtualBox->saveSettings();
|
---|
327 | #endif
|
---|
328 | return rc;
|
---|
329 | }
|
---|
330 |
|
---|
331 | STDMETHODIMP NATNetwork::COMGETTER(Gateway)(BSTR *aIPv4Gateway)
|
---|
332 | {
|
---|
333 | CheckComArgOutPointerValid(aIPv4Gateway);
|
---|
334 |
|
---|
335 | AutoCaller autoCaller(this);
|
---|
336 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
337 |
|
---|
338 | m->IPv4Gateway.cloneTo(aIPv4Gateway);
|
---|
339 |
|
---|
340 | return S_OK;
|
---|
341 | }
|
---|
342 |
|
---|
343 | STDMETHODIMP NATNetwork::COMGETTER(Network)(BSTR *aIPv4NetworkCidr)
|
---|
344 | {
|
---|
345 | CheckComArgOutPointerValid(aIPv4NetworkCidr);
|
---|
346 |
|
---|
347 | AutoCaller autoCaller(this);
|
---|
348 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
349 | m->IPv4NetworkCidr.cloneTo(aIPv4NetworkCidr);
|
---|
350 | return S_OK;
|
---|
351 | }
|
---|
352 |
|
---|
353 | STDMETHODIMP NATNetwork::COMSETTER(Network)(IN_BSTR aIPv4NetworkCidr)
|
---|
354 | {
|
---|
355 | CheckComArgOutPointerValid(aIPv4NetworkCidr);
|
---|
356 |
|
---|
357 | HRESULT rc = S_OK;
|
---|
358 | AutoCaller autoCaller(this);
|
---|
359 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
360 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
361 | /* silently ignore network cidr update */
|
---|
362 | if (m->mapName2PortForwardRule4.empty())
|
---|
363 | {
|
---|
364 |
|
---|
365 | unconst(m->IPv4NetworkCidr) = Bstr(aIPv4NetworkCidr);
|
---|
366 | recalculateIpv4AddressAssignments();
|
---|
367 | alock.release();
|
---|
368 |
|
---|
369 | #ifdef NAT_XML_SERIALIZATION
|
---|
370 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
371 | rc = mVirtualBox->saveSettings();
|
---|
372 | #endif
|
---|
373 | }
|
---|
374 | return rc;
|
---|
375 | }
|
---|
376 |
|
---|
377 | STDMETHODIMP NATNetwork::COMGETTER(IPv6Enabled)(BOOL *aIPv6Enabled)
|
---|
378 | {
|
---|
379 | CheckComArgOutPointerValid(aIPv6Enabled);
|
---|
380 |
|
---|
381 | AutoCaller autoCaller(this);
|
---|
382 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
383 |
|
---|
384 | *aIPv6Enabled = m->fIPv6Enabled;
|
---|
385 |
|
---|
386 | return S_OK;
|
---|
387 | }
|
---|
388 |
|
---|
389 | STDMETHODIMP NATNetwork::COMSETTER(IPv6Enabled)(BOOL aIPv6Enabled)
|
---|
390 | {
|
---|
391 | AutoCaller autoCaller(this);
|
---|
392 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
393 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
394 | m->fIPv6Enabled = aIPv6Enabled;
|
---|
395 |
|
---|
396 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
397 | alock.release();
|
---|
398 |
|
---|
399 | HRESULT rc = S_OK;
|
---|
400 | #ifdef NAT_XML_SERIALIZATION
|
---|
401 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
402 | rc = mVirtualBox->saveSettings();
|
---|
403 | #endif
|
---|
404 | return rc;
|
---|
405 | }
|
---|
406 |
|
---|
407 | STDMETHODIMP NATNetwork::COMGETTER(IPv6Prefix) (BSTR *aIPv6Prefix)
|
---|
408 | {
|
---|
409 | CheckComArgOutPointerValid(aIPv6Prefix);
|
---|
410 |
|
---|
411 | AutoCaller autoCaller(this);
|
---|
412 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
413 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
414 |
|
---|
415 | m->IPv6Prefix.cloneTo(aIPv6Prefix);
|
---|
416 |
|
---|
417 | return S_OK;
|
---|
418 | }
|
---|
419 |
|
---|
420 | STDMETHODIMP NATNetwork::COMSETTER(IPv6Prefix) (IN_BSTR aIPv6Prefix)
|
---|
421 | {
|
---|
422 | CheckComArgOutPointerValid(aIPv6Prefix);
|
---|
423 |
|
---|
424 | HRESULT rc = S_OK;
|
---|
425 | AutoCaller autoCaller(this);
|
---|
426 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
427 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
428 |
|
---|
429 | /* silently ignore network cidr update */
|
---|
430 | if (m->mapName2PortForwardRule6.empty())
|
---|
431 | {
|
---|
432 |
|
---|
433 | unconst(m->IPv6Prefix) = Bstr(aIPv6Prefix);
|
---|
434 | /* @todo: do we need recalculation ? */
|
---|
435 | alock.release();
|
---|
436 |
|
---|
437 | #ifdef NAT_XML_SERIALIZATION
|
---|
438 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
439 | rc = mVirtualBox->saveSettings();
|
---|
440 | #endif
|
---|
441 | }
|
---|
442 | return rc;
|
---|
443 | }
|
---|
444 |
|
---|
445 | STDMETHODIMP NATNetwork::COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(BOOL *aAdvertiseDefaultIPv6Route)
|
---|
446 | {
|
---|
447 | CheckComArgOutPointerValid(aAdvertiseDefaultIPv6Route);
|
---|
448 |
|
---|
449 | AutoCaller autoCaller(this);
|
---|
450 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
451 |
|
---|
452 | *aAdvertiseDefaultIPv6Route = m->fAdvertiseDefaultIPv6Route;
|
---|
453 |
|
---|
454 | return S_OK;
|
---|
455 | }
|
---|
456 |
|
---|
457 | STDMETHODIMP NATNetwork::COMSETTER(AdvertiseDefaultIPv6RouteEnabled)(BOOL aAdvertiseDefaultIPv6Route)
|
---|
458 | {
|
---|
459 | AutoCaller autoCaller(this);
|
---|
460 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
461 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
462 | m->fAdvertiseDefaultIPv6Route = aAdvertiseDefaultIPv6Route;
|
---|
463 |
|
---|
464 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
465 | alock.release();
|
---|
466 |
|
---|
467 | HRESULT rc = S_OK;
|
---|
468 | #ifdef NAT_XML_SERIALIZATION
|
---|
469 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
470 | rc = mVirtualBox->saveSettings();
|
---|
471 | #endif
|
---|
472 | return rc;
|
---|
473 | }
|
---|
474 |
|
---|
475 | STDMETHODIMP NATNetwork::COMGETTER(NeedDhcpServer)(BOOL *aNeedDhcpServer)
|
---|
476 | {
|
---|
477 | CheckComArgOutPointerValid(aNeedDhcpServer);
|
---|
478 |
|
---|
479 | AutoCaller autoCaller(this);
|
---|
480 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
481 |
|
---|
482 | *aNeedDhcpServer = m->fNeedDhcpServer;
|
---|
483 |
|
---|
484 | return S_OK;
|
---|
485 | }
|
---|
486 |
|
---|
487 | STDMETHODIMP NATNetwork::COMSETTER(NeedDhcpServer)(BOOL aNeedDhcpServer)
|
---|
488 | {
|
---|
489 | AutoCaller autoCaller(this);
|
---|
490 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
491 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
492 | m->fNeedDhcpServer = aNeedDhcpServer;
|
---|
493 |
|
---|
494 | recalculateIpv4AddressAssignments();
|
---|
495 |
|
---|
496 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
497 | alock.release();
|
---|
498 |
|
---|
499 | HRESULT rc = S_OK;
|
---|
500 | #ifdef NAT_XML_SERIALIZATION
|
---|
501 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
502 | rc = mVirtualBox->saveSettings();
|
---|
503 | #endif
|
---|
504 | return rc;
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | STDMETHODIMP NATNetwork::COMGETTER(LocalMappings)(ComSafeArrayOut(BSTR, aLocalMappings))
|
---|
509 | {
|
---|
510 | CheckComArgOutSafeArrayPointerValid(aLocalMappings);
|
---|
511 |
|
---|
512 | AutoCaller autoCaller(this);
|
---|
513 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
514 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
515 |
|
---|
516 | com::SafeArray<BSTR> sf(m->llNATLoopbackOffsetList.size());
|
---|
517 |
|
---|
518 | size_t i = 0;
|
---|
519 | settings::NATLoopbackOffsetList::const_iterator it;
|
---|
520 |
|
---|
521 | for (it = m->llNATLoopbackOffsetList.begin();
|
---|
522 | it != m->llNATLoopbackOffsetList.end(); ++it, ++i)
|
---|
523 | {
|
---|
524 | BstrFmt bstr("%s=%d",
|
---|
525 | (*it).strLoopbackHostAddress.c_str(),
|
---|
526 | (*it).u32Offset);
|
---|
527 | bstr.detachTo(&sf[i]);
|
---|
528 | }
|
---|
529 | sf.detachTo(ComSafeArrayOutArg(aLocalMappings));
|
---|
530 |
|
---|
531 | return S_OK;
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | STDMETHODIMP NATNetwork::AddLocalMapping(IN_BSTR aHostId, LONG aOffset)
|
---|
536 | {
|
---|
537 | AutoCaller autoCaller(this);
|
---|
538 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
539 |
|
---|
540 | //AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
541 |
|
---|
542 | RTNETADDRIPV4 addr, net, mask;
|
---|
543 |
|
---|
544 | int rc = RTNetStrToIPv4Addr(Utf8Str(aHostId).c_str(), &addr);
|
---|
545 | if (RT_FAILURE(rc))
|
---|
546 | return E_INVALIDARG;
|
---|
547 |
|
---|
548 | /* check against 127/8 */
|
---|
549 | if ((RT_N2H_U32(addr.u) >> IN_CLASSA_NSHIFT) != IN_LOOPBACKNET)
|
---|
550 | return E_INVALIDARG;
|
---|
551 |
|
---|
552 | /* check against networkid vs network mask */
|
---|
553 | rc = RTCidrStrToIPv4(Utf8Str(m->IPv4NetworkCidr).c_str(), &net, &mask);
|
---|
554 | if (RT_FAILURE(rc))
|
---|
555 | return E_INVALIDARG;
|
---|
556 |
|
---|
557 | if (((net.u + aOffset) & mask.u) != net.u)
|
---|
558 | return E_INVALIDARG;
|
---|
559 |
|
---|
560 | settings::NATLoopbackOffsetList::iterator it;
|
---|
561 |
|
---|
562 | it = std::find(m->llNATLoopbackOffsetList.begin(),
|
---|
563 | m->llNATLoopbackOffsetList.end(),
|
---|
564 | Utf8Str(aHostId).c_str());
|
---|
565 |
|
---|
566 | if (it != m->llNATLoopbackOffsetList.end())
|
---|
567 | {
|
---|
568 | if (aOffset == 0) /* erase */
|
---|
569 | m->llNATLoopbackOffsetList.erase(it, it);
|
---|
570 | else /* modify */
|
---|
571 | {
|
---|
572 | settings::NATLoopbackOffsetList::iterator it1;
|
---|
573 | it1 = std::find(m->llNATLoopbackOffsetList.begin(),
|
---|
574 | m->llNATLoopbackOffsetList.end(),
|
---|
575 | (uint32_t)aOffset);
|
---|
576 | if (it1 != m->llNATLoopbackOffsetList.end())
|
---|
577 | return E_INVALIDARG; /* this offset is already registered. */
|
---|
578 |
|
---|
579 | (*it).u32Offset = aOffset;
|
---|
580 | }
|
---|
581 |
|
---|
582 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
583 | return mVirtualBox->saveSettings();
|
---|
584 | }
|
---|
585 |
|
---|
586 | /* injection */
|
---|
587 | it = std::find(m->llNATLoopbackOffsetList.begin(),
|
---|
588 | m->llNATLoopbackOffsetList.end(),
|
---|
589 | (uint32_t)aOffset);
|
---|
590 |
|
---|
591 | if (it != m->llNATLoopbackOffsetList.end())
|
---|
592 | return E_INVALIDARG; /* offset is already registered. */
|
---|
593 |
|
---|
594 | settings::NATHostLoopbackOffset off;
|
---|
595 | off.strLoopbackHostAddress = aHostId;
|
---|
596 | off.u32Offset = (uint32_t)aOffset;
|
---|
597 | m->llNATLoopbackOffsetList.push_back(off);
|
---|
598 |
|
---|
599 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
600 | return mVirtualBox->saveSettings();
|
---|
601 | }
|
---|
602 |
|
---|
603 |
|
---|
604 | STDMETHODIMP NATNetwork::COMGETTER(LoopbackIp6)(LONG *aLoopbackIp6)
|
---|
605 | {
|
---|
606 | AutoCaller autoCaller(this);
|
---|
607 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
608 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
609 |
|
---|
610 | *aLoopbackIp6 = m->u32LoopbackIp6;
|
---|
611 | return S_OK;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | STDMETHODIMP NATNetwork::COMSETTER(LoopbackIp6)(LONG aLoopbackIp6)
|
---|
616 | {
|
---|
617 | AutoCaller autoCaller(this);
|
---|
618 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
619 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
620 |
|
---|
621 | m->u32LoopbackIp6 = aLoopbackIp6;
|
---|
622 |
|
---|
623 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
624 | return mVirtualBox->saveSettings();
|
---|
625 | }
|
---|
626 |
|
---|
627 | STDMETHODIMP NATNetwork::COMGETTER(PortForwardRules4)(ComSafeArrayOut(BSTR, aPortForwardRules4))
|
---|
628 | {
|
---|
629 | CheckComArgOutSafeArrayPointerValid(aPortForwardRules4);
|
---|
630 |
|
---|
631 | AutoCaller autoCaller(this);
|
---|
632 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
633 |
|
---|
634 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
635 | GetPortForwardRulesFromMap(ComSafeArrayInArg(aPortForwardRules4),
|
---|
636 | m->mapName2PortForwardRule4);
|
---|
637 | alock.release();
|
---|
638 | return S_OK;
|
---|
639 | }
|
---|
640 |
|
---|
641 | STDMETHODIMP NATNetwork::COMGETTER(PortForwardRules6)(ComSafeArrayOut(BSTR,
|
---|
642 | aPortForwardRules6))
|
---|
643 | {
|
---|
644 | CheckComArgOutSafeArrayPointerValid(aPortForwardRules6);
|
---|
645 |
|
---|
646 | AutoCaller autoCaller(this);
|
---|
647 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
648 |
|
---|
649 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
650 | GetPortForwardRulesFromMap(ComSafeArrayInArg(aPortForwardRules6), m->mapName2PortForwardRule6);
|
---|
651 | return S_OK;
|
---|
652 | }
|
---|
653 |
|
---|
654 | STDMETHODIMP NATNetwork::AddPortForwardRule(BOOL aIsIpv6,
|
---|
655 | IN_BSTR aPortForwardRuleName,
|
---|
656 | NATProtocol_T aProto,
|
---|
657 | IN_BSTR aHostIp,
|
---|
658 | USHORT aHostPort,
|
---|
659 | IN_BSTR aGuestIp,
|
---|
660 | USHORT aGuestPort)
|
---|
661 | {
|
---|
662 | int rc = S_OK;
|
---|
663 | AutoCaller autoCaller(this);
|
---|
664 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
665 |
|
---|
666 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
667 | Utf8Str name = aPortForwardRuleName;
|
---|
668 | Utf8Str proto;
|
---|
669 | settings::NATRule r;
|
---|
670 | NATRuleMap& mapRules = aIsIpv6 ? m->mapName2PortForwardRule6 : m->mapName2PortForwardRule4;
|
---|
671 | switch (aProto)
|
---|
672 | {
|
---|
673 | case NATProtocol_TCP:
|
---|
674 | proto = "tcp";
|
---|
675 | break;
|
---|
676 | case NATProtocol_UDP:
|
---|
677 | proto = "udp";
|
---|
678 | break;
|
---|
679 | default:
|
---|
680 | return E_INVALIDARG;
|
---|
681 | }
|
---|
682 | if (name.isEmpty())
|
---|
683 | name = Utf8StrFmt("%s_[%s]%%%d_[%s]%%%d", proto.c_str(),
|
---|
684 | Utf8Str(aHostIp).c_str(), aHostPort,
|
---|
685 | Utf8Str(aGuestIp).c_str(), aGuestPort);
|
---|
686 |
|
---|
687 | NATRuleMap::iterator it;
|
---|
688 |
|
---|
689 | for (it = mapRules.begin(); it != mapRules.end(); ++it)
|
---|
690 | {
|
---|
691 | r = it->second;
|
---|
692 | if (it->first == name)
|
---|
693 | return setError(E_INVALIDARG,
|
---|
694 | tr("A NAT rule of this name already exists"));
|
---|
695 | if ( r.strHostIP == Utf8Str(aHostIp)
|
---|
696 | && r.u16HostPort == aHostPort
|
---|
697 | && r.proto == aProto)
|
---|
698 | return setError(E_INVALIDARG,
|
---|
699 | tr("A NAT rule for this host port and this host IP already exists"));
|
---|
700 | }
|
---|
701 |
|
---|
702 | r.strName = name.c_str();
|
---|
703 | r.proto = aProto;
|
---|
704 | r.strHostIP = aHostIp;
|
---|
705 | r.u16HostPort = aHostPort;
|
---|
706 | r.strGuestIP = aGuestIp;
|
---|
707 | r.u16GuestPort = aGuestPort;
|
---|
708 | mapRules.insert(std::make_pair(name, r));
|
---|
709 |
|
---|
710 | alock.release();
|
---|
711 | mVirtualBox->onNATNetworkPortForward(mName.raw(), TRUE, aIsIpv6,
|
---|
712 | aPortForwardRuleName, aProto,
|
---|
713 | aHostIp, aHostPort,
|
---|
714 | aGuestIp, aGuestPort);
|
---|
715 |
|
---|
716 | /* Notify listerners listening on this network only */
|
---|
717 | fireNATNetworkPortForwardEvent(m->pEventSource, mName.raw(), TRUE,
|
---|
718 | aIsIpv6, aPortForwardRuleName, aProto,
|
---|
719 | aHostIp, aHostPort,
|
---|
720 | aGuestIp, aGuestPort);
|
---|
721 |
|
---|
722 | #ifdef NAT_XML_SERIALIZATION
|
---|
723 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
724 | rc = mVirtualBox->saveSettings();
|
---|
725 | #endif
|
---|
726 | return rc;
|
---|
727 | }
|
---|
728 |
|
---|
729 | STDMETHODIMP NATNetwork::RemovePortForwardRule(BOOL aIsIpv6, IN_BSTR aPortForwardRuleName)
|
---|
730 | {
|
---|
731 | AutoCaller autoCaller(this);
|
---|
732 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
733 |
|
---|
734 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
735 | NATRuleMap& mapRules = aIsIpv6 ? m->mapName2PortForwardRule6 : m->mapName2PortForwardRule4;
|
---|
736 | NATRuleMap::iterator it = mapRules.find(aPortForwardRuleName);
|
---|
737 |
|
---|
738 | if (it == mapRules.end())
|
---|
739 | return E_INVALIDARG;
|
---|
740 |
|
---|
741 | Utf8Str strHostIP = it->second.strHostIP;
|
---|
742 | Utf8Str strGuestIP = it->second.strGuestIP;
|
---|
743 | uint16_t u16HostPort = it->second.u16HostPort;
|
---|
744 | uint16_t u16GuestPort = it->second.u16GuestPort;
|
---|
745 | NATProtocol_T proto = it->second.proto;
|
---|
746 |
|
---|
747 | mapRules.erase(it);
|
---|
748 |
|
---|
749 | alock.release();
|
---|
750 |
|
---|
751 | mVirtualBox->onNATNetworkPortForward(mName.raw(), FALSE, aIsIpv6,
|
---|
752 | aPortForwardRuleName, proto,
|
---|
753 | Bstr(strHostIP).raw(), u16HostPort,
|
---|
754 | Bstr(strGuestIP).raw(), u16GuestPort);
|
---|
755 |
|
---|
756 | /* Notify listerners listening on this network only */
|
---|
757 | fireNATNetworkPortForwardEvent(m->pEventSource, mName.raw(), FALSE,
|
---|
758 | aIsIpv6, aPortForwardRuleName, proto,
|
---|
759 | Bstr(strHostIP).raw(), u16HostPort,
|
---|
760 | Bstr(strGuestIP).raw(), u16GuestPort);
|
---|
761 | HRESULT rc = S_OK;
|
---|
762 | #ifdef NAT_XML_SERIALIZATION
|
---|
763 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
764 | rc = mVirtualBox->saveSettings();
|
---|
765 | #endif
|
---|
766 |
|
---|
767 | return rc;
|
---|
768 | }
|
---|
769 |
|
---|
770 |
|
---|
771 | STDMETHODIMP NATNetwork::Start(IN_BSTR aTrunkType)
|
---|
772 | {
|
---|
773 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
774 | AutoCaller autoCaller(this);
|
---|
775 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
776 |
|
---|
777 | if (!m->fEnabled) return S_OK;
|
---|
778 |
|
---|
779 | m->NATRunner.setOption(NETCFG_NETNAME, mName, true);
|
---|
780 | m->NATRunner.setOption(NETCFG_TRUNKTYPE, Utf8Str(aTrunkType), true);
|
---|
781 | m->NATRunner.setOption(NETCFG_IPADDRESS, m->IPv4Gateway, true);
|
---|
782 | m->NATRunner.setOption(NETCFG_NETMASK, m->IPv4NetworkMask, true);
|
---|
783 |
|
---|
784 | /* No portforwarding rules from command-line, all will be fetched via API */
|
---|
785 |
|
---|
786 | if (m->fNeedDhcpServer)
|
---|
787 | {
|
---|
788 | /*
|
---|
789 | * Just to as idea... via API (on creation user pass the cidr of network and)
|
---|
790 | * and we calculate it's addreses (mutable?).
|
---|
791 | */
|
---|
792 |
|
---|
793 | /*
|
---|
794 | * Configuration and running DHCP server:
|
---|
795 | * 1. find server first createDHCPServer
|
---|
796 | * 2. if return status is E_INVALARG => server already exists just find and start.
|
---|
797 | * 3. if return status neither E_INVALRG nor S_OK => return E_FAIL
|
---|
798 | * 4. if return status S_OK proceed to DHCP server configuration
|
---|
799 | * 5. call setConfiguration() and pass all required parameters
|
---|
800 | * 6. start dhcp server.
|
---|
801 | */
|
---|
802 | int rc = mVirtualBox->FindDHCPServerByNetworkName(mName.raw(),
|
---|
803 | m->dhcpServer.asOutParam());
|
---|
804 | switch (rc)
|
---|
805 | {
|
---|
806 | case E_INVALIDARG:
|
---|
807 | /* server haven't beeen found let create it then */
|
---|
808 | rc = mVirtualBox->CreateDHCPServer(mName.raw(),
|
---|
809 | m->dhcpServer.asOutParam());
|
---|
810 | if (FAILED(rc))
|
---|
811 | return E_FAIL;
|
---|
812 | /* breakthrough */
|
---|
813 |
|
---|
814 | {
|
---|
815 | LogFunc(("gateway: %s, dhcpserver:%s, dhcplowerip:%s, dhcpupperip:%s\n",
|
---|
816 | Utf8Str(m->IPv4Gateway.raw()).c_str(),
|
---|
817 | Utf8Str(m->IPv4DhcpServer.raw()).c_str(),
|
---|
818 | Utf8Str(m->IPv4DhcpServerLowerIp.raw()).c_str(),
|
---|
819 | Utf8Str(m->IPv4DhcpServerUpperIp.raw()).c_str()));
|
---|
820 |
|
---|
821 | m->dhcpServer->AddGlobalOption(DhcpOpt_Router, m->IPv4Gateway.raw());
|
---|
822 |
|
---|
823 | rc = m->dhcpServer->COMSETTER(Enabled)(true);
|
---|
824 |
|
---|
825 | BSTR dhcpip = NULL;
|
---|
826 | BSTR netmask = NULL;
|
---|
827 | BSTR lowerip = NULL;
|
---|
828 | BSTR upperip = NULL;
|
---|
829 |
|
---|
830 | m->IPv4DhcpServer.cloneTo(&dhcpip);
|
---|
831 | m->IPv4NetworkMask.cloneTo(&netmask);
|
---|
832 | m->IPv4DhcpServerLowerIp.cloneTo(&lowerip);
|
---|
833 | m->IPv4DhcpServerUpperIp.cloneTo(&upperip);
|
---|
834 | rc = m->dhcpServer->SetConfiguration(dhcpip,
|
---|
835 | netmask,
|
---|
836 | lowerip,
|
---|
837 | upperip);
|
---|
838 | }
|
---|
839 | case S_OK:
|
---|
840 | break;
|
---|
841 |
|
---|
842 | default:
|
---|
843 | return E_FAIL;
|
---|
844 | }
|
---|
845 |
|
---|
846 | rc = m->dhcpServer->Start(mName.raw(), Bstr("").raw(), aTrunkType);
|
---|
847 | if (FAILED(rc))
|
---|
848 | {
|
---|
849 | m->dhcpServer.setNull();
|
---|
850 | return E_FAIL;
|
---|
851 | }
|
---|
852 | }
|
---|
853 |
|
---|
854 | if (RT_SUCCESS(m->NATRunner.start()))
|
---|
855 | {
|
---|
856 | mVirtualBox->onNATNetworkStartStop(mName.raw(), TRUE);
|
---|
857 | return S_OK;
|
---|
858 | }
|
---|
859 | /** @todo missing setError()! */
|
---|
860 | return E_FAIL;
|
---|
861 | #else
|
---|
862 | NOREF(aTrunkType);
|
---|
863 | ReturnComNotImplemented();
|
---|
864 | #endif
|
---|
865 | }
|
---|
866 |
|
---|
867 | STDMETHODIMP NATNetwork::Stop()
|
---|
868 | {
|
---|
869 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
870 | if (!m->dhcpServer.isNull())
|
---|
871 | m->dhcpServer->Stop();
|
---|
872 |
|
---|
873 | if (RT_SUCCESS(m->NATRunner.stop()))
|
---|
874 | {
|
---|
875 | mVirtualBox->onNATNetworkStartStop(mName.raw(), FALSE);
|
---|
876 | return S_OK;
|
---|
877 | }
|
---|
878 | /** @todo missing setError()! */
|
---|
879 | return E_FAIL;
|
---|
880 | #else
|
---|
881 | ReturnComNotImplemented();
|
---|
882 | #endif
|
---|
883 | }
|
---|
884 |
|
---|
885 | void NATNetwork::GetPortForwardRulesFromMap(ComSafeArrayOut(BSTR, aPortForwardRules), NATRuleMap& aRules)
|
---|
886 | {
|
---|
887 | com::SafeArray<BSTR> sf(aRules.size());
|
---|
888 | size_t i = 0;
|
---|
889 | NATRuleMap::const_iterator it;
|
---|
890 | for (it = aRules.begin();
|
---|
891 | it != aRules.end(); ++it, ++i)
|
---|
892 | {
|
---|
893 | settings::NATRule r = it->second;
|
---|
894 | BstrFmt bstr("%s:%s:[%s]:%d:[%s]:%d",
|
---|
895 | r.strName.c_str(),
|
---|
896 | (r.proto == NATProtocol_TCP? "tcp" : "udp"),
|
---|
897 | r.strHostIP.c_str(),
|
---|
898 | r.u16HostPort,
|
---|
899 | r.strGuestIP.c_str(),
|
---|
900 | r.u16GuestPort);
|
---|
901 | bstr.detachTo(&sf[i]);
|
---|
902 | }
|
---|
903 | sf.detachTo(ComSafeArrayOutArg(aPortForwardRules));
|
---|
904 | }
|
---|
905 |
|
---|
906 |
|
---|
907 | int NATNetwork::findFirstAvailableOffset(ADDRESSLOOKUPTYPE addrType, uint32_t *poff)
|
---|
908 | {
|
---|
909 | RTNETADDRIPV4 network, netmask;
|
---|
910 |
|
---|
911 | int rc = RTCidrStrToIPv4(Utf8Str(m->IPv4NetworkCidr.raw()).c_str(),
|
---|
912 | &network,
|
---|
913 | &netmask);
|
---|
914 | AssertRCReturn(rc, rc);
|
---|
915 |
|
---|
916 | uint32_t off;
|
---|
917 | settings::NATLoopbackOffsetList::iterator it;
|
---|
918 | for (off = 1; off < ~netmask.u; ++off)
|
---|
919 | {
|
---|
920 |
|
---|
921 | bool skip = false;
|
---|
922 | for (it = m->llNATLoopbackOffsetList.begin();
|
---|
923 | it != m->llNATLoopbackOffsetList.end();
|
---|
924 | ++it)
|
---|
925 | {
|
---|
926 | if ((*it).u32Offset == off)
|
---|
927 | {
|
---|
928 | skip = true;
|
---|
929 | break;
|
---|
930 | }
|
---|
931 |
|
---|
932 | }
|
---|
933 |
|
---|
934 | if (skip)
|
---|
935 | continue;
|
---|
936 |
|
---|
937 | if (off == m->offGateway)
|
---|
938 | {
|
---|
939 | if (addrType == ADDR_GATEWAY)
|
---|
940 | break;
|
---|
941 | else
|
---|
942 | continue;
|
---|
943 | }
|
---|
944 |
|
---|
945 | if (off == m->offDhcp)
|
---|
946 | {
|
---|
947 | if (addrType == ADDR_DHCP)
|
---|
948 | break;
|
---|
949 | else
|
---|
950 | continue;
|
---|
951 | }
|
---|
952 |
|
---|
953 | if (!skip)
|
---|
954 | break;
|
---|
955 | }
|
---|
956 |
|
---|
957 | if (poff)
|
---|
958 | *poff = off;
|
---|
959 |
|
---|
960 | return VINF_SUCCESS;
|
---|
961 | }
|
---|
962 |
|
---|
963 | int NATNetwork::recalculateIpv4AddressAssignments()
|
---|
964 | {
|
---|
965 | RTNETADDRIPV4 network, netmask;
|
---|
966 | int rc = RTCidrStrToIPv4(Utf8Str(m->IPv4NetworkCidr.raw()).c_str(),
|
---|
967 | &network,
|
---|
968 | &netmask);
|
---|
969 | AssertRCReturn(rc, rc);
|
---|
970 |
|
---|
971 | findFirstAvailableOffset(ADDR_GATEWAY, &m->offGateway);
|
---|
972 | if (m->fNeedDhcpServer)
|
---|
973 | findFirstAvailableOffset(ADDR_DHCP, &m->offDhcp);
|
---|
974 |
|
---|
975 | /* I don't remember the reason CIDR calculated on the host. */
|
---|
976 | RTNETADDRIPV4 gateway = network;
|
---|
977 | gateway.u += m->offGateway;
|
---|
978 | gateway.u = RT_H2N_U32(gateway.u);
|
---|
979 | char szTmpIp[16];
|
---|
980 | RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", gateway);
|
---|
981 | m->IPv4Gateway = szTmpIp;
|
---|
982 |
|
---|
983 | if (m->fNeedDhcpServer)
|
---|
984 | {
|
---|
985 | RTNETADDRIPV4 dhcpserver = network;
|
---|
986 | dhcpserver.u += m->offDhcp;
|
---|
987 |
|
---|
988 | /* XXX: adding more services should change the math here */
|
---|
989 | RTNETADDRIPV4 dhcplowerip = network;
|
---|
990 | uint32_t offDhcpLowerIp;
|
---|
991 | findFirstAvailableOffset(ADDR_DHCPLOWERIP, &offDhcpLowerIp);
|
---|
992 | dhcplowerip.u = RT_H2N_U32(dhcplowerip.u + offDhcpLowerIp);
|
---|
993 |
|
---|
994 | RTNETADDRIPV4 dhcpupperip;
|
---|
995 | dhcpupperip.u = RT_H2N_U32((network.u | ~netmask.u) - 1);
|
---|
996 |
|
---|
997 | dhcpserver.u = RT_H2N_U32(dhcpserver.u);
|
---|
998 | network.u = RT_H2N_U32(network.u);
|
---|
999 |
|
---|
1000 | RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcpserver);
|
---|
1001 | m->IPv4DhcpServer = szTmpIp;
|
---|
1002 | RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcplowerip);
|
---|
1003 | m->IPv4DhcpServerLowerIp = szTmpIp;
|
---|
1004 | RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcpupperip);
|
---|
1005 | m->IPv4DhcpServerUpperIp = szTmpIp;
|
---|
1006 |
|
---|
1007 | LogFunc(("network:%RTnaipv4, dhcpserver:%RTnaipv4, dhcplowerip:%RTnaipv4, dhcpupperip:%RTnaipv4\n",
|
---|
1008 | network, dhcpserver, dhcplowerip, dhcpupperip));
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | /* we need IPv4NetworkMask for NAT's gw service start */
|
---|
1012 | netmask.u = RT_H2N_U32(netmask.u);
|
---|
1013 | RTStrPrintf(szTmpIp, 16, "%RTnaipv4", netmask);
|
---|
1014 | m->IPv4NetworkMask = szTmpIp;
|
---|
1015 |
|
---|
1016 | LogFlowFunc(("getaway:%RTnaipv4, netmask:%RTnaipv4\n", gateway, netmask));
|
---|
1017 | return VINF_SUCCESS;
|
---|
1018 | }
|
---|