VirtualBox

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

Last change on this file since 27976 was 27976, checked in by vboxsync, 15 years ago

*: scm cleans up whitespace and adds a new line at the end of ApplianceimplPrivate.h.

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