VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/libalias/alias_dns.c@ 55363

Last change on this file since 55363 was 55363, checked in by vboxsync, 10 years ago

NAT: G/c old debug printf in host resolver that spams release log.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/* $Id: alias_dns.c 55363 2015-04-22 01:31:50Z vboxsync $ */
2/** @file
3 * libalias helper for using the host resolver instead of dnsproxy.
4 */
5
6/*
7 * Copyright (C) 2009-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. 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#ifndef RT_OS_WINDOWS
19# include <netdb.h>
20#endif
21#include <iprt/ctype.h>
22#include <iprt/assert.h>
23#include <slirp.h>
24#include "alias.h"
25#include "alias_local.h"
26#include "alias_mod.h"
27#define isdigit(ch) RT_C_IS_DIGIT(ch)
28#define isalpha(ch) RT_C_IS_ALPHA(ch)
29
30#define DNS_CONTROL_PORT_NUMBER 53
31/* see RFC 1035(4.1.1) */
32union dnsmsg_header
33{
34 struct
35 {
36 unsigned id:16;
37 unsigned rd:1;
38 unsigned tc:1;
39 unsigned aa:1;
40 unsigned opcode:4;
41 unsigned qr:1;
42 unsigned rcode:4;
43 unsigned Z:3;
44 unsigned ra:1;
45 uint16_t qdcount;
46 uint16_t ancount;
47 uint16_t nscount;
48 uint16_t arcount;
49 } X;
50 uint16_t raw[6];
51};
52AssertCompileSize(union dnsmsg_header, 12);
53
54struct dns_meta_data
55{
56 uint16_t type;
57 uint16_t class;
58};
59
60struct dnsmsg_answer
61{
62 uint16_t name;
63 struct dns_meta_data meta;
64 uint16_t ttl[2];
65 uint16_t rdata_len;
66 uint8_t rdata[1]; /* depends on value at rdata_len */
67};
68
69/* see RFC 1035(4.1) */
70static int dns_alias_handler(PNATState pData, int type);
71static void CStr2QStr(const char *pcszStr, char *pszQStr, size_t cQStr);
72static void QStr2CStr(const char *pcszQStr, char *pszStr, size_t cStr);
73#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
74static void alterHostentWithDataFromDNSMap(PNATState pData, struct hostent *pHostent);
75#endif
76
77static int
78fingerprint(struct libalias *la, struct ip *pIp, struct alias_data *ah)
79{
80
81 NOREF(la);
82 NOREF(pIp);
83 if (!ah->dport || !ah->sport || !ah->lnk)
84 return -1;
85
86 Log(("NAT:%s: ah(dport: %hd, sport: %hd) oaddr:%RTnaipv4 aaddr:%RTnaipv4\n",
87 __FUNCTION__, ntohs(*ah->dport), ntohs(*ah->sport),
88 ah->oaddr, ah->aaddr));
89
90 if ( (ntohs(*ah->dport) == DNS_CONTROL_PORT_NUMBER
91 || ntohs(*ah->sport) == DNS_CONTROL_PORT_NUMBER)
92 && (ah->oaddr->s_addr == htonl(ntohl(la->pData->special_addr.s_addr)|CTL_DNS)))
93 return 0;
94
95 return -1;
96}
97
98static void doanswer(union dnsmsg_header *pHdr, struct dns_meta_data *pReqMeta, char *pszQname, struct ip *pIp, struct hostent *pHostent)
99{
100 int i;
101
102 if (!pHostent)
103 {
104 pHdr->X.qr = 1; /* response */
105 pHdr->X.aa = 1;
106 pHdr->X.rd = 1;
107 pHdr->X.rcode = 3;
108 }
109 else
110 {
111 char *query;
112 char *answers;
113 uint16_t off;
114 char **cstr;
115 char *c;
116 uint16_t packet_len = 0;
117 uint16_t addr_off = (uint16_t)~0;
118 struct dns_meta_data *meta;
119
120#if 0
121 /* here is no compressed names+answers + new query */
122 m_inc(m, pHostent->h_length * sizeof(struct dnsmsg_answer) + strlen(pszQname) + 2 * sizeof(uint16_t));
123#endif
124 packet_len = (pIp->ip_hl << 2)
125 + sizeof(struct udphdr)
126 + sizeof(union dnsmsg_header)
127 + strlen(pszQname)
128 + sizeof(struct dns_meta_data); /* ip + udp + header + query */
129 query = (char *)&pHdr[1];
130
131 strcpy(query, pszQname);
132 query += strlen(pszQname) + 1;
133 /* class & type informations lay right after symbolic inforamtion. */
134 meta = (struct dns_meta_data *)query;
135 meta->type = pReqMeta->type;
136 meta->class = pReqMeta->class;
137
138 /* answers zone lays after query in response packet */
139 answers = (char *)&meta[1];
140
141 off = (char *)&pHdr[1] - (char *)pHdr;
142 off |= (0x3 << 14);
143
144 /* add aliases */
145 for (cstr = pHostent->h_aliases; cstr && *cstr; cstr++)
146 {
147 uint16_t len;
148 struct dnsmsg_answer *ans = (struct dnsmsg_answer *)answers;
149 ans->name = htons(off);
150 ans->meta.type = htons(5); /* CNAME */
151 ans->meta.class = htons(1);
152 *(uint32_t *)ans->ttl = htonl(3600); /* 1h */
153 c = (addr_off == (uint16_t)~0 ? pHostent->h_name : *cstr);
154 len = strlen(c) + 2;
155 ans->rdata_len = htons(len);
156 ans->rdata[len - 1] = 0;
157 CStr2QStr(c, (char *)ans->rdata, len);
158 off = (char *)&ans->rdata - (char *)pHdr;
159 off |= (0x3 << 14);
160 if (addr_off == (uint16_t)~0)
161 addr_off = off;
162 answers = (char *)&ans[1] + len - 2; /* note: 1 symbol already counted */
163 packet_len += sizeof(struct dnsmsg_answer) + len - 2;
164 pHdr->X.ancount++;
165 }
166 /* add addresses */
167
168 for(i = 0; i < pHostent->h_length && pHostent->h_addr_list[i] != NULL; ++i)
169 {
170 struct dnsmsg_answer *ans = (struct dnsmsg_answer *)answers;
171
172 ans->name = htons(off);
173 ans->meta.type = htons(1);
174 ans->meta.class = htons(1);
175 *(uint32_t *)ans->ttl = htonl(3600); /* 1h */
176 ans->rdata_len = htons(4); /* IPv4 */
177 *(uint32_t *)ans->rdata = *(uint32_t *)pHostent->h_addr_list[i];
178 answers = (char *)&ans[1] + 2;
179 packet_len += sizeof(struct dnsmsg_answer) + 3;
180 pHdr->X.ancount++;
181 }
182 pHdr->X.qr = 1; /* response */
183 pHdr->X.aa = 1;
184 pHdr->X.rd = 1;
185 pHdr->X.ra = 1;
186 pHdr->X.rcode = 0;
187 HTONS(pHdr->X.ancount);
188 /* don't forget update m_len */
189 pIp->ip_len = htons(packet_len);
190 }
191}
192
193static int
194protohandler(struct libalias *la, struct ip *pIp, struct alias_data *ah)
195{
196 int i;
197 /* Parse dns request */
198 char *qw_qname = NULL;
199 struct hostent *pHostent = NULL;
200 char pszCname[255];
201 int cname_len = 0;
202 struct dns_meta_data *meta;
203
204 struct udphdr *udp = NULL;
205 union dnsmsg_header *pHdr = NULL;
206 NOREF(la);
207 NOREF(ah);
208 udp = (struct udphdr *)ip_next(pIp);
209 pHdr = (union dnsmsg_header *)udp_next(udp);
210
211 if (pHdr->X.qr == 1)
212 return 0; /* this is respose */
213
214 memset(pszCname, 0, sizeof(pszCname));
215 qw_qname = (char *)&pHdr[1];
216 Assert((ntohs(pHdr->X.qdcount) == 1));
217 if ((ntohs(pHdr->X.qdcount) != 1))
218 {
219 static bool fMultiWarn;
220 if (!fMultiWarn)
221 {
222 LogRel(("NAT:alias_dns: multiple quieries isn't supported\n"));
223 fMultiWarn = true;
224 }
225 return 1;
226 }
227
228 for (i = 0; i < ntohs(pHdr->X.qdcount); ++i)
229 {
230 meta = (struct dns_meta_data *)(qw_qname + strlen(qw_qname) + 1);
231 Log(("pszQname:%s qtype:%hd qclass:%hd\n",
232 qw_qname, ntohs(meta->type), ntohs(meta->class)));
233
234 QStr2CStr(qw_qname, pszCname, sizeof(pszCname));
235 cname_len = RTStrNLen(pszCname, sizeof(pszCname));
236 /* Some guests like win-xp adds _dot_ after host name
237 * and after domain name (not passed with host resolver)
238 * that confuses host resolver.
239 */
240 if ( cname_len > 2
241 && pszCname[cname_len - 1] == '.'
242 && pszCname[cname_len - 2] == '.')
243 {
244 pszCname[cname_len - 1] = 0;
245 pszCname[cname_len - 2] = 0;
246 }
247 pHostent = gethostbyname(pszCname);
248#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
249 if ( pHostent
250 && !LIST_EMPTY(&la->pData->DNSMapHead))
251 alterHostentWithDataFromDNSMap(la->pData, pHostent);
252#endif
253 doanswer(pHdr, meta, qw_qname, pIp, pHostent);
254 }
255
256 /*
257 * We have changed the size and the content of udp, to avoid double csum calculation
258 * will assign to zero
259 */
260 udp->uh_sum = 0;
261 udp->uh_ulen = ntohs(htons(pIp->ip_len) - (pIp->ip_hl << 2));
262 pIp->ip_sum = 0;
263 pIp->ip_sum = LibAliasInternetChecksum(la, (uint16_t *)pIp, pIp->ip_hl << 2);
264 return 0;
265}
266
267/*
268 * qstr is z-string with -dot- replaced with \count to next -dot-
269 * e.g. ya.ru is \02ya\02ru
270 * Note: it's assumed that caller allocates buffer for cstr
271 */
272static void QStr2CStr(const char *pcszQStr, char *pszStr, size_t cStr)
273{
274 const char *q;
275 char *c;
276 size_t cLen = 0;
277
278 Assert(cStr > 0);
279 for (q = pcszQStr, c = pszStr; *q != '\0' && cLen < cStr-1; q++, cLen++)
280 {
281 if ( isalpha(*q)
282 || isdigit(*q)
283 || *q == '-'
284 || *q == '_')
285 {
286 *c = *q;
287 c++;
288 }
289 else if (c != &pszStr[0])
290 {
291 *c = '.';
292 c++;
293 }
294 }
295 *c = '\0';
296}
297
298/*
299 *
300 */
301static void CStr2QStr(const char *pcszStr, char *pszQStr, size_t cQStr)
302{
303 const char *c;
304 const char *pc;
305 char *q;
306 size_t cLen = 0;
307
308 Assert(cQStr > 0);
309 for (c = pcszStr, q = pszQStr; *c != '\0' && cLen < cQStr-1; q++, cLen++)
310 {
311 /* at the begining or at -dot- position */
312 if (*c == '.' || (c == pcszStr && q == pszQStr))
313 {
314 if (c != pcszStr)
315 c++;
316 pc = strchr(c, '.');
317 *q = pc ? (pc - c) : strlen(c);
318 }
319 else
320 {
321 *q = *c;
322 c++;
323 }
324 }
325 *q = '\0';
326}
327
328
329int
330dns_alias_load(PNATState pData)
331{
332 return dns_alias_handler(pData, MOD_LOAD);
333}
334
335int
336dns_alias_unload(PNATState pData)
337{
338 return dns_alias_handler(pData, MOD_UNLOAD);
339}
340
341#define handlers pData->dns_module
342static int
343dns_alias_handler(PNATState pData, int type)
344{
345 int error;
346
347 if (!handlers)
348 handlers = RTMemAllocZ(2 * sizeof(struct proto_handler));
349
350 handlers[0].pri = 20;
351 handlers[0].dir = IN;
352 handlers[0].proto = UDP;
353 handlers[0].fingerprint = &fingerprint;
354 handlers[0].protohandler = &protohandler;
355 handlers[1].pri = EOH;
356
357 switch (type)
358 {
359 case MOD_LOAD:
360 error = 0;
361 LibAliasAttachHandlers(pData, handlers);
362 break;
363
364 case MOD_UNLOAD:
365 error = 0;
366 LibAliasDetachHandlers(pData, handlers);
367 RTMemFree(handlers);
368 handlers = NULL;
369 break;
370
371 default:
372 error = EINVAL;
373 }
374 return error;
375}
376
377#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
378static bool isDnsMappingEntryMatchOrEqual2Str(const PDNSMAPPINGENTRY pDNSMapingEntry, const char *pcszString)
379{
380 return ( ( pDNSMapingEntry->pszCName
381 && !strcmp(pDNSMapingEntry->pszCName, pcszString))
382 || ( pDNSMapingEntry->pszPattern
383 && RTStrSimplePatternMultiMatch(pDNSMapingEntry->pszPattern, RTSTR_MAX, pcszString, RTSTR_MAX, NULL)));
384}
385
386static void alterHostentWithDataFromDNSMap(PNATState pData, struct hostent *pHostent)
387{
388 PDNSMAPPINGENTRY pDNSMapingEntry = NULL;
389 bool fMatch = false;
390 LIST_FOREACH(pDNSMapingEntry, &pData->DNSMapHead, MapList)
391 {
392 char **pszAlias = NULL;
393 if (isDnsMappingEntryMatchOrEqual2Str(pDNSMapingEntry, pHostent->h_name))
394 {
395 fMatch = true;
396 break;
397 }
398
399 for (pszAlias = pHostent->h_aliases; *pszAlias && !fMatch; pszAlias++)
400 {
401 if (isDnsMappingEntryMatchOrEqual2Str(pDNSMapingEntry, *pszAlias))
402 {
403
404 PDNSMAPPINGENTRY pDnsMapping = RTMemAllocZ(sizeof(DNSMAPPINGENTRY));
405 fMatch = true;
406 if (!pDnsMapping)
407 {
408 LogFunc(("Can't allocate DNSMAPPINGENTRY\n"));
409 LogFlowFuncLeave();
410 return;
411 }
412 pDnsMapping->u32IpAddress = pDNSMapingEntry->u32IpAddress;
413 pDnsMapping->pszCName = RTStrDup(pHostent->h_name);
414 if (!pDnsMapping->pszCName)
415 {
416 LogFunc(("Can't allocate enough room for %s\n", pHostent->h_name));
417 RTMemFree(pDnsMapping);
418 LogFlowFuncLeave();
419 return;
420 }
421 LIST_INSERT_HEAD(&pData->DNSMapHead, pDnsMapping, MapList);
422 LogRel(("NAT: user-defined mapping %s: %RTnaipv4 is registered\n",
423 pDnsMapping->pszCName ? pDnsMapping->pszCName : pDnsMapping->pszPattern,
424 pDnsMapping->u32IpAddress));
425 }
426 }
427 if (fMatch)
428 break;
429 }
430
431 /* h_lenght is lenght of h_addr_list in bytes, so we check that we have enough space for IPv4 address */
432 if ( fMatch
433 && pHostent->h_length >= sizeof(uint32_t)
434 && pDNSMapingEntry)
435 {
436 pHostent->h_length = 1;
437 *(uint32_t *)pHostent->h_addr_list[0] = pDNSMapingEntry->u32IpAddress;
438 }
439
440}
441#endif
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