]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc3596.c
Merged from trunk.
[thirdparty/squid.git] / lib / rfc3596.c
1 /*
2 * $Id$
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.
27 *
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.
32 *
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:
41 *
42 * UDP replies with TC set should be retried via TCP
43 */
44
45 /**
46 * April 2007
47 *
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.
51 *
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"
59 #include "compat/inet_pton.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_MEMORY_H
69 #include <memory.h>
70 #endif
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
161
162 rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query)
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);
175 }
176
177 ssize_t
178
179 rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query)
180 {
181 static char rev[RFC1035_MAXHOSTNAMESZ];
182 const uint8_t* r = addr.s6_addr;
183 char* p = rev;
184 int i; // NP: MUST allow signed for loop termination.
185
186 /* work from the raw addr field. anything else may have representation changes. */
187 /* The sin6_port and sin6_addr members shall be in network byte order. */
188 for (i = 15; i >= 0; i--, p+=4) {
189 snprintf(p, 5, "%1x.%1x.", ((r[i]>>4)&0xf), (r[i])&0xf );
190 }
191
192 snprintf(p,10,"ip6.arpa.");
193
194 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR);
195 }
196
197
198 #if DRIVER
199
200 /* driver needs the rfc1035 code _without_ the main() */
201 # define main(a,b) rfc1035_main(a,b)
202 # include "rfc1035.c"
203 # undef main(a,b)
204
205 #include <sys/socket.h>
206
207 int
208 main(int argc, char *argv[])
209 {
210 char input[512];
211 char buf[512];
212 char rbuf[512];
213 size_t sz = 512;
214 unsigned short sid, sidb;
215 int s;
216 int rl;
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 {
235 fprintf(stderr, "usage: %s [-6|-4] ip port\n", argv[0]);
236 return 1;
237 }
238
239 var++;
240 }
241
242 s = socket(PF_INET, SOCK_DGRAM, 0);
243
244 if (s < 0) {
245 perror("socket");
246 return 1;
247 }
248
249
250 memset(&S, '\0', sizeof(S));
251
252 if (prefer == 6) {
253 S = (struct sockaddr *) new sockaddr_in6;
254 memset(S,0,sizeof(struct sockaddr_in6));
255
256 ((struct sockaddr_in6 *)S)->sin6_family = AF_INET6;
257 ((struct sockaddr_in6 *)S)->sin6_port = htons(atoi(argv[var+1]));
258
259 if ( ! inet_pton(AF_INET6, argv[var], &((struct sockaddr_in6 *)S)->sin6_addr.s_addr) )
260 perror("listen address");
261 return 1;
262 }
263
264 s = socket(PF_INET6, SOCK_DGRAM, 0);
265 }
266 else
267 {
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, 512, stdin))
281 {
282
283 struct in6_addr junk6;
284
285 struct in_addr junk4;
286 strtok(input, "\r\n");
287 memset(buf, '\0', 512);
288 sz = 512;
289
290 if (inet_pton(AF_INET6, input, &junk6)) {
291 sid = rfc1035BuildPTRQuery6(junk6, buf, &sz);
292 sidb=0;
293 } else if (inet_pton(AF_INET, input, &junk4)) {
294 sid = rfc1035BuildPTRQuery4(junk4, buf, &sz);
295 sidb=0;
296 } else {
297 sid = rfc1035BuildAAAAQuery(input, buf, &sz);
298 sidb = rfc1035BuildAQuery(input, buf, &sz);
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', 512);
320 rl = recv(s, rbuf, 512, 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", rfc1035_errno);
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