1 | /* $Id: ComHostUtils.cpp 56300 2015-06-09 14:36:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ComHostUtils.cpp
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2015 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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #ifdef RT_OS_WINDOWS
|
---|
22 | # define VBOX_COM_OUTOFPROC_MODULE
|
---|
23 | #endif
|
---|
24 | #include <VBox/com/com.h>
|
---|
25 | #include <VBox/com/listeners.h>
|
---|
26 | #include <VBox/com/string.h>
|
---|
27 | #include <VBox/com/Guid.h>
|
---|
28 | #include <VBox/com/array.h>
|
---|
29 | #include <VBox/com/ErrorInfo.h>
|
---|
30 | #include <VBox/com/errorprint.h>
|
---|
31 | #include <VBox/com/EventQueue.h>
|
---|
32 | #include <VBox/com/VirtualBox.h>
|
---|
33 |
|
---|
34 | #include <iprt/alloca.h>
|
---|
35 | #include <iprt/buildconfig.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/net.h> /* must come before getopt */
|
---|
38 | #include <iprt/getopt.h>
|
---|
39 | #include <iprt/initterm.h>
|
---|
40 | #include <iprt/message.h>
|
---|
41 | #include <iprt/param.h>
|
---|
42 | #include <iprt/path.h>
|
---|
43 | #include <iprt/stream.h>
|
---|
44 | #include <iprt/time.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | #include "../NetLib/VBoxNetLib.h"
|
---|
49 | #include "../NetLib/shared_ptr.h"
|
---|
50 |
|
---|
51 | #include <vector>
|
---|
52 | #include <list>
|
---|
53 | #include <string>
|
---|
54 | #include <map>
|
---|
55 |
|
---|
56 | #include "../NetLib/VBoxNetBaseService.h"
|
---|
57 |
|
---|
58 | #ifdef RT_OS_WINDOWS /* WinMain */
|
---|
59 | # include <Windows.h>
|
---|
60 | # include <stdlib.h>
|
---|
61 | # ifdef INET_ADDRSTRLEN
|
---|
62 | /* On Windows INET_ADDRSTRLEN defined as 22 Ws2ipdef.h, because it include port number */
|
---|
63 | # undef INET_ADDRSTRLEN
|
---|
64 | # endif
|
---|
65 | # define INET_ADDRSTRLEN 16
|
---|
66 | #else
|
---|
67 | # include <netinet/in.h>
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | #include "utils.h"
|
---|
71 |
|
---|
72 |
|
---|
73 | VBOX_LISTENER_DECLARE(NATNetworkListenerImpl)
|
---|
74 |
|
---|
75 |
|
---|
76 | int localMappings(const ComNatPtr& nat, AddressToOffsetMapping& mapping)
|
---|
77 | {
|
---|
78 | mapping.clear();
|
---|
79 |
|
---|
80 | ComBstrArray strs;
|
---|
81 | size_t cStrs;
|
---|
82 | HRESULT hrc = nat->COMGETTER(LocalMappings)(ComSafeArrayAsOutParam(strs));
|
---|
83 | if ( SUCCEEDED(hrc)
|
---|
84 | && (cStrs = strs.size()))
|
---|
85 | {
|
---|
86 | for (size_t i = 0; i < cStrs; ++i)
|
---|
87 | {
|
---|
88 | char szAddr[17];
|
---|
89 | RTNETADDRIPV4 ip4addr;
|
---|
90 | char *pszTerm;
|
---|
91 | uint32_t u32Off;
|
---|
92 | com::Utf8Str strLo2Off(strs[i]);
|
---|
93 | const char *pszLo2Off = strLo2Off.c_str();
|
---|
94 |
|
---|
95 | RT_ZERO(szAddr);
|
---|
96 |
|
---|
97 | pszTerm = RTStrStr(pszLo2Off, "=");
|
---|
98 |
|
---|
99 | if ( pszTerm
|
---|
100 | && (pszTerm - pszLo2Off) <= INET_ADDRSTRLEN)
|
---|
101 | {
|
---|
102 | memcpy(szAddr, pszLo2Off, (pszTerm - pszLo2Off));
|
---|
103 | int rc = RTNetStrToIPv4Addr(szAddr, &ip4addr);
|
---|
104 | if (RT_SUCCESS(rc))
|
---|
105 | {
|
---|
106 | u32Off = RTStrToUInt32(pszTerm + 1);
|
---|
107 | if (u32Off != 0)
|
---|
108 | mapping.insert(
|
---|
109 | AddressToOffsetMapping::value_type(ip4addr, u32Off));
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | else
|
---|
115 | return VERR_NOT_FOUND;
|
---|
116 |
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * @note: const dropped here, because of map<K,V>::operator[] which isn't const, map<K,V>::at() has const
|
---|
122 | * variant but it's C++11.
|
---|
123 | */
|
---|
124 | int hostDnsServers(const ComHostPtr& host, const RTNETADDRIPV4& networkid,
|
---|
125 | /*const*/ AddressToOffsetMapping& mapping, AddressList& servers)
|
---|
126 | {
|
---|
127 | servers.clear();
|
---|
128 |
|
---|
129 | ComBstrArray strs;
|
---|
130 | if (SUCCEEDED(host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(strs))))
|
---|
131 | {
|
---|
132 | RTNETADDRIPV4 addr;
|
---|
133 | int rc;
|
---|
134 |
|
---|
135 | for (unsigned int i = 0; i < strs.size(); ++i)
|
---|
136 | {
|
---|
137 | rc = RTNetStrToIPv4Addr(com::Utf8Str(strs[i]).c_str(), &addr);
|
---|
138 | if (RT_SUCCESS(rc))
|
---|
139 | {
|
---|
140 | if (addr.au8[0] == 127)
|
---|
141 | {
|
---|
142 | /* XXX: here we want map<K,V>::at(const K& k) const */
|
---|
143 | if (mapping[addr] != 0)
|
---|
144 | {
|
---|
145 | addr.u = RT_H2N_U32(RT_N2H_U32(networkid.u)
|
---|
146 | + mapping[addr]);
|
---|
147 | }
|
---|
148 | else
|
---|
149 | continue; /* XXX: Warning here (local mapping wasn't registered) */
|
---|
150 | }
|
---|
151 |
|
---|
152 | servers.push_back(addr);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | else
|
---|
157 | return VERR_NOT_FOUND;
|
---|
158 |
|
---|
159 | return VINF_SUCCESS;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | int hostDnsSearchList(const ComHostPtr& host, std::vector<std::string>& strings)
|
---|
164 | {
|
---|
165 | strings.clear();
|
---|
166 |
|
---|
167 | ComBstrArray strs;
|
---|
168 | if (SUCCEEDED(host->COMGETTER(SearchStrings)(ComSafeArrayAsOutParam(strs))))
|
---|
169 | {
|
---|
170 | for (unsigned int i = 0; i < strs.size(); ++i)
|
---|
171 | {
|
---|
172 | strings.push_back(com::Utf8Str(strs[i]).c_str());
|
---|
173 | }
|
---|
174 | }
|
---|
175 | else
|
---|
176 | return VERR_NOT_FOUND;
|
---|
177 |
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | int hostDnsDomain(const ComHostPtr& host, std::string& domainStr)
|
---|
183 | {
|
---|
184 | com::Bstr domain;
|
---|
185 | if (SUCCEEDED(host->COMGETTER(DomainName)(domain.asOutParam())))
|
---|
186 | {
|
---|
187 | domainStr = com::Utf8Str(domain).c_str();
|
---|
188 | return VINF_SUCCESS;
|
---|
189 | }
|
---|
190 |
|
---|
191 | return VERR_NOT_FOUND;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | int createNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr,
|
---|
196 | NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events)
|
---|
197 | {
|
---|
198 | ComObjPtr<NATNetworkListenerImpl> obj;
|
---|
199 | HRESULT hrc = obj.createObject();
|
---|
200 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
201 |
|
---|
202 | hrc = obj->init(new NATNetworkListener(), adapter);
|
---|
203 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
204 |
|
---|
205 | ComPtr<IEventSource> esVBox;
|
---|
206 | hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
207 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
208 |
|
---|
209 | listener = obj;
|
---|
210 |
|
---|
211 | hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
|
---|
212 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
213 |
|
---|
214 | return VINF_SUCCESS;
|
---|
215 | }
|
---|
216 |
|
---|
217 | int destroyNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr)
|
---|
218 | {
|
---|
219 | if (listener)
|
---|
220 | {
|
---|
221 | ComPtr<IEventSource> esVBox;
|
---|
222 | HRESULT hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
223 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
224 | if (!esVBox.isNull())
|
---|
225 | {
|
---|
226 | hrc = esVBox->UnregisterListener(listener);
|
---|
227 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
228 | }
|
---|
229 | listener.setNull();
|
---|
230 | }
|
---|
231 | return VINF_SUCCESS;
|
---|
232 | }
|
---|
233 |
|
---|
234 | int createClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr,
|
---|
235 | NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events)
|
---|
236 | {
|
---|
237 | ComObjPtr<NATNetworkListenerImpl> obj;
|
---|
238 | HRESULT hrc = obj.createObject();
|
---|
239 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
240 |
|
---|
241 | hrc = obj->init(new NATNetworkListener(), adapter);
|
---|
242 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
243 |
|
---|
244 | ComPtr<IEventSource> esVBox;
|
---|
245 | hrc = vboxclientptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
246 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
247 |
|
---|
248 | listener = obj;
|
---|
249 |
|
---|
250 | hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
|
---|
251 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
252 |
|
---|
253 | return VINF_SUCCESS;
|
---|
254 | }
|
---|
255 |
|
---|
256 | int destroyClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr)
|
---|
257 | {
|
---|
258 | if (listener)
|
---|
259 | {
|
---|
260 | ComPtr<IEventSource> esVBox;
|
---|
261 | HRESULT hrc = vboxclientptr->COMGETTER(EventSource)(esVBox.asOutParam());
|
---|
262 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
263 | if (!esVBox.isNull())
|
---|
264 | {
|
---|
265 | hrc = esVBox->UnregisterListener(listener);
|
---|
266 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
267 | }
|
---|
268 | listener.setNull();
|
---|
269 | }
|
---|
270 | return VINF_SUCCESS;
|
---|
271 | }
|
---|