VirtualBox

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

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

VBoxNetLwipNAT: G/c removes uniused members and its initializarion: hReqIntNet and hSemSVC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 38.1 KB
Line 
1/* $Id: VBoxNetLwipNAT.cpp 49823 2013-12-09 06:57:03Z 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# ifdef RT_OS_LINUX
67# include <linux/icmp.h> /* ICMP_FILTER */
68# endif
69# include <netinet/icmp6.h>
70#endif
71
72#include <vector>
73#include <string>
74
75#include "../NetLib/VBoxNetLib.h"
76#include "../NetLib/VBoxNetBaseService.h"
77#include "VBoxLwipCore.h"
78
79extern "C"
80{
81/* bunch of LWIP headers */
82#include "lwip/opt.h"
83#ifdef LWIP_SOCKET
84# undef LWIP_SOCKET
85# define LWIP_SOCKET 0
86#endif
87#include "lwip/sys.h"
88#include "lwip/pbuf.h"
89#include "lwip/netif.h"
90#include "lwip/ethip6.h"
91#include "lwip/nd6.h" // for proxy_na_hook
92#include "lwip/mld6.h"
93#include "lwip/tcpip.h"
94#include "netif/etharp.h"
95
96#include "proxy.h"
97#include "pxremap.h"
98#include "portfwd.h"
99}
100
101#include "../NetLib/VBoxPortForwardString.h"
102
103static RTGETOPTDEF g_aGetOptDef[] =
104{
105 { "--port-forward4", 'p', RTGETOPT_REQ_STRING },
106 { "--port-forward6", 'P', RTGETOPT_REQ_STRING }
107};
108
109typedef struct NATSEVICEPORTFORWARDRULE
110{
111 PORTFORWARDRULE Pfr;
112 fwspec FWSpec;
113} NATSEVICEPORTFORWARDRULE, *PNATSEVICEPORTFORWARDRULE;
114
115typedef std::vector<NATSEVICEPORTFORWARDRULE> VECNATSERVICEPF;
116typedef VECNATSERVICEPF::iterator ITERATORNATSERVICEPF;
117typedef VECNATSERVICEPF::const_iterator CITERATORNATSERVICEPF;
118
119
120class VBoxNetLwipNAT;
121
122
123class NATNetworkListener
124{
125public:
126 NATNetworkListener():m_pNAT(NULL){}
127
128 HRESULT init(VBoxNetLwipNAT *pNAT)
129 {
130 AssertPtrReturn(pNAT, E_INVALIDARG);
131
132 m_pNAT = pNAT;
133 return S_OK;
134 }
135
136 HRESULT init()
137 {
138 m_pNAT = NULL;
139 return S_OK;
140 }
141
142 void uninit() { m_pNAT = NULL; }
143
144 STDMETHOD(HandleEvent)(VBoxEventType_T aEventType, IEvent *pEvent);
145
146private:
147 VBoxNetLwipNAT *m_pNAT;
148};
149typedef ListenerImpl<NATNetworkListener, VBoxNetLwipNAT *> NATNetworkListenerImpl;
150VBOX_LISTENER_DECLARE(NATNetworkListenerImpl)
151
152
153class VBoxNetLwipNAT: public VBoxNetBaseService
154{
155 friend class NATNetworkListener;
156 public:
157 VBoxNetLwipNAT(SOCKET icmpsock4, SOCKET icmpsock6);
158 virtual ~VBoxNetLwipNAT();
159 void usage(){ /* @todo: should be implemented */ };
160 int run();
161 virtual int init(void);
162 /* @todo: when configuration would be really needed */
163 virtual int parseOpt(int rc, const RTGETOPTUNION& getOptVal);
164 /* VBoxNetNAT always needs Main */
165 virtual bool isMainNeeded() const { return true; }
166 virtual int processFrame(void *, size_t);
167 virtual int processGSO(PCPDMNETWORKGSO, size_t);
168 virtual int processUDP(void *, size_t) { return VERR_IGNORED; }
169
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 /* thread where we're waiting for a frames, no semaphores needed */
183 RTTHREAD hThrIntNetRecv;
184
185 /* Our NAT network descriptor in Main */
186 ComPtr<INATNetwork> m_net;
187 ComObjPtr<NATNetworkListenerImpl> m_listener;
188
189 ComPtr<IHost> m_host;
190 ComObjPtr<NATNetworkListenerImpl> m_vboxListener;
191 static INTNETSEG aXmitSeg[64];
192
193 STDMETHOD(HandleEvent)(VBoxEventType_T aEventType, IEvent *pEvent);
194
195 const char **getHostNameservers();
196
197 /* Only for debug needs, by default NAT service should load rules from SVC
198 * on startup, and then on sync them on events.
199 */
200 bool fDontLoadRulesOnStartup;
201 static void onLwipTcpIpInit(void *arg);
202 static void onLwipTcpIpFini(void *arg);
203 static err_t netifInit(netif *pNetif);
204 static err_t netifLinkoutput(netif *pNetif, pbuf *pBuf);
205 static int intNetThreadRecv(RTTHREAD, void *);
206
207 VECNATSERVICEPF m_vecPortForwardRule4;
208 VECNATSERVICEPF m_vecPortForwardRule6;
209
210 static int natServicePfRegister(NATSEVICEPORTFORWARDRULE& natServicePf);
211 static int natServiceProcessRegisteredPf(VECNATSERVICEPF& vecPf);
212};
213
214
215static VBoxNetLwipNAT *g_pLwipNat;
216INTNETSEG VBoxNetLwipNAT::aXmitSeg[64];
217
218STDMETHODIMP NATNetworkListener::HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent)
219{
220 if (m_pNAT)
221 return m_pNAT->HandleEvent(aEventType, pEvent);
222 else
223 return E_FAIL;
224}
225
226
227
228STDMETHODIMP VBoxNetLwipNAT::HandleEvent(VBoxEventType_T aEventType,
229 IEvent *pEvent)
230{
231 HRESULT hrc = S_OK;
232 switch (aEventType)
233 {
234 case VBoxEventType_OnNATNetworkSetting:
235 {
236 ComPtr<INATNetworkSettingEvent> evSettings(pEvent);
237 // XXX: only handle IPv6 default route for now
238
239 if (!m_ProxyOptions.ipv6_enabled)
240 {
241 break;
242 }
243
244 BOOL fIPv6DefaultRoute = FALSE;
245 hrc = evSettings->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
246 AssertReturn(SUCCEEDED(hrc), hrc);
247
248 if (m_ProxyOptions.ipv6_defroute == fIPv6DefaultRoute)
249 {
250 break;
251 }
252
253 m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
254 tcpip_callback_with_block(proxy_rtadvd_do_quick, &m_LwipNetIf, 0);
255
256 break;
257 }
258
259 case VBoxEventType_OnNATNetworkPortForward:
260 {
261 com::Bstr name, strHostAddr, strGuestAddr;
262 LONG lHostPort, lGuestPort;
263 BOOL fCreateFW, fIPv6FW;
264 NATProtocol_T proto = NATProtocol_TCP;
265
266
267 ComPtr<INATNetworkPortForwardEvent> pfEvt = pEvent;
268
269 hrc = pfEvt->COMGETTER(Name)(name.asOutParam());
270 AssertReturn(SUCCEEDED(hrc), hrc);
271
272 hrc = pfEvt->COMGETTER(Proto)(&proto);
273 AssertReturn(SUCCEEDED(hrc), hrc);
274
275 hrc = pfEvt->COMGETTER(HostIp)(strHostAddr.asOutParam());
276 AssertReturn(SUCCEEDED(hrc), hrc);
277
278 hrc = pfEvt->COMGETTER(HostPort)(&lHostPort);
279 AssertReturn(SUCCEEDED(hrc), hrc);
280
281 hrc = pfEvt->COMGETTER(GuestIp)(strGuestAddr.asOutParam());
282 AssertReturn(SUCCEEDED(hrc), hrc);
283
284 hrc = pfEvt->COMGETTER(GuestPort)(&lGuestPort);
285 AssertReturn(SUCCEEDED(hrc), hrc);
286
287 hrc = pfEvt->COMGETTER(Create)(&fCreateFW);
288 AssertReturn(SUCCEEDED(hrc), hrc);
289
290 hrc = pfEvt->COMGETTER(Ipv6)(&fIPv6FW);
291 AssertReturn(SUCCEEDED(hrc), hrc);
292
293 VECNATSERVICEPF& rules = (fIPv6FW ?
294 m_vecPortForwardRule6 :
295 m_vecPortForwardRule4);
296
297 NATSEVICEPORTFORWARDRULE r;
298
299 RT_ZERO(r);
300
301 if (name.length() > sizeof(r.Pfr.szPfrName))
302 {
303 hrc = E_INVALIDARG;
304 goto port_forward_done;
305 }
306
307 r.Pfr.fPfrIPv6 = fIPv6FW;
308
309 switch (proto)
310 {
311 case NATProtocol_TCP:
312 r.Pfr.iPfrProto = IPPROTO_TCP;
313 break;
314 case NATProtocol_UDP:
315 r.Pfr.iPfrProto = IPPROTO_UDP;
316 break;
317 default:
318 goto port_forward_done;
319 }
320
321
322 RTStrPrintf(r.Pfr.szPfrName, sizeof(r.Pfr.szPfrName),
323 "%s", com::Utf8Str(name).c_str());
324
325 RTStrPrintf(r.Pfr.szPfrHostAddr, sizeof(r.Pfr.szPfrHostAddr),
326 "%s", com::Utf8Str(strHostAddr).c_str());
327
328 /* XXX: limits should be checked */
329 r.Pfr.u16PfrHostPort = (uint16_t)lHostPort;
330
331 RTStrPrintf(r.Pfr.szPfrGuestAddr, sizeof(r.Pfr.szPfrGuestAddr),
332 "%s", com::Utf8Str(strGuestAddr).c_str());
333
334 /* XXX: limits should be checked */
335 r.Pfr.u16PfrGuestPort = (uint16_t)lGuestPort;
336
337 if (fCreateFW) /* Addition */
338 {
339 rules.push_back(r);
340
341 natServicePfRegister(rules.back());
342 }
343 else /* Deletion */
344 {
345 ITERATORNATSERVICEPF it;
346 for (it = rules.begin(); it != rules.end(); ++it)
347 {
348 /* compare */
349 NATSEVICEPORTFORWARDRULE& natFw = *it;
350 if ( natFw.Pfr.iPfrProto == r.Pfr.iPfrProto
351 && natFw.Pfr.u16PfrHostPort == r.Pfr.u16PfrHostPort
352 && (strncmp(natFw.Pfr.szPfrHostAddr, r.Pfr.szPfrHostAddr, INET6_ADDRSTRLEN) == 0)
353 && natFw.Pfr.u16PfrGuestPort == r.Pfr.u16PfrGuestPort
354 && (strncmp(natFw.Pfr.szPfrGuestAddr, r.Pfr.szPfrGuestAddr, INET6_ADDRSTRLEN) == 0))
355 {
356 fwspec *pFwCopy = (fwspec *)RTMemAllocZ(sizeof(fwspec));
357 if (!pFwCopy)
358 {
359 break;
360 }
361 memcpy(pFwCopy, &natFw.FWSpec, sizeof(fwspec));
362
363 /* We shouldn't care about pFwCopy this memory will be freed when
364 * will message will arrive to the destination.
365 */
366 portfwd_rule_del(pFwCopy);
367
368 rules.erase(it);
369 break;
370 }
371 } /* loop over vector elements */
372 } /* condition add or delete */
373 port_forward_done:
374 /* clean up strings */
375 name.setNull();
376 strHostAddr.setNull();
377 strGuestAddr.setNull();
378 break;
379 }
380
381 case VBoxEventType_OnHostNameResolutionConfigurationChange:
382 {
383 const char **ppcszNameServers = getHostNameservers();
384 err_t error;
385
386 error = tcpip_callback_with_block(pxdns_set_nameservers,
387 ppcszNameServers,
388 /* :block */ 0);
389 if (error != ERR_OK && ppcszNameServers != NULL)
390 {
391 RTMemFree(ppcszNameServers);
392 }
393 break;
394 }
395 }
396 return hrc;
397}
398
399
400void VBoxNetLwipNAT::onLwipTcpIpInit(void* arg)
401{
402 AssertPtrReturnVoid(arg);
403 VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(arg);
404
405 HRESULT hrc = com::Initialize();
406 Assert(!FAILED(hrc));
407
408 proxy_arp_hook = pxremap_proxy_arp;
409 proxy_ip4_divert_hook = pxremap_ip4_divert;
410
411 proxy_na_hook = pxremap_proxy_na;
412 proxy_ip6_divert_hook = pxremap_ip6_divert;
413
414 /* lwip thread */
415 RTNETADDRIPV4 network;
416 RTNETADDRIPV4 address = g_pLwipNat->getIpv4Address();
417 RTNETADDRIPV4 netmask = g_pLwipNat->getIpv4Netmask();
418 network.u = address.u & netmask.u;
419
420 ip_addr LwipIpAddr, LwipIpNetMask, LwipIpNetwork;
421
422 memcpy(&LwipIpAddr, &address, sizeof(ip_addr));
423 memcpy(&LwipIpNetMask, &netmask, sizeof(ip_addr));
424 memcpy(&LwipIpNetwork, &network, 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 RTMAC mac = g_pLwipNat->getMacAddress();
511 memcpy(pNetif->hwaddr, &mac, sizeof(RTMAC));
512
513 pNat->m_u16Mtu = 1500; // XXX: FIXME
514 pNetif->mtu = pNat->m_u16Mtu;
515
516 pNetif->flags = NETIF_FLAG_BROADCAST
517 | NETIF_FLAG_ETHARP /* Don't bother driver with ARP and let Lwip resolve ARP handling */
518 | NETIF_FLAG_ETHERNET; /* Lwip works with ethernet too */
519
520 pNetif->linkoutput = netifLinkoutput; /* ether-level-pipe */
521 pNetif->output = lwip_etharp_output; /* ip-pipe */
522
523 if (pNat->m_ProxyOptions.ipv6_enabled) {
524 pNetif->output_ip6 = ethip6_output;
525
526 /* IPv6 link-local address in slot 0 */
527 netif_create_ip6_linklocal_address(pNetif, /* :from_mac_48bit */ 1);
528 netif_ip6_addr_set_state(pNetif, 0, IP6_ADDR_PREFERRED); // skip DAD
529
530 /*
531 * RFC 4193 Locally Assigned Global ID (ULA) in slot 1
532 * [fd17:625c:f037:XXXX::1] where XXXX, 16 bit Subnet ID, are two
533 * bytes from the middle of the IPv4 address, e.g. :dead: for
534 * 10.222.173.1
535 */
536 u8_t nethi = ip4_addr2(&pNetif->ip_addr);
537 u8_t netlo = ip4_addr3(&pNetif->ip_addr);
538
539 ip6_addr_t *paddr = netif_ip6_addr(pNetif, 1);
540 IP6_ADDR(paddr, 0, 0xFD, 0x17, 0x62, 0x5C);
541 IP6_ADDR(paddr, 1, 0xF0, 0x37, nethi, netlo);
542 IP6_ADDR(paddr, 2, 0x00, 0x00, 0x00, 0x00);
543 IP6_ADDR(paddr, 3, 0x00, 0x00, 0x00, 0x01);
544 netif_ip6_addr_set_state(pNetif, 1, IP6_ADDR_PREFERRED);
545
546#if LWIP_IPV6_SEND_ROUTER_SOLICIT
547 pNetif->rs_count = 0;
548#endif
549 }
550
551 LogFlowFunc(("LEAVE: %d\n", rcLwip));
552 return rcLwip;
553}
554
555
556/**
557 * Intnet-recv thread
558 */
559int VBoxNetLwipNAT::intNetThreadRecv(RTTHREAD, void *)
560{
561 int rc = VINF_SUCCESS;
562
563 /* 1. initialization and connection */
564 HRESULT hrc = com::Initialize();
565 if (FAILED(hrc))
566 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM!");
567
568 g_pLwipNat->doReceiveLoop();
569 /* 3. deinitilization and termination */
570 LogFlowFuncLeaveRC(rc);
571 return rc;
572}
573
574
575err_t VBoxNetLwipNAT::netifLinkoutput(netif *pNetif, pbuf *pPBuf)
576{
577 AssertPtrReturn(pNetif, ERR_ARG);
578 AssertPtrReturn(pPBuf, ERR_ARG);
579 AssertReturn((void *)g_pLwipNat == pNetif->state, ERR_ARG);
580
581 LogFlowFunc(("ENTER: pNetif[%c%c%d], pPbuf:%p\n",
582 pNetif->name[0],
583 pNetif->name[1],
584 pNetif->num,
585 pPBuf));
586
587 RT_ZERO(VBoxNetLwipNAT::aXmitSeg);
588
589 unsigned idx = 0;
590 for( struct pbuf *pPBufPtr = pPBuf;
591 pPBufPtr;
592 pPBufPtr = pPBufPtr->next, ++idx)
593 {
594 AssertReturn(idx < RT_ELEMENTS(VBoxNetLwipNAT::aXmitSeg), ERR_MEM);
595 VBoxNetLwipNAT::aXmitSeg[idx].pv = pPBufPtr->payload;
596 VBoxNetLwipNAT::aXmitSeg[idx].cb = pPBufPtr->len;
597 }
598
599 int rc = g_pLwipNat->sendBufferOnWire(VBoxNetLwipNAT::aXmitSeg, idx, pPBuf->tot_len);
600 AssertRCReturn(rc, ERR_IF);
601
602 g_pLwipNat->flushWire();
603
604 LogFlowFunc(("LEAVE: %d\n", ERR_OK));
605 return ERR_OK;
606}
607
608
609VBoxNetLwipNAT::VBoxNetLwipNAT(SOCKET icmpsock4, SOCKET icmpsock6) : VBoxNetBaseService("VBoxNetNAT", "nat-network")
610{
611 LogFlowFuncEnter();
612
613 m_ProxyOptions.ipv6_enabled = 0;
614 m_ProxyOptions.ipv6_defroute = 0;
615 m_ProxyOptions.icmpsock4 = icmpsock4;
616 m_ProxyOptions.icmpsock6 = icmpsock6;
617 m_ProxyOptions.tftp_root = NULL;
618 m_ProxyOptions.src4 = NULL;
619 m_ProxyOptions.src6 = NULL;
620 memset(&m_src4, 0, sizeof(m_src4));
621 memset(&m_src6, 0, sizeof(m_src6));
622 m_src4.sin_family = AF_INET;
623 m_src6.sin6_family = AF_INET6;
624#if HAVE_SA_LEN
625 m_src4.sin_len = sizeof(m_src4);
626 m_src6.sin6_len = sizeof(m_src6);
627#endif
628 m_ProxyOptions.nameservers = NULL;
629
630 m_LwipNetIf.name[0] = 'N';
631 m_LwipNetIf.name[1] = 'T';
632
633 RTMAC mac;
634 mac.au8[0] = 0x52;
635 mac.au8[1] = 0x54;
636 mac.au8[2] = 0;
637 mac.au8[3] = 0x12;
638 mac.au8[4] = 0x35;
639 mac.au8[5] = 0;
640 setMacAddress(mac);
641
642 RTNETADDRIPV4 address;
643 address.u = RT_MAKE_U32_FROM_U8( 10, 0, 2, 2); // NB: big-endian
644 setIpv4Address(address);
645
646 address.u = RT_H2N_U32_C(0xffffff00);
647 setIpv4Netmask(address);
648
649 fDontLoadRulesOnStartup = false;
650
651 for(unsigned int i = 0; i < RT_ELEMENTS(g_aGetOptDef); ++i)
652 addCommandLineOption(&g_aGetOptDef[i]);
653
654 LogFlowFuncLeave();
655}
656
657
658VBoxNetLwipNAT::~VBoxNetLwipNAT()
659{
660 if (m_ProxyOptions.tftp_root != NULL)
661 {
662 RTStrFree((char *)m_ProxyOptions.tftp_root);
663 }
664}
665
666
667int VBoxNetLwipNAT::natServicePfRegister(NATSEVICEPORTFORWARDRULE& natPf)
668{
669 int lrc = 0;
670 int rc = VINF_SUCCESS;
671 int socketSpec = SOCK_STREAM;
672 char *pszHostAddr;
673 int sockFamily = (natPf.Pfr.fPfrIPv6 ? PF_INET6 : PF_INET);
674
675 switch(natPf.Pfr.iPfrProto)
676 {
677 case IPPROTO_TCP:
678 socketSpec = SOCK_STREAM;
679 break;
680 case IPPROTO_UDP:
681 socketSpec = SOCK_DGRAM;
682 break;
683 default:
684 return VERR_IGNORED; /* Ah, just ignore the garbage */
685 }
686
687 pszHostAddr = natPf.Pfr.szPfrHostAddr;
688
689 /* XXX: workaround for inet_pton and an empty ipv4 address
690 * in rule declaration.
691 */
692 if ( sockFamily == PF_INET
693 && pszHostAddr[0] == 0)
694 pszHostAddr = (char *)"0.0.0.0"; /* XXX: fix it! without type cast */
695
696
697 lrc = fwspec_set(&natPf.FWSpec,
698 sockFamily,
699 socketSpec,
700 pszHostAddr,
701 natPf.Pfr.u16PfrHostPort,
702 natPf.Pfr.szPfrGuestAddr,
703 natPf.Pfr.u16PfrGuestPort);
704
705 AssertReturn(!lrc, VERR_IGNORED);
706
707 fwspec *pFwCopy = (fwspec *)RTMemAllocZ(sizeof(fwspec));
708 AssertPtrReturn(pFwCopy, VERR_IGNORED);
709
710 /*
711 * We need pass the copy, because we can't be sure
712 * how much this pointer will be valid in LWIP environment.
713 */
714 memcpy(pFwCopy, &natPf.FWSpec, sizeof(fwspec));
715
716 lrc = portfwd_rule_add(pFwCopy);
717
718 AssertReturn(!lrc, VERR_IGNORED);
719
720 return VINF_SUCCESS;
721}
722
723
724int VBoxNetLwipNAT::natServiceProcessRegisteredPf(VECNATSERVICEPF& vecRules){
725 ITERATORNATSERVICEPF it;
726 for (it = vecRules.begin();
727 it != vecRules.end(); ++it)
728 {
729 int rc = natServicePfRegister((*it));
730 if (RT_FAILURE(rc))
731 {
732 LogRel(("PF: %s is ignored\n", (*it).Pfr.szPfrName));
733 continue;
734 }
735 }
736 return VINF_SUCCESS;
737}
738
739
740int VBoxNetLwipNAT::init()
741{
742 HRESULT hrc;
743 LogFlowFuncEnter();
744
745
746 /* virtualbox initialized in super class */
747
748 int rc = ::VBoxNetBaseService::init();
749 AssertRCReturn(rc, rc);
750
751 std::string networkName = getNetwork();
752 hrc = virtualbox->FindNATNetworkByName(com::Bstr(networkName.c_str()).raw(),
753 m_net.asOutParam());
754 AssertComRCReturn(hrc, VERR_NOT_FOUND);
755
756 hrc = m_listener.createObject();
757 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
758
759 hrc = m_listener->init(new NATNetworkListener(), this);
760 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
761
762 ComPtr<IEventSource> esNet;
763 hrc = m_net->COMGETTER(EventSource)(esNet.asOutParam());
764 AssertComRC(hrc);
765
766 com::SafeArray<VBoxEventType_T> aNetEvents;
767 aNetEvents.push_back(VBoxEventType_OnNATNetworkPortForward);
768 aNetEvents.push_back(VBoxEventType_OnNATNetworkSetting);
769 hrc = esNet->RegisterListener(m_listener, ComSafeArrayAsInParam(aNetEvents), true);
770 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
771
772
773 // resolver changes are reported on vbox but are retrieved from
774 // host so stash a pointer for future lookups
775 hrc = virtualbox->COMGETTER(Host)(m_host.asOutParam());
776 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
777
778 hrc = m_vboxListener.createObject();
779 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
780
781 hrc = m_vboxListener->init(new NATNetworkListener(), this);
782 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
783
784 ComPtr<IEventSource> esVBox;
785 hrc = virtualbox->COMGETTER(EventSource)(esVBox.asOutParam());
786 AssertComRC(hrc);
787
788 com::SafeArray<VBoxEventType_T> aVBoxEvents;
789 aVBoxEvents.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange);
790 hrc = esVBox->RegisterListener(m_vboxListener, ComSafeArrayAsInParam(aVBoxEvents), true);
791 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
792
793
794 BOOL fIPv6Enabled = FALSE;
795 hrc = m_net->COMGETTER(IPv6Enabled)(&fIPv6Enabled);
796 AssertComRCReturn(hrc, VERR_NOT_FOUND);
797
798 BOOL fIPv6DefaultRoute = FALSE;
799 if (fIPv6Enabled)
800 {
801 hrc = m_net->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
802 AssertComRCReturn(hrc, VERR_NOT_FOUND);
803 }
804
805 m_ProxyOptions.ipv6_enabled = fIPv6Enabled;
806 m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
807
808
809 com::Bstr bstrSourceIp4Key = com::BstrFmt("NAT/%s/SourceIp4", networkName.c_str());
810 com::Bstr bstrSourceIpX;
811 hrc = virtualbox->GetExtraData(bstrSourceIp4Key.raw(), bstrSourceIpX.asOutParam());
812 if (SUCCEEDED(hrc))
813 {
814 RTNETADDRIPV4 addr;
815 rc = RTNetStrToIPv4Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr);
816 if (RT_SUCCESS(rc))
817 {
818 RT_ZERO(m_src4);
819
820 m_src4.sin_addr.s_addr = addr.u;
821 m_ProxyOptions.src4 = &m_src4;
822
823 bstrSourceIpX.setNull();
824 }
825 }
826
827 if (!fDontLoadRulesOnStartup)
828 {
829 /* XXX: extract function and do not duplicate */
830 com::SafeArray<BSTR> rules;
831 hrc = m_net->COMGETTER(PortForwardRules4)(ComSafeArrayAsOutParam(rules));
832 Assert(SUCCEEDED(hrc));
833
834 size_t idxRules = 0;
835 for (idxRules = 0; idxRules < rules.size(); ++idxRules)
836 {
837 Log(("%d-rule: %ls\n", idxRules, rules[idxRules]));
838 NATSEVICEPORTFORWARDRULE Rule;
839 RT_ZERO(Rule);
840 rc = netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 0, &Rule.Pfr);
841 AssertRC(rc);
842 m_vecPortForwardRule4.push_back(Rule);
843 }
844
845 rules.setNull();
846 hrc = m_net->COMGETTER(PortForwardRules6)(ComSafeArrayAsOutParam(rules));
847 Assert(SUCCEEDED(hrc));
848
849 for (idxRules = 0; idxRules < rules.size(); ++idxRules)
850 {
851 Log(("%d-rule: %ls\n", idxRules, rules[idxRules]));
852 NATSEVICEPORTFORWARDRULE Rule;
853 netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 1, &Rule.Pfr);
854 m_vecPortForwardRule6.push_back(Rule);
855 }
856 } /* if (!fDontLoadRulesOnStartup) */
857
858 com::SafeArray<BSTR> strs;
859 int count_strs;
860 hrc = m_net->COMGETTER(LocalMappings)(ComSafeArrayAsOutParam(strs));
861 if ( SUCCEEDED(hrc)
862 && (count_strs = strs.size()))
863 {
864 unsigned int j = 0;
865 int i;
866
867 for (i = 0; i < count_strs && j < RT_ELEMENTS(m_lo2off); ++i)
868 {
869 char szAddr[17];
870 RTNETADDRIPV4 ip4addr;
871 char *pszTerm;
872 uint32_t u32Off;
873 com::Utf8Str strLo2Off(strs[i]);
874 const char *pszLo2Off = strLo2Off.c_str();
875
876 RT_ZERO(szAddr);
877
878 pszTerm = RTStrStr(pszLo2Off, "=");
879
880 if ( !pszTerm
881 || (pszTerm - pszLo2Off) >= 17)
882 continue;
883
884 memcpy(szAddr, pszLo2Off, (pszTerm - pszLo2Off));
885 rc = RTNetStrToIPv4Addr(szAddr, &ip4addr);
886 if (RT_FAILURE(rc))
887 continue;
888
889 u32Off = RTStrToUInt32(pszTerm + 1);
890 if (u32Off == 0)
891 continue;
892
893 ip4_addr_set_u32(&m_lo2off[j].loaddr, ip4addr.u);
894 m_lo2off[j].off = u32Off;
895 ++j;
896 }
897
898 m_loOptDescriptor.lomap = m_lo2off;
899 m_loOptDescriptor.num_lomap = j;
900 m_ProxyOptions.lomap_desc = &m_loOptDescriptor;
901 }
902
903 com::Bstr bstr;
904 hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
905 AssertComRCReturn(hrc, VERR_NOT_FOUND);
906 if (!bstr.isEmpty())
907 {
908 com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s",
909 bstr.raw(), RTPATH_DELIMITER, "TFTP"));
910 char *pszStrTemp; // avoid const char ** vs char **
911 rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str());
912 AssertRC(rc);
913 m_ProxyOptions.tftp_root = pszStrTemp;
914 }
915
916 m_ProxyOptions.nameservers = getHostNameservers();
917
918 /* end of COM initialization */
919
920 rc = g_pLwipNat->tryGoOnline();
921 if (RT_FAILURE(rc))
922 {
923 return rc;
924 }
925
926 vboxLwipCoreInitialize(VBoxNetLwipNAT::onLwipTcpIpInit, this);
927
928 rc = RTThreadCreate(&g_pLwipNat->hThrIntNetRecv, /* thread handle*/
929 VBoxNetLwipNAT::intNetThreadRecv, /* routine */
930 NULL, /* user data */
931 128 * _1K, /* stack size */
932 RTTHREADTYPE_IO, /* type */
933 0, /* flags, @todo: waitable ?*/
934 "INTNET-RECV");
935 AssertRCReturn(rc,rc);
936
937
938
939 LogFlowFuncLeaveRC(rc);
940 return rc;
941}
942
943
944const char **VBoxNetLwipNAT::getHostNameservers()
945{
946 HRESULT hrc;
947
948 if (m_host.isNull())
949 {
950 return NULL;
951 }
952
953 com::SafeArray<BSTR> aNameServers;
954 hrc = m_host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(aNameServers));
955 if (FAILED(hrc))
956 {
957 return NULL;
958 }
959
960 const size_t cNameServers = aNameServers.size();
961 if (cNameServers == 0)
962 {
963 return NULL;
964 }
965
966 const char **ppcszNameServers =
967 (const char **)RTMemAllocZ(sizeof(char *) * (cNameServers + 1));
968 if (ppcszNameServers == NULL)
969 {
970 return NULL;
971 }
972
973 size_t idxLast = 0;
974 for (size_t i = 0; i < cNameServers; ++i)
975 {
976 com::Utf8Str strNameServer(aNameServers[i]);
977 ppcszNameServers[idxLast] = RTStrDup(strNameServer.c_str());
978 if (ppcszNameServers[idxLast] != NULL)
979 {
980 ++idxLast;
981 }
982 }
983
984 if (idxLast == 0)
985 {
986 RTMemFree(ppcszNameServers);
987 return NULL;
988 }
989
990 return ppcszNameServers;
991}
992
993
994int VBoxNetLwipNAT::parseOpt(int rc, const RTGETOPTUNION& Val)
995{
996 switch (rc)
997 {
998 case 'p':
999 case 'P':
1000 {
1001 NATSEVICEPORTFORWARDRULE Rule;
1002 VECNATSERVICEPF& rules = (rc == 'P'?
1003 m_vecPortForwardRule6
1004 : m_vecPortForwardRule4);
1005
1006 fDontLoadRulesOnStartup = true;
1007
1008 RT_ZERO(Rule);
1009
1010 int irc = netPfStrToPf(Val.psz, (rc == 'P'), &Rule.Pfr);
1011 rules.push_back(Rule);
1012 return VINF_SUCCESS;
1013 }
1014 default:;
1015 }
1016 return VERR_NOT_FOUND;
1017}
1018
1019
1020int VBoxNetLwipNAT::processFrame(void *pvFrame, size_t cbFrame)
1021{
1022 AssertReturn(pvFrame && cbFrame, VERR_INVALID_PARAMETER);
1023
1024 struct pbuf *pPbufHdr, *pPbuf;
1025 pPbufHdr = pPbuf = pbuf_alloc(PBUF_RAW, cbFrame, PBUF_POOL);
1026
1027 AssertMsgReturn(pPbuf, ("NAT: Can't allocate send buffer cbFrame=%u\n", cbFrame), VERR_INTERNAL_ERROR);
1028 AssertReturn(pPbufHdr->tot_len == cbFrame, VERR_INTERNAL_ERROR);
1029
1030 uint8_t *pu8Frame = (uint8_t *)pvFrame;
1031 while(pPbuf)
1032 {
1033 memcpy(pPbuf->payload, pu8Frame, pPbuf->len);
1034 pu8Frame += pPbuf->len;
1035 pPbuf = pPbuf->next;
1036 }
1037
1038 m_LwipNetIf.input(pPbufHdr, &m_LwipNetIf);
1039
1040 return VINF_SUCCESS;
1041}
1042
1043
1044int VBoxNetLwipNAT::processGSO(PCPDMNETWORKGSO pGso, size_t cbFrame)
1045{
1046 if (!PDMNetGsoIsValid(pGso, cbFrame,
1047 cbFrame - sizeof(PDMNETWORKGSO)))
1048 return VERR_INVALID_PARAMETER;
1049
1050 cbFrame -= sizeof(PDMNETWORKGSO);
1051 uint8_t abHdrScratch[256];
1052 uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso,
1053 cbFrame);
1054 for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
1055 {
1056 uint32_t cbSegFrame;
1057 void *pvSegFrame =
1058 PDMNetGsoCarveSegmentQD(pGso,
1059 (uint8_t *)(pGso + 1),
1060 cbFrame,
1061 abHdrScratch,
1062 iSeg,
1063 cSegs,
1064 &cbSegFrame);
1065
1066 struct pbuf *pPbuf = pbuf_alloc(PBUF_RAW, cbSegFrame, PBUF_POOL);
1067
1068 AssertMsgReturn(pPbuf, ("NAT: Can't allocate send buffer cbFrame=%u\n", cbSegFrame), VERR_INTERNAL_ERROR);
1069 AssertReturn(!pPbuf->next && pPbuf->len == cbSegFrame, VERR_INTERNAL_ERROR);
1070
1071 memcpy(pPbuf->payload, pvSegFrame, cbSegFrame);
1072 m_LwipNetIf.input(pPbuf, &g_pLwipNat->m_LwipNetIf);
1073 }
1074
1075 return VINF_SUCCESS;
1076}
1077
1078
1079int VBoxNetLwipNAT::run()
1080{
1081 /* EventQueue processing from VBoxHeadless.cpp */
1082 com::NativeEventQueue *gEventQ = NULL;
1083 gEventQ = com::NativeEventQueue::getMainEventQueue();
1084 while(true)
1085 {
1086 /* XXX:todo: graceful termination */
1087 gEventQ->processEventQueue(0);
1088 gEventQ->processEventQueue(500);
1089 }
1090
1091 vboxLwipCoreFinalize(VBoxNetLwipNAT::onLwipTcpIpFini, this);
1092
1093 m_vecPortForwardRule4.clear();
1094 m_vecPortForwardRule6.clear();
1095
1096 return VINF_SUCCESS;
1097}
1098
1099
1100/**
1101 * Entry point.
1102 */
1103extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
1104{
1105 LogFlowFuncEnter();
1106
1107 NOREF(envp);
1108
1109#ifdef RT_OS_WINDOWS
1110 WSADATA wsaData;
1111 int err;
1112
1113 err = WSAStartup(MAKEWORD(2,2), &wsaData);
1114 if (err)
1115 {
1116 fprintf(stderr, "wsastartup: failed (%d)\n", err);
1117 return 1;
1118 }
1119#endif
1120
1121 SOCKET icmpsock4 = INVALID_SOCKET;
1122 SOCKET icmpsock6 = INVALID_SOCKET;
1123#ifndef RT_OS_DARWIN
1124 const int icmpstype = SOCK_RAW;
1125#else
1126 /* on OS X it's not privileged */
1127 const int icmpstype = SOCK_DGRAM;
1128#endif
1129
1130 icmpsock4 = socket(AF_INET, icmpstype, IPPROTO_ICMP);
1131 if (icmpsock4 == INVALID_SOCKET)
1132 {
1133 perror("IPPROTO_ICMP");
1134 }
1135
1136 if (icmpsock4 != INVALID_SOCKET)
1137 {
1138#ifdef ICMP_FILTER // Linux specific
1139 struct icmp_filter flt = {
1140 ~(uint32_t)(
1141 (1U << ICMP_ECHOREPLY)
1142 | (1U << ICMP_DEST_UNREACH)
1143 | (1U << ICMP_TIME_EXCEEDED)
1144 )
1145 };
1146
1147 int status = setsockopt(icmpsock4, SOL_RAW, ICMP_FILTER,
1148 &flt, sizeof(flt));
1149 if (status < 0)
1150 {
1151 perror("ICMP_FILTER");
1152 }
1153#endif
1154 }
1155
1156 icmpsock6 = socket(AF_INET6, icmpstype, IPPROTO_ICMPV6);
1157 if (icmpsock6 == INVALID_SOCKET)
1158 {
1159 perror("IPPROTO_ICMPV6");
1160 }
1161
1162 if (icmpsock6 != INVALID_SOCKET)
1163 {
1164#ifdef ICMP6_FILTER // Windows doesn't support RFC 3542 API
1165 /*
1166 * XXX: We do this here for now, not in pxping.c, to avoid
1167 * name clashes between lwIP and system headers.
1168 */
1169 struct icmp6_filter flt;
1170 ICMP6_FILTER_SETBLOCKALL(&flt);
1171
1172 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &flt);
1173
1174 ICMP6_FILTER_SETPASS(ICMP6_DST_UNREACH, &flt);
1175 ICMP6_FILTER_SETPASS(ICMP6_PACKET_TOO_BIG, &flt);
1176 ICMP6_FILTER_SETPASS(ICMP6_TIME_EXCEEDED, &flt);
1177 ICMP6_FILTER_SETPASS(ICMP6_PARAM_PROB, &flt);
1178
1179 int status = setsockopt(icmpsock6, IPPROTO_ICMPV6, ICMP6_FILTER,
1180 &flt, sizeof(flt));
1181 if (status < 0)
1182 {
1183 perror("ICMP6_FILTER");
1184 }
1185#endif
1186 }
1187
1188 HRESULT hrc = com::Initialize();
1189#ifdef VBOX_WITH_XPCOM
1190 if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
1191 {
1192 char szHome[RTPATH_MAX] = "";
1193 com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
1194 return RTMsgErrorExit(RTEXITCODE_FAILURE,
1195 "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
1196 }
1197#endif
1198 if (FAILED(hrc))
1199 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM!");
1200
1201 g_pLwipNat = new VBoxNetLwipNAT(icmpsock4, icmpsock6);
1202
1203 Log2(("NAT: initialization\n"));
1204 int rc = g_pLwipNat->parseArgs(argc - 1, argv + 1);
1205 rc = (rc == 0) ? VINF_SUCCESS : VERR_GENERAL_FAILURE; /* XXX: FIXME */
1206
1207 if (RT_SUCCESS(rc))
1208 {
1209 rc = g_pLwipNat->init();
1210 }
1211
1212 if (RT_SUCCESS(rc))
1213 {
1214 g_pLwipNat->run();
1215 }
1216
1217 delete g_pLwipNat;
1218 return 0;
1219}
1220
1221
1222#ifndef VBOX_WITH_HARDENING
1223
1224int main(int argc, char **argv, char **envp)
1225{
1226 int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
1227 if (RT_FAILURE(rc))
1228 return RTMsgInitFailure(rc);
1229
1230 return TrustedMain(argc, argv, envp);
1231}
1232
1233# if defined(RT_OS_WINDOWS)
1234
1235static LRESULT CALLBACK WindowProc(HWND hwnd,
1236 UINT uMsg,
1237 WPARAM wParam,
1238 LPARAM lParam
1239)
1240{
1241 if(uMsg == WM_DESTROY)
1242 {
1243 PostQuitMessage(0);
1244 return 0;
1245 }
1246 return DefWindowProc (hwnd, uMsg, wParam, lParam);
1247}
1248
1249static LPCWSTR g_WndClassName = L"VBoxNetNatLwipClass";
1250
1251static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
1252{
1253 HWND hwnd = 0;
1254 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
1255 bool bExit = false;
1256
1257 /* Register the Window Class. */
1258 WNDCLASS wc;
1259 wc.style = 0;
1260 wc.lpfnWndProc = WindowProc;
1261 wc.cbClsExtra = 0;
1262 wc.cbWndExtra = sizeof(void *);
1263 wc.hInstance = hInstance;
1264 wc.hIcon = NULL;
1265 wc.hCursor = NULL;
1266 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
1267 wc.lpszMenuName = NULL;
1268 wc.lpszClassName = g_WndClassName;
1269
1270 ATOM atomWindowClass = RegisterClass(&wc);
1271
1272 if (atomWindowClass != 0)
1273 {
1274 /* Create the window. */
1275 hwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
1276 g_WndClassName, g_WndClassName,
1277 WS_POPUPWINDOW,
1278 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
1279
1280 if (hwnd)
1281 {
1282 SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
1283 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
1284
1285 MSG msg;
1286 while (GetMessage(&msg, NULL, 0, 0))
1287 {
1288 TranslateMessage(&msg);
1289 DispatchMessage(&msg);
1290 }
1291
1292 DestroyWindow (hwnd);
1293
1294 bExit = true;
1295 }
1296
1297 UnregisterClass (g_WndClassName, hInstance);
1298 }
1299
1300 if(bExit)
1301 {
1302 /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
1303 exit(0);
1304 }
1305
1306 return 0;
1307}
1308
1309
1310
1311/** (We don't want a console usually.) */
1312int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
1313{
1314#if 0
1315 NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
1316
1317 HANDLE hThread = CreateThread(
1318 NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
1319 0, /*__in SIZE_T dwStackSize, */
1320 MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
1321 NULL, /*__in_opt LPVOID lpParameter,*/
1322 0, /*__in DWORD dwCreationFlags,*/
1323 NULL /*__out_opt LPDWORD lpThreadId*/
1324 );
1325
1326 if(hThread != NULL)
1327 CloseHandle(hThread);
1328
1329#endif
1330 return main(__argc, __argv, environ);
1331}
1332# endif /* RT_OS_WINDOWS */
1333
1334#endif /* !VBOX_WITH_HARDENING */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette