VirtualBox

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

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

extracts Main-helper functions and listener declaration for sharing between network services.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: ComHostUtils.cpp 49819 2013-12-09 06:22:16Z vboxsync $ */
2/** @file
3 * ComHostUtils.cpp
4 */
5
6/*
7 * Copyright (C) 2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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#include <VBox/com/com.h>
22#include <VBox/com/listeners.h>
23#include <VBox/com/string.h>
24#include <VBox/com/Guid.h>
25#include <VBox/com/array.h>
26#include <VBox/com/ErrorInfo.h>
27#include <VBox/com/errorprint.h>
28#include <VBox/com/EventQueue.h>
29#include <VBox/com/VirtualBox.h>
30
31#include <iprt/alloca.h>
32#include <iprt/buildconfig.h>
33#include <iprt/err.h>
34#include <iprt/net.h> /* must come before getopt */
35#include <iprt/getopt.h>
36#include <iprt/initterm.h>
37#include <iprt/message.h>
38#include <iprt/param.h>
39#include <iprt/path.h>
40#include <iprt/stream.h>
41#include <iprt/time.h>
42#include <iprt/string.h>
43
44
45#include "../NetLib/VBoxNetLib.h"
46#include "../NetLib/shared_ptr.h"
47
48#include <vector>
49#include <list>
50#include <string>
51#include <map>
52
53#include "../NetLib/VBoxNetBaseService.h"
54
55#ifdef RT_OS_WINDOWS /* WinMain */
56# include <Windows.h>
57# include <stdlib.h>
58# ifdef INET_ADDRSTRLEN
59/* On Windows INET_ADDRSTRLEN defined as 22 Ws2ipdef.h, because it include port number */
60# undef INET_ADDRSTRLEN
61# endif
62# define INET_ADDRSTRLEN 16
63#else
64# include <netinet/in.h>
65#endif
66
67#include "utils.h"
68
69
70VBOX_LISTENER_DECLARE(NATNetworkListenerImpl)
71
72
73int localMappings(const ComNatPtr& nat, AddressToOffsetMapping& mapping)
74{
75 mapping.clear();
76
77 ComBstrArray strs;
78 int cStrs;
79 HRESULT hrc = nat->COMGETTER(LocalMappings)(ComSafeArrayAsOutParam(strs));
80 if ( SUCCEEDED(hrc)
81 && (cStrs = strs.size()))
82 {
83 for (int i = 0; i < cStrs; ++i)
84 {
85 char szAddr[17];
86 RTNETADDRIPV4 ip4addr;
87 char *pszTerm;
88 uint32_t u32Off;
89 com::Utf8Str strLo2Off(strs[i]);
90 const char *pszLo2Off = strLo2Off.c_str();
91
92 RT_ZERO(szAddr);
93
94 pszTerm = RTStrStr(pszLo2Off, "=");
95
96 if ( pszTerm
97 && (pszTerm - pszLo2Off) <= INET_ADDRSTRLEN)
98 {
99 memcpy(szAddr, pszLo2Off, (pszTerm - pszLo2Off));
100 int rc = RTNetStrToIPv4Addr(szAddr, &ip4addr);
101 if (RT_SUCCESS(rc))
102 {
103 u32Off = RTStrToUInt32(pszTerm + 1);
104 if (u32Off != 0)
105 mapping.insert(
106 AddressToOffsetMapping::value_type(ip4addr, u32Off));
107 }
108 }
109 }
110 }
111 else
112 return VERR_NOT_FOUND;
113
114 return VINF_SUCCESS;
115}
116
117/**
118 * @note: const dropped here, because of map<K,V>::operator[] which isn't const, map<K,V>::at() has const
119 * variant but it's C++11.
120 */
121int hostDnsServers(const ComHostPtr& host, const RTNETADDRIPV4& networkid,
122 /*const*/ AddressToOffsetMapping& mapping, AddressList& servers)
123{
124 servers.clear();
125
126 ComBstrArray strs;
127 if (SUCCEEDED(host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(strs))))
128 {
129 RTNETADDRIPV4 addr;
130 int rc;
131
132 for (unsigned int i = 0; i < strs.size(); ++i)
133 {
134 rc = RTNetStrToIPv4Addr(com::Utf8Str(strs[i]).c_str(), &addr);
135 if (RT_SUCCESS(rc))
136 {
137 if (addr.au8[0] == 127)
138 {
139 /* XXX: here we want map<K,V>::at(const K& k) const */
140 if (mapping[addr] != 0)
141 {
142 addr.u = RT_H2N_U32(RT_N2H_U32(networkid.u)
143 + mapping[addr]);
144 }
145 else
146 continue; /* XXX: Warning here (local mapping wasn't registered) */
147 }
148
149 servers.push_back(addr);
150 }
151 }
152 }
153 else
154 return VERR_NOT_FOUND;
155
156 return VINF_SUCCESS;
157}
158
159
160int hostDnsSearchList(const ComHostPtr& host, std::vector<std::string>& strings)
161{
162 strings.clear();
163
164 ComBstrArray strs;
165 if (SUCCEEDED(host->COMGETTER(SearchStrings)(ComSafeArrayAsOutParam(strs))))
166 {
167 for (unsigned int i = 0; i < strs.size(); ++i)
168 {
169 strings.push_back(com::Utf8Str(strs[i]).c_str());
170 }
171 }
172 else
173 return VERR_NOT_FOUND;
174
175 return VINF_SUCCESS;
176}
177
178
179int hostDnsDomain(const ComHostPtr& host, std::string& domainStr)
180{
181 com::Bstr domain;
182 if (SUCCEEDED(host->COMGETTER(DomainName)(domain.asOutParam())))
183 {
184 domainStr = com::Utf8Str(domain).c_str();
185 return VINF_SUCCESS;
186 }
187
188 return VERR_NOT_FOUND;
189}
190
191int createNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr,
192 NATNetworkEventAdapter *adapter, const ComEventTypeArray& events)
193{
194 ComObjPtr<NATNetworkListenerImpl> obj;
195 HRESULT hrc = obj.createObject();
196 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
197
198 hrc = obj->init(new NATNetworkListener(), adapter);
199 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
200
201 ComPtr<IEventSource> esVBox;
202 hrc = vboxptr->COMGETTER(EventSource)(esVBox.asOutParam());
203 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
204
205 listener = obj;
206
207 hrc = esVBox->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
208 AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
209}
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