1 | /* $Id: HostDnsService.cpp 53122 2014-10-22 20:43:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Base class fo Host DNS & Co services.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2014 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 | #include <VBox/com/array.h>
|
---|
19 | #include <VBox/com/ptr.h>
|
---|
20 | #include <VBox/com/string.h>
|
---|
21 |
|
---|
22 | #include <iprt/cpp/utils.h>
|
---|
23 |
|
---|
24 | #include "Logging.h"
|
---|
25 | #include "VirtualBoxImpl.h"
|
---|
26 | #include <iprt/thread.h>
|
---|
27 | #include <iprt/semaphore.h>
|
---|
28 | #include <iprt/critsect.h>
|
---|
29 |
|
---|
30 | #include <algorithm>
|
---|
31 | #include <string>
|
---|
32 | #include "HostDnsService.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | static HostDnsMonitor *g_monitor;
|
---|
36 |
|
---|
37 | static void dumpHostDnsInformation(const HostDnsInformation&);
|
---|
38 | static void dumpHostDnsStrVector(const std::string&, const std::vector<std::string>&);
|
---|
39 |
|
---|
40 | /* Lockee */
|
---|
41 | Lockee::Lockee()
|
---|
42 | {
|
---|
43 | RTCritSectInit(&mLock);
|
---|
44 | }
|
---|
45 |
|
---|
46 | Lockee::~Lockee()
|
---|
47 | {
|
---|
48 | RTCritSectDelete(&mLock);
|
---|
49 | }
|
---|
50 |
|
---|
51 | const RTCRITSECT* Lockee::lock() const
|
---|
52 | {
|
---|
53 | return &mLock;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /* ALock */
|
---|
57 | ALock::ALock(const Lockee *aLockee)
|
---|
58 | : lockee(aLockee)
|
---|
59 | {
|
---|
60 | RTCritSectEnter(const_cast<PRTCRITSECT>(lockee->lock()));
|
---|
61 | }
|
---|
62 |
|
---|
63 | ALock::~ALock()
|
---|
64 | {
|
---|
65 | RTCritSectLeave(const_cast<PRTCRITSECT>(lockee->lock()));
|
---|
66 | }
|
---|
67 |
|
---|
68 | inline static void detachVectorOfString(const std::vector<std::string>& v,
|
---|
69 | std::vector<com::Utf8Str> &aArray)
|
---|
70 | {
|
---|
71 | aArray.resize(v.size());
|
---|
72 | size_t i = 0;
|
---|
73 | for (std::vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it, ++i)
|
---|
74 | aArray[i] = Utf8Str(it->c_str());
|
---|
75 | }
|
---|
76 |
|
---|
77 | struct HostDnsMonitor::Data
|
---|
78 | {
|
---|
79 | Data(bool aThreaded):fThreaded(aThreaded){}
|
---|
80 |
|
---|
81 | std::vector<PCHostDnsMonitorProxy> proxies;
|
---|
82 | HostDnsInformation info;
|
---|
83 | const bool fThreaded;
|
---|
84 | RTTHREAD hMonitoringThread;
|
---|
85 | RTSEMEVENT hDnsInitEvent;
|
---|
86 | };
|
---|
87 |
|
---|
88 | struct HostDnsMonitorProxy::Data
|
---|
89 | {
|
---|
90 | Data(const HostDnsMonitor *aMonitor, const VirtualBox *aParent)
|
---|
91 | : info(NULL)
|
---|
92 | , virtualbox(aParent)
|
---|
93 | , monitor(aMonitor)
|
---|
94 | , fModified(true)
|
---|
95 | {}
|
---|
96 |
|
---|
97 | virtual ~Data()
|
---|
98 | {
|
---|
99 | if (info)
|
---|
100 | {
|
---|
101 | delete info;
|
---|
102 | info = NULL;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | HostDnsInformation *info;
|
---|
107 | const VirtualBox *virtualbox;
|
---|
108 | const HostDnsMonitor *monitor;
|
---|
109 | bool fModified;
|
---|
110 | };
|
---|
111 |
|
---|
112 |
|
---|
113 | HostDnsMonitor::HostDnsMonitor(bool fThreaded)
|
---|
114 | : m(NULL)
|
---|
115 | {
|
---|
116 | m = new HostDnsMonitor::Data(fThreaded);
|
---|
117 | }
|
---|
118 |
|
---|
119 | HostDnsMonitor::~HostDnsMonitor()
|
---|
120 | {
|
---|
121 | if (m)
|
---|
122 | {
|
---|
123 | delete m;
|
---|
124 | m = NULL;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | const HostDnsMonitor *HostDnsMonitor::getHostDnsMonitor()
|
---|
129 | {
|
---|
130 | /* XXX: Moved initialization from HostImpl.cpp */
|
---|
131 | if (!g_monitor)
|
---|
132 | {
|
---|
133 | # if defined (RT_OS_DARWIN)
|
---|
134 | g_monitor = new HostDnsServiceDarwin();
|
---|
135 | # elif defined(RT_OS_WINDOWS)
|
---|
136 | g_monitor = new HostDnsServiceWin();
|
---|
137 | # elif defined(RT_OS_LINUX)
|
---|
138 | g_monitor = new HostDnsServiceLinux();
|
---|
139 | # elif defined(RT_OS_SOLARIS)
|
---|
140 | g_monitor = new HostDnsServiceSolaris();
|
---|
141 | # elif defined(RT_OS_FREEBSD)
|
---|
142 | g_monitor = new HostDnsServiceFreebsd();
|
---|
143 | # elif defined(RT_OS_OS2)
|
---|
144 | g_monitor = new HostDnsServiceOs2();
|
---|
145 | # else
|
---|
146 | g_monitor = new HostDnsService();
|
---|
147 | # endif
|
---|
148 | g_monitor->init();
|
---|
149 | }
|
---|
150 |
|
---|
151 | return g_monitor;
|
---|
152 | }
|
---|
153 |
|
---|
154 | void HostDnsMonitor::addMonitorProxy(PCHostDnsMonitorProxy proxy) const
|
---|
155 | {
|
---|
156 | ALock l(this);
|
---|
157 | m->proxies.push_back(proxy);
|
---|
158 | proxy->notify();
|
---|
159 | }
|
---|
160 |
|
---|
161 | void HostDnsMonitor::releaseMonitorProxy(PCHostDnsMonitorProxy proxy) const
|
---|
162 | {
|
---|
163 | ALock l(this);
|
---|
164 | std::vector<PCHostDnsMonitorProxy>::iterator it;
|
---|
165 | it = std::find(m->proxies.begin(), m->proxies.end(), proxy);
|
---|
166 |
|
---|
167 | if (it == m->proxies.end())
|
---|
168 | return;
|
---|
169 |
|
---|
170 | m->proxies.erase(it);
|
---|
171 | }
|
---|
172 |
|
---|
173 | void HostDnsMonitor::shutdown()
|
---|
174 | {
|
---|
175 | if (g_monitor)
|
---|
176 | {
|
---|
177 | delete g_monitor;
|
---|
178 | g_monitor = NULL;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | const HostDnsInformation &HostDnsMonitor::getInfo() const
|
---|
183 | {
|
---|
184 | return m->info;
|
---|
185 | }
|
---|
186 |
|
---|
187 | void HostDnsMonitor::notifyAll() const
|
---|
188 | {
|
---|
189 | ALock l(this);
|
---|
190 | std::vector<PCHostDnsMonitorProxy>::const_iterator it;
|
---|
191 | for (it = m->proxies.begin(); it != m->proxies.end(); ++it)
|
---|
192 | (*it)->notify();
|
---|
193 | }
|
---|
194 |
|
---|
195 | void HostDnsMonitor::setInfo(const HostDnsInformation &info)
|
---|
196 | {
|
---|
197 | ALock l(this);
|
---|
198 | m->info = info;
|
---|
199 | }
|
---|
200 |
|
---|
201 | HRESULT HostDnsMonitor::init()
|
---|
202 | {
|
---|
203 | if (m->fThreaded)
|
---|
204 | {
|
---|
205 | int rc = RTSemEventCreate(&m->hDnsInitEvent);
|
---|
206 | AssertRCReturn(rc, E_FAIL);
|
---|
207 |
|
---|
208 | rc = RTThreadCreate(&m->hMonitoringThread,
|
---|
209 | HostDnsMonitor::threadMonitoringRoutine,
|
---|
210 | this, 128 * _1K, RTTHREADTYPE_IO, 0, "dns-monitor");
|
---|
211 | AssertRCReturn(rc, E_FAIL);
|
---|
212 |
|
---|
213 | RTSemEventWait(m->hDnsInitEvent, RT_INDEFINITE_WAIT);
|
---|
214 | }
|
---|
215 | return S_OK;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | void HostDnsMonitor::monitorThreadInitializationDone()
|
---|
220 | {
|
---|
221 | RTSemEventSignal(m->hDnsInitEvent);
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | int HostDnsMonitor::threadMonitoringRoutine(RTTHREAD, void *pvUser)
|
---|
226 | {
|
---|
227 | HostDnsMonitor *pThis = static_cast<HostDnsMonitor *>(pvUser);
|
---|
228 | return pThis->monitorWorker();
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* HostDnsMonitorProxy */
|
---|
232 | HostDnsMonitorProxy::HostDnsMonitorProxy()
|
---|
233 | : m(NULL)
|
---|
234 | {
|
---|
235 | }
|
---|
236 |
|
---|
237 | HostDnsMonitorProxy::~HostDnsMonitorProxy()
|
---|
238 | {
|
---|
239 | if (m)
|
---|
240 | {
|
---|
241 | if (m->monitor)
|
---|
242 | m->monitor->releaseMonitorProxy(this);
|
---|
243 | delete m;
|
---|
244 | m = NULL;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | void HostDnsMonitorProxy::init(const HostDnsMonitor *mon, const VirtualBox* aParent)
|
---|
249 | {
|
---|
250 | m = new HostDnsMonitorProxy::Data(mon, aParent);
|
---|
251 | m->monitor->addMonitorProxy(this);
|
---|
252 | updateInfo();
|
---|
253 | }
|
---|
254 |
|
---|
255 | void HostDnsMonitorProxy::notify() const
|
---|
256 | {
|
---|
257 | m->fModified = true;
|
---|
258 | const_cast<VirtualBox *>(m->virtualbox)->i_onHostNameResolutionConfigurationChange();
|
---|
259 | }
|
---|
260 |
|
---|
261 | HRESULT HostDnsMonitorProxy::GetNameServers(std::vector<com::Utf8Str> &aNameServers)
|
---|
262 | {
|
---|
263 | AssertReturn(m && m->info, E_FAIL);
|
---|
264 | ALock l(this);
|
---|
265 |
|
---|
266 | if (m->fModified)
|
---|
267 | updateInfo();
|
---|
268 |
|
---|
269 | LogRel(("HostDnsMonitorProxy::GetNameServers:\n"));
|
---|
270 | dumpHostDnsStrVector("Name Server", m->info->servers);
|
---|
271 |
|
---|
272 | detachVectorOfString(m->info->servers, aNameServers);
|
---|
273 |
|
---|
274 | return S_OK;
|
---|
275 | }
|
---|
276 |
|
---|
277 | HRESULT HostDnsMonitorProxy::GetDomainName(com::Utf8Str *pDomainName)
|
---|
278 | {
|
---|
279 | AssertReturn(m && m->info, E_FAIL);
|
---|
280 | ALock l(this);
|
---|
281 |
|
---|
282 | if (m->fModified)
|
---|
283 | updateInfo();
|
---|
284 |
|
---|
285 | LogRel(("HostDnsMonitorProxy::GetDomainName: %s\n", m->info->domain.c_str()));
|
---|
286 |
|
---|
287 | *pDomainName = m->info->domain.c_str();
|
---|
288 |
|
---|
289 | return S_OK;
|
---|
290 | }
|
---|
291 |
|
---|
292 | HRESULT HostDnsMonitorProxy::GetSearchStrings(std::vector<com::Utf8Str> &aSearchStrings)
|
---|
293 | {
|
---|
294 | AssertReturn(m && m->info, E_FAIL);
|
---|
295 | ALock l(this);
|
---|
296 |
|
---|
297 | if (m->fModified)
|
---|
298 | updateInfo();
|
---|
299 |
|
---|
300 | LogRel(("HostDnsMonitorProxy::GetSearchStrings:\n"));
|
---|
301 | dumpHostDnsStrVector("Search String", m->info->searchList);
|
---|
302 |
|
---|
303 | detachVectorOfString(m->info->searchList, aSearchStrings);
|
---|
304 |
|
---|
305 | return S_OK;
|
---|
306 | }
|
---|
307 |
|
---|
308 | bool HostDnsMonitorProxy::operator==(PCHostDnsMonitorProxy& rhs)
|
---|
309 | {
|
---|
310 | if (!m || !rhs->m)
|
---|
311 | return false;
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * we've assigned to the same instance of VirtualBox.
|
---|
315 | */
|
---|
316 | return m->virtualbox == rhs->m->virtualbox;
|
---|
317 | }
|
---|
318 |
|
---|
319 | void HostDnsMonitorProxy::updateInfo()
|
---|
320 | {
|
---|
321 | HostDnsInformation *info = new HostDnsInformation(m->monitor->getInfo());
|
---|
322 | HostDnsInformation *old = m->info;
|
---|
323 |
|
---|
324 | LogRel(("HostDnsMonitorProxy: Host's DNS information updated:\n"));
|
---|
325 | dumpHostDnsInformation(*info);
|
---|
326 |
|
---|
327 | m->info = info;
|
---|
328 | if (old)
|
---|
329 | {
|
---|
330 | LogRel(("HostDnsMonitorProxy: Old host information:\n"));
|
---|
331 | dumpHostDnsInformation(*old);
|
---|
332 |
|
---|
333 | delete old;
|
---|
334 | }
|
---|
335 |
|
---|
336 | m->fModified = false;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | static void dumpHostDnsInformation(const HostDnsInformation& info)
|
---|
341 | {
|
---|
342 | dumpHostDnsStrVector("DNS server", info.servers);
|
---|
343 | dumpHostDnsStrVector("SearchString", info.searchList);
|
---|
344 |
|
---|
345 | if (!info.domain.empty())
|
---|
346 | LogRel(("DNS domain: %s\n", info.domain.c_str()));
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | static void dumpHostDnsStrVector(const std::string& prefix, const std::vector<std::string>& v)
|
---|
351 | {
|
---|
352 | int i = 1;
|
---|
353 | for (std::vector<std::string>::const_iterator it = v.begin();
|
---|
354 | it != v.end();
|
---|
355 | ++it, ++i)
|
---|
356 | LogRel(("%s %d: %s\n", prefix.c_str(), i, it->c_str()));
|
---|
357 | }
|
---|