VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/slirp_dns.c@ 71202

Last change on this file since 71202 was 71202, checked in by vboxsync, 7 years ago

NAT: nameserver 0.0.0.0 is a valid setting. Map it to host's loopback.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: slirp_dns.c 71202 2018-03-05 14:45:44Z vboxsync $ */
2/** @file
3 * NAT - dns initialization.
4 */
5
6/*
7 * Copyright (C) 2012-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "slirp.h"
19#ifdef RT_OS_OS2
20# include <paths.h>
21#endif
22
23#include <VBox/err.h>
24#include <VBox/vmm/pdmdrv.h>
25#include <iprt/assert.h>
26#include <iprt/file.h>
27
28#ifdef RT_OS_WINDOWS
29# include <Winnls.h>
30# define _WINSOCK2API_
31# include <iprt/win/iphlpapi.h>
32
33static int get_dns_addr_domain(PNATState pData)
34{
35 /*ULONG flags = GAA_FLAG_INCLUDE_PREFIX;*/ /*GAA_FLAG_INCLUDE_ALL_INTERFACES;*/ /* all interfaces registered in NDIS */
36 PIP_ADAPTER_ADDRESSES pAdapterAddr = NULL;
37 PIP_ADAPTER_ADDRESSES pAddr = NULL;
38 PIP_ADAPTER_DNS_SERVER_ADDRESS pDnsAddr = NULL;
39 ULONG size;
40 char *pszSuffix;
41 struct dns_domain_entry *pDomain = NULL;
42 ULONG ret = ERROR_SUCCESS;
43
44 /** @todo add SKIPing flags to get only required information */
45
46 /* determine size of buffer */
47 size = 0;
48 ret = pData->pfnGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, pAdapterAddr, &size);
49 if (ret != ERROR_BUFFER_OVERFLOW)
50 {
51 Log(("NAT: error %lu occurred on capacity detection operation\n", ret));
52 return -1;
53 }
54 if (size == 0)
55 {
56 Log(("NAT: Win socket API returns non capacity\n"));
57 return -1;
58 }
59
60 pAdapterAddr = RTMemAllocZ(size);
61 if (!pAdapterAddr)
62 {
63 Log(("NAT: No memory available\n"));
64 return -1;
65 }
66 ret = pData->pfnGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, pAdapterAddr, &size);
67 if (ret != ERROR_SUCCESS)
68 {
69 Log(("NAT: error %lu occurred on fetching adapters info\n", ret));
70 RTMemFree(pAdapterAddr);
71 return -1;
72 }
73
74 for (pAddr = pAdapterAddr; pAddr != NULL; pAddr = pAddr->Next)
75 {
76 int found;
77 if (pAddr->OperStatus != IfOperStatusUp)
78 continue;
79
80 for (pDnsAddr = pAddr->FirstDnsServerAddress; pDnsAddr != NULL; pDnsAddr = pDnsAddr->Next)
81 {
82 struct sockaddr *SockAddr = pDnsAddr->Address.lpSockaddr;
83 struct in_addr InAddr;
84 struct dns_entry *pDns;
85
86 if (SockAddr->sa_family != AF_INET)
87 continue;
88
89 InAddr = ((struct sockaddr_in *)SockAddr)->sin_addr;
90
91 /* add dns server to list */
92 pDns = RTMemAllocZ(sizeof(struct dns_entry));
93 if (!pDns)
94 {
95 Log(("NAT: Can't allocate buffer for DNS entry\n"));
96 RTMemFree(pAdapterAddr);
97 return VERR_NO_MEMORY;
98 }
99
100 Log(("NAT: adding %RTnaipv4 to DNS server list\n", InAddr));
101 if ((InAddr.s_addr & RT_H2N_U32_C(IN_CLASSA_NET)) == RT_N2H_U32_C(INADDR_LOOPBACK & IN_CLASSA_NET))
102 pDns->de_addr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
103 else
104 pDns->de_addr.s_addr = InAddr.s_addr;
105
106 TAILQ_INSERT_HEAD(&pData->pDnsList, pDns, de_list);
107
108 if (pAddr->DnsSuffix == NULL)
109 continue;
110
111 /* uniq */
112 RTUtf16ToUtf8(pAddr->DnsSuffix, &pszSuffix);
113 if (!pszSuffix || strlen(pszSuffix) == 0)
114 {
115 RTStrFree(pszSuffix);
116 continue;
117 }
118
119 found = 0;
120 LIST_FOREACH(pDomain, &pData->pDomainList, dd_list)
121 {
122 if ( pDomain->dd_pszDomain != NULL
123 && strcmp(pDomain->dd_pszDomain, pszSuffix) == 0)
124 {
125 found = 1;
126 RTStrFree(pszSuffix);
127 break;
128 }
129 }
130 if (!found)
131 {
132 pDomain = RTMemAllocZ(sizeof(struct dns_domain_entry));
133 if (!pDomain)
134 {
135 Log(("NAT: not enough memory\n"));
136 RTStrFree(pszSuffix);
137 RTMemFree(pAdapterAddr);
138 return VERR_NO_MEMORY;
139 }
140 pDomain->dd_pszDomain = pszSuffix;
141 Log(("NAT: adding domain name %s to search list\n", pDomain->dd_pszDomain));
142 LIST_INSERT_HEAD(&pData->pDomainList, pDomain, dd_list);
143 }
144 }
145 }
146 RTMemFree(pAdapterAddr);
147 return 0;
148}
149
150#else /* !RT_OS_WINDOWS */
151
152#include "resolv_conf_parser.h"
153
154static int get_dns_addr_domain(PNATState pData)
155{
156 struct rcp_state st;
157 int rc;
158 unsigned i;
159
160 /* XXX: perhaps IPv6 shouldn't be ignored if we're using DNS proxy */
161 st.rcps_flags = RCPSF_IGNORE_IPV6;
162 rc = rcp_parse(&st, RESOLV_CONF_FILE);
163
164 if (rc < 0)
165 return -1;
166
167 /* for historical reasons: Slirp returns -1 if no nameservers were found */
168 if (st.rcps_num_nameserver == 0)
169 return -1;
170
171
172 /* XXX: We're composing the list, but we already knows
173 * its size so we can allocate array instead (Linux guests
174 * dont like >3 servers in the list anyway)
175 * or use pre-allocated array in NATState.
176 */
177 for (i = 0; i != st.rcps_num_nameserver; ++i)
178 {
179 struct dns_entry *pDns;
180 RTNETADDRU *address = &st.rcps_nameserver[i].uAddr;
181 if ( (address->IPv4.u & RT_H2N_U32_C(IN_CLASSA_NET))
182 == RT_N2H_U32_C(INADDR_LOOPBACK & IN_CLASSA_NET))
183 {
184 /**
185 * XXX: Note shouldn't patch the address in case of using DNS proxy,
186 * because DNS proxy we do revert it back actually.
187 */
188 if (address->IPv4.u == RT_N2H_U32_C(INADDR_LOOPBACK))
189 address->IPv4.u = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
190 else if (pData->fUseDnsProxy == 0) {
191 /* We detects that using some address in 127/8 network */
192 LogRel(("NAT: DNS server %RTnaipv4 registration detected, switching to the DNS proxy\n", address->IPv4));
193 pData->fUseDnsProxy = 1;
194 }
195 }
196 else if (address->IPv4.u == INADDR_ANY)
197 {
198 /*
199 * This doesn't seem to be very well documented except for
200 * RTFS of res_init.c, but INADDR_ANY is a valid value for
201 * for "nameserver".
202 */
203 address->IPv4.u = pData->special_addr.s_addr | RT_H2N_U32_C(CTL_ALIAS);
204 }
205
206 pDns = RTMemAllocZ(sizeof(struct dns_entry));
207 if (pDns == NULL)
208 {
209 slirpReleaseDnsSettings(pData);
210 return VERR_NO_MEMORY;
211 }
212
213 pDns->de_addr.s_addr = address->IPv4.u;
214 TAILQ_INSERT_HEAD(&pData->pDnsList, pDns, de_list);
215 }
216
217 if (st.rcps_domain != 0)
218 {
219 struct dns_domain_entry *pDomain = RTMemAllocZ(sizeof(struct dns_domain_entry));
220 if (pDomain == NULL)
221 {
222 slirpReleaseDnsSettings(pData);
223 return -1;
224 }
225
226 pDomain->dd_pszDomain = RTStrDup(st.rcps_domain);
227 LogRel(("NAT: Adding domain name %s\n", pDomain->dd_pszDomain));
228 LIST_INSERT_HEAD(&pData->pDomainList, pDomain, dd_list);
229 }
230
231 return 0;
232}
233
234#endif /* !RT_OS_WINDOWS */
235
236int slirpInitializeDnsSettings(PNATState pData)
237{
238 int rc = VINF_SUCCESS;
239 AssertPtrReturn(pData, VERR_INVALID_PARAMETER);
240 LogFlowFuncEnter();
241 if (!pData->fUseHostResolverPermanent)
242 {
243 TAILQ_INIT(&pData->pDnsList);
244 LIST_INIT(&pData->pDomainList);
245
246 /*
247 * Some distributions haven't got /etc/resolv.conf
248 * so we should other way to configure DNS settings.
249 */
250 if (get_dns_addr_domain(pData) < 0)
251 pData->fUseHostResolver = true;
252 else
253 {
254 pData->fUseHostResolver = false;
255 dnsproxy_init(pData);
256 }
257
258 if (!pData->fUseHostResolver)
259 {
260 struct dns_entry *pDNSEntry = NULL;
261 int cDNSListEntry = 0;
262 TAILQ_FOREACH_REVERSE(pDNSEntry, &pData->pDnsList, dns_list_head, de_list)
263 {
264 LogRel(("NAT: DNS#%i: %RTnaipv4\n", cDNSListEntry, pDNSEntry->de_addr.s_addr));
265 cDNSListEntry++;
266 }
267 }
268 }
269
270 LogFlowFuncLeaveRC(rc);
271 return rc;
272}
273
274int slirpReleaseDnsSettings(PNATState pData)
275{
276 struct dns_entry *pDns = NULL;
277 struct dns_domain_entry *pDomain = NULL;
278 int rc = VINF_SUCCESS;
279 AssertPtrReturn(pData, VERR_INVALID_PARAMETER);
280 LogFlowFuncEnter();
281
282 while (!TAILQ_EMPTY(&pData->pDnsList))
283 {
284 pDns = TAILQ_FIRST(&pData->pDnsList);
285 TAILQ_REMOVE(&pData->pDnsList, pDns, de_list);
286 RTMemFree(pDns);
287 }
288
289 while (!LIST_EMPTY(&pData->pDomainList))
290 {
291 pDomain = LIST_FIRST(&pData->pDomainList);
292 LIST_REMOVE(pDomain, dd_list);
293 if (pDomain->dd_pszDomain != NULL)
294 RTStrFree(pDomain->dd_pszDomain);
295 RTMemFree(pDomain);
296 }
297
298 /* tell any pending dnsproxy requests their copy is expired */
299 ++pData->dnsgen;
300
301 LogFlowFuncLeaveRC(rc);
302 return rc;
303}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette