1 | /* $Id: ComHostUtils.cpp 87710 2021-02-11 02:14:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ComHostUtils.cpp
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2020 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #if defined(RT_OS_WINDOWS) && !defined(VBOX_COM_OUTOFPROC_MODULE)
|
---|
23 | # define VBOX_COM_OUTOFPROC_MODULE
|
---|
24 | #endif
|
---|
25 | #include <VBox/com/com.h>
|
---|
26 | #include <VBox/com/listeners.h>
|
---|
27 | #include <VBox/com/string.h>
|
---|
28 | #include <VBox/com/Guid.h>
|
---|
29 | #include <VBox/com/array.h>
|
---|
30 | #include <VBox/com/ErrorInfo.h>
|
---|
31 | #include <VBox/com/errorprint.h>
|
---|
32 | #include <VBox/com/EventQueue.h>
|
---|
33 | #include <VBox/com/VirtualBox.h>
|
---|
34 |
|
---|
35 | #include <iprt/alloca.h>
|
---|
36 | #include <iprt/buildconfig.h>
|
---|
37 | #include <iprt/errcore.h>
|
---|
38 | #include <iprt/net.h> /* must come before getopt */
|
---|
39 | #include <iprt/getopt.h>
|
---|
40 | #include <iprt/initterm.h>
|
---|
41 | #include <iprt/message.h>
|
---|
42 | #include <iprt/param.h>
|
---|
43 | #include <iprt/path.h>
|
---|
44 | #include <iprt/stream.h>
|
---|
45 | #include <iprt/time.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | #include "../NetLib/VBoxNetLib.h"
|
---|
50 | #include "../NetLib/shared_ptr.h"
|
---|
51 |
|
---|
52 | #include <vector>
|
---|
53 | #include <list>
|
---|
54 | #include <iprt/sanitized/string>
|
---|
55 | #include <map>
|
---|
56 |
|
---|
57 | #include "../NetLib/VBoxNetBaseService.h"
|
---|
58 |
|
---|
59 | #ifdef RT_OS_WINDOWS /* WinMain */
|
---|
60 | # include <iprt/win/windows.h>
|
---|
61 | # include <stdlib.h>
|
---|
62 | # ifdef INET_ADDRSTRLEN
|
---|
63 | /* On Windows INET_ADDRSTRLEN defined as 22 Ws2ipdef.h, because it include port number */
|
---|
64 | # undef INET_ADDRSTRLEN
|
---|
65 | # endif
|
---|
66 | # define INET_ADDRSTRLEN 16
|
---|
67 | #else
|
---|
68 | # include <netinet/in.h>
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #include "utils.h"
|
---|
72 |
|
---|
73 |
|
---|
74 | VBOX_LISTENER_DECLARE(NATNetworkListenerImpl)
|
---|
75 |
|
---|
76 |
|
---|
77 | int createNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr,
|
---|
78 | NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events)
|
---|
79 | {
|
---|
80 | ComObjPtr<NATNetworkListenerImpl> obj;
|
---|
81 | HRESULT hrc = obj.createObject();
|
---|
82 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
83 |
|
---|
84 | hrc = obj->init(new NATNetworkListener(), adapter);
|
---|
85 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
86 |
|
---|
87 | ComPtr<IEventSource> esVBox;
|
---|
88 | hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
89 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
90 |
|
---|
91 | listener = obj;
|
---|
92 |
|
---|
93 | hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
|
---|
94 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
95 |
|
---|
96 | return VINF_SUCCESS;
|
---|
97 | }
|
---|
98 |
|
---|
99 | int destroyNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr)
|
---|
100 | {
|
---|
101 | if (listener)
|
---|
102 | {
|
---|
103 | ComPtr<IEventSource> esVBox;
|
---|
104 | HRESULT hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
105 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
106 | if (!esVBox.isNull())
|
---|
107 | {
|
---|
108 | hrc = esVBox->UnregisterListener(listener);
|
---|
109 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
110 | }
|
---|
111 | listener.setNull();
|
---|
112 | }
|
---|
113 | return VINF_SUCCESS;
|
---|
114 | }
|
---|
115 |
|
---|
116 | int createClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr,
|
---|
117 | NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events)
|
---|
118 | {
|
---|
119 | ComObjPtr<NATNetworkListenerImpl> obj;
|
---|
120 | HRESULT hrc = obj.createObject();
|
---|
121 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
122 |
|
---|
123 | hrc = obj->init(new NATNetworkListener(), adapter);
|
---|
124 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
125 |
|
---|
126 | ComPtr<IEventSource> esVBox;
|
---|
127 | hrc = vboxclientptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
128 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
129 |
|
---|
130 | listener = obj;
|
---|
131 |
|
---|
132 | hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
|
---|
133 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
134 |
|
---|
135 | return VINF_SUCCESS;
|
---|
136 | }
|
---|
137 |
|
---|
138 | int destroyClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr)
|
---|
139 | {
|
---|
140 | if (listener)
|
---|
141 | {
|
---|
142 | ComPtr<IEventSource> esVBox;
|
---|
143 | HRESULT hrc = vboxclientptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
144 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
145 | if (!esVBox.isNull())
|
---|
146 | {
|
---|
147 | hrc = esVBox->UnregisterListener(listener);
|
---|
148 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
149 | }
|
---|
150 | listener.setNull();
|
---|
151 | }
|
---|
152 | return VINF_SUCCESS;
|
---|
153 | }
|
---|