1 | /* $Id: ComHostUtils.cpp 87698 2021-02-10 17:21:46Z 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.alldomusa.eu.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 localMappings(const ComNatPtr& nat, AddressToOffsetMapping& mapping)
|
---|
78 | {
|
---|
79 | mapping.clear();
|
---|
80 |
|
---|
81 | ComBstrArray strs;
|
---|
82 | size_t cStrs;
|
---|
83 | HRESULT hrc = nat->COMGETTER(LocalMappings)(ComSafeArrayAsOutParam(strs));
|
---|
84 | if ( SUCCEEDED(hrc)
|
---|
85 | && (cStrs = strs.size()))
|
---|
86 | {
|
---|
87 | for (size_t i = 0; i < cStrs; ++i)
|
---|
88 | {
|
---|
89 | char szAddr[17];
|
---|
90 | RTNETADDRIPV4 ip4addr;
|
---|
91 | char *pszTerm;
|
---|
92 | uint32_t u32Off;
|
---|
93 | com::Utf8Str strLo2Off(strs[i]);
|
---|
94 | const char *pszLo2Off = strLo2Off.c_str();
|
---|
95 |
|
---|
96 | RT_ZERO(szAddr);
|
---|
97 |
|
---|
98 | pszTerm = RTStrStr(pszLo2Off, "=");
|
---|
99 |
|
---|
100 | if ( pszTerm
|
---|
101 | && (pszTerm - pszLo2Off) <= INET_ADDRSTRLEN)
|
---|
102 | {
|
---|
103 | memcpy(szAddr, pszLo2Off, (pszTerm - pszLo2Off));
|
---|
104 | int rc = RTNetStrToIPv4Addr(szAddr, &ip4addr);
|
---|
105 | if (RT_SUCCESS(rc))
|
---|
106 | {
|
---|
107 | u32Off = RTStrToUInt32(pszTerm + 1);
|
---|
108 | if (u32Off != 0)
|
---|
109 | mapping.insert(
|
---|
110 | AddressToOffsetMapping::value_type(ip4addr, u32Off));
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else
|
---|
116 | return VERR_NOT_FOUND;
|
---|
117 |
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | int createNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr,
|
---|
123 | NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events)
|
---|
124 | {
|
---|
125 | ComObjPtr<NATNetworkListenerImpl> obj;
|
---|
126 | HRESULT hrc = obj.createObject();
|
---|
127 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
128 |
|
---|
129 | hrc = obj->init(new NATNetworkListener(), adapter);
|
---|
130 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
131 |
|
---|
132 | ComPtr<IEventSource> esVBox;
|
---|
133 | hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
134 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
135 |
|
---|
136 | listener = obj;
|
---|
137 |
|
---|
138 | hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
|
---|
139 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
140 |
|
---|
141 | return VINF_SUCCESS;
|
---|
142 | }
|
---|
143 |
|
---|
144 | int destroyNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr)
|
---|
145 | {
|
---|
146 | if (listener)
|
---|
147 | {
|
---|
148 | ComPtr<IEventSource> esVBox;
|
---|
149 | HRESULT hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
150 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
151 | if (!esVBox.isNull())
|
---|
152 | {
|
---|
153 | hrc = esVBox->UnregisterListener(listener);
|
---|
154 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
155 | }
|
---|
156 | listener.setNull();
|
---|
157 | }
|
---|
158 | return VINF_SUCCESS;
|
---|
159 | }
|
---|
160 |
|
---|
161 | int createClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr,
|
---|
162 | NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events)
|
---|
163 | {
|
---|
164 | ComObjPtr<NATNetworkListenerImpl> obj;
|
---|
165 | HRESULT hrc = obj.createObject();
|
---|
166 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
167 |
|
---|
168 | hrc = obj->init(new NATNetworkListener(), adapter);
|
---|
169 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
170 |
|
---|
171 | ComPtr<IEventSource> esVBox;
|
---|
172 | hrc = vboxclientptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
173 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
174 |
|
---|
175 | listener = obj;
|
---|
176 |
|
---|
177 | hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
|
---|
178 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
179 |
|
---|
180 | return VINF_SUCCESS;
|
---|
181 | }
|
---|
182 |
|
---|
183 | int destroyClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr)
|
---|
184 | {
|
---|
185 | if (listener)
|
---|
186 | {
|
---|
187 | ComPtr<IEventSource> esVBox;
|
---|
188 | HRESULT hrc = vboxclientptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
189 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
190 | if (!esVBox.isNull())
|
---|
191 | {
|
---|
192 | hrc = esVBox->UnregisterListener(listener);
|
---|
193 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
194 | }
|
---|
195 | listener.setNull();
|
---|
196 | }
|
---|
197 | return VINF_SUCCESS;
|
---|
198 | }
|
---|