VirtualBox

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

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

Various FreeBSD fixes submitted Bernhard Froehlich

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/* $Id: HostDnsService.cpp 49718 2013-11-29 10:51:54Z 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.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#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 "VirtualBoxImpl.h"
25#include <iprt/thread.h>
26#include <iprt/semaphore.h>
27#include <iprt/critsect.h>
28
29#include <algorithm>
30#include <string>
31#include "HostDnsService.h"
32
33
34static HostDnsMonitor *g_monitor;
35
36
37/* Lockee */
38Lockee::Lockee()
39{
40 RTCritSectInit(&mLock);
41}
42
43Lockee::~Lockee()
44{
45 RTCritSectDelete(&mLock);
46}
47
48const RTCRITSECT* Lockee::lock() const
49{
50 return &mLock;
51}
52
53/* ALock */
54ALock::ALock(const Lockee *aLockee)
55 : lockee(aLockee)
56{
57 RTCritSectEnter(const_cast<PRTCRITSECT>(lockee->lock()));
58}
59
60ALock::~ALock()
61{
62 RTCritSectLeave(const_cast<PRTCRITSECT>(lockee->lock()));
63}
64
65inline static void detachVectorOfString(const std::vector<std::string>& v,
66 ComSafeArrayOut(BSTR, aBstrArray))
67{
68 com::SafeArray<BSTR> aBstr(v.size());
69
70 std::vector<std::string>::const_iterator it;
71
72 int i = 0;
73 it = v.begin();
74 for (; it != v.end(); ++it, ++i)
75 Utf8Str(it->c_str()).cloneTo(&aBstr[i]);
76
77 aBstr.detachTo(ComSafeArrayOutArg(aBstrArray));
78}
79
80struct HostDnsMonitor::Data
81{
82 std::vector<PCHostDnsMonitorProxy> proxies;
83 HostDnsInformation info;
84};
85
86struct HostDnsMonitorProxy::Data
87{
88 Data(const HostDnsMonitor *aMonitor, const VirtualBox *aParent)
89 : info(NULL)
90 , virtualbox(aParent)
91 , monitor(aMonitor)
92 , fModified(true)
93 {}
94
95 virtual ~Data()
96 {
97 if (info)
98 {
99 delete info;
100 info = NULL;
101 }
102 }
103
104 HostDnsInformation *info;
105 const VirtualBox *virtualbox;
106 const HostDnsMonitor *monitor;
107 bool fModified;
108};
109
110
111HostDnsMonitor::HostDnsMonitor()
112 : m(NULL)
113{
114}
115
116HostDnsMonitor::~HostDnsMonitor()
117{
118 if (m)
119 {
120 delete m;
121 m = NULL;
122 }
123}
124
125const HostDnsMonitor *HostDnsMonitor::getHostDnsMonitor()
126{
127 /* XXX: Moved initialization from HostImpl.cpp */
128 if (!g_monitor)
129 {
130# if defined (RT_OS_DARWIN)
131 g_monitor = new HostDnsServiceDarwin();
132# elif defined(RT_OS_WINDOWS)
133 g_monitor = new HostDnsServiceWin();
134# elif defined(RT_OS_LINUX)
135 g_monitor = new HostDnsServiceLinux();
136# elif defined(RT_OS_SOLARIS)
137 g_monitor = new HostDnsServiceSolaris();
138# elif defined(RT_OS_FREEBSD)
139 g_monitor = new HostDnsServiceFreebsd();
140# elif defined(RT_OS_OS2)
141 g_monitor = new HostDnsServiceOs2();
142# else
143 g_monitor = new HostDnsService();
144# endif
145 g_monitor->init();
146 }
147
148 return g_monitor;
149}
150
151void HostDnsMonitor::addMonitorProxy(PCHostDnsMonitorProxy proxy) const
152{
153 ALock l(this);
154 m->proxies.push_back(proxy);
155 proxy->notify();
156}
157
158void HostDnsMonitor::releaseMonitorProxy(PCHostDnsMonitorProxy proxy) const
159{
160 ALock l(this);
161 std::vector<PCHostDnsMonitorProxy>::iterator it;
162 it = std::find(m->proxies.begin(), m->proxies.end(), proxy);
163
164 if (it == m->proxies.end())
165 return;
166
167 m->proxies.erase(it);
168}
169
170void HostDnsMonitor::shutdown()
171{
172 if (g_monitor)
173 {
174 delete g_monitor;
175 g_monitor = NULL;
176 }
177}
178
179const HostDnsInformation &HostDnsMonitor::getInfo() const
180{
181 return m->info;
182}
183
184void HostDnsMonitor::notifyAll() const
185{
186 ALock l(this);
187 std::vector<PCHostDnsMonitorProxy>::const_iterator it;
188 for (it = m->proxies.begin(); it != m->proxies.end(); ++it)
189 (*it)->notify();
190}
191
192void HostDnsMonitor::setInfo(const HostDnsInformation &info)
193{
194 ALock l(this);
195 m->info = info;
196}
197
198HRESULT HostDnsMonitor::init()
199{
200 m = new HostDnsMonitor::Data();
201 return S_OK;
202}
203
204
205/* HostDnsMonitorProxy */
206HostDnsMonitorProxy::HostDnsMonitorProxy()
207 : m(NULL)
208{
209}
210
211HostDnsMonitorProxy::~HostDnsMonitorProxy()
212{
213 if (m)
214 {
215 if (m->monitor)
216 m->monitor->releaseMonitorProxy(this);
217 delete m;
218 m = NULL;
219 }
220}
221
222void HostDnsMonitorProxy::init(const HostDnsMonitor *mon, const VirtualBox* aParent)
223{
224 m = new HostDnsMonitorProxy::Data(mon, aParent);
225 m->monitor->addMonitorProxy(this);
226 updateInfo();
227}
228
229void HostDnsMonitorProxy::notify() const
230{
231 m->fModified = true;
232 const_cast<VirtualBox *>(m->virtualbox)->onHostNameResolutionConfigurationChange();
233}
234
235HRESULT HostDnsMonitorProxy::GetNameServers(ComSafeArrayOut(BSTR, aNameServers))
236{
237 AssertReturn(m && m->info, E_FAIL);
238 ALock l(this);
239
240 if (m->fModified)
241 updateInfo();
242
243 detachVectorOfString(m->info->servers, ComSafeArrayOutArg(aNameServers));
244
245 return S_OK;
246}
247
248HRESULT HostDnsMonitorProxy::GetDomainName(BSTR *aDomainName)
249{
250 AssertReturn(m && m->info, E_FAIL);
251 ALock l(this);
252
253 if (m->fModified)
254 updateInfo();
255
256 Utf8Str(m->info->domain.c_str()).cloneTo(aDomainName);
257
258 return S_OK;
259}
260
261HRESULT HostDnsMonitorProxy::GetSearchStrings(ComSafeArrayOut(BSTR, aSearchStrings))
262{
263 AssertReturn(m && m->info, E_FAIL);
264 ALock l(this);
265
266 if (m->fModified)
267 updateInfo();
268
269 detachVectorOfString(m->info->searchList, ComSafeArrayOutArg(aSearchStrings));
270
271 return S_OK;
272}
273
274bool HostDnsMonitorProxy::operator==(PCHostDnsMonitorProxy& rhs)
275{
276 if (!m || !rhs->m)
277 return false;
278
279 /**
280 * we've assigned to the same instance of VirtualBox.
281 */
282 return m->virtualbox == rhs->m->virtualbox;
283}
284
285void HostDnsMonitorProxy::updateInfo()
286{
287 HostDnsInformation *info = new HostDnsInformation(m->monitor->getInfo());
288 HostDnsInformation *old = m->info;
289
290 m->info = info;
291 if (old)
292 delete old;
293
294 m->fModified = false;
295}
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