VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostDnsServiceResolvConf.cpp@ 49386

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

Main/HostDnsService: splits HostDnsService on "singleton" HostDnsMonitor
which monitors host changes and share DnsInformation to per HostImpl/VirtualBoxImpl objects
HostDnsMonitorProxy.

TODO: Win/Darwin parts might burn (not tested)
TODO: find good place to call HostDnsMonitor::shutdown() to stop
monitoring thread. (ref counting could be used on
HostDnsMonitor::addMonitorProxy and HostDnsMonitor::releaseMonitorProxy,
but it better to pausing monitoring on no --auto-shutdown launches of VBoxSVC).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1/* -*- indent-tabs-mode: nil; -*- */
2#include <VBox/com/string.h>
3#include <VBox/com/ptr.h>
4
5
6#ifdef RT_OS_OS2
7# include <sys/socket.h>
8typedef int socklen_t;
9#endif
10
11#include <stdio.h>
12#include <sys/socket.h>
13#include <netinet/in.h>
14#include <arpa/inet.h>
15
16
17#include <iprt/assert.h>
18#include <iprt/err.h>
19#include <iprt/file.h>
20#include <iprt/critsect.h>
21
22#include <VBox/log.h>
23
24#include <string>
25#include <vector>
26
27#include "HostDnsService.h"
28
29
30struct HostDnsServiceResolvConf::Data
31{
32 Data(const char *fileName):resolvConfFilename(fileName){};
33
34 std::string resolvConfFilename;
35};
36
37
38const std::string& HostDnsServiceResolvConf::resolvConf()
39{
40 return m->resolvConfFilename;
41}
42
43
44static int fileGets(RTFILE File, void *pvBuf, size_t cbBufSize, size_t *pcbRead)
45{
46 size_t cbRead;
47 char bTest;
48 int rc = VERR_NO_MEMORY;
49 char *pu8Buf = (char *)pvBuf;
50 *pcbRead = 0;
51
52 while ( RT_SUCCESS(rc = RTFileRead(File, &bTest, 1, &cbRead))
53 && (pu8Buf - (char *)pvBuf) >= 0
54 && (size_t)(pu8Buf - (char *)pvBuf) < cbBufSize)
55 {
56 if (cbRead == 0)
57 return VERR_EOF;
58
59 if (bTest == '\r' || bTest == '\n')
60 {
61 *pu8Buf = 0;
62 return VINF_SUCCESS;
63 }
64 *pu8Buf = bTest;
65 pu8Buf++;
66 (*pcbRead)++;
67 }
68 return rc;
69}
70
71
72HostDnsServiceResolvConf::~HostDnsServiceResolvConf()
73{
74 if (m) delete m;
75}
76
77
78HRESULT HostDnsServiceResolvConf::init(const char *aResolvConfFileName)
79{
80 HostDnsMonitor::init();
81
82 m = new Data(aResolvConfFileName);
83 readResolvConf();
84
85 return S_OK;
86}
87
88
89HRESULT HostDnsServiceResolvConf::readResolvConf()
90{
91 char buff[256];
92 char buff2[256];
93 int cNameserversFound = 0;
94 bool fWarnTooManyDnsServers = false;
95 struct in_addr tmp_addr;
96 size_t bytes;
97 HostDnsInformation info;
98 RTFILE resolvConfFile;
99
100 int rc = RTFileOpen(&resolvConfFile, m->resolvConfFilename.c_str(),
101 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
102 AssertRCReturn(rc, E_FAIL);
103
104 while ( RT_SUCCESS(rc = fileGets(resolvConfFile, buff, sizeof(buff), &bytes))
105 && rc != VERR_EOF)
106 {
107 if ( cNameserversFound == 4
108 && !fWarnTooManyDnsServers
109 && sscanf(buff, "nameserver%*[ \t]%255s", buff2) == 1)
110 {
111 fWarnTooManyDnsServers = true;
112 LogRel(("NAT: too many nameservers registered.\n"));
113 }
114 if ( sscanf(buff, "nameserver%*[ \t]%255s", buff2) == 1
115 && cNameserversFound < 4) /* Unix doesn't accept more than 4 name servers*/
116 {
117 if (!inet_aton(buff2, &tmp_addr))
118 continue;
119
120 info.servers.push_back(std::string(buff2));
121
122 cNameserversFound++;
123 }
124 if ((!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
125 {
126 char *tok;
127 char *saveptr;
128
129 tok = strtok_r(&buff[6], " \t\n", &saveptr);
130
131 if (tok != NULL)
132 info.domain = std::string(tok);
133 }
134 }
135
136 RTFileClose(resolvConfFile);
137
138 setInfo(info);
139
140 return S_OK;
141}
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