VirtualBox

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

Last change on this file since 49644 was 49409, checked in by vboxsync, 11 years ago

Main/HostDnsService: fixed object deletion and coding style

  • 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
26#include "HostDnsService.h"
27
28
29struct HostDnsServiceResolvConf::Data
30{
31 Data(const char *fileName):resolvConfFilename(fileName){};
32
33 std::string resolvConfFilename;
34};
35
36const std::string &HostDnsServiceResolvConf::resolvConf()
37{
38 return m->resolvConfFilename;
39}
40
41static int fileGets(RTFILE File, void *pvBuf, size_t cbBufSize, size_t *pcbRead)
42{
43 size_t cbRead;
44 char bTest;
45 int rc = VERR_NO_MEMORY;
46 char *pu8Buf = (char *)pvBuf;
47 *pcbRead = 0;
48
49 while ( RT_SUCCESS(rc = RTFileRead(File, &bTest, 1, &cbRead))
50 && (pu8Buf - (char *)pvBuf) >= 0
51 && (size_t)(pu8Buf - (char *)pvBuf) < cbBufSize)
52 {
53 if (cbRead == 0)
54 return VERR_EOF;
55
56 if (bTest == '\r' || bTest == '\n')
57 {
58 *pu8Buf = 0;
59 return VINF_SUCCESS;
60 }
61 *pu8Buf = bTest;
62 pu8Buf++;
63 (*pcbRead)++;
64 }
65 return rc;
66}
67
68HostDnsServiceResolvConf::~HostDnsServiceResolvConf()
69{
70 if (m)
71 {
72 delete m;
73 m = NULL;
74 }
75}
76
77HRESULT HostDnsServiceResolvConf::init(const char *aResolvConfFileName)
78{
79 HostDnsMonitor::init();
80
81 m = new Data(aResolvConfFileName);
82 readResolvConf();
83
84 return S_OK;
85}
86
87HRESULT HostDnsServiceResolvConf::readResolvConf()
88{
89 char buff[256];
90 char buff2[256];
91 int cNameserversFound = 0;
92 bool fWarnTooManyDnsServers = false;
93 struct in_addr tmp_addr;
94 size_t bytes;
95 HostDnsInformation info;
96 RTFILE resolvConfFile;
97
98 int rc = RTFileOpen(&resolvConfFile, m->resolvConfFilename.c_str(),
99 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
100 AssertRCReturn(rc, E_FAIL);
101
102 while ( RT_SUCCESS(rc = fileGets(resolvConfFile, buff, sizeof(buff), &bytes))
103 && rc != VERR_EOF)
104 {
105 if ( cNameserversFound == 4
106 && !fWarnTooManyDnsServers
107 && sscanf(buff, "nameserver%*[ \t]%255s", buff2) == 1)
108 {
109 fWarnTooManyDnsServers = true;
110 LogRel(("NAT: too many nameservers registered.\n"));
111 }
112 if ( sscanf(buff, "nameserver%*[ \t]%255s", buff2) == 1
113 && cNameserversFound < 4) /* Unix doesn't accept more than 4 name servers*/
114 {
115 if (!inet_aton(buff2, &tmp_addr))
116 continue;
117
118 info.servers.push_back(std::string(buff2));
119
120 cNameserversFound++;
121 }
122 if ( !strncmp(buff, "domain", 6)
123 || !strncmp(buff, "search", 6))
124 {
125 char *tok;
126 char *saveptr;
127
128 tok = strtok_r(&buff[6], " \t\n", &saveptr);
129
130 if (tok != NULL)
131 info.domain = std::string(tok);
132 }
133 }
134
135 RTFileClose(resolvConfFile);
136
137 setInfo(info);
138
139 return S_OK;
140}
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