VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/DHCP/VBoxNetDHCP.cpp@ 49842

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

DHCP/NAT and NAT are acting in the same manner: polling for events on main thread and waiting for packets on internal network on RECV thread. DHCP/Host-Only waits for packets on main thread.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.7 KB
Line 
1/* $Id: VBoxNetDHCP.cpp 49842 2013-12-09 13:49:46Z vboxsync $ */
2/** @file
3 * VBoxNetDHCP - DHCP Service for connecting to IntNet.
4 */
5
6/*
7 * Copyright (C) 2009-2011 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_dhcp VBoxNetDHCP
19 *
20 * Write a few words...
21 *
22 */
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#include <VBox/com/com.h>
28#include <VBox/com/listeners.h>
29#include <VBox/com/string.h>
30#include <VBox/com/Guid.h>
31#include <VBox/com/array.h>
32#include <VBox/com/ErrorInfo.h>
33#include <VBox/com/errorprint.h>
34#include <VBox/com/EventQueue.h>
35#include <VBox/com/VirtualBox.h>
36
37#include <iprt/alloca.h>
38#include <iprt/buildconfig.h>
39#include <iprt/err.h>
40#include <iprt/net.h> /* must come before getopt */
41#include <iprt/getopt.h>
42#include <iprt/initterm.h>
43#include <iprt/message.h>
44#include <iprt/param.h>
45#include <iprt/path.h>
46#include <iprt/stream.h>
47#include <iprt/time.h>
48#include <iprt/string.h>
49
50#include <VBox/sup.h>
51#include <VBox/intnet.h>
52#include <VBox/intnetinline.h>
53#include <VBox/vmm/vmm.h>
54#include <VBox/version.h>
55
56
57#include "../NetLib/VBoxNetLib.h"
58#include "../NetLib/shared_ptr.h"
59
60#include <vector>
61#include <list>
62#include <string>
63#include <map>
64
65#include "../NetLib/VBoxNetBaseService.h"
66#include "../NetLib/utils.h"
67
68#ifdef RT_OS_WINDOWS /* WinMain */
69# include <Windows.h>
70# include <stdlib.h>
71# ifdef INET_ADDRSTRLEN
72/* On Windows INET_ADDRSTRLEN defined as 22 Ws2ipdef.h, because it include port number */
73# undef INET_ADDRSTRLEN
74# endif
75# define INET_ADDRSTRLEN 16
76#else
77# include <netinet/in.h>
78#endif
79
80
81#include "Config.h"
82/*******************************************************************************
83* Structures and Typedefs *
84*******************************************************************************/
85/**
86 * DHCP server instance.
87 */
88class VBoxNetDhcp: public VBoxNetBaseService, public NATNetworkEventAdapter
89{
90public:
91 VBoxNetDhcp();
92 virtual ~VBoxNetDhcp();
93
94 int init();
95 void usage(void) { /* XXX: document options */ };
96 int parseOpt(int rc, const RTGETOPTUNION& getOptVal);
97 int processFrame(void *, size_t) {return VERR_IGNORED; };
98 int processGSO(PCPDMNETWORKGSO, size_t) {return VERR_IGNORED; };
99 int processUDP(void *, size_t);
100
101protected:
102 bool handleDhcpMsg(uint8_t uMsgType, PCRTNETBOOTP pDhcpMsg, size_t cb);
103
104 void debugPrintV(int32_t iMinLevel, bool fMsg, const char *pszFmt, va_list va) const;
105 static const char *debugDhcpName(uint8_t uMsgType);
106
107private:
108 int initNoMain();
109 int initWithMain();
110 HRESULT HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent);
111 int fetchAndUpdateDnsInfo();
112
113protected:
114 /** @name The DHCP server specific configuration data members.
115 * @{ */
116 /*
117 * XXX: what was the plan? SQL3 or plain text file?
118 * How it will coexists with managment from VBoxManagement, who should manage db
119 * in that case (VBoxManage, VBoxSVC ???)
120 */
121 std::string m_LeaseDBName;
122
123 /** @} */
124
125 /* corresponding dhcp server description in Main */
126 ComPtr<IDHCPServer> m_DhcpServer;
127
128 ComPtr<INATNetwork> m_NATNetwork;
129
130 /** Listener for Host DNS changes */
131 ComPtr<NATNetworkListenerImpl> m_vboxListener;
132 /*
133 * We will ignore cmd line parameters IFF there will be some DHCP specific arguments
134 * otherwise all paramters will come from Main.
135 */
136 bool m_fIgnoreCmdLineParameters;
137
138 /*
139 * -b -n 10.0.1.2 -m 255.255.255.0 -> to the list processing in
140 */
141 typedef struct
142 {
143 char Key;
144 std::string strValue;
145 } CMDLNPRM;
146 std::list<CMDLNPRM> CmdParameterll;
147 typedef std::list<CMDLNPRM>::iterator CmdParameterIterator;
148
149 /** @name Debug stuff
150 * @{ */
151 int32_t m_cVerbosity;
152 uint8_t m_uCurMsgType;
153 size_t m_cbCurMsg;
154 PCRTNETBOOTP m_pCurMsg;
155 VBOXNETUDPHDRS m_CurHdrs;
156 /** @} */
157};
158
159/*******************************************************************************
160* Global Variables *
161*******************************************************************************/
162/** Pointer to the DHCP server. */
163static VBoxNetDhcp *g_pDhcp;
164
165/* DHCP server specific options */
166static RTGETOPTDEF g_aOptionDefs[] =
167{
168 { "--lease-db", 'D', RTGETOPT_REQ_STRING },
169 { "--begin-config", 'b', RTGETOPT_REQ_NOTHING },
170 { "--gateway", 'g', RTGETOPT_REQ_IPV4ADDR },
171 { "--lower-ip", 'l', RTGETOPT_REQ_IPV4ADDR },
172 { "--upper-ip", 'u', RTGETOPT_REQ_IPV4ADDR },
173};
174
175/**
176 * Construct a DHCP server with a default configuration.
177 */
178VBoxNetDhcp::VBoxNetDhcp():VBoxNetBaseService("VBoxNetDhcp", "VBoxNetDhcp")
179{
180 /* m_enmTrunkType = kIntNetTrunkType_WhateverNone; */
181 RTMAC mac;
182 mac.au8[0] = 0x08;
183 mac.au8[1] = 0x00;
184 mac.au8[2] = 0x27;
185 mac.au8[3] = 0x40;
186 mac.au8[4] = 0x41;
187 mac.au8[5] = 0x42;
188 setMacAddress(mac);
189
190 RTNETADDRIPV4 address;
191 address.u = RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8( 10, 0, 2, 5)));
192 setIpv4Address(address);
193
194 setSendBufSize(8 * _1K);
195 setRecvBufSize(50 * _1K);
196
197 m_uCurMsgType = UINT8_MAX;
198 m_cbCurMsg = 0;
199 m_pCurMsg = NULL;
200 memset(&m_CurHdrs, '\0', sizeof(m_CurHdrs));
201
202 m_fIgnoreCmdLineParameters = true;
203
204 for(unsigned int i = 0; i < RT_ELEMENTS(g_aOptionDefs); ++i)
205 addCommandLineOption(&g_aOptionDefs[i]);
206}
207
208
209/**
210 * Destruct a DHCP server.
211 */
212VBoxNetDhcp::~VBoxNetDhcp()
213{
214}
215
216
217
218
219/**
220 * Parse the DHCP specific arguments.
221 *
222 * This callback caled for each paramenter so
223 * ....
224 * we nee post analisys of the parameters, at least
225 * for -b, -g, -l, -u, -m
226 */
227int VBoxNetDhcp::parseOpt(int rc, const RTGETOPTUNION& Val)
228{
229 CMDLNPRM prm;
230
231 /* Ok, we've entered here, thus we can't ignore cmd line parameters anymore */
232 m_fIgnoreCmdLineParameters = false;
233
234 prm.Key = rc;
235
236 switch (rc)
237 {
238 case 'l':
239 case 'u':
240 case 'g':
241 {
242 char buf[17];
243 RTStrPrintf(buf, 17, "%RTnaipv4", Val.IPv4Addr.u);
244 prm.strValue = buf;
245 CmdParameterll.push_back(prm);
246 }
247 break;
248
249 case 'b': // ignore
250 case 'D': // ignore
251 break;
252
253 default:
254 rc = RTGetOptPrintError(rc, &Val);
255 RTPrintf("Use --help for more information.\n");
256 return rc;
257 }
258
259 return VINF_SUCCESS;
260}
261
262int VBoxNetDhcp::init()
263{
264 int rc = this->VBoxNetBaseService::init();
265 AssertRCReturn(rc, rc);
266
267 NetworkManager *netManager = NetworkManager::getNetworkManager();
268
269 netManager->setOurAddress(getIpv4Address());
270 netManager->setOurNetmask(getIpv4Netmask());
271 netManager->setOurMac(getMacAddress());
272 netManager->setService(this);
273
274 if (isMainNeeded())
275 rc = initWithMain();
276 else
277 rc = initNoMain();
278
279 AssertRCReturn(rc, rc);
280
281 return VINF_SUCCESS;
282}
283
284
285int VBoxNetDhcp::processUDP(void *pv, size_t cbPv)
286{
287 PCRTNETBOOTP pDhcpMsg = (PCRTNETBOOTP)pv;
288 m_pCurMsg = pDhcpMsg;
289 m_cbCurMsg = cbPv;
290
291 uint8_t uMsgType;
292 if (RTNetIPv4IsDHCPValid(NULL /* why is this here? */, pDhcpMsg, cbPv, &uMsgType))
293 {
294 m_uCurMsgType = uMsgType;
295 {
296 /* To avoid fight with event processing thread */
297 VBoxNetALock(this);
298 handleDhcpMsg(uMsgType, pDhcpMsg, cbPv);
299 }
300 m_uCurMsgType = UINT8_MAX;
301 }
302 else
303 debugPrint(1, true, "VBoxNetDHCP: Skipping invalid DHCP packet.\n"); /** @todo handle pure bootp clients too? */
304
305 m_pCurMsg = NULL;
306 m_cbCurMsg = 0;
307
308 return VINF_SUCCESS;
309}
310
311
312/**
313 * Handles a DHCP message.
314 *
315 * @returns true if handled, false if not.
316 * @param uMsgType The message type.
317 * @param pDhcpMsg The DHCP message.
318 * @param cb The size of the DHCP message.
319 */
320bool VBoxNetDhcp::handleDhcpMsg(uint8_t uMsgType, PCRTNETBOOTP pDhcpMsg, size_t cb)
321{
322 if (pDhcpMsg->bp_op == RTNETBOOTP_OP_REQUEST)
323 {
324 NetworkManager *networkManager = NetworkManager::getNetworkManager();
325
326 switch (uMsgType)
327 {
328 case RTNET_DHCP_MT_DISCOVER:
329 return networkManager->handleDhcpReqDiscover(pDhcpMsg, cb);
330
331 case RTNET_DHCP_MT_REQUEST:
332 return networkManager->handleDhcpReqRequest(pDhcpMsg, cb);
333
334 case RTNET_DHCP_MT_DECLINE:
335 return networkManager->handleDhcpReqDecline(pDhcpMsg, cb);
336
337 case RTNET_DHCP_MT_RELEASE:
338 return networkManager->handleDhcpReqRelease(pDhcpMsg, cb);
339
340 case RTNET_DHCP_MT_INFORM:
341 debugPrint(0, true, "Should we handle this?");
342 break;
343
344 default:
345 debugPrint(0, true, "Unexpected.");
346 break;
347 }
348 }
349 return false;
350}
351
352/**
353 * Print debug message depending on the m_cVerbosity level.
354 *
355 * @param iMinLevel The minimum m_cVerbosity level for this message.
356 * @param fMsg Whether to dump parts for the current DHCP message.
357 * @param pszFmt The message format string.
358 * @param va Optional arguments.
359 */
360void VBoxNetDhcp::debugPrintV(int iMinLevel, bool fMsg, const char *pszFmt, va_list va) const
361{
362 if (iMinLevel <= m_cVerbosity)
363 {
364 va_list vaCopy; /* This dude is *very* special, thus the copy. */
365 va_copy(vaCopy, va);
366 RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: %s: %N\n", iMinLevel >= 2 ? "debug" : "info", pszFmt, &vaCopy);
367 va_end(vaCopy);
368
369 if ( fMsg
370 && m_cVerbosity >= 2
371 && m_pCurMsg)
372 {
373 /* XXX: export this to debugPrinfDhcpMsg or variant and other method export
374 * to base class
375 */
376 const char *pszMsg = m_uCurMsgType != UINT8_MAX ? debugDhcpName(m_uCurMsgType) : "";
377 RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: debug: %8s chaddr=%.6Rhxs ciaddr=%d.%d.%d.%d yiaddr=%d.%d.%d.%d siaddr=%d.%d.%d.%d xid=%#x\n",
378 pszMsg,
379 &m_pCurMsg->bp_chaddr,
380 m_pCurMsg->bp_ciaddr.au8[0], m_pCurMsg->bp_ciaddr.au8[1], m_pCurMsg->bp_ciaddr.au8[2], m_pCurMsg->bp_ciaddr.au8[3],
381 m_pCurMsg->bp_yiaddr.au8[0], m_pCurMsg->bp_yiaddr.au8[1], m_pCurMsg->bp_yiaddr.au8[2], m_pCurMsg->bp_yiaddr.au8[3],
382 m_pCurMsg->bp_siaddr.au8[0], m_pCurMsg->bp_siaddr.au8[1], m_pCurMsg->bp_siaddr.au8[2], m_pCurMsg->bp_siaddr.au8[3],
383 m_pCurMsg->bp_xid);
384 }
385 }
386}
387
388
389/**
390 * Gets the name of given DHCP message type.
391 *
392 * @returns Readonly name.
393 * @param uMsgType The message number.
394 */
395/* static */ const char *VBoxNetDhcp::debugDhcpName(uint8_t uMsgType)
396{
397 switch (uMsgType)
398 {
399 case 0: return "MT_00";
400 case RTNET_DHCP_MT_DISCOVER: return "DISCOVER";
401 case RTNET_DHCP_MT_OFFER: return "OFFER";
402 case RTNET_DHCP_MT_REQUEST: return "REQUEST";
403 case RTNET_DHCP_MT_DECLINE: return "DECLINE";
404 case RTNET_DHCP_MT_ACK: return "ACK";
405 case RTNET_DHCP_MT_NAC: return "NAC";
406 case RTNET_DHCP_MT_RELEASE: return "RELEASE";
407 case RTNET_DHCP_MT_INFORM: return "INFORM";
408 case 9: return "MT_09";
409 case 10: return "MT_0a";
410 case 11: return "MT_0b";
411 case 12: return "MT_0c";
412 case 13: return "MT_0d";
413 case 14: return "MT_0e";
414 case 15: return "MT_0f";
415 case 16: return "MT_10";
416 case 17: return "MT_11";
417 case 18: return "MT_12";
418 case 19: return "MT_13";
419 case UINT8_MAX: return "MT_ff";
420 default: return "UNKNOWN";
421 }
422}
423
424
425int VBoxNetDhcp::initNoMain()
426{
427 CmdParameterIterator it;
428
429 RTNETADDRIPV4 address = getIpv4Address();
430 RTNETADDRIPV4 netmask = getIpv4Netmask();
431 RTNETADDRIPV4 networkId;
432 networkId.u = address.u & netmask.u;
433
434 RTNETADDRIPV4 UpperAddress;
435 RTNETADDRIPV4 LowerAddress = networkId;
436 UpperAddress.u = RT_H2N_U32(RT_N2H_U32(LowerAddress.u) | RT_N2H_U32(netmask.u));
437
438 for (it = CmdParameterll.begin(); it != CmdParameterll.end(); ++it)
439 {
440 switch(it->Key)
441 {
442 case 'l':
443 RTNetStrToIPv4Addr(it->strValue.c_str(), &LowerAddress);
444 break;
445
446 case 'u':
447 RTNetStrToIPv4Addr(it->strValue.c_str(), &UpperAddress);
448 break;
449 case 'b':
450 break;
451
452 }
453 }
454
455 ConfigurationManager *confManager = ConfigurationManager::getConfigurationManager();
456 AssertPtrReturn(confManager, VERR_INTERNAL_ERROR);
457 confManager->addNetwork(unconst(g_RootConfig),
458 networkId,
459 netmask,
460 LowerAddress,
461 UpperAddress);
462
463 return VINF_SUCCESS;
464}
465
466
467int VBoxNetDhcp::initWithMain()
468{
469 /* ok, here we should initiate instance of dhcp server
470 * and listener for Dhcp configuration events
471 */
472 AssertRCReturn(virtualbox.isNull(), VERR_INTERNAL_ERROR);
473 std::string networkName = getNetwork();
474
475 int rc = findDhcpServer(virtualbox, networkName, m_DhcpServer);
476 AssertRCReturn(rc, rc);
477
478 rc = findNatNetwork(virtualbox, networkName, m_NATNetwork);
479 AssertRCReturn(rc, rc);
480
481 BOOL fNeedDhcpServer = isDhcpRequired(m_NATNetwork);
482 if (!fNeedDhcpServer)
483 return VERR_CANCELLED;
484
485 RTNETADDRIPV4 gateway;
486 com::Bstr strGateway;
487 HRESULT hrc = m_NATNetwork->COMGETTER(Gateway)(strGateway.asOutParam());
488 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
489 RTNetStrToIPv4Addr(com::Utf8Str(strGateway).c_str(), &gateway);
490
491 ConfigurationManager *confManager = ConfigurationManager::getConfigurationManager();
492 AssertPtrReturn(confManager, VERR_INTERNAL_ERROR);
493 confManager->addToAddressList(RTNET_DHCP_OPT_ROUTERS, gateway);
494
495 rc = fetchAndUpdateDnsInfo();
496 AssertMsgRCReturn(rc, ("Wasn't able to fetch Dns info"), rc);
497
498 ComEventTypeArray aVBoxEvents;
499 aVBoxEvents.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange);
500 rc = createNatListener(m_vboxListener, virtualbox, this, aVBoxEvents);
501 AssertRCReturn(rc, rc);
502
503 /* XXX: extract function here. */
504 com::Bstr strUpperIp, strLowerIp;
505 RTNETADDRIPV4 LowerAddress;
506 RTNETADDRIPV4 UpperAddress;
507 hrc = m_DhcpServer->COMGETTER(UpperIP)(strUpperIp.asOutParam());
508 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
509 RTNetStrToIPv4Addr(com::Utf8Str(strUpperIp).c_str(), &UpperAddress);
510 hrc = m_DhcpServer->COMGETTER(LowerIP)(strLowerIp.asOutParam());
511 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
512 RTNetStrToIPv4Addr(com::Utf8Str(strLowerIp).c_str(), &LowerAddress);
513
514 RTNETADDRIPV4 address = getIpv4Address();
515 RTNETADDRIPV4 netmask = getIpv4Netmask();
516 RTNETADDRIPV4 networkId = networkid(address, netmask);
517 std::string name = std::string("default");
518
519 confManager->addNetwork(unconst(g_RootConfig),
520 networkId,
521 netmask,
522 LowerAddress,
523 UpperAddress);
524
525 com::Bstr bstr;
526 hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
527 std::string strXmlLeaseFile(com::Utf8StrFmt("%ls%c%s.leases",
528 bstr.raw(), RTPATH_DELIMITER, networkName.c_str()).c_str());
529 confManager->loadFromFile(strXmlLeaseFile);
530
531 return VINF_SUCCESS;
532}
533
534
535int VBoxNetDhcp::fetchAndUpdateDnsInfo()
536{
537 ComHostPtr host;
538 if (SUCCEEDED(virtualbox->COMGETTER(Host)(host.asOutParam())))
539 {
540 AddressToOffsetMapping mapIp4Addr2Off;
541 int rc = localMappings(m_NATNetwork, mapIp4Addr2Off);
542 /* XXX: here could be several cases: 1. COM error, 2. not found (empty) 3. ? */
543 AssertMsgRCReturn(rc, ("Can't fetch local mappings"), rc);
544
545 RTNETADDRIPV4 address = getIpv4Address();
546 RTNETADDRIPV4 netmask = getIpv4Netmask();
547
548 AddressList nameservers;
549 rc = hostDnsServers(host, networkid(address, netmask), mapIp4Addr2Off, nameservers);
550 AssertMsgRCReturn(rc, ("Debug me!!!"), rc);
551 /* XXX: Search strings */
552
553 std::string domain;
554 rc = hostDnsDomain(host, domain);
555 AssertMsgRCReturn(rc, ("Debug me!!"), rc);
556
557 {
558 VBoxNetALock(this);
559 ConfigurationManager *confManager = ConfigurationManager::getConfigurationManager();
560 confManager->flushAddressList(RTNET_DHCP_OPT_DNS);
561
562 for (AddressList::iterator it = nameservers.begin(); it != nameservers.end(); ++it)
563 confManager->addToAddressList(RTNET_DHCP_OPT_DNS, *it);
564
565 confManager->setString(RTNET_DHCP_OPT_DOMAIN_NAME, domain);
566 }
567 }
568
569 return VINF_SUCCESS;
570}
571
572
573HRESULT VBoxNetDhcp::HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent)
574{
575 switch(aEventType)
576 {
577 case VBoxEventType_OnHostNameResolutionConfigurationChange:
578 fetchAndUpdateDnsInfo();
579 break;
580 }
581
582 return S_OK;
583}
584
585/**
586 * Entry point.
587 */
588extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv)
589{
590 /*
591 * Instantiate the DHCP server and hand it the options.
592 */
593
594 VBoxNetDhcp *pDhcp = new VBoxNetDhcp();
595 if (!pDhcp)
596 {
597 RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: new VBoxNetDhcp failed!\n");
598 return 1;
599 }
600 int rc = pDhcp->parseArgs(argc - 1, argv + 1);
601 if (rc)
602 return rc;
603
604 pDhcp->init();
605
606 /*
607 * Try connect the server to the network.
608 */
609 rc = pDhcp->tryGoOnline();
610 if (RT_FAILURE(rc))
611 {
612 delete pDhcp;
613 return 1;
614 }
615
616 /*
617 * Process requests.
618 */
619 g_pDhcp = pDhcp;
620 rc = pDhcp->run();
621 g_pDhcp = NULL;
622 delete pDhcp;
623
624 return 0;
625}
626
627
628#ifndef VBOX_WITH_HARDENING
629
630int main(int argc, char **argv)
631{
632 int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
633 if (RT_FAILURE(rc))
634 return RTMsgInitFailure(rc);
635
636 return TrustedMain(argc, argv);
637}
638
639# ifdef RT_OS_WINDOWS
640
641static LRESULT CALLBACK WindowProc(HWND hwnd,
642 UINT uMsg,
643 WPARAM wParam,
644 LPARAM lParam
645)
646{
647 if(uMsg == WM_DESTROY)
648 {
649 PostQuitMessage(0);
650 return 0;
651 }
652 return DefWindowProc (hwnd, uMsg, wParam, lParam);
653}
654
655static LPCWSTR g_WndClassName = L"VBoxNetDHCPClass";
656
657static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
658{
659 HWND hwnd = 0;
660 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
661 bool bExit = false;
662
663 /* Register the Window Class. */
664 WNDCLASS wc;
665 wc.style = 0;
666 wc.lpfnWndProc = WindowProc;
667 wc.cbClsExtra = 0;
668 wc.cbWndExtra = sizeof(void *);
669 wc.hInstance = hInstance;
670 wc.hIcon = NULL;
671 wc.hCursor = NULL;
672 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
673 wc.lpszMenuName = NULL;
674 wc.lpszClassName = g_WndClassName;
675
676 ATOM atomWindowClass = RegisterClass(&wc);
677
678 if (atomWindowClass != 0)
679 {
680 /* Create the window. */
681 hwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
682 g_WndClassName, g_WndClassName,
683 WS_POPUPWINDOW,
684 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
685
686 if (hwnd)
687 {
688 SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
689 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
690
691 MSG msg;
692 while (GetMessage(&msg, NULL, 0, 0))
693 {
694 TranslateMessage(&msg);
695 DispatchMessage(&msg);
696 }
697
698 DestroyWindow (hwnd);
699
700 bExit = true;
701 }
702
703 UnregisterClass (g_WndClassName, hInstance);
704 }
705
706 if(bExit)
707 {
708 /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
709 exit(0);
710 }
711
712 return 0;
713}
714
715
716/** (We don't want a console usually.) */
717int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
718{
719 NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
720
721 HANDLE hThread = CreateThread(
722 NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
723 0, /*__in SIZE_T dwStackSize, */
724 MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
725 NULL, /*__in_opt LPVOID lpParameter,*/
726 0, /*__in DWORD dwCreationFlags,*/
727 NULL /*__out_opt LPDWORD lpThreadId*/
728 );
729
730 if(hThread != NULL)
731 CloseHandle(hThread);
732
733 return main(__argc, __argv);
734}
735# endif /* RT_OS_WINDOWS */
736
737#endif /* !VBOX_WITH_HARDENING */
738
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