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