1 | /* $Id: HostDnsService.h 108012 2025-02-01 02:19:11Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Host DNS listener.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef MAIN_INCLUDED_SRC_src_server_HostDnsService_h
|
---|
29 | #define MAIN_INCLUDED_SRC_src_server_HostDnsService_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 | #include "VirtualBoxBase.h"
|
---|
34 |
|
---|
35 | #include <iprt/err.h> /* VERR_IGNORED */
|
---|
36 | #include <iprt/cpp/lock.h>
|
---|
37 |
|
---|
38 | #include <list>
|
---|
39 | #include <vector>
|
---|
40 |
|
---|
41 | typedef std::list<com::Utf8Str> Utf8StrList;
|
---|
42 | typedef Utf8StrList::iterator Utf8StrListIterator;
|
---|
43 |
|
---|
44 | class HostDnsMonitorProxy;
|
---|
45 | typedef const HostDnsMonitorProxy *PCHostDnsMonitorProxy;
|
---|
46 |
|
---|
47 | class HostDnsInformation
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | static const uint32_t IGNORE_SERVER_ORDER = RT_BIT_32(0);
|
---|
51 | static const uint32_t IGNORE_SUFFIXES = RT_BIT_32(1);
|
---|
52 |
|
---|
53 | public:
|
---|
54 | std::vector<com::Utf8Str> servers;
|
---|
55 | com::Utf8Str domain;
|
---|
56 | std::vector<com::Utf8Str> searchList;
|
---|
57 | bool equals(const HostDnsInformation &, uint32_t fLaxComparison = 0) const;
|
---|
58 | };
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Base class for host DNS service implementations.
|
---|
62 | *
|
---|
63 | * This class supposed to be a real DNS monitor object as a singleton,
|
---|
64 | * so it lifecycle starts and ends together with VBoxSVC.
|
---|
65 | */
|
---|
66 | class HostDnsServiceBase
|
---|
67 | {
|
---|
68 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(HostDnsServiceBase);
|
---|
69 |
|
---|
70 | public:
|
---|
71 |
|
---|
72 | static HostDnsServiceBase *createHostDnsMonitor(void);
|
---|
73 |
|
---|
74 | public:
|
---|
75 |
|
---|
76 | /* @note: method will wait till client call
|
---|
77 | HostDnsService::monitorThreadInitializationDone() */
|
---|
78 | virtual HRESULT init(HostDnsMonitorProxy *pProxy);
|
---|
79 | virtual void uninit(void);
|
---|
80 |
|
---|
81 | virtual ~HostDnsServiceBase();
|
---|
82 |
|
---|
83 | protected:
|
---|
84 |
|
---|
85 | explicit HostDnsServiceBase(bool fThreaded = false);
|
---|
86 |
|
---|
87 | void setInfo(const HostDnsInformation &);
|
---|
88 |
|
---|
89 | /* this function used only if HostDnsMonitor::HostDnsMonitor(true) */
|
---|
90 | void onMonitorThreadInitDone();
|
---|
91 |
|
---|
92 | public:
|
---|
93 |
|
---|
94 | virtual int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs)
|
---|
95 | {
|
---|
96 | RT_NOREF(uTimeoutMs); AssertFailed(); return VERR_NOT_IMPLEMENTED;
|
---|
97 | }
|
---|
98 |
|
---|
99 | virtual int monitorThreadProc(void) { AssertFailed(); return VERR_NOT_IMPLEMENTED; }
|
---|
100 |
|
---|
101 | private:
|
---|
102 |
|
---|
103 | static DECLCALLBACK(int) threadMonitorProc(RTTHREAD, void *);
|
---|
104 |
|
---|
105 | protected:
|
---|
106 |
|
---|
107 | mutable RTCLockMtx m_LockMtx;
|
---|
108 |
|
---|
109 | //public: /** @todo r=andy Why is this public? */
|
---|
110 |
|
---|
111 | struct Data;
|
---|
112 | Data *m;
|
---|
113 | };
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * This class supposed to be a proxy for events on changing Host Name Resolving configurations.
|
---|
117 | */
|
---|
118 | class HostDnsMonitorProxy
|
---|
119 | : public Lockable
|
---|
120 | {
|
---|
121 | public:
|
---|
122 |
|
---|
123 | HostDnsMonitorProxy();
|
---|
124 | virtual ~HostDnsMonitorProxy();
|
---|
125 |
|
---|
126 | public:
|
---|
127 |
|
---|
128 | HRESULT init(VirtualBox *virtualbox);
|
---|
129 | void uninit(void);
|
---|
130 | void notify(const HostDnsInformation &info);
|
---|
131 |
|
---|
132 | HRESULT GetNameServers(std::vector<com::Utf8Str> &aNameServers);
|
---|
133 | HRESULT GetDomainName(com::Utf8Str *pDomainName);
|
---|
134 | HRESULT GetSearchStrings(std::vector<com::Utf8Str> &aSearchStrings);
|
---|
135 |
|
---|
136 | LockHandle *lockHandle() const RT_OVERRIDE;
|
---|
137 |
|
---|
138 | private:
|
---|
139 |
|
---|
140 | uint32_t pollGlobalExtraData(AutoWriteLock &aLock);
|
---|
141 | bool updateInfo(const HostDnsInformation &info);
|
---|
142 |
|
---|
143 | private:
|
---|
144 | struct Data;
|
---|
145 | Data *m;
|
---|
146 | /** Object lock object. */
|
---|
147 | mutable util::RWLockHandle m_ObjectLock;
|
---|
148 | };
|
---|
149 |
|
---|
150 | # if defined(RT_OS_DARWIN) || defined(DOXYGEN_RUNNING)
|
---|
151 | class HostDnsServiceDarwin : public HostDnsServiceBase
|
---|
152 | {
|
---|
153 | public:
|
---|
154 |
|
---|
155 | HostDnsServiceDarwin();
|
---|
156 | virtual ~HostDnsServiceDarwin();
|
---|
157 |
|
---|
158 | public:
|
---|
159 |
|
---|
160 | HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE;
|
---|
161 | void uninit(void) RT_OVERRIDE;
|
---|
162 |
|
---|
163 | protected:
|
---|
164 |
|
---|
165 | int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs) RT_OVERRIDE;
|
---|
166 | int monitorThreadProc(void) RT_OVERRIDE;
|
---|
167 |
|
---|
168 | private:
|
---|
169 |
|
---|
170 | int updateInfo(void);
|
---|
171 | static void hostDnsServiceStoreCallback(void *store, void *arrayRef, void *info);
|
---|
172 | struct Data;
|
---|
173 | Data *m;
|
---|
174 | };
|
---|
175 | # endif
|
---|
176 | # if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
|
---|
177 | class HostDnsServiceWin : public HostDnsServiceBase
|
---|
178 | {
|
---|
179 | public:
|
---|
180 | HostDnsServiceWin();
|
---|
181 | virtual ~HostDnsServiceWin();
|
---|
182 |
|
---|
183 | public:
|
---|
184 |
|
---|
185 | HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE;
|
---|
186 | void uninit(void) RT_OVERRIDE;
|
---|
187 |
|
---|
188 | protected:
|
---|
189 |
|
---|
190 | int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs) RT_OVERRIDE;
|
---|
191 | int monitorThreadProc(void) RT_OVERRIDE;
|
---|
192 |
|
---|
193 | private:
|
---|
194 |
|
---|
195 | HRESULT updateInfo(void);
|
---|
196 |
|
---|
197 | private:
|
---|
198 |
|
---|
199 | struct Data;
|
---|
200 | Data *m;
|
---|
201 | };
|
---|
202 | # endif
|
---|
203 | # if defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_FREEBSD) \
|
---|
204 | || defined(DOXYGEN_RUNNING)
|
---|
205 | class HostDnsServiceResolvConf: public HostDnsServiceBase
|
---|
206 | {
|
---|
207 | public:
|
---|
208 |
|
---|
209 | explicit HostDnsServiceResolvConf(bool fThreaded = false) : HostDnsServiceBase(fThreaded), m(NULL) {}
|
---|
210 | virtual ~HostDnsServiceResolvConf();
|
---|
211 |
|
---|
212 | public:
|
---|
213 |
|
---|
214 | HRESULT init(HostDnsMonitorProxy *pProxy, const char *aResolvConfFileName);
|
---|
215 | void uninit(void) RT_OVERRIDE;
|
---|
216 |
|
---|
217 | const Utf8Str &getResolvConf(void) const;
|
---|
218 |
|
---|
219 | protected:
|
---|
220 |
|
---|
221 | HRESULT readResolvConf(void);
|
---|
222 |
|
---|
223 | protected:
|
---|
224 |
|
---|
225 | struct Data;
|
---|
226 | Data *m;
|
---|
227 | };
|
---|
228 | # if defined(RT_OS_SOLARIS) || defined(DOXYGEN_RUNNING)
|
---|
229 | /**
|
---|
230 | * XXX: https://blogs.oracle.com/praks/entry/file_events_notification
|
---|
231 | */
|
---|
232 | class HostDnsServiceSolaris : public HostDnsServiceResolvConf
|
---|
233 | {
|
---|
234 | public:
|
---|
235 |
|
---|
236 | HostDnsServiceSolaris() {}
|
---|
237 | virtual ~HostDnsServiceSolaris() {}
|
---|
238 |
|
---|
239 | public:
|
---|
240 |
|
---|
241 | virtual HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE
|
---|
242 | {
|
---|
243 | return HostDnsServiceResolvConf::init(pProxy, "/etc/resolv.conf");
|
---|
244 | }
|
---|
245 | };
|
---|
246 |
|
---|
247 | # endif
|
---|
248 | # if defined(RT_OS_LINUX) || defined(DOXYGEN_RUNNING)
|
---|
249 | class HostDnsServiceLinux : public HostDnsServiceResolvConf
|
---|
250 | {
|
---|
251 | public:
|
---|
252 |
|
---|
253 | HostDnsServiceLinux() : HostDnsServiceResolvConf(true), m_fdShutdown(-1) {}
|
---|
254 | virtual ~HostDnsServiceLinux();
|
---|
255 |
|
---|
256 | public:
|
---|
257 |
|
---|
258 | HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE;
|
---|
259 |
|
---|
260 | protected:
|
---|
261 |
|
---|
262 | int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs) RT_OVERRIDE;
|
---|
263 | int monitorThreadProc(void) RT_OVERRIDE;
|
---|
264 |
|
---|
265 | /** Socket end to write shutdown notification to, so the monitor thread will
|
---|
266 | * wake up and terminate. */
|
---|
267 | int m_fdShutdown;
|
---|
268 | };
|
---|
269 |
|
---|
270 | # endif
|
---|
271 | # if defined(RT_OS_FREEBSD) || defined(DOXYGEN_RUNNING)
|
---|
272 | class HostDnsServiceFreebsd: public HostDnsServiceResolvConf
|
---|
273 | {
|
---|
274 | public:
|
---|
275 |
|
---|
276 | HostDnsServiceFreebsd(){}
|
---|
277 | virtual ~HostDnsServiceFreebsd() {}
|
---|
278 |
|
---|
279 | public:
|
---|
280 |
|
---|
281 | virtual HRESULT init(HostDnsMonitorProxy *pProxy)
|
---|
282 | {
|
---|
283 | return HostDnsServiceResolvConf::init(pProxy, "/etc/resolv.conf");
|
---|
284 | }
|
---|
285 | };
|
---|
286 |
|
---|
287 | # endif
|
---|
288 | # if defined(RT_OS_OS2) || defined(DOXYGEN_RUNNING)
|
---|
289 | class HostDnsServiceOs2 : public HostDnsServiceResolvConf
|
---|
290 | {
|
---|
291 | public:
|
---|
292 |
|
---|
293 | HostDnsServiceOs2() {}
|
---|
294 | virtual ~HostDnsServiceOs2() {}
|
---|
295 |
|
---|
296 | public:
|
---|
297 |
|
---|
298 | /* XXX: \\MPTN\\ETC should be taken from environment variable ETC */
|
---|
299 | virtual HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE
|
---|
300 | {
|
---|
301 | return HostDnsServiceResolvConf::init(pProxy, "\\MPTN\\ETC\\RESOLV2");
|
---|
302 | }
|
---|
303 | };
|
---|
304 |
|
---|
305 | # endif
|
---|
306 | # endif
|
---|
307 |
|
---|
308 | #endif /* !MAIN_INCLUDED_SRC_src_server_HostDnsService_h */
|
---|