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