VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/VBoxNetNAT.cpp@ 30318

Last change on this file since 30318 was 30017, checked in by vboxsync, 14 years ago

NATService: clean up.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.3 KB
Line 
1/* $Id: VBoxNetNAT.cpp 30017 2010-06-03 18:31:57Z 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/** @page pg_net_nat VBoxNetNAT
19 *
20 * Write a few words...
21 *
22 */
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#include <iprt/net.h>
28#include <iprt/initterm.h>
29#include <iprt/alloca.h>
30#include <iprt/err.h>
31#include <iprt/time.h>
32#include <iprt/timer.h>
33#include <iprt/thread.h>
34#include <iprt/stream.h>
35#include <iprt/path.h>
36#include <iprt/param.h>
37#include <iprt/getopt.h>
38#include <iprt/string.h>
39#include <iprt/mem.h>
40#include <iprt/req.h>
41#include <iprt/file.h>
42#include <iprt/semaphore.h>
43#define LOG_GROUP LOG_GROUP_NAT_SERVICE
44#include <VBox/log.h>
45
46#include <VBox/sup.h>
47#include <VBox/intnet.h>
48#include <VBox/intnetinline.h>
49#include <VBox/pdmnetinline.h>
50#include <VBox/vmm.h>
51#include <VBox/version.h>
52
53#include <vector>
54#include <string>
55
56#include "../NetLib/VBoxNetLib.h"
57#include "../NetLib/VBoxNetBaseService.h"
58#include <libslirp.h>
59
60#ifdef RT_OS_WINDOWS /* WinMain */
61# include <Windows.h>
62# include <stdlib.h>
63#else
64# include <errno.h>
65#endif
66
67
68
69/*******************************************************************************
70* Structures and Typedefs *
71*******************************************************************************/
72
73class VBoxNetNAT : public VBoxNetBaseService
74{
75public:
76 VBoxNetNAT();
77 virtual ~VBoxNetNAT();
78 void usage(void);
79 void run(void);
80 void init(void);
81
82public:
83 PNATState m_pNATState;
84 RTNETADDRIPV4 m_Ipv4Netmask;
85 bool m_fPassDomain;
86 RTTHREAD m_ThrNAT;
87 RTTHREAD m_ThrSndNAT;
88 RTTHREAD m_ThrUrgSndNAT;
89#ifdef RT_OS_WINDOWS
90 HANDLE m_hWakeupEvent;
91#else
92 RTFILE m_PipeWrite;
93 RTFILE m_PipeRead;
94#endif
95 /** Queue for NAT-thread-external events. */
96 /** event to wakeup the guest receive thread */
97 RTSEMEVENT m_EventSend;
98 /** event to wakeup the guest urgent receive thread */
99 RTSEMEVENT m_EventUrgSend;
100
101 PRTREQQUEUE m_pReqQueue;
102 PRTREQQUEUE m_pSendQueue;
103 PRTREQQUEUE m_pUrgSendQueue;
104 volatile uint32_t cUrgPkt;
105 volatile uint32_t cPkt;
106 bool fIsRunning;
107};
108
109
110
111/*******************************************************************************
112* Global Variables *
113*******************************************************************************/
114/** Pointer to the NAT server. */
115class VBoxNetNAT *g_pNAT;
116static DECLCALLBACK(int) AsyncIoThread(RTTHREAD pThread, void *pvUser);
117static DECLCALLBACK(int) natSndThread(RTTHREAD pThread, void *pvUser);
118static DECLCALLBACK(int) natUrgSndThread(RTTHREAD pThread, void *pvUser);
119static void SendWorker(struct mbuf *m, size_t cb);
120static void IntNetSendWorker(bool urg, const void *pvFrame, size_t cbFrame, struct mbuf *m);
121
122
123static void natNotifyNATThread(void)
124{
125 int rc;
126#ifndef RT_OS_WINDOWS
127 /* kick select() */
128 rc = RTFileWrite(g_pNAT->m_PipeWrite, "", 1, NULL);
129#else
130 /* kick WSAWaitForMultipleEvents */
131 rc = WSASetEvent(g_pNAT->hWakeupEvent);
132#endif
133 AssertRC(rc);
134}
135
136VBoxNetNAT::VBoxNetNAT()
137{
138#if defined(RT_OS_WINDOWS)
139 /*@todo check if we can remove this*/
140 VBoxNetBaseService();
141#endif
142 m_enmTrunkType = kIntNetTrunkType_WhateverNone;
143 m_TrunkName = "";
144 m_MacAddress.au8[0] = 0x08;
145 m_MacAddress.au8[1] = 0x00;
146 m_MacAddress.au8[2] = 0x27;
147 m_MacAddress.au8[3] = 0x40;
148 m_MacAddress.au8[4] = 0x41;
149 m_MacAddress.au8[5] = 0x42;
150 m_Ipv4Address.u = RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8( 10, 0, 2, 1)));
151 m_Ipv4Netmask.u = 0xffff0000;
152 cPkt = 0;
153 cUrgPkt = 0;
154}
155
156VBoxNetNAT::~VBoxNetNAT() { }
157void VBoxNetNAT::init()
158{
159 int rc;
160
161 /*
162 * Initialize slirp.
163 */
164 rc = slirp_init(&m_pNATState, m_Ipv4Address.u, m_Ipv4Netmask.u, m_fPassDomain, false, 0, this);
165 AssertReleaseRC(rc);
166
167 slirp_set_ethaddr_and_activate_port_forwarding(m_pNATState, &m_MacAddress.au8[0], INADDR_ANY);
168#ifndef RT_OS_WINDOWS
169 /*
170 * Create the control pipe.
171 */
172 int fds[2];
173 if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
174 {
175 rc = RTErrConvertFromErrno(errno);
176 AssertReleaseRC(rc);
177 return;
178 }
179 m_PipeRead = fds[0];
180 m_PipeWrite = fds[1];
181#else
182 m_hWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto-reset event */
183 slirp_register_external_event(m_pNATState, m_hWakeupEvent, VBOX_WAKEUP_EVENT_INDEX);
184#endif
185 rc = RTReqCreateQueue(&m_pReqQueue);
186 AssertReleaseRC(rc);
187
188 rc = RTReqCreateQueue(&m_pSendQueue);
189 AssertReleaseRC(rc);
190
191 rc = RTReqCreateQueue(&m_pUrgSendQueue);
192 AssertReleaseRC(rc);
193
194 rc = RTThreadCreate(&m_ThrNAT, AsyncIoThread, this, 128 * _1K, RTTHREADTYPE_DEFAULT, 0, "NAT");
195 rc = RTThreadCreate(&m_ThrSndNAT, natSndThread, this, 128 * _1K, RTTHREADTYPE_DEFAULT, 0, "SndNAT");
196 rc = RTThreadCreate(&m_ThrUrgSndNAT, natUrgSndThread, this, 128 * _1K, RTTHREADTYPE_DEFAULT, 0, "UrgSndNAT");
197 rc = RTSemEventCreate(&m_EventSend);
198 rc = RTSemEventCreate(&m_EventUrgSend);
199 AssertReleaseRC(rc);
200}
201
202/* Mandatory functions */
203void VBoxNetNAT::run()
204{
205
206 /*
207 * The loop.
208 */
209 fIsRunning = true;
210 PINTNETRINGBUF pRingBuf = &m_pIfBuf->Recv;
211 //RTThreadSetType(RTThreadSelf(), RTTHREADTYPE_IO);
212 for (;;)
213 {
214 /*
215 * Wait for a packet to become available.
216 */
217 INTNETIFWAITREQ WaitReq;
218 WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
219 WaitReq.Hdr.cbReq = sizeof(WaitReq);
220 WaitReq.pSession = m_pSession;
221 WaitReq.hIf = m_hIf;
222 WaitReq.cMillies = 2000; /* 2 secs - the sleep is for some reason uninterruptible... */ /** @todo fix interruptability in SrvIntNet! */
223#if 1
224 RTReqProcess(m_pSendQueue, 0);
225 RTReqProcess(m_pUrgSendQueue, 0);
226#endif
227 int rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_WAIT, 0, &WaitReq.Hdr);
228 if (RT_FAILURE(rc))
229 {
230 if (rc == VERR_TIMEOUT)
231 continue;
232 LogRel(("VBoxNetNAT: VMMR0_DO_INTNET_IF_WAIT returned %Rrc\n", rc));
233 return;
234 }
235
236 /*
237 * Process the receive buffer.
238 */
239 PCINTNETHDR pHdr;
240 while ((pHdr = IntNetRingGetNextFrameToRead(pRingBuf)) != NULL)
241 {
242 uint16_t const u16Type = pHdr->u16Type;
243 if (RT_LIKELY( u16Type == INTNETHDR_TYPE_FRAME
244 || u16Type == INTNETHDR_TYPE_GSO))
245 {
246 size_t cbFrame = pHdr->cbFrame;
247 size_t cbIgnored;
248 void *pvSlirpFrame;
249 struct mbuf *m;
250 if (u16Type == INTNETHDR_TYPE_FRAME)
251 {
252 m = slirp_ext_m_get(g_pNAT->m_pNATState, cbFrame, &pvSlirpFrame, &cbIgnored);
253 if (!m)
254 {
255 LogRel(("NAT: Can't allocate send buffer cbFrame=%u\n", cbFrame));
256 break;
257 }
258 memcpy(pvSlirpFrame, IntNetHdrGetFramePtr(pHdr, m_pIfBuf), cbFrame);
259 IntNetRingSkipFrame(&m_pIfBuf->Recv);
260
261 /* don't wait, we may have to wakeup the NAT thread first */
262 rc = RTReqCallEx(m_pReqQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
263 (PFNRT)SendWorker, 2, m, cbFrame);
264 AssertReleaseRC(rc);
265 }
266 else
267 {
268 /** @todo pass these unmodified. */
269 PCPDMNETWORKGSO pGso = IntNetHdrGetGsoContext(pHdr, m_pIfBuf);
270 if (!PDMNetGsoIsValid(pGso, cbFrame, cbFrame - sizeof(*pGso)))
271 {
272 IntNetRingSkipFrame(&m_pIfBuf->Recv);
273 STAM_REL_COUNTER_INC(&m_pIfBuf->cStatBadFrames);
274 continue;
275 }
276
277 uint8_t abHdrScratch[256];
278 uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso, cbFrame - sizeof(*pGso));
279 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
280 {
281 uint32_t cbSegFrame;
282 void *pvSegFrame = PDMNetGsoCarveSegmentQD(pGso, (uint8_t *)(pGso + 1), cbFrame, abHdrScratch,
283 iSeg, cSegs, &cbSegFrame);
284 m = slirp_ext_m_get(g_pNAT->m_pNATState, cbFrame, &pvSlirpFrame, &cbIgnored);
285 if (!m)
286 {
287 LogRel(("NAT: Can't allocate send buffer cbSegFrame=%u seg=%u/%u\n", cbSegFrame, iSeg, cSegs));
288 break;
289 }
290 memcpy(pvSlirpFrame, pvSegFrame, cbFrame);
291
292 rc = RTReqCallEx(m_pReqQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
293 (PFNRT)SendWorker, 2, m, cbSegFrame);
294 AssertReleaseRC(rc);
295 }
296 IntNetRingSkipFrame(&m_pIfBuf->Recv);
297 }
298
299#ifndef RT_OS_WINDOWS
300 /* kick select() */
301 rc = RTFileWrite(m_PipeWrite, "", 1, NULL);
302 AssertRC(rc);
303#else
304 /* kick WSAWaitForMultipleEvents */
305 rc = WSASetEvent(m_hWakeupEvent);
306 AssertRelease(rc == TRUE);
307#endif
308 }
309 else if (u16Type == INTNETHDR_TYPE_PADDING)
310 IntNetRingSkipFrame(&m_pIfBuf->Recv);
311 else
312 {
313 IntNetRingSkipFrame(&m_pIfBuf->Recv);
314 STAM_REL_COUNTER_INC(&m_pIfBuf->cStatBadFrames);
315 }
316 }
317
318 }
319 fIsRunning = false;
320}
321
322void VBoxNetNAT::usage()
323{
324}
325
326/**
327 * Entry point.
328 */
329extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
330{
331 Log2(("NAT: main\n"));
332 g_pNAT = new VBoxNetNAT();
333 Log2(("NAT: parsing command line\n"));
334 int rc = g_pNAT->parseArgs(argc - 1, argv + 1);
335 if (!rc)
336 {
337 Log2(("NAT: initialization\n"));
338 g_pNAT->init();
339 Log2(("NAT: try go online\n"));
340 g_pNAT->tryGoOnline();
341 Log2(("NAT: main loop\n"));
342 g_pNAT->run();
343 }
344 delete g_pNAT;
345 return 0;
346}
347
348/** slirp's hooks */
349extern "C" int slirp_can_output(void * pvUser)
350{
351 return 1;
352}
353
354extern "C" void slirp_urg_output(void *pvUser, struct mbuf *m, const uint8_t *pu8Buf, int cb)
355{
356 int rc = RTReqCallEx(g_pNAT->m_pUrgSendQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
357 (PFNRT)IntNetSendWorker, 4, (uintptr_t)1, (uintptr_t)pu8Buf, (uintptr_t)cb, (uintptr_t)m);
358 ASMAtomicIncU32(&g_pNAT->cUrgPkt);
359 RTSemEventSignal(g_pNAT->m_EventUrgSend);
360 AssertReleaseRC(rc);
361}
362extern "C" void slirp_output(void *pvUser, struct mbuf *m, const uint8_t *pu8Buf, int cb)
363{
364 AssertRelease(g_pNAT == pvUser);
365 int rc = RTReqCallEx(g_pNAT->m_pSendQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
366 (PFNRT)IntNetSendWorker, 4, (uintptr_t)0, (uintptr_t)pu8Buf, (uintptr_t)cb, (uintptr_t)m);
367 ASMAtomicIncU32(&g_pNAT->cPkt);
368 RTSemEventSignal(g_pNAT->m_EventSend);
369 AssertReleaseRC(rc);
370}
371
372/**
373 * Worker function for drvNATSend().
374 * @thread "NAT" thread.
375 */
376static void SendWorker(struct mbuf *m, size_t cb)
377{
378 slirp_input(g_pNAT->m_pNATState, m, cb);
379}
380
381static void IntNetSendWorker(bool urg, const void *pvFrame, size_t cbFrame, struct mbuf *m)
382{
383 Log2(("VBoxNetNAT: going to send some bytes ... \n"));
384 VBoxNetNAT *pThis = g_pNAT;
385 INTNETIFSENDREQ SendReq;
386 int rc;
387
388 if (!urg)
389 {
390 while (ASMAtomicReadU32(&g_pNAT->cUrgPkt) != 0
391 || ASMAtomicReadU32(&g_pNAT->cPkt) == 0)
392 rc = RTSemEventWait(g_pNAT->m_EventSend, RT_INDEFINITE_WAIT);
393 }
394 else
395 {
396 while (ASMAtomicReadU32(&g_pNAT->cUrgPkt) == 0)
397 rc = RTSemEventWait(g_pNAT->m_EventUrgSend, RT_INDEFINITE_WAIT);
398 }
399 rc = IntNetRingWriteFrame(&pThis->m_pIfBuf->Send, pvFrame, cbFrame);
400 if (RT_FAILURE(rc))
401 {
402 SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
403 SendReq.Hdr.cbReq = sizeof(SendReq);
404 SendReq.pSession = pThis->m_pSession;
405 SendReq.hIf = pThis->m_hIf;
406 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
407
408 rc = IntNetRingWriteFrame(&pThis->m_pIfBuf->Send, pvFrame, cbFrame);
409
410 }
411 if (RT_SUCCESS(rc))
412 {
413 SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
414 SendReq.Hdr.cbReq = sizeof(SendReq);
415 SendReq.pSession = pThis->m_pSession;
416 SendReq.hIf = pThis->m_hIf;
417 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
418 }
419 if (RT_FAILURE(rc))
420 Log2(("VBoxNetNAT: Failed to send packet; rc=%Rrc\n", rc));
421
422 if (!urg)
423 {
424 ASMAtomicDecU32(&g_pNAT->cPkt);
425 }
426 else {
427 if (ASMAtomicDecU32(&g_pNAT->cUrgPkt) == 0)
428 RTSemEventSignal(g_pNAT->m_EventSend);
429 }
430 natNotifyNATThread();
431 slirp_ext_m_free(pThis->m_pNATState, m);
432 RTMemFree((void *)pvFrame);
433}
434
435static DECLCALLBACK(int) AsyncIoThread(RTTHREAD pThread, void *pvUser)
436{
437 VBoxNetNAT *pThis = (VBoxNetNAT *)pvUser;
438 int nFDs = -1;
439#ifdef RT_OS_WINDOWS
440 HANDLE *pahEvents = slirp_get_events(pThis->m_pNATState);
441#else /* RT_OS_WINDOWS */
442 unsigned int cPollNegRet = 0;
443#endif /* !RT_OS_WINDOWS */
444
445 LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
446
447 /*
448 * Polling loop.
449 */
450 for(;;)
451 {
452 /*
453 * To prevent concurent execution of sending/receving threads
454 */
455#ifndef RT_OS_WINDOWS
456 nFDs = slirp_get_nsock(pThis->m_pNATState);
457 /* allocation for all sockets + Management pipe */
458 struct pollfd *polls = (struct pollfd *)RTMemAlloc((1 + nFDs) * sizeof(struct pollfd) + sizeof(uint32_t));
459 if (polls == NULL)
460 return VERR_NO_MEMORY;
461
462 /* don't pass the managemant pipe */
463 slirp_select_fill(pThis->m_pNATState, &nFDs, &polls[1]);
464 unsigned int cMsTimeout = slirp_get_timeout_ms(pThis->m_pNATState);
465
466 polls[0].fd = pThis->m_PipeRead;
467 /* POLLRDBAND usually doesn't used on Linux but seems used on Solaris */
468 polls[0].events = POLLRDNORM|POLLPRI|POLLRDBAND;
469 polls[0].revents = 0;
470
471 int cChangedFDs = poll(polls, nFDs + 1, cMsTimeout);
472 if (cChangedFDs < 0)
473 {
474 if (errno == EINTR)
475 {
476 Log2(("NAT: signal was caught while sleep on poll\n"));
477 /* No error, just process all outstanding requests but don't wait */
478 cChangedFDs = 0;
479 }
480 else if (cPollNegRet++ > 128)
481 {
482 LogRel(("NAT:Poll returns (%s) suppressed %d\n", strerror(errno), cPollNegRet));
483 cPollNegRet = 0;
484 }
485 }
486
487 if (cChangedFDs >= 0)
488 {
489 slirp_select_poll(pThis->m_pNATState, &polls[1], nFDs);
490 if (polls[0].revents & (POLLRDNORM|POLLPRI|POLLRDBAND))
491 {
492 /* drain the pipe */
493 char ch[1];
494 size_t cbRead;
495 int counter = 0;
496 /*
497 * drvNATSend decoupled so we don't know how many times
498 * device's thread sends before we've entered multiplex,
499 * so to avoid false alarm drain pipe here to the very end
500 *
501 * @todo: Probably we should counter drvNATSend to count how
502 * deep pipe has been filed before drain.
503 *
504 * XXX:Make it reading exactly we need to drain the pipe.
505 */
506 RTFileRead(pThis->m_PipeRead, &ch, 1, &cbRead);
507 }
508 }
509 /* process _all_ outstanding requests but don't wait */
510 RTReqProcess(pThis->m_pReqQueue, 0);
511 RTMemFree(polls);
512
513#else /* RT_OS_WINDOWS */
514 nFDs = -1;
515 slirp_select_fill(pThis->m_pNATState, &nFDs);
516 DWORD dwEvent = WSAWaitForMultipleEvents(nFDs, pahEvents, FALSE,
517 slirp_get_timeout_ms(pThis->m_pNATState),
518 FALSE);
519 if ( (dwEvent < WSA_WAIT_EVENT_0 || dwEvent > WSA_WAIT_EVENT_0 + nFDs - 1)
520 && dwEvent != WSA_WAIT_TIMEOUT)
521 {
522 int error = WSAGetLastError();
523 LogRel(("NAT: WSAWaitForMultipleEvents returned %d (error %d)\n", dwEvent, error));
524 RTAssertReleasePanic();
525 }
526
527 if (dwEvent == WSA_WAIT_TIMEOUT)
528 {
529 /* only check for slow/fast timers */
530 slirp_select_poll(pThis->m_pNATState, /* fTimeout=*/true, /*fIcmp=*/false);
531 continue;
532 }
533
534 /* poll the sockets in any case */
535 slirp_select_poll(pThis->m_pNATState, /* fTimeout=*/false, /* fIcmp=*/(dwEvent == WSA_WAIT_EVENT_0));
536 /* process _all_ outstanding requests but don't wait */
537 RTReqProcess(pThis->m_pReqQueue, 0);
538#endif /* RT_OS_WINDOWS */
539 }
540
541 return VINF_SUCCESS;
542}
543
544static DECLCALLBACK(int) natSndThread(RTTHREAD pThread, void *pvUser)
545{
546 while (g_pNAT->fIsRunning)
547 RTReqProcess(g_pNAT->m_pSendQueue, 0);
548 return VINF_SUCCESS;
549}
550static DECLCALLBACK(int) natUrgSndThread(RTTHREAD pThread, void *pvUser)
551{
552 while (g_pNAT->fIsRunning)
553 RTReqProcess(g_pNAT->m_pUrgSendQueue, 0);
554 return VINF_SUCCESS;
555}
556
557#ifndef VBOX_WITH_HARDENING
558
559int main(int argc, char **argv, char **envp)
560{
561 int rc = RTR3InitAndSUPLib();
562 if (RT_FAILURE(rc))
563 {
564 RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: RTR3InitAndSupLib failed, rc=%Rrc\n", rc);
565 return 1;
566 }
567
568 return TrustedMain(argc, argv, envp);
569}
570
571# if defined(RT_OS_WINDOWS)
572
573static LRESULT CALLBACK WindowProc(HWND hwnd,
574 UINT uMsg,
575 WPARAM wParam,
576 LPARAM lParam
577)
578{
579 if(uMsg == WM_DESTROY)
580 {
581 PostQuitMessage(0);
582 return 0;
583 }
584 return DefWindowProc (hwnd, uMsg, wParam, lParam);
585}
586
587static LPCSTR g_WndClassName = "VBoxNetNatClass";
588
589static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
590{
591 HWND hwnd = 0;
592 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
593 bool bExit = false;
594
595 /* Register the Window Class. */
596 WNDCLASS wc;
597 wc.style = 0;
598 wc.lpfnWndProc = WindowProc;
599 wc.cbClsExtra = 0;
600 wc.cbWndExtra = sizeof(void *);
601 wc.hInstance = hInstance;
602 wc.hIcon = NULL;
603 wc.hCursor = NULL;
604 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
605 wc.lpszMenuName = NULL;
606 wc.lpszClassName = g_WndClassName;
607
608 ATOM atomWindowClass = RegisterClass(&wc);
609
610 if (atomWindowClass != 0)
611 {
612 /* Create the window. */
613 hwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
614 g_WndClassName, g_WndClassName,
615 WS_POPUPWINDOW,
616 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
617
618 if (hwnd)
619 {
620 SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
621 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
622
623 MSG msg;
624 while (GetMessage(&msg, NULL, 0, 0))
625 {
626 TranslateMessage(&msg);
627 DispatchMessage(&msg);
628 }
629
630 DestroyWindow (hwnd);
631
632 bExit = true;
633 }
634
635 UnregisterClass (g_WndClassName, hInstance);
636 }
637
638 if(bExit)
639 {
640 /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
641 exit(0);
642 }
643
644 return 0;
645}
646
647
648
649/** (We don't want a console usually.) */
650int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
651{
652#if 0
653 NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
654
655 HANDLE hThread = CreateThread(
656 NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
657 0, /*__in SIZE_T dwStackSize, */
658 MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
659 NULL, /*__in_opt LPVOID lpParameter,*/
660 0, /*__in DWORD dwCreationFlags,*/
661 NULL /*__out_opt LPDWORD lpThreadId*/
662 );
663
664 if(hThread != NULL)
665 CloseHandle(hThread);
666
667#endif
668 return main(__argc, __argv, environ);
669}
670# endif /* RT_OS_WINDOWS */
671
672#endif /* !VBOX_WITH_HARDENING */
673
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