VirtualBox

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

Last change on this file since 45322 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

  • 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 44529 2013-02-04 15:54:15Z 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 fprintf(stderr, "pszCname:%s\n", pszCname);
254 doanswer(pHdr, meta, qw_qname, pIp, pHostent);
255 }
256
257 /*
258 * We have changed the size and the content of udp, to avoid double csum calculation
259 * will assign to zero
260 */
261 udp->uh_sum = 0;
262 udp->uh_ulen = ntohs(htons(pIp->ip_len) - (pIp->ip_hl << 2));
263 pIp->ip_sum = 0;
264 pIp->ip_sum = LibAliasInternetChecksum(la, (uint16_t *)pIp, pIp->ip_hl << 2);
265 return 0;
266}
267
268/*
269 * qstr is z-string with -dot- replaced with \count to next -dot-
270 * e.g. ya.ru is \02ya\02ru
271 * Note: it's assumed that caller allocates buffer for cstr
272 */
273static void QStr2CStr(const char *pcszQStr, char *pszStr, size_t cStr)
274{
275 const char *q;
276 char *c;
277 size_t cLen = 0;
278
279 Assert(cStr > 0);
280 for (q = pcszQStr, c = pszStr; *q != '\0' && cLen < cStr-1; q++, cLen++)
281 {
282 if ( isalpha(*q)
283 || isdigit(*q)
284 || *q == '-'
285 || *q == '_')
286 {
287 *c = *q;
288 c++;
289 }
290 else if (c != &pszStr[0])
291 {
292 *c = '.';
293 c++;
294 }
295 }
296 *c = '\0';
297}
298
299/*
300 *
301 */
302static void CStr2QStr(const char *pcszStr, char *pszQStr, size_t cQStr)
303{
304 const char *c;
305 const char *pc;
306 char *q;
307 size_t cLen = 0;
308
309 Assert(cQStr > 0);
310 for (c = pcszStr, q = pszQStr; *c != '\0' && cLen < cQStr-1; q++, cLen++)
311 {
312 /* at the begining or at -dot- position */
313 if (*c == '.' || (c == pcszStr && q == pszQStr))
314 {
315 if (c != pcszStr)
316 c++;
317 pc = strchr(c, '.');
318 *q = pc ? (pc - c) : strlen(c);
319 }
320 else
321 {
322 *q = *c;
323 c++;
324 }
325 }
326 *q = '\0';
327}
328
329
330int
331dns_alias_load(PNATState pData)
332{
333 return dns_alias_handler(pData, MOD_LOAD);
334}
335
336int
337dns_alias_unload(PNATState pData)
338{
339 return dns_alias_handler(pData, MOD_UNLOAD);
340}
341
342#define handlers pData->dns_module
343static int
344dns_alias_handler(PNATState pData, int type)
345{
346 int error;
347
348 if (!handlers)
349 handlers = RTMemAllocZ(2 * sizeof(struct proto_handler));
350
351 handlers[0].pri = 20;
352 handlers[0].dir = IN;
353 handlers[0].proto = UDP;
354 handlers[0].fingerprint = &fingerprint;
355 handlers[0].protohandler = &protohandler;
356 handlers[1].pri = EOH;
357
358 switch (type)
359 {
360 case MOD_LOAD:
361 error = 0;
362 LibAliasAttachHandlers(pData, handlers);
363 break;
364
365 case MOD_UNLOAD:
366 error = 0;
367 LibAliasDetachHandlers(pData, handlers);
368 RTMemFree(handlers);
369 handlers = NULL;
370 break;
371
372 default:
373 error = EINVAL;
374 }
375 return error;
376}
377
378#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
379static bool isDnsMappingEntryMatchOrEqual2Str(const PDNSMAPPINGENTRY pDNSMapingEntry, const char *pcszString)
380{
381 return ( ( pDNSMapingEntry->pszCName
382 && !strcmp(pDNSMapingEntry->pszCName, pcszString))
383 || ( pDNSMapingEntry->pszPattern
384 && RTStrSimplePatternMultiMatch(pDNSMapingEntry->pszPattern, RTSTR_MAX, pcszString, RTSTR_MAX, NULL)));
385}
386
387static void alterHostentWithDataFromDNSMap(PNATState pData, struct hostent *pHostent)
388{
389 PDNSMAPPINGENTRY pDNSMapingEntry = NULL;
390 bool fMatch = false;
391 LIST_FOREACH(pDNSMapingEntry, &pData->DNSMapHead, MapList)
392 {
393 char **pszAlias = NULL;
394 if (isDnsMappingEntryMatchOrEqual2Str(pDNSMapingEntry, pHostent->h_name))
395 {
396 fMatch = true;
397 break;
398 }
399
400 for (pszAlias = pHostent->h_aliases; *pszAlias && !fMatch; pszAlias++)
401 {
402 if (isDnsMappingEntryMatchOrEqual2Str(pDNSMapingEntry, *pszAlias))
403 {
404
405 PDNSMAPPINGENTRY pDnsMapping = RTMemAllocZ(sizeof(DNSMAPPINGENTRY));
406 fMatch = true;
407 if (!pDnsMapping)
408 {
409 LogFunc(("Can't allocate DNSMAPPINGENTRY\n"));
410 LogFlowFuncLeave();
411 return;
412 }
413 pDnsMapping->u32IpAddress = pDNSMapingEntry->u32IpAddress;
414 pDnsMapping->pszCName = RTStrDup(pHostent->h_name);
415 if (!pDnsMapping->pszCName)
416 {
417 LogFunc(("Can't allocate enough room for %s\n", pHostent->h_name));
418 RTMemFree(pDnsMapping);
419 LogFlowFuncLeave();
420 return;
421 }
422 LIST_INSERT_HEAD(&pData->DNSMapHead, pDnsMapping, MapList);
423 LogRel(("NAT: user-defined mapping %s: %RTnaipv4 is registered\n",
424 pDnsMapping->pszCName ? pDnsMapping->pszCName : pDnsMapping->pszPattern,
425 pDnsMapping->u32IpAddress));
426 }
427 }
428 if (fMatch)
429 break;
430 }
431
432 /* h_lenght is lenght of h_addr_list in bytes, so we check that we have enough space for IPv4 address */
433 if ( fMatch
434 && pHostent->h_length >= sizeof(uint32_t)
435 && pDNSMapingEntry)
436 {
437 pHostent->h_length = 1;
438 *(uint32_t *)pHostent->h_addr_list[0] = pDNSMapingEntry->u32IpAddress;
439 }
440
441}
442#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