1 | /* $Id: HostDnsService.h 76542 2018-12-30 06:33:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Host DNS listener.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2017 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 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 | #include "VirtualBoxBase.h"
|
---|
24 |
|
---|
25 | #include <iprt/err.h> /* VERR_IGNORED */
|
---|
26 | #include <iprt/cpp/lock.h>
|
---|
27 |
|
---|
28 | #include <list>
|
---|
29 | #include <vector>
|
---|
30 |
|
---|
31 | typedef std::list<com::Utf8Str> Utf8StrList;
|
---|
32 | typedef Utf8StrList::iterator Utf8StrListIterator;
|
---|
33 |
|
---|
34 | class HostDnsMonitorProxy;
|
---|
35 | typedef const HostDnsMonitorProxy *PCHostDnsMonitorProxy;
|
---|
36 |
|
---|
37 | class HostDnsInformation
|
---|
38 | {
|
---|
39 | public:
|
---|
40 | static const uint32_t IGNORE_SERVER_ORDER = RT_BIT_32(0);
|
---|
41 | static const uint32_t IGNORE_SUFFIXES = RT_BIT_32(1);
|
---|
42 |
|
---|
43 | public:
|
---|
44 | std::vector<std::string> servers;
|
---|
45 | std::string domain;
|
---|
46 | std::vector<std::string> searchList;
|
---|
47 | bool equals(const HostDnsInformation &, uint32_t fLaxComparison = 0) const;
|
---|
48 | };
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * This class supposed to be a real DNS monitor object it should be singleton,
|
---|
52 | * it lifecycle starts and ends together with VBoxSVC.
|
---|
53 | */
|
---|
54 | class HostDnsMonitor
|
---|
55 | {
|
---|
56 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(HostDnsMonitor);
|
---|
57 |
|
---|
58 | public:
|
---|
59 | static HostDnsMonitor *createHostDnsMonitor();
|
---|
60 | static void shutdown();
|
---|
61 |
|
---|
62 | /* @note: method will wait till client call
|
---|
63 | HostDnsService::monitorThreadInitializationDone() */
|
---|
64 | virtual HRESULT init(HostDnsMonitorProxy *proxy);
|
---|
65 |
|
---|
66 | protected:
|
---|
67 | explicit HostDnsMonitor(bool fThreaded = false);
|
---|
68 | virtual ~HostDnsMonitor();
|
---|
69 |
|
---|
70 | void setInfo(const HostDnsInformation &);
|
---|
71 |
|
---|
72 | /* this function used only if HostDnsMonitor::HostDnsMonitor(true) */
|
---|
73 | void monitorThreadInitializationDone();
|
---|
74 | virtual void monitorThreadShutdown() = 0;
|
---|
75 | virtual int monitorWorker() = 0;
|
---|
76 |
|
---|
77 | private:
|
---|
78 | static DECLCALLBACK(int) threadMonitoringRoutine(RTTHREAD, void *);
|
---|
79 |
|
---|
80 | protected:
|
---|
81 | mutable RTCLockMtx m_LockMtx;
|
---|
82 |
|
---|
83 | public:
|
---|
84 | struct Data;
|
---|
85 | Data *m;
|
---|
86 | };
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * This class supposed to be a proxy for events on changing Host Name Resolving configurations.
|
---|
90 | */
|
---|
91 | class HostDnsMonitorProxy
|
---|
92 | {
|
---|
93 | public:
|
---|
94 | HostDnsMonitorProxy();
|
---|
95 | ~HostDnsMonitorProxy();
|
---|
96 | void init(VirtualBox *virtualbox);
|
---|
97 | void notify(const HostDnsInformation &info);
|
---|
98 |
|
---|
99 | HRESULT GetNameServers(std::vector<com::Utf8Str> &aNameServers);
|
---|
100 | HRESULT GetDomainName(com::Utf8Str *pDomainName);
|
---|
101 | HRESULT GetSearchStrings(std::vector<com::Utf8Str> &aSearchStrings);
|
---|
102 |
|
---|
103 | private:
|
---|
104 | void pollGlobalExtraData();
|
---|
105 | bool updateInfo(const HostDnsInformation &info);
|
---|
106 |
|
---|
107 | private:
|
---|
108 | mutable RTCLockMtx m_LockMtx;
|
---|
109 |
|
---|
110 | private:
|
---|
111 | struct Data;
|
---|
112 | Data *m;
|
---|
113 | };
|
---|
114 |
|
---|
115 | # if defined(RT_OS_DARWIN) || defined(DOXYGEN_RUNNING)
|
---|
116 | class HostDnsServiceDarwin : public HostDnsMonitor
|
---|
117 | {
|
---|
118 | public:
|
---|
119 | HostDnsServiceDarwin();
|
---|
120 | ~HostDnsServiceDarwin();
|
---|
121 | virtual HRESULT init(HostDnsMonitorProxy *proxy);
|
---|
122 |
|
---|
123 | protected:
|
---|
124 | virtual void monitorThreadShutdown();
|
---|
125 | virtual int monitorWorker();
|
---|
126 |
|
---|
127 | private:
|
---|
128 | HRESULT updateInfo();
|
---|
129 | static void hostDnsServiceStoreCallback(void *store, void *arrayRef, void *info);
|
---|
130 | struct Data;
|
---|
131 | Data *m;
|
---|
132 | };
|
---|
133 | # endif
|
---|
134 | # if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
|
---|
135 | class HostDnsServiceWin : public HostDnsMonitor
|
---|
136 | {
|
---|
137 | public:
|
---|
138 | HostDnsServiceWin();
|
---|
139 | ~HostDnsServiceWin();
|
---|
140 | virtual HRESULT init(HostDnsMonitorProxy *proxy);
|
---|
141 |
|
---|
142 | protected:
|
---|
143 | virtual void monitorThreadShutdown();
|
---|
144 | virtual int monitorWorker();
|
---|
145 |
|
---|
146 | private:
|
---|
147 | HRESULT updateInfo();
|
---|
148 |
|
---|
149 | private:
|
---|
150 | struct Data;
|
---|
151 | Data *m;
|
---|
152 | };
|
---|
153 | # endif
|
---|
154 | # if defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_FREEBSD) \
|
---|
155 | || defined(DOXYGEN_RUNNING)
|
---|
156 | class HostDnsServiceResolvConf: public HostDnsMonitor
|
---|
157 | {
|
---|
158 | public:
|
---|
159 | explicit HostDnsServiceResolvConf(bool fThreaded = false) : HostDnsMonitor(fThreaded), m(NULL) {}
|
---|
160 | virtual ~HostDnsServiceResolvConf();
|
---|
161 | virtual HRESULT init(HostDnsMonitorProxy *proxy, const char *aResolvConfFileName);
|
---|
162 | const std::string& resolvConf() const;
|
---|
163 |
|
---|
164 | protected:
|
---|
165 | HRESULT readResolvConf();
|
---|
166 | /* While not all hosts supports Hosts DNS change notifiaction
|
---|
167 | * default implementation offers return VERR_IGNORE.
|
---|
168 | */
|
---|
169 | virtual void monitorThreadShutdown() {}
|
---|
170 | virtual int monitorWorker() {return VERR_IGNORED;}
|
---|
171 |
|
---|
172 | protected:
|
---|
173 | struct Data;
|
---|
174 | Data *m;
|
---|
175 | };
|
---|
176 | # if defined(RT_OS_SOLARIS) || defined(DOXYGEN_RUNNING)
|
---|
177 | /**
|
---|
178 | * XXX: https://blogs.oracle.com/praks/entry/file_events_notification
|
---|
179 | */
|
---|
180 | class HostDnsServiceSolaris : public HostDnsServiceResolvConf
|
---|
181 | {
|
---|
182 | public:
|
---|
183 | HostDnsServiceSolaris(){}
|
---|
184 | ~HostDnsServiceSolaris(){}
|
---|
185 | virtual HRESULT init(HostDnsMonitorProxy *proxy) {
|
---|
186 | return HostDnsServiceResolvConf::init(proxy, "/etc/resolv.conf");
|
---|
187 | }
|
---|
188 | };
|
---|
189 |
|
---|
190 | # endif
|
---|
191 | # if defined(RT_OS_LINUX) || defined(DOXYGEN_RUNNING)
|
---|
192 | class HostDnsServiceLinux : public HostDnsServiceResolvConf
|
---|
193 | {
|
---|
194 | public:
|
---|
195 | HostDnsServiceLinux():HostDnsServiceResolvConf(true){}
|
---|
196 | virtual ~HostDnsServiceLinux();
|
---|
197 | virtual HRESULT init(HostDnsMonitorProxy *proxy) {
|
---|
198 | return HostDnsServiceResolvConf::init(proxy, "/etc/resolv.conf");
|
---|
199 | }
|
---|
200 |
|
---|
201 | protected:
|
---|
202 | virtual void monitorThreadShutdown();
|
---|
203 | virtual int monitorWorker();
|
---|
204 | };
|
---|
205 |
|
---|
206 | # endif
|
---|
207 | # if defined(RT_OS_FREEBSD) || defined(DOXYGEN_RUNNING)
|
---|
208 | class HostDnsServiceFreebsd: public HostDnsServiceResolvConf
|
---|
209 | {
|
---|
210 | public:
|
---|
211 | HostDnsServiceFreebsd(){}
|
---|
212 | ~HostDnsServiceFreebsd(){}
|
---|
213 | virtual HRESULT init(HostDnsMonitorProxy *proxy) {
|
---|
214 | return HostDnsServiceResolvConf::init(proxy, "/etc/resolv.conf");
|
---|
215 | }
|
---|
216 | };
|
---|
217 |
|
---|
218 | # endif
|
---|
219 | # if defined(RT_OS_OS2) || defined(DOXYGEN_RUNNING)
|
---|
220 | class HostDnsServiceOs2 : public HostDnsServiceResolvConf
|
---|
221 | {
|
---|
222 | public:
|
---|
223 | HostDnsServiceOs2(){}
|
---|
224 | ~HostDnsServiceOs2(){}
|
---|
225 | /* XXX: \\MPTN\\ETC should be taken from environment variable ETC */
|
---|
226 | virtual HRESULT init(HostDnsMonitorProxy *proxy) {
|
---|
227 | return HostDnsServiceResolvConf::init(proxy, "\\MPTN\\ETC\\RESOLV2");
|
---|
228 | }
|
---|
229 | };
|
---|
230 |
|
---|
231 | # endif
|
---|
232 | # endif
|
---|
233 |
|
---|
234 | #endif /* !___H_DNSHOSTSERVICE */
|
---|