VirtualBox

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

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

Main/HostDnsService: Renaming and cleanup (no functional changes).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 KB
Line 
1/* $Id: HostDnsService.cpp 77984 2019-04-02 14:06:13Z vboxsync $ */
2/** @file
3 * Base class for Host DNS & Co services.
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.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#define LOG_GROUP LOG_GROUP_MAIN_HOST
19#include <VBox/com/array.h>
20#include <VBox/com/ptr.h>
21#include <VBox/com/string.h>
22
23#include <iprt/cpp/utils.h>
24
25#include "LoggingNew.h"
26#include "VirtualBoxImpl.h"
27#include <iprt/time.h>
28#include <iprt/thread.h>
29#include <iprt/semaphore.h>
30#include <iprt/critsect.h>
31
32#include <algorithm>
33#include <set>
34#include <string>
35#include "HostDnsService.h"
36
37
38static void dumpHostDnsInformation(const HostDnsInformation&);
39static void dumpHostDnsStrVector(const std::string&, const std::vector<std::string>&);
40
41
42bool HostDnsInformation::equals(const HostDnsInformation &info, uint32_t fLaxComparison) const
43{
44 bool fSameServers;
45 if ((fLaxComparison & IGNORE_SERVER_ORDER) == 0)
46 {
47 fSameServers = (servers == info.servers);
48 }
49 else
50 {
51 std::set<std::string> l(servers.begin(), servers.end());
52 std::set<std::string> r(info.servers.begin(), info.servers.end());
53
54 fSameServers = (l == r);
55 }
56
57 bool fSameDomain, fSameSearchList;
58 if ((fLaxComparison & IGNORE_SUFFIXES) == 0)
59 {
60 fSameDomain = (domain == info.domain);
61 fSameSearchList = (searchList == info.searchList);
62 }
63 else
64 {
65 fSameDomain = fSameSearchList = true;
66 }
67
68 return fSameServers && fSameDomain && fSameSearchList;
69}
70
71inline static void detachVectorOfString(const std::vector<std::string>& v,
72 std::vector<com::Utf8Str> &aArray)
73{
74 aArray.resize(v.size());
75 size_t i = 0;
76 for (std::vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it, ++i)
77 aArray[i] = Utf8Str(it->c_str());
78}
79
80struct HostDnsServiceBase::Data
81{
82 Data(bool aThreaded)
83 : pProxy(NULL)
84 , fThreaded(aThreaded)
85 {}
86
87 /** Weak pointer to parent proxy object. */
88 HostDnsMonitorProxy *pProxy;
89 /** Whether the DNS monitor implementation has a dedicated monitoring thread. Optional. */
90 const bool fThreaded;
91 /** Event for the monitor thread, if any. */
92 RTSEMEVENT hMonitorThreadEvent;
93 /** Handle of the monitor thread, if any. */
94 RTTHREAD hMonitorThread;
95 /** Generic host DNS information. */
96 HostDnsInformation info;
97};
98
99struct HostDnsMonitorProxy::Data
100{
101 Data(HostDnsServiceBase *aMonitor, VirtualBox *aParent)
102 : pVirtualBox(aParent)
103 , pMonitorImpl(aMonitor)
104 , uLastExtraDataPoll(0)
105 , fLaxComparison(0)
106 , info()
107 {}
108
109 VirtualBox *pVirtualBox;
110 HostDnsServiceBase *pMonitorImpl;
111
112 uint64_t uLastExtraDataPoll;
113 uint32_t fLaxComparison;
114 HostDnsInformation info;
115};
116
117
118HostDnsServiceBase::HostDnsServiceBase(bool fThreaded)
119 : m(NULL)
120{
121 m = new HostDnsServiceBase::Data(fThreaded);
122}
123
124HostDnsServiceBase::~HostDnsServiceBase()
125{
126 if (m)
127 {
128 delete m;
129 m = NULL;
130 }
131}
132
133HostDnsServiceBase *HostDnsServiceBase::createHostDnsMonitor(void)
134{
135 HostDnsServiceBase *pMonitor = NULL;
136
137#if defined (RT_OS_DARWIN)
138 pMonitor = new HostDnsServiceDarwin();
139#elif defined(RT_OS_WINDOWS)
140 pMonitor = new HostDnsServiceWin();
141#elif defined(RT_OS_LINUX)
142 pMonitor = new HostDnsServiceLinux();
143#elif defined(RT_OS_SOLARIS)
144 pMonitor = new HostDnsServiceSolaris();
145#elif defined(RT_OS_FREEBSD)
146 pMonitor = new HostDnsServiceFreebsd();
147#elif defined(RT_OS_OS2)
148 pMonitor = new HostDnsServiceOs2();
149#else
150 pMonitor = new HostDnsServiceBase();
151#endif
152
153 return pMonitor;
154}
155
156void HostDnsServiceBase::shutdown(void)
157{
158 monitorThreadShutdown();
159 int rc = RTThreadWait(m->hMonitorThread, 5000, NULL);
160 AssertRCSuccess(rc);
161}
162
163
164void HostDnsServiceBase::setInfo(const HostDnsInformation &info)
165{
166 if (m->pProxy != NULL)
167 m->pProxy->notify(info);
168}
169
170HRESULT HostDnsServiceBase::init(HostDnsMonitorProxy *pProxy)
171{
172 m->pProxy = pProxy;
173
174 if (m->fThreaded)
175 {
176 int rc = RTSemEventCreate(&m->hMonitorThreadEvent);
177 AssertRCReturn(rc, E_FAIL);
178
179 rc = RTThreadCreate(&m->hMonitorThread,
180 HostDnsServiceBase::threadMonitoringRoutine,
181 this, 128 * _1K, RTTHREADTYPE_IO,
182 RTTHREADFLAGS_WAITABLE, "dns-monitor");
183 AssertRCReturn(rc, E_FAIL);
184
185 RTSemEventWait(m->hMonitorThreadEvent, RT_INDEFINITE_WAIT);
186 }
187 return S_OK;
188}
189
190void HostDnsMonitorProxy::pollGlobalExtraData(void)
191{
192 VirtualBox *pVirtualBox = m->pVirtualBox;
193 if (RT_UNLIKELY(pVirtualBox == NULL))
194 return;
195
196 uint64_t uNow = RTTimeNanoTS();
197 if (uNow - m->uLastExtraDataPoll >= RT_NS_30SEC || m->uLastExtraDataPoll == 0)
198 {
199 m->uLastExtraDataPoll = uNow;
200
201 /*
202 * Should we ignore the order of DNS servers?
203 */
204 const com::Bstr bstrHostDNSOrderIgnoreKey("VBoxInternal2/HostDNSOrderIgnore");
205 com::Bstr bstrHostDNSOrderIgnore;
206 pVirtualBox->GetExtraData(bstrHostDNSOrderIgnoreKey.raw(),
207 bstrHostDNSOrderIgnore.asOutParam());
208 uint32_t fDNSOrderIgnore = 0;
209 if (bstrHostDNSOrderIgnore.isNotEmpty())
210 {
211 if (bstrHostDNSOrderIgnore != "0")
212 fDNSOrderIgnore = HostDnsInformation::IGNORE_SERVER_ORDER;
213 }
214
215 if (fDNSOrderIgnore != (m->fLaxComparison & HostDnsInformation::IGNORE_SERVER_ORDER))
216 {
217
218 m->fLaxComparison ^= HostDnsInformation::IGNORE_SERVER_ORDER;
219 LogRel(("HostDnsMonitor: %ls=%ls\n",
220 bstrHostDNSOrderIgnoreKey.raw(),
221 bstrHostDNSOrderIgnore.raw()));
222 }
223
224 /*
225 * Should we ignore changes to the domain name or the search list?
226 */
227 const com::Bstr bstrHostDNSSuffixesIgnoreKey("VBoxInternal2/HostDNSSuffixesIgnore");
228 com::Bstr bstrHostDNSSuffixesIgnore;
229 pVirtualBox->GetExtraData(bstrHostDNSSuffixesIgnoreKey.raw(),
230 bstrHostDNSSuffixesIgnore.asOutParam());
231 uint32_t fDNSSuffixesIgnore = 0;
232 if (bstrHostDNSSuffixesIgnore.isNotEmpty())
233 {
234 if (bstrHostDNSSuffixesIgnore != "0")
235 fDNSSuffixesIgnore = HostDnsInformation::IGNORE_SUFFIXES;
236 }
237
238 if (fDNSSuffixesIgnore != (m->fLaxComparison & HostDnsInformation::IGNORE_SUFFIXES))
239 {
240
241 m->fLaxComparison ^= HostDnsInformation::IGNORE_SUFFIXES;
242 LogRel(("HostDnsMonitor: %ls=%ls\n",
243 bstrHostDNSSuffixesIgnoreKey.raw(),
244 bstrHostDNSSuffixesIgnore.raw()));
245 }
246 }
247}
248
249void HostDnsServiceBase::monitorThreadInitializationDone(void)
250{
251 RTSemEventSignal(m->hMonitorThreadEvent);
252}
253
254DECLCALLBACK(int) HostDnsServiceBase::threadMonitoringRoutine(RTTHREAD, void *pvUser)
255{
256 HostDnsServiceBase *pThis = static_cast<HostDnsServiceBase *>(pvUser);
257 return pThis->monitorWorker();
258}
259
260/* HostDnsMonitorProxy */
261HostDnsMonitorProxy::HostDnsMonitorProxy()
262 : m(NULL)
263{
264}
265
266HostDnsMonitorProxy::~HostDnsMonitorProxy()
267{
268 Assert(!m);
269}
270
271void HostDnsMonitorProxy::init(VirtualBox* aParent)
272{
273 HostDnsServiceBase *pMonitor = HostDnsServiceBase::createHostDnsMonitor();
274 m = new HostDnsMonitorProxy::Data(pMonitor, aParent);
275 m->pMonitorImpl->init(this);
276}
277
278void HostDnsMonitorProxy::uninit(void)
279{
280 if (m)
281 {
282 m->pMonitorImpl->shutdown();
283 delete m;
284 m = NULL;
285 }
286}
287
288void HostDnsMonitorProxy::notify(const HostDnsInformation &info)
289{
290 bool fNotify = updateInfo(info);
291 if (fNotify)
292 m->pVirtualBox->i_onHostNameResolutionConfigurationChange();
293}
294
295HRESULT HostDnsMonitorProxy::GetNameServers(std::vector<com::Utf8Str> &aNameServers)
296{
297 AssertReturn(m != NULL, E_FAIL);
298 RTCLock grab(m_LockMtx);
299
300 LogRel(("HostDnsMonitorProxy::GetNameServers:\n"));
301 dumpHostDnsStrVector("name server", m->info.servers);
302
303 detachVectorOfString(m->info.servers, aNameServers);
304
305 return S_OK;
306}
307
308HRESULT HostDnsMonitorProxy::GetDomainName(com::Utf8Str *pDomainName)
309{
310 AssertReturn(m != NULL, E_FAIL);
311 RTCLock grab(m_LockMtx);
312
313 LogRel(("HostDnsMonitorProxy::GetDomainName: %s\n",
314 m->info.domain.empty() ? "no domain set" : m->info.domain.c_str()));
315
316 *pDomainName = m->info.domain.c_str();
317
318 return S_OK;
319}
320
321HRESULT HostDnsMonitorProxy::GetSearchStrings(std::vector<com::Utf8Str> &aSearchStrings)
322{
323 AssertReturn(m != NULL, E_FAIL);
324 RTCLock grab(m_LockMtx);
325
326 LogRel(("HostDnsMonitorProxy::GetSearchStrings:\n"));
327 dumpHostDnsStrVector("search string", m->info.searchList);
328
329 detachVectorOfString(m->info.searchList, aSearchStrings);
330
331 return S_OK;
332}
333
334bool HostDnsMonitorProxy::updateInfo(const HostDnsInformation &info)
335{
336 LogRel(("HostDnsMonitor::updateInfo\n"));
337 RTCLock grab(m_LockMtx);
338
339 if (info.equals(m->info))
340 {
341 LogRel(("HostDnsMonitor: unchanged\n"));
342 return false;
343 }
344
345 pollGlobalExtraData();
346
347 LogRel(("HostDnsMonitor: old information\n"));
348 dumpHostDnsInformation(m->info);
349 LogRel(("HostDnsMonitor: new information\n"));
350 dumpHostDnsInformation(info);
351
352 bool fIgnore = m->fLaxComparison != 0 && info.equals(m->info, m->fLaxComparison);
353 m->info = info;
354
355 if (fIgnore)
356 {
357 LogRel(("HostDnsMonitor: lax comparison %#x, not notifying\n", m->fLaxComparison));
358 return false;
359 }
360
361 return true;
362}
363
364static void dumpHostDnsInformation(const HostDnsInformation& info)
365{
366 dumpHostDnsStrVector("server", info.servers);
367
368 if (!info.domain.empty())
369 LogRel((" domain: %s\n", info.domain.c_str()));
370 else
371 LogRel((" no domain set\n"));
372
373 dumpHostDnsStrVector("search string", info.searchList);
374}
375
376
377static void dumpHostDnsStrVector(const std::string& prefix, const std::vector<std::string>& v)
378{
379 int i = 1;
380 for (std::vector<std::string>::const_iterator it = v.begin();
381 it != v.end();
382 ++it, ++i)
383 LogRel((" %s %d: %s\n", prefix.c_str(), i, it->c_str()));
384 if (v.empty())
385 LogRel((" no %s entries\n", prefix.c_str()));
386}
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