VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostDnsService.cpp@ 49963

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

HostDnsService.cpp: adds helper routines to dump fetched host DNS information and dump it (host DNS information) on change and on fetch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/* $Id: HostDnsService.cpp 49818 2013-12-09 05:52:34Z vboxsync $ */
2/** @file
3 * Base class fo Host DNS & Co services.
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.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
35static HostDnsMonitor *g_monitor;
36
37static void dumpHostDnsInformation(const HostDnsInformation&);
38static void dumpHostDnsStrVector(const std::string&, const std::vector<std::string>&);
39
40/* Lockee */
41Lockee::Lockee()
42{
43 RTCritSectInit(&mLock);
44}
45
46Lockee::~Lockee()
47{
48 RTCritSectDelete(&mLock);
49}
50
51const RTCRITSECT* Lockee::lock() const
52{
53 return &mLock;
54}
55
56/* ALock */
57ALock::ALock(const Lockee *aLockee)
58 : lockee(aLockee)
59{
60 RTCritSectEnter(const_cast<PRTCRITSECT>(lockee->lock()));
61}
62
63ALock::~ALock()
64{
65 RTCritSectLeave(const_cast<PRTCRITSECT>(lockee->lock()));
66}
67
68inline static void detachVectorOfString(const std::vector<std::string>& v,
69 ComSafeArrayOut(BSTR, aBstrArray))
70{
71 com::SafeArray<BSTR> aBstr(v.size());
72
73 std::vector<std::string>::const_iterator it;
74
75 int i = 0;
76 it = v.begin();
77 for (; it != v.end(); ++it, ++i)
78 Utf8Str(it->c_str()).cloneTo(&aBstr[i]);
79
80 aBstr.detachTo(ComSafeArrayOutArg(aBstrArray));
81}
82
83struct HostDnsMonitor::Data
84{
85 std::vector<PCHostDnsMonitorProxy> proxies;
86 HostDnsInformation info;
87};
88
89struct HostDnsMonitorProxy::Data
90{
91 Data(const HostDnsMonitor *aMonitor, const VirtualBox *aParent)
92 : info(NULL)
93 , virtualbox(aParent)
94 , monitor(aMonitor)
95 , fModified(true)
96 {}
97
98 virtual ~Data()
99 {
100 if (info)
101 {
102 delete info;
103 info = NULL;
104 }
105 }
106
107 HostDnsInformation *info;
108 const VirtualBox *virtualbox;
109 const HostDnsMonitor *monitor;
110 bool fModified;
111};
112
113
114HostDnsMonitor::HostDnsMonitor()
115 : m(NULL)
116{
117}
118
119HostDnsMonitor::~HostDnsMonitor()
120{
121 if (m)
122 {
123 delete m;
124 m = NULL;
125 }
126}
127
128const 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
154void HostDnsMonitor::addMonitorProxy(PCHostDnsMonitorProxy proxy) const
155{
156 ALock l(this);
157 m->proxies.push_back(proxy);
158 proxy->notify();
159}
160
161void 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
173void HostDnsMonitor::shutdown()
174{
175 if (g_monitor)
176 {
177 delete g_monitor;
178 g_monitor = NULL;
179 }
180}
181
182const HostDnsInformation &HostDnsMonitor::getInfo() const
183{
184 return m->info;
185}
186
187void 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
195void HostDnsMonitor::setInfo(const HostDnsInformation &info)
196{
197 ALock l(this);
198 m->info = info;
199}
200
201HRESULT HostDnsMonitor::init()
202{
203 m = new HostDnsMonitor::Data();
204 return S_OK;
205}
206
207
208/* HostDnsMonitorProxy */
209HostDnsMonitorProxy::HostDnsMonitorProxy()
210 : m(NULL)
211{
212}
213
214HostDnsMonitorProxy::~HostDnsMonitorProxy()
215{
216 if (m)
217 {
218 if (m->monitor)
219 m->monitor->releaseMonitorProxy(this);
220 delete m;
221 m = NULL;
222 }
223}
224
225void HostDnsMonitorProxy::init(const HostDnsMonitor *mon, const VirtualBox* aParent)
226{
227 m = new HostDnsMonitorProxy::Data(mon, aParent);
228 m->monitor->addMonitorProxy(this);
229 updateInfo();
230}
231
232void HostDnsMonitorProxy::notify() const
233{
234 m->fModified = true;
235 const_cast<VirtualBox *>(m->virtualbox)->onHostNameResolutionConfigurationChange();
236}
237
238HRESULT HostDnsMonitorProxy::GetNameServers(ComSafeArrayOut(BSTR, aNameServers))
239{
240 AssertReturn(m && m->info, E_FAIL);
241 ALock l(this);
242
243 if (m->fModified)
244 updateInfo();
245
246 LogRel(("HostDnsMonitorProxy::GetNameServers:\n"));
247 dumpHostDnsStrVector("Name Server", m->info->servers);
248
249 detachVectorOfString(m->info->servers, ComSafeArrayOutArg(aNameServers));
250
251 return S_OK;
252}
253
254HRESULT HostDnsMonitorProxy::GetDomainName(BSTR *aDomainName)
255{
256 AssertReturn(m && m->info, E_FAIL);
257 ALock l(this);
258
259 if (m->fModified)
260 updateInfo();
261
262 LogRel(("HostDnsMonitorProxy::GetDomainName:%s\n", m->info->domain.c_str()));
263
264 Utf8Str(m->info->domain.c_str()).cloneTo(aDomainName);
265
266 return S_OK;
267}
268
269HRESULT HostDnsMonitorProxy::GetSearchStrings(ComSafeArrayOut(BSTR, aSearchStrings))
270{
271 AssertReturn(m && m->info, E_FAIL);
272 ALock l(this);
273
274 if (m->fModified)
275 updateInfo();
276
277 LogRel(("HostDnsMonitorProxy::GetSearchStrings:\n"));
278 dumpHostDnsStrVector("Search String", m->info->searchList);
279
280 detachVectorOfString(m->info->searchList, ComSafeArrayOutArg(aSearchStrings));
281
282 return S_OK;
283}
284
285bool HostDnsMonitorProxy::operator==(PCHostDnsMonitorProxy& rhs)
286{
287 if (!m || !rhs->m)
288 return false;
289
290 /**
291 * we've assigned to the same instance of VirtualBox.
292 */
293 return m->virtualbox == rhs->m->virtualbox;
294}
295
296void HostDnsMonitorProxy::updateInfo()
297{
298 HostDnsInformation *info = new HostDnsInformation(m->monitor->getInfo());
299 HostDnsInformation *old = m->info;
300
301 LogRel(("HostDnsMonitorProxy: Host's DNS information updated:\n"));
302 dumpHostDnsInformation(*info);
303
304 m->info = info;
305 if (old)
306 {
307 LogRel(("HostDnsMonitorProxy: Old host information:\n"));
308 dumpHostDnsInformation(*old);
309
310 delete old;
311 }
312
313 m->fModified = false;
314}
315
316
317static void dumpHostDnsInformation(const HostDnsInformation& info)
318{
319 dumpHostDnsStrVector("DNS server", info.servers);
320 dumpHostDnsStrVector("SearchString", info.searchList);
321
322 if (!info.domain.empty())
323 LogRel(("DNS domain: %s\n", info.domain.c_str()));
324}
325
326
327static void dumpHostDnsStrVector(const std::string& prefix, const std::vector<std::string>& v)
328{
329 int i = 0;
330 for (std::vector<std::string>::const_iterator it = v.begin();
331 it != v.end();
332 ++it, ++i)
333 LogRel(("%s %d: %s\n", prefix.c_str(), i, it->c_str()));
334}
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