VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NetLib/ComHostUtils.cpp@ 77807

Last change on this file since 77807 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1/* $Id: ComHostUtils.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * ComHostUtils.cpp
4 */
5
6/*
7 * Copyright (C) 2013-2019 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 <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
74VBOX_LISTENER_DECLARE(NATNetworkListenerImpl)
75
76
77int 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
122int hostDnsSearchList(const ComHostPtr& host, std::vector<std::string>& strings)
123{
124 strings.clear();
125
126 ComBstrArray strs;
127 if (SUCCEEDED(host->COMGETTER(SearchStrings)(ComSafeArrayAsOutParam(strs))))
128 {
129 for (unsigned int i = 0; i < strs.size(); ++i)
130 {
131 strings.push_back(com::Utf8Str(strs[i]).c_str());
132 }
133 }
134 else
135 return VERR_NOT_FOUND;
136
137 return VINF_SUCCESS;
138}
139
140
141int hostDnsDomain(const ComHostPtr& host, std::string& domainStr)
142{
143 com::Bstr domain;
144 if (SUCCEEDED(host->COMGETTER(DomainName)(domain.asOutParam())))
145 {
146 domainStr = com::Utf8Str(domain).c_str();
147 return VINF_SUCCESS;
148 }
149
150 return VERR_NOT_FOUND;
151}
152
153
154int createNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr,
155 NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events)
156{
157 ComObjPtr<NATNetworkListenerImpl> obj;
158 HRESULT hrc = obj.createObject();
159 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
160
161 hrc = obj->init(new NATNetworkListener(), adapter);
162 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
163
164 ComPtr<IEventSource> esVBox;
165 hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
166 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
167
168 listener = obj;
169
170 hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
171 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
172
173 return VINF_SUCCESS;
174}
175
176int destroyNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr)
177{
178 if (listener)
179 {
180 ComPtr<IEventSource> esVBox;
181 HRESULT hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
182 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
183 if (!esVBox.isNull())
184 {
185 hrc = esVBox->UnregisterListener(listener);
186 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
187 }
188 listener.setNull();
189 }
190 return VINF_SUCCESS;
191}
192
193int createClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr,
194 NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events)
195{
196 ComObjPtr<NATNetworkListenerImpl> obj;
197 HRESULT hrc = obj.createObject();
198 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
199
200 hrc = obj->init(new NATNetworkListener(), adapter);
201 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
202
203 ComPtr<IEventSource> esVBox;
204 hrc = vboxclientptr->COMGETTER(EventSource)(esVBox.asOutParam());
205 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
206
207 listener = obj;
208
209 hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
210 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
211
212 return VINF_SUCCESS;
213}
214
215int destroyClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr)
216{
217 if (listener)
218 {
219 ComPtr<IEventSource> esVBox;
220 HRESULT hrc = vboxclientptr->COMGETTER(EventSource)(esVBox.asOutParam());
221 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
222 if (!esVBox.isNull())
223 {
224 hrc = esVBox->UnregisterListener(listener);
225 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
226 }
227 listener.setNull();
228 }
229 return VINF_SUCCESS;
230}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette