]>
Commit | Line | Data |
---|---|---|
0710cbcd | 1 | |
2 | /* | |
262a0e14 | 3 | * $Id$ |
0710cbcd | 4 | * |
5 | * Low level DNS protocol routines | |
6 | * AUTHOR: Amos Jeffries, Rafael Martinez Torres | |
7 | * | |
8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
9 | * ---------------------------------------------------------- | |
10 | * | |
11 | * Squid is the result of efforts by numerous individuals from | |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
19 | * | |
20 | * This code is copyright (C) 2007 by Treehouse Networks Ltd of | |
21 | * New Zealand. It is published and Lisenced as an extension of | |
22 | * squid under the same conditions as the main squid application. | |
23 | * | |
24 | * This program is free software; you can redistribute it and/or modify | |
25 | * it under the terms of the GNU General Public License as published by | |
26 | * the Free Software Foundation; either version 2 of the License, or | |
27 | * (at your option) any later version. | |
26ac0430 | 28 | * |
0710cbcd | 29 | * This program is distributed in the hope that it will be useful, |
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
32 | * GNU General Public License for more details. | |
26ac0430 | 33 | * |
0710cbcd | 34 | * You should have received a copy of the GNU General Public License |
35 | * along with this program; if not, write to the Free Software | |
36 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
37 | * | |
38 | */ | |
39 | ||
40 | /* | |
41 | * KNOWN BUGS: | |
26ac0430 | 42 | * |
0710cbcd | 43 | * UDP replies with TC set should be retried via TCP |
44 | */ | |
45 | ||
46 | /** | |
47 | * April 2007 | |
26ac0430 | 48 | * |
0710cbcd | 49 | * Provides RFC3596 functions to handle purely IPv6 DNS. |
50 | * Adds AAAA and IPv6 PTR records. | |
51 | * Other IPv6 records are not mentioned by this RFC. | |
26ac0430 | 52 | * |
0710cbcd | 53 | * IPv4 equivalents are taken care of by the RFC1035 library. |
54 | * Where one protocol lookup must be followed by another, the caller | |
55 | * is resposible for the order and handling of the lookups. | |
56 | * | |
57 | */ | |
58 | ||
59 | #include "config.h" | |
60 | #include "util.h" | |
61 | ||
62 | #if HAVE_STDIO_H | |
63 | #include <stdio.h> | |
64 | #endif | |
65 | #if HAVE_UNISTD_H | |
66 | #include <unistd.h> | |
67 | #endif | |
68 | #if HAVE_STDLIB_H | |
69 | #include <stdlib.h> | |
70 | #endif | |
71 | #if HAVE_MEMORY_H | |
72 | #include <memory.h> | |
73 | #endif | |
74 | #if HAVE_SYS_TYPES_H | |
75 | #include <sys/types.h> | |
76 | #endif | |
77 | #if HAVE_ASSERT_H | |
78 | #include <assert.h> | |
79 | #endif | |
80 | #if HAVE_NETINET_IN_H | |
81 | #include <netinet/in.h> | |
82 | #endif | |
83 | #if HAVE_STRINGS_H | |
84 | #include <strings.h> | |
85 | #endif | |
86 | ||
87 | #include "rfc3596.h" | |
88 | ||
89 | #ifndef SQUID_RFC1035_H | |
90 | #error RFC3596 Library depends on RFC1035 | |
91 | #endif | |
92 | ||
93 | /** | |
94 | * Builds a message buffer with a QUESTION to lookup records | |
95 | * for a hostname. Caller must allocate 'buf' which should | |
96 | * probably be at least 512 octets. The 'szp' initially | |
97 | * specifies the size of the buffer, on return it contains | |
98 | * the size of the message (i.e. how much to write). | |
99 | * Returns the size of the query | |
100 | */ | |
101 | ssize_t | |
102 | rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, int qtype) | |
103 | { | |
104 | static rfc1035_message h; | |
105 | size_t offset = 0; | |
106 | memset(&h, '\0', sizeof(h)); | |
107 | h.id = qid; | |
108 | h.qr = 0; | |
109 | h.rd = 1; | |
110 | h.opcode = 0; /* QUERY */ | |
111 | h.qdcount = (unsigned int) 1; | |
112 | offset += rfc1035HeaderPack(buf + offset, sz - offset, &h); | |
113 | offset += rfc1035QuestionPack(buf + offset, | |
114 | sz - offset, | |
115 | hostname, | |
116 | qtype, | |
117 | RFC1035_CLASS_IN); | |
118 | ||
119 | if (query) { | |
120 | query->qtype = qtype; | |
121 | query->qclass = RFC1035_CLASS_IN; | |
122 | xstrncpy(query->name, hostname, sizeof(query->name)); | |
123 | } | |
124 | ||
125 | assert(offset <= sz); | |
126 | return offset; | |
127 | } | |
128 | ||
129 | /** | |
130 | * Builds a message buffer with a QUESTION to lookup A records | |
131 | * for a hostname. Caller must allocate 'buf' which should | |
132 | * probably be at least 512 octets. The 'szp' initially | |
133 | * specifies the size of the buffer, on return it contains | |
134 | * the size of the message (i.e. how much to write). | |
135 | * \return the size of the query | |
136 | */ | |
137 | ssize_t | |
138 | rfc3596BuildAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query) | |
139 | { | |
140 | return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_A); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Builds a message buffer with a QUESTION to lookup AAAA records | |
145 | * for a hostname. Caller must allocate 'buf' which should | |
146 | * probably be at least 512 octets. The 'szp' initially | |
147 | * specifies the size of the buffer, on return it contains | |
148 | * the size of the message (i.e. how much to write). | |
149 | * \return the size of the query | |
150 | */ | |
151 | ssize_t | |
152 | rfc3596BuildAAAAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query) | |
153 | { | |
154 | return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_AAAA); | |
155 | } | |
156 | ||
157 | ||
158 | /** | |
159 | * Builds a message buffer with a QUESTION to lookup PTR records | |
160 | * for an address. Caller must allocate 'buf' which should | |
161 | * probably be at least 512 octets. The 'szp' initially | |
162 | * specifies the size of the buffer, on return it contains | |
163 | * the size of the message (i.e. how much to write). | |
164 | * \return the size of the query | |
165 | */ | |
166 | ssize_t | |
167 | ||
168 | rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query) | |
169 | { | |
170 | static char rev[RFC1035_MAXHOSTNAMESZ]; | |
171 | unsigned int i; | |
172 | ||
173 | i = (unsigned int) ntohl(addr.s_addr); | |
174 | snprintf(rev, RFC1035_MAXHOSTNAMESZ, "%u.%u.%u.%u.in-addr.arpa.", | |
175 | i & 255, | |
176 | (i >> 8) & 255, | |
177 | (i >> 16) & 255, | |
178 | (i >> 24) & 255); | |
179 | ||
180 | return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR); | |
181 | } | |
182 | ||
183 | ssize_t | |
184 | ||
185 | rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query) | |
186 | { | |
187 | static char rev[RFC1035_MAXHOSTNAMESZ]; | |
188 | const uint8_t* r = addr.s6_addr; | |
189 | char* p = rev; | |
190 | int i; // NP: MUST allow signed for loop termination. | |
191 | ||
192 | /* work from the raw addr field. anything else may have representation changes. */ | |
193 | /* The sin6_port and sin6_addr members shall be in network byte order. */ | |
26ac0430 | 194 | for (i = 15; i >= 0; i--, p+=4) { |
0710cbcd | 195 | snprintf(p, 5, "%1x.%1x.", ((r[i]>>4)&0xf), (r[i])&0xf ); |
196 | } | |
197 | ||
198 | snprintf(p,10,"ip6.arpa."); | |
199 | ||
200 | return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR); | |
201 | } | |
202 | ||
203 | ||
204 | #if DRIVER | |
205 | ||
206 | /* driver needs the rfc1035 code _without_ the main() */ | |
207 | # define main(a,b) rfc1035_main(a,b) | |
208 | # include "rfc1035.c" | |
209 | # undef main(a,b) | |
210 | ||
211 | #include <sys/socket.h> | |
212 | #include <sys/time.h> | |
213 | ||
214 | int | |
215 | main(int argc, char *argv[]) | |
216 | { | |
217 | char input[512]; | |
218 | char buf[512]; | |
219 | char rbuf[512]; | |
220 | size_t sz = 512; | |
221 | unsigned short sid, sidb; | |
222 | int s; | |
223 | int rl; | |
224 | ||
225 | struct sockaddr* S; | |
226 | int var = 1; | |
227 | ||
228 | if ( argc < 3 || argc > 4) { | |
229 | fprintf(stderr, "usage: %s [-6|-4] ip port\n", argv[0]); | |
230 | return 1; | |
231 | } | |
232 | ||
233 | setbuf(stdout, NULL); | |
234 | setbuf(stderr, NULL); | |
235 | ||
26ac0430 AJ |
236 | if (argv[var][0] == '-') { |
237 | if (argv[var][1] == '4') | |
0710cbcd | 238 | prefer = AF_INET; |
26ac0430 | 239 | else if (argv[var][1] == '6') |
0710cbcd | 240 | prefer = AF_INET6; |
241 | else { | |
242 | fprintf(stderr, "usage: %s [-6|-4] ip port\n", argv[0]); | |
243 | return 1; | |
244 | } | |
245 | ||
246 | var++; | |
247 | } | |
248 | ||
249 | s = socket(PF_INET, SOCK_DGRAM, 0); | |
250 | ||
251 | if (s < 0) { | |
252 | perror("socket"); | |
253 | return 1; | |
254 | } | |
255 | ||
256 | ||
257 | memset(&S, '\0', sizeof(S)); | |
258 | ||
26ac0430 | 259 | if (prefer == 6) { |
0710cbcd | 260 | S = (struct sockaddr *) new sockaddr_in6; |
261 | memset(S,0,sizeof(struct sockaddr_in6)); | |
262 | ||
263 | ((struct sockaddr_in6 *)S)->sin6_family = AF_INET6; | |
264 | ((struct sockaddr_in6 *)S)->sin6_port = htons(atoi(argv[var+1])); | |
265 | ||
26ac0430 | 266 | if ( ! xinet_pton(AF_INET6, argv[var], &((struct sockaddr_in6 *)S)->sin6_addr.s_addr) ) |
0710cbcd | 267 | perror("listen address"); |
26ac0430 AJ |
268 | return 1; |
269 | } | |
0710cbcd | 270 | |
26ac0430 AJ |
271 | s = socket(PF_INET6, SOCK_DGRAM, 0); |
272 | } | |
273 | else | |
274 | { | |
275 | S = (struct sockaddr *) new sockaddr_in; | |
276 | memset(S,0,sizeof(struct sockaddr_in)); | |
0710cbcd | 277 | |
26ac0430 AJ |
278 | ((struct sockaddr_in *)S)->sin_family = AF_INET; |
279 | ((struct sockaddr_in *)S)->sin_port = htons(atoi(argv[var+1])); | |
0710cbcd | 280 | |
26ac0430 AJ |
281 | if ( ! xinet_pton(AF_INET, argv[var], &((struct sockaddr_in *)S)->sin_addr.s_addr) ) |
282 | perror("listen address"); | |
283 | return 1; | |
284 | } | |
285 | } | |
0710cbcd | 286 | |
26ac0430 AJ |
287 | while (fgets(input, 512, stdin)) |
288 | { | |
0710cbcd | 289 | |
26ac0430 | 290 | struct in6_addr junk6; |
0710cbcd | 291 | |
26ac0430 AJ |
292 | struct in_addr junk4; |
293 | strtok(input, "\r\n"); | |
294 | memset(buf, '\0', 512); | |
295 | sz = 512; | |
0710cbcd | 296 | |
26ac0430 AJ |
297 | if (xinet_pton(AF_INET6, input, &junk6)) { |
298 | sid = rfc1035BuildPTRQuery6(junk6, buf, &sz); | |
299 | sidb=0; | |
300 | } else if (xinet_pton(AF_INET, input, &junk4)) { | |
301 | sid = rfc1035BuildPTRQuery4(junk4, buf, &sz); | |
302 | sidb=0; | |
303 | } else { | |
304 | sid = rfc1035BuildAAAAQuery(input, buf, &sz); | |
305 | sidb = rfc1035BuildAQuery(input, buf, &sz); | |
306 | } | |
0710cbcd | 307 | |
26ac0430 | 308 | sendto(s, buf, sz, 0, S, sizeof(*S)); |
0710cbcd | 309 | |
26ac0430 AJ |
310 | do { |
311 | fd_set R; | |
0710cbcd | 312 | |
26ac0430 AJ |
313 | struct timeval to; |
314 | FD_ZERO(&R); | |
315 | FD_SET(s, &R); | |
316 | to.tv_sec = 10; | |
317 | to.tv_usec = 0; | |
318 | rl = select(s + 1, &R, NULL, NULL, &to); | |
319 | } while (0); | |
0710cbcd | 320 | |
26ac0430 AJ |
321 | if (rl < 1) { |
322 | printf("TIMEOUT\n"); | |
323 | continue; | |
324 | } | |
0710cbcd | 325 | |
26ac0430 AJ |
326 | memset(rbuf, '\0', 512); |
327 | rl = recv(s, rbuf, 512, 0); | |
328 | { | |
329 | unsigned short rid = 0; | |
330 | int i; | |
331 | int n; | |
332 | rfc1035_rr *answers = NULL; | |
333 | n = rfc1035AnswersUnpack(rbuf, | |
334 | rl, | |
335 | &answers, | |
336 | &rid); | |
337 | ||
338 | if (n < 0) { | |
339 | printf("ERROR %d\n", rfc1035_errno); | |
340 | } else if (rid != sid && rid != sidb) { | |
341 | printf("ERROR, ID mismatch (%#hx, %#hx)\n", sid, rid); | |
342 | printf("ERROR, ID mismatch (%#hx, %#hx)\n", sidb, rid); | |
343 | } else { | |
344 | printf("%d answers\n", n); | |
345 | ||
346 | for (i = 0; i < n; i++) { | |
347 | if (answers[i].type == RFC1035_TYPE_A) { | |
348 | ||
349 | struct in_addr a; | |
2f0b84f7 | 350 | char tmp[16]; |
26ac0430 | 351 | memcpy(&a, answers[i].rdata, 4); |
2f0b84f7 | 352 | printf("A\t%d\t%s\n", answers[i].ttl, inet_ntop(AF_INET,&a,tmp,16)); |
26ac0430 AJ |
353 | } else if (answers[i].type == RFC1035_TYPE_AAAA) { |
354 | ||
355 | struct in6_addr a; | |
2f0b84f7 | 356 | char tmp[INET6_ADDRSTRLEN]; |
26ac0430 | 357 | memcpy(&a, answers[i].rdata, 16); |
2f0b84f7 | 358 | printf("AAAA\t%d\t%s\n", answers[i].ttl, inet_ntop(AF_INET6,&a,tmp,sizeof(tmp))); |
26ac0430 AJ |
359 | } else if (answers[i].type == RFC1035_TYPE_PTR) { |
360 | char ptr[RFC1035_MAXHOSTNAMESZ]; | |
361 | strncpy(ptr, answers[i].rdata, answers[i].rdlength); | |
362 | printf("PTR\t%d\t%s\n", answers[i].ttl, ptr); | |
363 | } else if (answers[i].type == RFC1035_TYPE_CNAME) { | |
364 | char ptr[RFC1035_MAXHOSTNAMESZ]; | |
365 | strncpy(ptr, answers[i].rdata, answers[i].rdlength); | |
366 | printf("CNAME\t%d\t%s\n", answers[i].ttl, ptr); | |
367 | } else { | |
368 | fprintf(stderr, "can't print answer type %d\n", | |
369 | (int) answers[i].type); | |
0710cbcd | 370 | } |
371 | } | |
372 | } | |
373 | } | |
0710cbcd | 374 | } |
375 | ||
26ac0430 AJ |
376 | return 0; |
377 | } | |
378 | ||
0710cbcd | 379 | #endif |