summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-server/HostDnsService.h
blob: 76ad0ea1951a6473a9bb63b4fc6b53584ab3c88d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* $Id: HostDnsService.h $ */
/** @file
 * Host DNS listener.
 */

/*
 * Copyright (C) 2005-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#ifndef MAIN_INCLUDED_SRC_src_server_HostDnsService_h
#define MAIN_INCLUDED_SRC_src_server_HostDnsService_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include "VirtualBoxBase.h"

#include <iprt/err.h> /* VERR_IGNORED */
#include <iprt/cpp/lock.h>

#include <list>
#include <iprt/sanitized/string>
#include <vector>

typedef std::list<com::Utf8Str> Utf8StrList;
typedef Utf8StrList::iterator Utf8StrListIterator;

class HostDnsMonitorProxy;
typedef const HostDnsMonitorProxy *PCHostDnsMonitorProxy;

class HostDnsInformation
{
public:
    static const uint32_t IGNORE_SERVER_ORDER = RT_BIT_32(0);
    static const uint32_t IGNORE_SUFFIXES     = RT_BIT_32(1);

public:
    /** @todo r=bird: Why on earth are we using std::string and not Utf8Str?   */
    std::vector<std::string> servers;
    std::string domain;
    std::vector<std::string> searchList;
    bool equals(const HostDnsInformation &, uint32_t fLaxComparison = 0) const;
};

/**
 * Base class for host DNS service implementations.
 * This class supposed to be a real DNS monitor object as a singleton,
 * so it lifecycle starts and ends together with VBoxSVC.
 */
class HostDnsServiceBase
{
    DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(HostDnsServiceBase);

public:

    static HostDnsServiceBase *createHostDnsMonitor(void);

public:

    /* @note: method will wait till client call
       HostDnsService::monitorThreadInitializationDone() */
    virtual HRESULT init(HostDnsMonitorProxy *pProxy);
    virtual void uninit(void);

    virtual ~HostDnsServiceBase();

protected:

    explicit HostDnsServiceBase(bool fThreaded = false);

    void setInfo(const HostDnsInformation &);

    /* this function used only if HostDnsMonitor::HostDnsMonitor(true) */
    void onMonitorThreadInitDone();

public:

    virtual int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs)
    {
        RT_NOREF(uTimeoutMs); AssertFailed(); return VERR_NOT_IMPLEMENTED;
    }

    virtual int monitorThreadProc(void) { AssertFailed(); return VERR_NOT_IMPLEMENTED; }

private:

    static DECLCALLBACK(int) threadMonitorProc(RTTHREAD, void *);

protected:

    mutable RTCLockMtx m_LockMtx;

public: /** @todo r=andy Why is this public? */

    struct Data;
    Data *m;
};

/**
 * This class supposed to be a proxy for events on changing Host Name Resolving configurations.
 */
class HostDnsMonitorProxy
{
public:

    HostDnsMonitorProxy();
    virtual ~HostDnsMonitorProxy();

public:

    HRESULT init(VirtualBox *virtualbox);
    void uninit(void);
    void notify(const HostDnsInformation &info);

    HRESULT GetNameServers(std::vector<com::Utf8Str> &aNameServers);
    HRESULT GetDomainName(com::Utf8Str *pDomainName);
    HRESULT GetSearchStrings(std::vector<com::Utf8Str> &aSearchStrings);

private:

    void pollGlobalExtraData(void);
    bool updateInfo(const HostDnsInformation &info);

private:

    mutable RTCLockMtx m_LockMtx;

    struct Data;
    Data *m;
};

# if defined(RT_OS_DARWIN) || defined(DOXYGEN_RUNNING)
class HostDnsServiceDarwin : public HostDnsServiceBase
{
public:

    HostDnsServiceDarwin();
    virtual ~HostDnsServiceDarwin();

public:

    HRESULT init(HostDnsMonitorProxy *pProxy);
    void uninit(void);

protected:

    int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs);
    int monitorThreadProc(void);

private:

    int updateInfo(void);
    static void hostDnsServiceStoreCallback(void *store, void *arrayRef, void *info);
    struct Data;
    Data *m;
};
# endif
# if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
class HostDnsServiceWin : public HostDnsServiceBase
{
public:
    HostDnsServiceWin();
    virtual ~HostDnsServiceWin();

public:

    HRESULT init(HostDnsMonitorProxy *pProxy);
    void uninit(void);

protected:

    int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs);
    int monitorThreadProc(void);

private:

    HRESULT updateInfo(void);

private:

    struct Data;
    Data *m;
};
# endif
# if defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_FREEBSD) \
    || defined(DOXYGEN_RUNNING)
class HostDnsServiceResolvConf: public HostDnsServiceBase
{
public:

    explicit HostDnsServiceResolvConf(bool fThreaded = false) : HostDnsServiceBase(fThreaded), m(NULL) {}
    virtual ~HostDnsServiceResolvConf();

public:

    HRESULT init(HostDnsMonitorProxy *pProxy, const char *aResolvConfFileName);
    void uninit(void);

    const std::string& getResolvConf(void) const;

protected:

    HRESULT readResolvConf(void);

protected:

    struct Data;
    Data *m;
};
#  if defined(RT_OS_SOLARIS) || defined(DOXYGEN_RUNNING)
/**
 * XXX: https://blogs.oracle.com/praks/entry/file_events_notification
 */
class HostDnsServiceSolaris : public HostDnsServiceResolvConf
{
public:

    HostDnsServiceSolaris() {}
    virtual ~HostDnsServiceSolaris() {}

public:

    virtual HRESULT init(HostDnsMonitorProxy *pProxy) {
        return HostDnsServiceResolvConf::init(pProxy, "/etc/resolv.conf");
    }
};

#  endif
#  if defined(RT_OS_LINUX) || defined(DOXYGEN_RUNNING)
class HostDnsServiceLinux : public HostDnsServiceResolvConf
{
public:

    HostDnsServiceLinux() : HostDnsServiceResolvConf(true), m_fdShutdown(-1) {}
    virtual ~HostDnsServiceLinux();

public:

    HRESULT init(HostDnsMonitorProxy *pProxy);

protected:

    int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs);
    int monitorThreadProc(void);

    /** Socket end to write shutdown notification to, so the monitor thread will
     *  wake up and terminate. */
    int m_fdShutdown;
};

#  endif
#  if defined(RT_OS_FREEBSD) || defined(DOXYGEN_RUNNING)
class HostDnsServiceFreebsd: public HostDnsServiceResolvConf
{
public:

    HostDnsServiceFreebsd(){}
    virtual ~HostDnsServiceFreebsd() {}

public:

    virtual HRESULT init(HostDnsMonitorProxy *pProxy)
    {
        return HostDnsServiceResolvConf::init(pProxy, "/etc/resolv.conf");
    }
};

#  endif
#  if defined(RT_OS_OS2) || defined(DOXYGEN_RUNNING)
class HostDnsServiceOs2 : public HostDnsServiceResolvConf
{
public:

    HostDnsServiceOs2() {}
    virtual ~HostDnsServiceOs2() {}

public:

    /* XXX: \\MPTN\\ETC should be taken from environment variable ETC  */
    virtual HRESULT init(HostDnsMonitorProxy *pProxy)
    {
        return HostDnsServiceResolvConf::init(pProxy, "\\MPTN\\ETC\\RESOLV2");
    }
};

#  endif
# endif

#endif /* !MAIN_INCLUDED_SRC_src_server_HostDnsService_h */