VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/VBoxNetLwipNAT.cpp@ 49591

Last change on this file since 49591 was 49560, checked in by vboxsync, 11 years ago

NetworkServices: VBoxNetBaseService::isMainNeeded() +const.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.0 KB
Line 
1/* $Id: VBoxNetLwipNAT.cpp 49560 2013-11-20 03:05:10Z vboxsync $ */
2/** @file
3 * VBoxNetNAT - NAT Service for connecting to IntNet.
4 */
5
6/*
7 * Copyright (C) 2009 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 "winutils.h"
19
20#include <VBox/com/com.h>
21#include <VBox/com/listeners.h>
22#include <VBox/com/string.h>
23#include <VBox/com/Guid.h>
24#include <VBox/com/array.h>
25#include <VBox/com/ErrorInfo.h>
26#include <VBox/com/errorprint.h>
27#include <VBox/com/VirtualBox.h>
28#include <VBox/com/NativeEventQueue.h>
29
30#include <iprt/net.h>
31#include <iprt/initterm.h>
32#include <iprt/alloca.h>
33#ifndef RT_OS_WINDOWS
34# include <arpa/inet.h>
35#endif
36#include <iprt/err.h>
37#include <iprt/time.h>
38#include <iprt/timer.h>
39#include <iprt/thread.h>
40#include <iprt/stream.h>
41#include <iprt/path.h>
42#include <iprt/param.h>
43#include <iprt/pipe.h>
44#include <iprt/getopt.h>
45#include <iprt/string.h>
46#include <iprt/mem.h>
47#include <iprt/message.h>
48#include <iprt/req.h>
49#include <iprt/file.h>
50#include <iprt/semaphore.h>
51#include <iprt/cpp/utils.h>
52#define LOG_GROUP LOG_GROUP_NAT_SERVICE
53#include <VBox/log.h>
54
55#include <VBox/sup.h>
56#include <VBox/intnet.h>
57#include <VBox/intnetinline.h>
58#include <VBox/vmm/pdmnetinline.h>
59#include <VBox/vmm/vmm.h>
60#include <VBox/version.h>
61
62#ifndef RT_OS_WINDOWS
63# include <sys/poll.h>
64# include <sys/socket.h>
65# include <netinet/in.h>
66#endif
67
68#include <vector>
69#include <string>
70
71#include "../NetLib/VBoxNetLib.h"
72#include "../NetLib/VBoxNetBaseService.h"
73#include "VBoxLwipCore.h"
74
75extern "C"
76{
77/* bunch of LWIP headers */
78#include "lwip/opt.h"
79#ifdef LWIP_SOCKET
80# undef LWIP_SOCKET
81# define LWIP_SOCKET 0
82#endif
83#include "lwip/sys.h"
84#include "lwip/stats.h"
85#include "lwip/mem.h"
86#include "lwip/memp.h"
87#include "lwip/pbuf.h"
88#include "lwip/netif.h"
89#include "lwip/api.h"
90#include "lwip/tcp_impl.h"
91#include "ipv6/lwip/ethip6.h"
92#include "lwip/nd6.h" // for proxy_na_hook
93#include "lwip/mld6.h"
94#include "lwip/udp.h"
95#include "lwip/tcp.h"
96#include "lwip/tcpip.h"
97#include "lwip/sockets.h"
98#include "netif/etharp.h"
99
100#include "proxy.h"
101#include "pxremap.h"
102#include "portfwd.h"
103}
104
105#include "../NetLib/VBoxPortForwardString.h"
106
107static RTGETOPTDEF g_aGetOptDef[] =
108{
109 { "--port-forward4", 'p', RTGETOPT_REQ_STRING },
110 { "--port-forward6", 'P', RTGETOPT_REQ_STRING }
111};
112
113typedef struct NATSEVICEPORTFORWARDRULE
114{
115 PORTFORWARDRULE Pfr;
116 fwspec FWSpec;
117} NATSEVICEPORTFORWARDRULE, *PNATSEVICEPORTFORWARDRULE;
118
119typedef std::vector<NATSEVICEPORTFORWARDRULE> VECNATSERVICEPF;
120typedef VECNATSERVICEPF::iterator ITERATORNATSERVICEPF;
121typedef VECNATSERVICEPF::const_iterator CITERATORNATSERVICEPF;
122
123
124class VBoxNetLwipNAT;
125
126
127class NATNetworkListener
128{
129public:
130 NATNetworkListener():m_pNAT(NULL){}
131
132 HRESULT init(VBoxNetLwipNAT *pNAT)
133 {
134 AssertPtrReturn(pNAT, E_INVALIDARG);
135
136 m_pNAT = pNAT;
137 return S_OK;
138 }
139
140 HRESULT init()
141 {
142 m_pNAT = NULL;
143 return S_OK;
144 }
145
146 void uninit() { m_pNAT = NULL; }
147
148 STDMETHOD(HandleEvent)(VBoxEventType_T aEventType, IEvent *pEvent);
149
150private:
151 VBoxNetLwipNAT *m_pNAT;
152};
153typedef ListenerImpl<NATNetworkListener, VBoxNetLwipNAT *> NATNetworkListenerImpl;
154VBOX_LISTENER_DECLARE(NATNetworkListenerImpl)
155
156
157class VBoxNetLwipNAT: public VBoxNetBaseService
158{
159 friend class NATNetworkListener;
160 public:
161 VBoxNetLwipNAT();
162 virtual ~VBoxNetLwipNAT();
163 void usage(){ /* @todo: should be implemented */ };
164 int run();
165 virtual int init(void);
166 /* @todo: when configuration would be really needed */
167 virtual int parseOpt(int rc, const RTGETOPTUNION& getOptVal);
168 /* VBoxNetNAT always needs Main */
169 virtual bool isMainNeeded() const { return true; }
170 private:
171 struct proxy_options m_ProxyOptions;
172 struct sockaddr_in m_src4;
173 struct sockaddr_in6 m_src6;
174 /**
175 * place for registered local interfaces.
176 */
177 ip4_lomap m_lo2off[10];
178 ip4_lomap_desc m_loOptDescriptor;
179
180 uint16_t m_u16Mtu;
181 netif m_LwipNetIf;
182 /* Queues */
183 RTREQQUEUE hReqIntNet;
184 /* thread where we're waiting for a frames, no semaphores needed */
185 RTTHREAD hThrIntNetRecv;
186
187 /* Our NAT network descriptor in Main */
188 ComPtr<INATNetwork> m_net;
189 ComObjPtr<NATNetworkListenerImpl> m_listener;
190
191 ComPtr<IHost> m_host;
192 ComObjPtr<NATNetworkListenerImpl> m_vboxListener;
193
194 STDMETHOD(HandleEvent)(VBoxEventType_T aEventType, IEvent *pEvent);
195
196 const char **getHostNameservers();
197
198 RTSEMEVENT hSemSVC;
199 /* Only for debug needs, by default NAT service should load rules from SVC
200 * on startup, and then on sync them on events.
201 */
202 bool fDontLoadRulesOnStartup;
203 static void onLwipTcpIpInit(void *arg);
204 static void onLwipTcpIpFini(void *arg);
205 static err_t netifInit(netif *pNetif);
206 static err_t netifLinkoutput(netif *pNetif, pbuf *pBuf);
207 static int intNetThreadRecv(RTTHREAD, void *);
208 static void vboxNetLwipNATProcessXmit(void);
209
210 VECNATSERVICEPF m_vecPortForwardRule4;
211 VECNATSERVICEPF m_vecPortForwardRule6;
212
213 static int natServicePfRegister(NATSEVICEPORTFORWARDRULE& natServicePf);
214 static int natServiceProcessRegisteredPf(VECNATSERVICEPF& vecPf);
215};
216
217
218static VBoxNetLwipNAT *g_pLwipNat;
219
220STDMETHODIMP NATNetworkListener::HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent)
221{
222 if (m_pNAT)
223 return m_pNAT->HandleEvent(aEventType, pEvent);
224 else
225 return E_FAIL;
226}
227
228
229
230STDMETHODIMP VBoxNetLwipNAT::HandleEvent(VBoxEventType_T aEventType,
231 IEvent *pEvent)
232{
233 HRESULT hrc = S_OK;
234 switch (aEventType)
235 {
236 case VBoxEventType_OnNATNetworkSetting:
237 {
238 ComPtr<INATNetworkSettingEvent> evSettings(pEvent);
239 // XXX: only handle IPv6 default route for now
240
241 if (!m_ProxyOptions.ipv6_enabled)
242 {
243 break;
244 }
245
246 BOOL fIPv6DefaultRoute = FALSE;
247 hrc = evSettings->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
248 AssertReturn(SUCCEEDED(hrc), hrc);
249
250 if (m_ProxyOptions.ipv6_defroute == fIPv6DefaultRoute)
251 {
252 break;
253 }
254
255 m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
256 tcpip_callback_with_block(proxy_rtadvd_do_quick, &m_LwipNetIf, 0);
257
258 break;
259 }
260
261 case VBoxEventType_OnNATNetworkPortForward:
262 {
263 com::Bstr name, strHostAddr, strGuestAddr;
264 LONG lHostPort, lGuestPort;
265 BOOL fCreateFW, fIPv6FW;
266 NATProtocol_T proto = NATProtocol_TCP;
267
268
269 ComPtr<INATNetworkPortForwardEvent> pfEvt = pEvent;
270
271 hrc = pfEvt->COMGETTER(Name)(name.asOutParam());
272 AssertReturn(SUCCEEDED(hrc), hrc);
273
274 hrc = pfEvt->COMGETTER(Proto)(&proto);
275 AssertReturn(SUCCEEDED(hrc), hrc);
276
277 hrc = pfEvt->COMGETTER(HostIp)(strHostAddr.asOutParam());
278 AssertReturn(SUCCEEDED(hrc), hrc);
279
280 hrc = pfEvt->COMGETTER(HostPort)(&lHostPort);
281 AssertReturn(SUCCEEDED(hrc), hrc);
282
283 hrc = pfEvt->COMGETTER(GuestIp)(strGuestAddr.asOutParam());
284 AssertReturn(SUCCEEDED(hrc), hrc);
285
286 hrc = pfEvt->COMGETTER(GuestPort)(&lGuestPort);
287 AssertReturn(SUCCEEDED(hrc), hrc);
288
289 hrc = pfEvt->COMGETTER(Create)(&fCreateFW);
290 AssertReturn(SUCCEEDED(hrc), hrc);
291
292 hrc = pfEvt->COMGETTER(Ipv6)(&fIPv6FW);
293 AssertReturn(SUCCEEDED(hrc), hrc);
294
295 VECNATSERVICEPF& rules = (fIPv6FW ?
296 m_vecPortForwardRule6 :
297 m_vecPortForwardRule4);
298
299 NATSEVICEPORTFORWARDRULE r;
300
301 RT_ZERO(r);
302
303 if (name.length() > sizeof(r.Pfr.szPfrName))
304 {
305 hrc = E_INVALIDARG;
306 goto port_forward_done;
307 }
308
309 r.Pfr.fPfrIPv6 = fIPv6FW;
310
311 switch (proto)
312 {
313 case NATProtocol_TCP:
314 r.Pfr.iPfrProto = IPPROTO_TCP;
315 break;
316 case NATProtocol_UDP:
317 r.Pfr.iPfrProto = IPPROTO_UDP;
318 break;
319 default:
320 goto port_forward_done;
321 }
322
323
324 RTStrPrintf(r.Pfr.szPfrName, sizeof(r.Pfr.szPfrName),
325 "%s", com::Utf8Str(name).c_str());
326
327 RTStrPrintf(r.Pfr.szPfrHostAddr, sizeof(r.Pfr.szPfrHostAddr),
328 "%s", com::Utf8Str(strHostAddr).c_str());
329
330 /* XXX: limits should be checked */
331 r.Pfr.u16PfrHostPort = (uint16_t)lHostPort;
332
333 RTStrPrintf(r.Pfr.szPfrGuestAddr, sizeof(r.Pfr.szPfrGuestAddr),
334 "%s", com::Utf8Str(strGuestAddr).c_str());
335
336 /* XXX: limits should be checked */
337 r.Pfr.u16PfrGuestPort = (uint16_t)lGuestPort;
338
339 if (fCreateFW) /* Addition */
340 {
341 rules.push_back(r);
342
343 natServicePfRegister(rules.back());
344 }
345 else /* Deletion */
346 {
347 ITERATORNATSERVICEPF it;
348 for (it = rules.begin(); it != rules.end(); ++it)
349 {
350 /* compare */
351 NATSEVICEPORTFORWARDRULE& natFw = *it;
352 if ( natFw.Pfr.iPfrProto == r.Pfr.iPfrProto
353 && natFw.Pfr.u16PfrHostPort == r.Pfr.u16PfrHostPort
354 && (strncmp(natFw.Pfr.szPfrHostAddr, r.Pfr.szPfrHostAddr, INET6_ADDRSTRLEN) == 0)
355 && natFw.Pfr.u16PfrGuestPort == r.Pfr.u16PfrGuestPort
356 && (strncmp(natFw.Pfr.szPfrGuestAddr, r.Pfr.szPfrGuestAddr, INET6_ADDRSTRLEN) == 0))
357 {
358 fwspec *pFwCopy = (fwspec *)RTMemAllocZ(sizeof(fwspec));
359 if (!pFwCopy)
360 {
361 break;
362 }
363 memcpy(pFwCopy, &natFw.FWSpec, sizeof(fwspec));
364
365 /* We shouldn't care about pFwCopy this memory will be freed when
366 * will message will arrive to the destination.
367 */
368 portfwd_rule_del(pFwCopy);
369
370 rules.erase(it);
371 break;
372 }
373 } /* loop over vector elements */
374 } /* condition add or delete */
375 port_forward_done:
376 /* clean up strings */
377 name.setNull();
378 strHostAddr.setNull();
379 strGuestAddr.setNull();
380 break;
381 }
382
383 case VBoxEventType_OnHostNameResolutionConfigurationChange:
384 {
385 const char **ppcszNameServers = getHostNameservers();
386 err_t error;
387
388 error = tcpip_callback_with_block(pxdns_set_nameservers,
389 ppcszNameServers,
390 /* :block */ 0);
391 if (error != ERR_OK && ppcszNameServers != NULL)
392 {
393 RTMemFree(ppcszNameServers);
394 }
395 break;
396 }
397 }
398 return hrc;
399}
400
401
402void VBoxNetLwipNAT::onLwipTcpIpInit(void* arg)
403{
404 AssertPtrReturnVoid(arg);
405 VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(arg);
406
407 HRESULT hrc = com::Initialize();
408 Assert(!FAILED(hrc));
409
410 proxy_arp_hook = pxremap_proxy_arp;
411 proxy_ip4_divert_hook = pxremap_ip4_divert;
412
413 proxy_na_hook = pxremap_proxy_na;
414 proxy_ip6_divert_hook = pxremap_ip6_divert;
415
416 /* lwip thread */
417 RTNETADDRIPV4 IpNetwork;
418 IpNetwork.u = g_pLwipNat->m_Ipv4Address.u & g_pLwipNat->m_Ipv4Netmask.u;
419
420 ip_addr LwipIpAddr, LwipIpNetMask, LwipIpNetwork;
421
422 memcpy(&LwipIpAddr, &g_pLwipNat->m_Ipv4Address, sizeof(ip_addr));
423 memcpy(&LwipIpNetMask, &g_pLwipNat->m_Ipv4Netmask, sizeof(ip_addr));
424 memcpy(&LwipIpNetwork, &IpNetwork, sizeof(ip_addr));
425
426 netif *pNetif = netif_add(&g_pLwipNat->m_LwipNetIf /* Lwip Interface */,
427 &LwipIpAddr /* IP address*/,
428 &LwipIpNetMask /* Network mask */,
429 &LwipIpAddr /* gateway address, @todo: is self IP acceptable? */,
430 g_pLwipNat /* state */,
431 VBoxNetLwipNAT::netifInit /* netif_init_fn */,
432 lwip_tcpip_input /* netif_input_fn */);
433
434 AssertPtrReturnVoid(pNetif);
435
436 netif_set_up(pNetif);
437 netif_set_link_up(pNetif);
438
439 if (pNat->m_ProxyOptions.ipv6_enabled) {
440 /*
441 * XXX: lwIP currently only ever calls mld6_joingroup() in
442 * nd6_tmr() for fresh tentative addresses, which is a wrong place
443 * to do it - but I'm not keen on fixing this properly for now
444 * (with correct handling of interface up and down transitions,
445 * etc). So stick it here as a kludge.
446 */
447 for (int i = 0; i <= 1; ++i) {
448 ip6_addr_t *paddr = netif_ip6_addr(pNetif, i);
449
450 ip6_addr_t solicited_node_multicast_address;
451 ip6_addr_set_solicitednode(&solicited_node_multicast_address,
452 paddr->addr[3]);
453 mld6_joingroup(paddr, &solicited_node_multicast_address);
454 }
455
456 /*
457 * XXX: We must join the solicited-node multicast for the
458 * addresses we do IPv6 NA-proxy for. We map IPv6 loopback to
459 * proxy address + 1. We only need the low 24 bits, and those are
460 * fixed.
461 */
462 {
463 ip6_addr_t solicited_node_multicast_address;
464
465 ip6_addr_set_solicitednode(&solicited_node_multicast_address,
466 /* last 24 bits of the address */
467 PP_HTONL(0x00000002));
468 mld6_netif_joingroup(pNetif, &solicited_node_multicast_address);
469 }
470 }
471
472 proxy_init(&g_pLwipNat->m_LwipNetIf, &g_pLwipNat->m_ProxyOptions);
473
474 natServiceProcessRegisteredPf(g_pLwipNat->m_vecPortForwardRule4);
475 natServiceProcessRegisteredPf(g_pLwipNat->m_vecPortForwardRule6);
476}
477
478
479void VBoxNetLwipNAT::onLwipTcpIpFini(void* arg)
480{
481 AssertPtrReturnVoid(arg);
482 VBoxNetLwipNAT *pThis = (VBoxNetLwipNAT *)arg;
483
484 /* XXX: proxy finalization */
485 netif_set_link_down(&g_pLwipNat->m_LwipNetIf);
486 netif_set_down(&g_pLwipNat->m_LwipNetIf);
487 netif_remove(&g_pLwipNat->m_LwipNetIf);
488
489}
490
491/*
492 * Callback for netif_add() to initialize the interface.
493 */
494err_t VBoxNetLwipNAT::netifInit(netif *pNetif)
495{
496 err_t rcLwip = ERR_OK;
497
498 AssertPtrReturn(pNetif, ERR_ARG);
499
500 VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(pNetif->state);
501 AssertPtrReturn(pNat, ERR_ARG);
502
503 LogFlowFunc(("ENTER: pNetif[%c%c%d]\n", pNetif->name[0], pNetif->name[1], pNetif->num));
504 /* validity */
505 AssertReturn( pNetif->name[0] == 'N'
506 && pNetif->name[1] == 'T', ERR_ARG);
507
508
509 pNetif->hwaddr_len = sizeof(RTMAC);
510 memcpy(pNetif->hwaddr, &pNat->m_MacAddress, sizeof(RTMAC));
511
512 pNat->m_u16Mtu = 1500; // XXX: FIXME
513 pNetif->mtu = pNat->m_u16Mtu;
514
515 pNetif->flags = NETIF_FLAG_BROADCAST
516 | NETIF_FLAG_ETHARP /* Don't bother driver with ARP and let Lwip resolve ARP handling */
517 | NETIF_FLAG_ETHERNET; /* Lwip works with ethernet too */
518
519 pNetif->linkoutput = netifLinkoutput; /* ether-level-pipe */
520 pNetif->output = lwip_etharp_output; /* ip-pipe */
521
522 if (pNat->m_ProxyOptions.ipv6_enabled) {
523 pNetif->output_ip6 = ethip6_output;
524
525 /* IPv6 link-local address in slot 0 */
526 netif_create_ip6_linklocal_address(pNetif, /* :from_mac_48bit */ 1);
527 netif_ip6_addr_set_state(pNetif, 0, IP6_ADDR_PREFERRED); // skip DAD
528
529 /*
530 * RFC 4193 Locally Assigned Global ID (ULA) in slot 1
531 * [fd17:625c:f037:XXXX::1] where XXXX, 16 bit Subnet ID, are two
532 * bytes from the middle of the IPv4 address, e.g. :dead: for
533 * 10.222.173.1
534 */
535 u8_t nethi = ip4_addr2(&pNetif->ip_addr);
536 u8_t netlo = ip4_addr3(&pNetif->ip_addr);
537
538 ip6_addr_t *paddr = netif_ip6_addr(pNetif, 1);
539 IP6_ADDR(paddr, 0, 0xFD, 0x17, 0x62, 0x5C);
540 IP6_ADDR(paddr, 1, 0xF0, 0x37, nethi, netlo);
541 IP6_ADDR(paddr, 2, 0x00, 0x00, 0x00, 0x00);
542 IP6_ADDR(paddr, 3, 0x00, 0x00, 0x00, 0x01);
543 netif_ip6_addr_set_state(pNetif, 1, IP6_ADDR_PREFERRED);
544
545#if LWIP_IPV6_SEND_ROUTER_SOLICIT
546 pNetif->rs_count = 0;
547#endif
548 }
549
550 LogFlowFunc(("LEAVE: %d\n", rcLwip));
551 return rcLwip;
552}
553
554
555/**
556 * Intnet-recv thread
557 */
558int VBoxNetLwipNAT::intNetThreadRecv(RTTHREAD, void *)
559{
560 int rc = VINF_SUCCESS;
561
562 /* 1. initialization and connection */
563 HRESULT hrc = com::Initialize();
564 if (FAILED(hrc))
565 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM!");
566
567 /* Well we're ready */
568 PINTNETRINGBUF pRingBuf = &g_pLwipNat->m_pIfBuf->Recv;
569
570 for (;;)
571 {
572 /*
573 * Wait for a packet to become available.
574 */
575 /* 2. waiting for request for */
576 rc = g_pLwipNat->waitForIntNetEvent(2000);
577 if (RT_FAILURE(rc))
578 {
579 if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
580 {
581 /* do we want interrupt anyone ??? */
582 continue;
583 }
584 LogRel(("VBoxNetNAT: waitForIntNetEvent returned %Rrc\n", rc));
585 AssertRCReturn(rc,ERR_IF);
586 }
587
588 /*
589 * Process the receive buffer.
590 */
591 PCINTNETHDR pHdr;
592
593 while ((pHdr = IntNetRingGetNextFrameToRead(pRingBuf)) != NULL)
594 {
595 uint8_t const u8Type = pHdr->u8Type;
596 size_t cbFrame = pHdr->cbFrame;
597 uint8_t *pu8Frame = NULL;
598 pbuf *pPbufHdr = NULL;
599 pbuf *pPbuf = NULL;
600 switch (u8Type)
601 {
602
603 case INTNETHDR_TYPE_FRAME:
604 /* @todo:should it be really here?
605 * Well well well, we're accessing lwip code here
606 */
607 pPbufHdr = pPbuf = pbuf_alloc(PBUF_RAW, pHdr->cbFrame, PBUF_POOL);
608 if (!pPbuf)
609 {
610 LogRel(("NAT: Can't allocate send buffer cbFrame=%u\n", cbFrame));
611 break;
612 }
613 Assert(pPbufHdr->tot_len == cbFrame);
614 pu8Frame = (uint8_t *)IntNetHdrGetFramePtr(pHdr, g_pLwipNat->m_pIfBuf);
615 while(pPbuf)
616 {
617 memcpy(pPbuf->payload, pu8Frame, pPbuf->len);
618 pu8Frame += pPbuf->len;
619 pPbuf = pPbuf->next;
620 }
621
622 g_pLwipNat->m_LwipNetIf.input(pPbufHdr, &g_pLwipNat->m_LwipNetIf);
623
624 AssertReleaseRC(rc);
625 break;
626 case INTNETHDR_TYPE_GSO:
627 {
628 PCPDMNETWORKGSO pGso = IntNetHdrGetGsoContext(pHdr,
629 g_pLwipNat->m_pIfBuf);
630 if (!PDMNetGsoIsValid(pGso, cbFrame,
631 cbFrame - sizeof(PDMNETWORKGSO)))
632 break;
633 cbFrame -= sizeof(PDMNETWORKGSO);
634 uint8_t abHdrScratch[256];
635 uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso,
636 cbFrame);
637 for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
638 {
639 uint32_t cbSegFrame;
640 void *pvSegFrame =
641 PDMNetGsoCarveSegmentQD(pGso,
642 (uint8_t *)(pGso + 1),
643 cbFrame,
644 abHdrScratch,
645 iSeg,
646 cSegs,
647 &cbSegFrame);
648
649 pPbuf = pbuf_alloc(PBUF_RAW, cbSegFrame, PBUF_POOL);
650 if (!pPbuf)
651 {
652 LogRel(("NAT: Can't allocate send buffer cbFrame=%u\n", cbSegFrame));
653 break;
654 }
655 Assert( !pPbuf->next
656 && pPbuf->len == cbSegFrame);
657 memcpy(pPbuf->payload, pvSegFrame, cbSegFrame);
658 g_pLwipNat->m_LwipNetIf.input(pPbuf, &g_pLwipNat->m_LwipNetIf);
659
660 }
661
662 }
663 break;
664 case INTNETHDR_TYPE_PADDING:
665 break;
666 default:
667 STAM_REL_COUNTER_INC(&g_pLwipNat->m_pIfBuf->cStatBadFrames);
668 break;
669 }
670 IntNetRingSkipFrame(&g_pLwipNat->m_pIfBuf->Recv);
671
672 } /* loop */
673 }
674 /* 3. deinitilization and termination */
675 LogFlowFuncLeaveRC(rc);
676 return rc;
677}
678
679
680/**
681 *
682 */
683void VBoxNetLwipNAT::vboxNetLwipNATProcessXmit()
684{
685 int rc = VINF_SUCCESS;
686 INTNETIFSENDREQ SendReq;
687 SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
688 SendReq.Hdr.cbReq = sizeof(SendReq);
689 SendReq.pSession = g_pLwipNat->m_pSession;
690 SendReq.hIf = g_pLwipNat->m_hIf;
691 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
692 AssertRC(rc);
693}
694
695
696err_t VBoxNetLwipNAT::netifLinkoutput(netif *pNetif, pbuf *pPBuf)
697{
698 int rc = VINF_SUCCESS;
699 err_t rcLwip = ERR_OK;
700 AssertPtrReturn(pNetif, ERR_ARG);
701 AssertPtrReturn(pPBuf, ERR_ARG);
702 AssertReturn((void *)g_pLwipNat == pNetif->state, ERR_ARG);
703 LogFlowFunc(("ENTER: pNetif[%c%c%d], pPbuf:%p\n",
704 pNetif->name[0],
705 pNetif->name[1],
706 pNetif->num,
707 pPBuf));
708
709 /*
710 * We're on the lwip thread ...
711 * try accure Xmit lock (actually we DO accure the lock ... )
712 * 1. we've entered csXmit so we should create frame
713 * 1.a. Frame creation success see 2.
714 * 1.b. (hm ... what about queue processing in place)
715 * 1.c. 2nd attempt create frame
716 * 1.d. Unlock the Xmit
717 * 1.e. goto BUSY.1
718 * 2. Copy pbuf to the frame
719 * 3. Send
720 * 4. leave csXmit & return.
721 *
722 * @todo: perhaps we can use it for optimization,
723 * e.g. drop UDP and reoccure lock on TCP NOTE: now BUSY is unachievable!
724 * Otherwise (BUSY)
725 * 1. Unbuffered (drop)
726 * (buffered)
727 * 1. Copy pbuf to entermediate buffer.
728 * 2. Add call buffer to the queue
729 * 3. return.
730 */
731 /* see p.1 */
732 rc = VINF_SUCCESS;
733 PINTNETHDR pHdr = NULL;
734 uint8_t *pu8Frame = NULL;
735 int offFrame = 0;
736 int idxSg = 0;
737 struct pbuf *pPBufPtr = pPBuf;
738 /* Allocate frame, and pad it if required. */
739 rc = IntNetRingAllocateFrame(&g_pLwipNat->m_pIfBuf->Send, pPBuf->tot_len, &pHdr, (void **)&pu8Frame);
740 if (RT_SUCCESS(rc))
741 {
742 /* see p. 2 */
743 while (pPBufPtr)
744 {
745 memcpy(&pu8Frame[offFrame], pPBufPtr->payload, pPBufPtr->len);
746 offFrame += pPBufPtr->len;
747 pPBufPtr = pPBufPtr->next;
748 }
749 }
750 if (RT_FAILURE(rc))
751 {
752 /* Could it be that some frames are still in the ring buffer */
753 /* 1.c */
754 AssertMsgFailed(("Debug Me!"));
755 }
756
757 /* Commit - what really this function do */
758 IntNetRingCommitFrameEx(&g_pLwipNat->m_pIfBuf->Send, pHdr, pPBuf->tot_len);
759
760 g_pLwipNat->vboxNetLwipNATProcessXmit();
761
762 AssertRCReturn(rc, ERR_IF);
763 LogFlowFunc(("LEAVE: %d\n", rcLwip));
764 return rcLwip;
765}
766
767
768VBoxNetLwipNAT::VBoxNetLwipNAT()
769{
770 LogFlowFuncEnter();
771
772 m_ProxyOptions.ipv6_enabled = 0;
773 m_ProxyOptions.ipv6_defroute = 0;
774 m_ProxyOptions.tftp_root = NULL;
775 m_ProxyOptions.src4 = NULL;
776 m_ProxyOptions.src6 = NULL;
777 memset(&m_src4, 0, sizeof(m_src4));
778 memset(&m_src6, 0, sizeof(m_src6));
779 m_src4.sin_family = AF_INET;
780 m_src6.sin6_family = AF_INET6;
781#if HAVE_SA_LEN
782 m_src4.sin_len = sizeof(m_src4);
783 m_src6.sin6_len = sizeof(m_src6);
784#endif
785 m_ProxyOptions.nameservers = NULL;
786
787 m_LwipNetIf.name[0] = 'N';
788 m_LwipNetIf.name[1] = 'T';
789 m_MacAddress.au8[0] = 0x52;
790 m_MacAddress.au8[1] = 0x54;
791 m_MacAddress.au8[2] = 0;
792 m_MacAddress.au8[3] = 0x12;
793 m_MacAddress.au8[4] = 0x35;
794 m_MacAddress.au8[5] = 0;
795 m_Ipv4Address.u = RT_MAKE_U32_FROM_U8( 10, 0, 2, 2); // NB: big-endian
796 m_Ipv4Netmask.u = RT_H2N_U32_C(0xffffff00);
797
798 fDontLoadRulesOnStartup = false;
799
800 for(unsigned int i = 0; i < RT_ELEMENTS(g_aGetOptDef); ++i)
801 m_vecOptionDefs.push_back(&g_aGetOptDef[i]);
802
803 m_enmTrunkType = kIntNetTrunkType_SrvNat;
804
805 LogFlowFuncLeave();
806}
807
808
809VBoxNetLwipNAT::~VBoxNetLwipNAT()
810{
811 if (m_ProxyOptions.tftp_root != NULL)
812 {
813 RTStrFree((char *)m_ProxyOptions.tftp_root);
814 }
815}
816
817
818int VBoxNetLwipNAT::natServicePfRegister(NATSEVICEPORTFORWARDRULE& natPf)
819{
820 int lrc = 0;
821 int rc = VINF_SUCCESS;
822 int socketSpec = SOCK_STREAM;
823 char *pszHostAddr;
824 int sockFamily = (natPf.Pfr.fPfrIPv6 ? PF_INET6 : PF_INET);
825
826 switch(natPf.Pfr.iPfrProto)
827 {
828 case IPPROTO_TCP:
829 socketSpec = SOCK_STREAM;
830 break;
831 case IPPROTO_UDP:
832 socketSpec = SOCK_DGRAM;
833 break;
834 default:
835 return VERR_IGNORED; /* Ah, just ignore the garbage */
836 }
837
838 pszHostAddr = natPf.Pfr.szPfrHostAddr;
839
840 /* XXX: workaround for inet_pton and an empty ipv4 address
841 * in rule declaration.
842 */
843 if ( sockFamily == PF_INET
844 && pszHostAddr[0] == 0)
845 pszHostAddr = (char *)"0.0.0.0"; /* XXX: fix it! without type cast */
846
847
848 lrc = fwspec_set(&natPf.FWSpec,
849 sockFamily,
850 socketSpec,
851 pszHostAddr,
852 natPf.Pfr.u16PfrHostPort,
853 natPf.Pfr.szPfrGuestAddr,
854 natPf.Pfr.u16PfrGuestPort);
855
856 AssertReturn(!lrc, VERR_IGNORED);
857
858 fwspec *pFwCopy = (fwspec *)RTMemAllocZ(sizeof(fwspec));
859 AssertPtrReturn(pFwCopy, VERR_IGNORED);
860
861 /*
862 * We need pass the copy, because we can't be sure
863 * how much this pointer will be valid in LWIP environment.
864 */
865 memcpy(pFwCopy, &natPf.FWSpec, sizeof(fwspec));
866
867 lrc = portfwd_rule_add(pFwCopy);
868
869 AssertReturn(!lrc, VERR_IGNORED);
870
871 return VINF_SUCCESS;
872}
873
874
875int VBoxNetLwipNAT::natServiceProcessRegisteredPf(VECNATSERVICEPF& vecRules){
876 ITERATORNATSERVICEPF it;
877 for (it = vecRules.begin();
878 it != vecRules.end(); ++it)
879 {
880 int rc = natServicePfRegister((*it));
881 if (RT_FAILURE(rc))
882 {
883 LogRel(("PF: %s is ignored\n", (*it).Pfr.szPfrName));
884 continue;
885 }
886 }
887 return VINF_SUCCESS;
888}
889
890
891int VBoxNetLwipNAT::init()
892{
893 HRESULT hrc;
894 LogFlowFuncEnter();
895
896
897 /* virtualbox initialized in super class */
898
899 int rc = ::VBoxNetBaseService::init();
900 AssertRCReturn(rc, rc);
901
902 hrc = virtualbox->FindNATNetworkByName(com::Bstr(m_Network.c_str()).raw(),
903 m_net.asOutParam());
904 AssertComRCReturn(hrc, VERR_NOT_FOUND);
905
906 hrc = m_listener.createObject();
907 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
908
909 hrc = m_listener->init(new NATNetworkListener(), this);
910 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
911
912 ComPtr<IEventSource> esNet;
913 hrc = m_net->COMGETTER(EventSource)(esNet.asOutParam());
914 AssertComRC(hrc);
915
916 com::SafeArray<VBoxEventType_T> aNetEvents;
917 aNetEvents.push_back(VBoxEventType_OnNATNetworkPortForward);
918 aNetEvents.push_back(VBoxEventType_OnNATNetworkSetting);
919 hrc = esNet->RegisterListener(m_listener, ComSafeArrayAsInParam(aNetEvents), true);
920 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
921
922
923 // resolver changes are reported on vbox but are retrieved from
924 // host so stash a pointer for future lookups
925 hrc = virtualbox->COMGETTER(Host)(m_host.asOutParam());
926 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
927
928 hrc = m_vboxListener.createObject();
929 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
930
931 hrc = m_vboxListener->init(new NATNetworkListener(), this);
932 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
933
934 ComPtr<IEventSource> esVBox;
935 hrc = virtualbox->COMGETTER(EventSource)(esVBox.asOutParam());
936 AssertComRC(hrc);
937
938 com::SafeArray<VBoxEventType_T> aVBoxEvents;
939 aVBoxEvents.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange);
940 hrc = esVBox->RegisterListener(m_vboxListener, ComSafeArrayAsInParam(aVBoxEvents), true);
941 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
942
943
944 BOOL fIPv6Enabled = FALSE;
945 hrc = m_net->COMGETTER(IPv6Enabled)(&fIPv6Enabled);
946 AssertComRCReturn(hrc, VERR_NOT_FOUND);
947
948 BOOL fIPv6DefaultRoute = FALSE;
949 if (fIPv6Enabled)
950 {
951 hrc = m_net->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
952 AssertComRCReturn(hrc, VERR_NOT_FOUND);
953 }
954
955 m_ProxyOptions.ipv6_enabled = fIPv6Enabled;
956 m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
957
958
959 com::Bstr bstrSourceIp4Key = com::BstrFmt("NAT/%s/SourceIp4",m_Network.c_str());
960 com::Bstr bstrSourceIpX;
961 hrc = virtualbox->GetExtraData(bstrSourceIp4Key.raw(), bstrSourceIpX.asOutParam());
962 if (SUCCEEDED(hrc))
963 {
964 RTNETADDRIPV4 addr;
965 rc = RTNetStrToIPv4Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr);
966 if (RT_SUCCESS(rc))
967 {
968 RT_ZERO(m_src4);
969
970 m_src4.sin_addr.s_addr = addr.u;
971 m_ProxyOptions.src4 = &m_src4;
972
973 bstrSourceIpX.setNull();
974 }
975 }
976
977 if (!fDontLoadRulesOnStartup)
978 {
979 /* XXX: extract function and do not duplicate */
980 com::SafeArray<BSTR> rules;
981 hrc = m_net->COMGETTER(PortForwardRules4)(ComSafeArrayAsOutParam(rules));
982 Assert(SUCCEEDED(hrc));
983
984 size_t idxRules = 0;
985 for (idxRules = 0; idxRules < rules.size(); ++idxRules)
986 {
987 Log(("%d-rule: %ls\n", idxRules, rules[idxRules]));
988 NATSEVICEPORTFORWARDRULE Rule;
989 RT_ZERO(Rule);
990 rc = netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 0, &Rule.Pfr);
991 AssertRC(rc);
992 m_vecPortForwardRule4.push_back(Rule);
993 }
994
995 rules.setNull();
996 hrc = m_net->COMGETTER(PortForwardRules6)(ComSafeArrayAsOutParam(rules));
997 Assert(SUCCEEDED(hrc));
998
999 for (idxRules = 0; idxRules < rules.size(); ++idxRules)
1000 {
1001 Log(("%d-rule: %ls\n", idxRules, rules[idxRules]));
1002 NATSEVICEPORTFORWARDRULE Rule;
1003 netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 1, &Rule.Pfr);
1004 m_vecPortForwardRule6.push_back(Rule);
1005 }
1006 } /* if (!fDontLoadRulesOnStartup) */
1007
1008 com::SafeArray<BSTR> strs;
1009 int count_strs;
1010 hrc = m_net->COMGETTER(LocalMappings)(ComSafeArrayAsOutParam(strs));
1011 if ( SUCCEEDED(hrc)
1012 && (count_strs = strs.size()))
1013 {
1014 unsigned int j = 0;
1015 int i;
1016
1017 for (i = 0; i < count_strs && j < RT_ELEMENTS(m_lo2off); ++i)
1018 {
1019 char szAddr[17];
1020 RTNETADDRIPV4 ip4addr;
1021 char *pszTerm;
1022 uint32_t u32Off;
1023 com::Utf8Str strLo2Off(strs[i]);
1024 const char *pszLo2Off = strLo2Off.c_str();
1025
1026 RT_ZERO(szAddr);
1027
1028 pszTerm = RTStrStr(pszLo2Off, "=");
1029
1030 if ( !pszTerm
1031 || (pszTerm - pszLo2Off) >= 17)
1032 continue;
1033
1034 memcpy(szAddr, pszLo2Off, (pszTerm - pszLo2Off));
1035 rc = RTNetStrToIPv4Addr(szAddr, &ip4addr);
1036 if (RT_FAILURE(rc))
1037 continue;
1038
1039 u32Off = RTStrToUInt32(pszTerm + 1);
1040 if (u32Off == 0)
1041 continue;
1042
1043 ip4_addr_set_u32(&m_lo2off[j].loaddr, ip4addr.u);
1044 m_lo2off[j].off = u32Off;
1045 ++j;
1046 }
1047
1048 m_loOptDescriptor.lomap = m_lo2off;
1049 m_loOptDescriptor.num_lomap = j;
1050 m_ProxyOptions.lomap_desc = &m_loOptDescriptor;
1051 }
1052
1053 com::Bstr bstr;
1054 hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
1055 AssertComRCReturn(hrc, VERR_NOT_FOUND);
1056 if (!bstr.isEmpty())
1057 {
1058 com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s",
1059 bstr.raw(), RTPATH_DELIMITER, "TFTP"));
1060 char *pszStrTemp; // avoid const char ** vs char **
1061 rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str());
1062 AssertRC(rc);
1063 m_ProxyOptions.tftp_root = pszStrTemp;
1064 }
1065
1066 m_ProxyOptions.nameservers = getHostNameservers();
1067
1068 /* end of COM initialization */
1069
1070 rc = RTSemEventCreate(&hSemSVC);
1071 AssertRCReturn(rc, rc);
1072
1073 rc = RTReqQueueCreate(&hReqIntNet);
1074 AssertRCReturn(rc, rc);
1075
1076 rc = g_pLwipNat->tryGoOnline();
1077 if (RT_FAILURE(rc))
1078 {
1079 return rc;
1080 }
1081
1082 vboxLwipCoreInitialize(VBoxNetLwipNAT::onLwipTcpIpInit, this);
1083
1084 rc = RTThreadCreate(&g_pLwipNat->hThrIntNetRecv, /* thread handle*/
1085 VBoxNetLwipNAT::intNetThreadRecv, /* routine */
1086 NULL, /* user data */
1087 128 * _1K, /* stack size */
1088 RTTHREADTYPE_IO, /* type */
1089 0, /* flags, @todo: waitable ?*/
1090 "INTNET-RECV");
1091 AssertRCReturn(rc,rc);
1092
1093
1094
1095 LogFlowFuncLeaveRC(rc);
1096 return rc;
1097}
1098
1099
1100const char **VBoxNetLwipNAT::getHostNameservers()
1101{
1102 HRESULT hrc;
1103
1104 if (m_host.isNull())
1105 {
1106 return NULL;
1107 }
1108
1109 com::SafeArray<BSTR> aNameServers;
1110 hrc = m_host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(aNameServers));
1111 if (FAILED(hrc))
1112 {
1113 return NULL;
1114 }
1115
1116 const size_t cNameServers = aNameServers.size();
1117 if (cNameServers == 0)
1118 {
1119 return NULL;
1120 }
1121
1122 const char **ppcszNameServers =
1123 (const char **)RTMemAllocZ(sizeof(char *) * (cNameServers + 1));
1124 if (ppcszNameServers == NULL)
1125 {
1126 return NULL;
1127 }
1128
1129 size_t idxLast = 0;
1130 for (size_t i = 0; i < cNameServers; ++i)
1131 {
1132 com::Utf8Str strNameServer(aNameServers[i]);
1133 ppcszNameServers[idxLast] = RTStrDup(strNameServer.c_str());
1134 if (ppcszNameServers[idxLast] != NULL)
1135 {
1136 ++idxLast;
1137 }
1138 }
1139
1140 if (idxLast == 0)
1141 {
1142 RTMemFree(ppcszNameServers);
1143 return NULL;
1144 }
1145
1146 return ppcszNameServers;
1147}
1148
1149
1150int VBoxNetLwipNAT::parseOpt(int rc, const RTGETOPTUNION& Val)
1151{
1152 switch (rc)
1153 {
1154 case 'p':
1155 case 'P':
1156 {
1157 NATSEVICEPORTFORWARDRULE Rule;
1158 VECNATSERVICEPF& rules = (rc == 'P'?
1159 m_vecPortForwardRule6
1160 : m_vecPortForwardRule4);
1161
1162 fDontLoadRulesOnStartup = true;
1163
1164 RT_ZERO(Rule);
1165
1166 int irc = netPfStrToPf(Val.psz, (rc == 'P'), &Rule.Pfr);
1167 rules.push_back(Rule);
1168 return VINF_SUCCESS;
1169 }
1170 default:;
1171 }
1172 return VERR_NOT_FOUND;
1173}
1174
1175
1176int VBoxNetLwipNAT::run()
1177{
1178 /* EventQueue processing from VBoxHeadless.cpp */
1179 com::NativeEventQueue *gEventQ = NULL;
1180 gEventQ = com::NativeEventQueue::getMainEventQueue();
1181 while(true)
1182 {
1183 /* XXX:todo: graceful termination */
1184 gEventQ->processEventQueue(0);
1185 gEventQ->processEventQueue(500);
1186 }
1187
1188 vboxLwipCoreFinalize(VBoxNetLwipNAT::onLwipTcpIpFini, this);
1189
1190 m_vecPortForwardRule4.clear();
1191 m_vecPortForwardRule6.clear();
1192
1193 return VINF_SUCCESS;
1194}
1195
1196
1197/**
1198 * Entry point.
1199 */
1200extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
1201{
1202 LogFlowFuncEnter();
1203
1204 NOREF(envp);
1205
1206#ifdef RT_OS_WINDOWS
1207 WSADATA wsaData;
1208 int err;
1209
1210 err = WSAStartup(MAKEWORD(2,2), &wsaData);
1211 if (err)
1212 {
1213 fprintf(stderr, "wsastartup: failed (%d)\n", err);
1214 return 1;
1215 }
1216#endif
1217
1218 HRESULT hrc = com::Initialize();
1219#ifdef VBOX_WITH_XPCOM
1220 if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
1221 {
1222 char szHome[RTPATH_MAX] = "";
1223 com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
1224 return RTMsgErrorExit(RTEXITCODE_FAILURE,
1225 "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
1226 }
1227#endif
1228 if (FAILED(hrc))
1229 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM!");
1230
1231 g_pLwipNat = new VBoxNetLwipNAT();
1232
1233 Log2(("NAT: initialization\n"));
1234 int rc = g_pLwipNat->parseArgs(argc - 1, argv + 1);
1235 rc = (rc == 0) ? VINF_SUCCESS : VERR_GENERAL_FAILURE; /* XXX: FIXME */
1236
1237 if (RT_SUCCESS(rc))
1238 {
1239 rc = g_pLwipNat->init();
1240 }
1241
1242 if (RT_SUCCESS(rc))
1243 {
1244 g_pLwipNat->run();
1245 }
1246
1247 delete g_pLwipNat;
1248 return 0;
1249}
1250
1251
1252#ifndef VBOX_WITH_HARDENING
1253
1254int main(int argc, char **argv, char **envp)
1255{
1256 int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
1257 if (RT_FAILURE(rc))
1258 return RTMsgInitFailure(rc);
1259
1260 return TrustedMain(argc, argv, envp);
1261}
1262
1263# if defined(RT_OS_WINDOWS)
1264
1265static LRESULT CALLBACK WindowProc(HWND hwnd,
1266 UINT uMsg,
1267 WPARAM wParam,
1268 LPARAM lParam
1269)
1270{
1271 if(uMsg == WM_DESTROY)
1272 {
1273 PostQuitMessage(0);
1274 return 0;
1275 }
1276 return DefWindowProc (hwnd, uMsg, wParam, lParam);
1277}
1278
1279static LPCWSTR g_WndClassName = L"VBoxNetNatLwipClass";
1280
1281static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
1282{
1283 HWND hwnd = 0;
1284 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
1285 bool bExit = false;
1286
1287 /* Register the Window Class. */
1288 WNDCLASS wc;
1289 wc.style = 0;
1290 wc.lpfnWndProc = WindowProc;
1291 wc.cbClsExtra = 0;
1292 wc.cbWndExtra = sizeof(void *);
1293 wc.hInstance = hInstance;
1294 wc.hIcon = NULL;
1295 wc.hCursor = NULL;
1296 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
1297 wc.lpszMenuName = NULL;
1298 wc.lpszClassName = g_WndClassName;
1299
1300 ATOM atomWindowClass = RegisterClass(&wc);
1301
1302 if (atomWindowClass != 0)
1303 {
1304 /* Create the window. */
1305 hwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
1306 g_WndClassName, g_WndClassName,
1307 WS_POPUPWINDOW,
1308 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
1309
1310 if (hwnd)
1311 {
1312 SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
1313 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
1314
1315 MSG msg;
1316 while (GetMessage(&msg, NULL, 0, 0))
1317 {
1318 TranslateMessage(&msg);
1319 DispatchMessage(&msg);
1320 }
1321
1322 DestroyWindow (hwnd);
1323
1324 bExit = true;
1325 }
1326
1327 UnregisterClass (g_WndClassName, hInstance);
1328 }
1329
1330 if(bExit)
1331 {
1332 /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
1333 exit(0);
1334 }
1335
1336 return 0;
1337}
1338
1339
1340
1341/** (We don't want a console usually.) */
1342int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
1343{
1344#if 0
1345 NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
1346
1347 HANDLE hThread = CreateThread(
1348 NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
1349 0, /*__in SIZE_T dwStackSize, */
1350 MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
1351 NULL, /*__in_opt LPVOID lpParameter,*/
1352 0, /*__in DWORD dwCreationFlags,*/
1353 NULL /*__out_opt LPDWORD lpThreadId*/
1354 );
1355
1356 if(hThread != NULL)
1357 CloseHandle(hThread);
1358
1359#endif
1360 return main(__argc, __argv, environ);
1361}
1362# endif /* RT_OS_WINDOWS */
1363
1364#endif /* !VBOX_WITH_HARDENING */
1365
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