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