VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostDnsService.h@ 53151

Last change on this file since 53151 was 53151, checked in by vboxsync, 10 years ago

Main/HostDnsService: bring back Windows changes of r96331 but with
plain std::string in HostDnsInformation and using explicit Unicode API
and wchar_t when dealing with registry (we always compile with
-DUNICODE, so no need for TCHAR shim). No changes to the original
except minor const correctness tweaks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: HostDnsService.h 53151 2014-10-27 12:34:31Z vboxsync $ */
2/** @file
3 * Host DNS listener.
4 */
5
6/*
7 * Copyright (C) 2005-2012 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#ifndef ___H_DNSHOSTSERVICE
19#define ___H_DNSHOSTSERVICE
20#include "VirtualBoxBase.h"
21
22#include <iprt/cdefs.h>
23#include <iprt/critsect.h>
24#include <iprt/types.h>
25
26#include <list>
27#include <vector>
28
29typedef std::list<com::Utf8Str> Utf8StrList;
30typedef Utf8StrList::iterator Utf8StrListIterator;
31
32class HostDnsMonitorProxy;
33typedef const HostDnsMonitorProxy *PCHostDnsMonitorProxy;
34
35class Lockee
36{
37 public:
38 Lockee();
39 virtual ~Lockee();
40 const RTCRITSECT* lock() const;
41
42 private:
43 RTCRITSECT mLock;
44};
45
46class ALock
47{
48 public:
49 explicit ALock(const Lockee *l);
50 ~ALock();
51
52 private:
53 const Lockee *lockee;
54};
55
56class HostDnsInformation
57{
58 public:
59 std::vector<std::string> servers;
60 std::string domain;
61 std::vector<std::string> searchList;
62};
63
64/**
65 * This class supposed to be a real DNS monitor object it should be singleton,
66 * it lifecycle starts and ends together with VBoxSVC.
67 */
68class HostDnsMonitor : public Lockee
69{
70 public:
71 static const HostDnsMonitor *getHostDnsMonitor();
72 static void shutdown();
73
74 void addMonitorProxy(PCHostDnsMonitorProxy) const;
75 void releaseMonitorProxy(PCHostDnsMonitorProxy) const;
76 const HostDnsInformation &getInfo() const;
77 /* @note: method will wait till client call
78 HostDnsService::monitorThreadInitializationDone() */
79 virtual HRESULT init();
80
81 protected:
82 explicit HostDnsMonitor(bool fThreaded = false);
83 virtual ~HostDnsMonitor();
84
85 void notifyAll() const;
86 void setInfo(const HostDnsInformation &);
87
88 /* this function used only if HostDnsMonitor::HostDnsMonitor(true) */
89 void monitorThreadInitializationDone();
90 virtual void monitorThreadShutdown() = 0;
91 virtual int monitorWorker() = 0;
92
93 private:
94 HostDnsMonitor(const HostDnsMonitor &);
95 HostDnsMonitor& operator= (const HostDnsMonitor &);
96 static int threadMonitoringRoutine(RTTHREAD, void *);
97
98 public:
99 struct Data;
100 Data *m;
101};
102
103/**
104 * This class supposed to be a proxy for events on changing Host Name Resolving configurations.
105 */
106class HostDnsMonitorProxy : public Lockee
107{
108 public:
109 HostDnsMonitorProxy();
110 ~HostDnsMonitorProxy();
111 void init(const HostDnsMonitor *aMonitor, const VirtualBox *aParent);
112 void notify() const;
113
114 HRESULT GetNameServers(std::vector<com::Utf8Str> &aNameServers);
115 HRESULT GetDomainName(com::Utf8Str *pDomainName);
116 HRESULT GetSearchStrings(std::vector<com::Utf8Str> &aSearchStrings);
117
118 bool operator==(PCHostDnsMonitorProxy&);
119
120 private:
121 void updateInfo();
122
123 private:
124 struct Data;
125 Data *m;
126};
127
128# ifdef RT_OS_DARWIN
129class HostDnsServiceDarwin : public HostDnsMonitor
130{
131 public:
132 HostDnsServiceDarwin();
133 ~HostDnsServiceDarwin();
134 HRESULT init();
135
136 protected:
137 virtual void monitorThreadShutdown();
138 virtual int monitorWorker();
139
140 private:
141 HRESULT updateInfo();
142 static void hostDnsServiceStoreCallback(void *store, void *arrayRef, void *info);
143 struct Data;
144 Data *m;
145};
146# endif
147# ifdef RT_OS_WINDOWS
148/* Maximum size of Windows registry key (according to MSDN). */
149#define VBOX_KEY_NAME_LEN_MAX (255)
150
151class HostDnsServiceWin : public HostDnsMonitor
152{
153public:
154 HostDnsServiceWin();
155 ~HostDnsServiceWin();
156
157 HRESULT init();
158
159protected:
160 virtual void monitorThreadShutdown();
161 virtual int monitorWorker();
162
163private:
164 /*
165 * Bit flags to determine what was exactly was changed when
166 * Windows triggered event notification.
167 */
168 enum {
169 VBOX_EVENT_NO_CHANGES = 0,
170 VBOX_EVENT_SERVERS_CHANGED = RT_BIT(1),
171 VBOX_EVENT_DOMAIN_CHANGED = RT_BIT(2),
172 VBOX_EVENT_SEARCHLIST_CHANGED = RT_BIT(3),
173 };
174
175 /* m_aWarehouse array offsets. */
176 enum {
177 VBOX_OFFSET_SHUTDOWN_EVENT = 0,
178 VBOX_OFFSET_TREE_EVENT = 1,
179 VBOX_OFFSET_SUBTREE_EVENTS = 2,
180 };
181
182 /*
183 * This structure is used in order to link Windows registry key
184 * with an event which is generated once the key has been changed
185 * (when interface has updated its DNS setting) or one of the
186 * sub-keys has been deleted or added (when interface added or
187 * deleted).
188 */
189 struct Item
190 {
191 HKEY hKey; /* Key handle. */
192 HANDLE hEvent; /* Event handle. */
193 wchar_t wcsInterface[VBOX_KEY_NAME_LEN_MAX]; /* Path to key within Windows registry. */
194 };
195
196
197 static const wchar_t pwcKeyRoot[];
198
199 /*
200 * This flag indicates whether constructor performed
201 * initialization correctly. If not set, monitorWorker() will not
202 * perform any action and will be terminated as soon as there will
203 * be an attempt to run it.
204 */
205 bool m_fInitialized;
206
207 /*
208 * Keys and events storage. Size of this vector should not be
209 * greater than MAXIMUM_WAIT_OBJECTS because this is exactly
210 * maximum amount of events which we allowed to wait for.
211 */
212 std::vector<struct Item> m_aWarehouse;
213
214 /* Cached host network configuration. */
215 HostDnsInformation m_hostInfoCache;
216
217
218 /* Do actual unsubscription for given item. */
219 bool releaseWarehouseItem(int idxItem);
220
221 /* Release all allocated resources and unsubscribe from everything. */
222 void releaseResources();
223
224 /* Remove subscription from DNS change notifications and release corresponding resources. */
225 bool dropSubTreeNotifications();
226
227 /* Create & add event into the list of events we monitor to. */
228 bool subscribeTo(const wchar_t *wcsPath, const wchar_t *wcsInterface, DWORD fFilter);
229
230 /* Subscribe to DNS changes. */
231 bool enumerateSubTree();
232
233 /* Get plain array of event handles. */
234 void getEventHandles(HANDLE *ahEvents);
235 void extendVectorWithStrings(std::vector<std::string>& pVectorToExtend, const std::wstring &wcsParameter);
236
237 HRESULT updateInfo(uint8_t *fWhatsChanged);
238};
239# endif
240# if defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_FREEBSD)
241class HostDnsServiceResolvConf: public HostDnsMonitor
242{
243 public:
244 explicit HostDnsServiceResolvConf(bool fThreaded = false) : HostDnsMonitor(fThreaded), m(NULL) {}
245 virtual ~HostDnsServiceResolvConf();
246 virtual HRESULT init(const char *aResolvConfFileName);
247 const std::string& resolvConf() const;
248
249 protected:
250 HRESULT readResolvConf();
251 /* While not all hosts supports Hosts DNS change notifiaction
252 * default implementation offers return VERR_IGNORE.
253 */
254 virtual void monitorThreadShutdown() {}
255 virtual int monitorWorker() {return VERR_IGNORED;}
256
257 protected:
258 struct Data;
259 Data *m;
260};
261# if defined(RT_OS_SOLARIS)
262/**
263 * XXX: https://blogs.oracle.com/praks/entry/file_events_notification
264 */
265class HostDnsServiceSolaris : public HostDnsServiceResolvConf
266{
267 public:
268 HostDnsServiceSolaris(){}
269 ~HostDnsServiceSolaris(){}
270 HRESULT init(){ return HostDnsServiceResolvConf::init("/etc/resolv.conf");}
271};
272
273# elif defined(RT_OS_LINUX)
274class HostDnsServiceLinux : public HostDnsServiceResolvConf
275{
276 public:
277 HostDnsServiceLinux():HostDnsServiceResolvConf(true){}
278 virtual ~HostDnsServiceLinux();
279 virtual HRESULT init(){ return HostDnsServiceResolvConf::init("/etc/resolv.conf");}
280
281 protected:
282 virtual void monitorThreadShutdown();
283 virtual int monitorWorker();
284};
285
286# elif defined(RT_OS_FREEBSD)
287class HostDnsServiceFreebsd: public HostDnsServiceResolvConf
288{
289 public:
290 HostDnsServiceFreebsd(){}
291 ~HostDnsServiceFreebsd(){}
292 HRESULT init(){ return HostDnsServiceResolvConf::init("/etc/resolv.conf");}
293};
294
295# elif defined(RT_OS_OS2)
296class HostDnsServiceOs2 : public HostDnsServiceResolvConf
297{
298 public:
299 HostDnsServiceOs2(){}
300 ~HostDnsServiceOs2(){}
301 /* XXX: \\MPTN\\ETC should be taken from environment variable ETC */
302 HRESULT init(){ return init("\\MPTN\\ETC\\RESOLV2");}
303};
304
305# endif
306# endif
307
308#endif /* !___H_DNSHOSTSERVICE */
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