VirtualBox

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

最後變更 在這個檔案從46758是 46758,由 vboxsync 提交於 12 年 前

Devices/Input/UsbMouse: add basic unit test harness.

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

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