VirtualBox

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

Last change on this file since 49409 was 49249, checked in by vboxsync, 11 years ago

Finish DNS proxy code and hook it in.

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