]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc3596.c
SourceFormat Enforcements
[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 #include "rfc2671.h"
83
84 #ifndef SQUID_RFC1035_H
85 #error RFC3596 Library depends on RFC1035
86 #endif
87
88 /**
89 * Builds a message buffer with a QUESTION to lookup records
90 * for a hostname. Caller must allocate 'buf' which should
91 * probably be at least 512 octets. The 'szp' initially
92 * specifies the size of the buffer, on return it contains
93 * the size of the message (i.e. how much to write).
94 * Returns the size of the query
95 */
96 ssize_t
97 rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, int qtype, ssize_t edns_sz)
98 {
99 static rfc1035_message h;
100 size_t offset = 0;
101 memset(&h, '\0', sizeof(h));
102 h.id = qid;
103 h.qr = 0;
104 h.rd = 1;
105 h.opcode = 0; /* QUERY */
106 h.qdcount = (unsigned int) 1;
107 h.arcount = (edns_sz > 0 ? 1 : 0);
108 offset += rfc1035HeaderPack(buf + offset, sz - offset, &h);
109 offset += rfc1035QuestionPack(buf + offset,
110 sz - offset,
111 hostname,
112 qtype,
113 RFC1035_CLASS_IN);
114 if (edns_sz > 0)
115 offset += rfc2671RROptPack(buf + offset, sz - offset, edns_sz);
116
117 if (query) {
118 query->qtype = qtype;
119 query->qclass = RFC1035_CLASS_IN;
120 xstrncpy(query->name, hostname, sizeof(query->name));
121 }
122
123 assert(offset <= sz);
124 return offset;
125 }
126
127 /**
128 * Builds a message buffer with a QUESTION to lookup A records
129 * for a hostname. Caller must allocate 'buf' which should
130 * probably be at least 512 octets. The 'szp' initially
131 * specifies the size of the buffer, on return it contains
132 * the size of the message (i.e. how much to write).
133 * \return the size of the query
134 */
135 ssize_t
136 rfc3596BuildAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
137 {
138 return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_A, edns_sz);
139 }
140
141 /**
142 * Builds a message buffer with a QUESTION to lookup AAAA records
143 * for a hostname. Caller must allocate 'buf' which should
144 * probably be at least 512 octets. The 'szp' initially
145 * specifies the size of the buffer, on return it contains
146 * the size of the message (i.e. how much to write).
147 * \return the size of the query
148 */
149 ssize_t
150 rfc3596BuildAAAAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
151 {
152 return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_AAAA, edns_sz);
153 }
154
155
156 /**
157 * Builds a message buffer with a QUESTION to lookup PTR records
158 * for an address. Caller must allocate 'buf' which should
159 * probably be at least 512 octets. The 'szp' initially
160 * specifies the size of the buffer, on return it contains
161 * the size of the message (i.e. how much to write).
162 * \return the size of the query
163 */
164 ssize_t
165 rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
166 {
167 static char rev[RFC1035_MAXHOSTNAMESZ];
168 unsigned int i;
169
170 i = (unsigned int) ntohl(addr.s_addr);
171 snprintf(rev, RFC1035_MAXHOSTNAMESZ, "%u.%u.%u.%u.in-addr.arpa.",
172 i & 255,
173 (i >> 8) & 255,
174 (i >> 16) & 255,
175 (i >> 24) & 255);
176
177 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
178 }
179
180 ssize_t
181 rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
182 {
183 static char rev[RFC1035_MAXHOSTNAMESZ];
184 const uint8_t* r = addr.s6_addr;
185 char* p = rev;
186 int i; /* NP: MUST allow signed for loop termination. */
187
188 /* work from the raw addr field. anything else may have representation changes. */
189 /* The sin6_port and sin6_addr members shall be in network byte order. */
190 for (i = 15; i >= 0; i--, p+=4) {
191 snprintf(p, 5, "%1x.%1x.", ((r[i]>>4)&0xf), (r[i])&0xf );
192 }
193
194 snprintf(p,10,"ip6.arpa.");
195
196 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
197 }
198
199
200 #if DRIVER
201
202 /* driver needs the rfc1035 code _without_ the main() */
203 # define main(a,b) rfc1035_main(a,b)
204 # include "rfc1035.c"
205 # undef main(a,b)
206
207 #include <sys/socket.h>
208
209 int
210 main(int argc, char *argv[])
211 {
212 #define PACKET_BUFSZ 1024
213 char input[PACKET_BUFSZ];
214 char buf[PACKET_BUFSZ];
215 char rbuf[PACKET_BUFSZ];
216 size_t sz = PACKET_BUFSZ;
217 unsigned short sid, sidb;
218 int s;
219 int rl;
220 ssize_t edns_max = -1;
221
222 struct sockaddr* S;
223 int var = 1;
224
225 if ( argc < 3 || argc > 4) {
226 fprintf(stderr, "usage: %s [-6|-4] ip port\n", argv[0]);
227 return 1;
228 }
229
230 setbuf(stdout, NULL);
231 setbuf(stderr, NULL);
232
233 if (argv[var][0] == '-') {
234 if (argv[var][1] == '4')
235 prefer = AF_INET;
236 else if (argv[var][1] == '6')
237 prefer = AF_INET6;
238 else if (argv[var][1] == 'E')
239 edns_max = atoi(argv[var++]);
240 else {
241 fprintf(stderr, "usage: %s [-6|-4] [-E packet-size] ip port\n", argv[0]);
242 fprintf(stderr, " EDNS packets my be up to %d\n", PACKET_BUFSZ);
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
259 if (prefer == 6) {
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
266 if ( ! inet_pton(AF_INET6, argv[var], &((struct sockaddr_in6 *)S)->sin6_addr.s_addr) ) {
267 perror("listen address");
268 return 1;
269 }
270
271 s = socket(PF_INET6, SOCK_DGRAM, 0);
272 } else {
273 S = (struct sockaddr *) new sockaddr_in;
274 memset(S,0,sizeof(struct sockaddr_in));
275
276 ((struct sockaddr_in *)S)->sin_family = AF_INET;
277 ((struct sockaddr_in *)S)->sin_port = htons(atoi(argv[var+1]));
278
279 if ( ! inet_pton(AF_INET, argv[var], &((struct sockaddr_in *)S)->sin_addr.s_addr) )
280 perror("listen address");
281 return 1;
282 }
283 }
284
285 while (fgets(input, PACKET_BUFSZ, stdin))
286 {
287
288 struct in6_addr junk6;
289
290 struct in_addr junk4;
291 strtok(input, "\r\n");
292 memset(buf, '\0', PACKET_BUFSZ);
293 sz = PACKET_BUFSZ;
294
295 if (inet_pton(AF_INET6, input, &junk6)) {
296 sid = rfc1035BuildPTRQuery6(junk6, buf, &sz, edns_max);
297 sidb=0;
298 } else if (inet_pton(AF_INET, input, &junk4)) {
299 sid = rfc1035BuildPTRQuery4(junk4, buf, &sz, edns_max);
300 sidb=0;
301 } else {
302 sid = rfc1035BuildAAAAQuery(input, buf, &sz, edns_max);
303 sidb = rfc1035BuildAQuery(input, buf, &sz, edns_max);
304 }
305
306 sendto(s, buf, sz, 0, S, sizeof(*S));
307
308 do {
309 fd_set R;
310
311 struct timeval to;
312 FD_ZERO(&R);
313 FD_SET(s, &R);
314 to.tv_sec = 10;
315 to.tv_usec = 0;
316 rl = select(s + 1, &R, NULL, NULL, &to);
317 } while (0);
318
319 if (rl < 1) {
320 printf("TIMEOUT\n");
321 continue;
322 }
323
324 memset(rbuf, '\0', PACKET_BUFSZ);
325 rl = recv(s, rbuf, PACKET_BUFSZ, 0);
326 {
327 unsigned short rid = 0;
328 int i;
329 int n;
330 rfc1035_rr *answers = NULL;
331 n = rfc1035AnswersUnpack(rbuf,
332 rl,
333 &answers,
334 &rid);
335
336 if (n < 0) {
337 printf("ERROR %d\n", -n);
338 } else if (rid != sid && rid != sidb) {
339 printf("ERROR, ID mismatch (%#hx, %#hx)\n", sid, rid);
340 printf("ERROR, ID mismatch (%#hx, %#hx)\n", sidb, rid);
341 } else {
342 printf("%d answers\n", n);
343
344 for (i = 0; i < n; i++) {
345 if (answers[i].type == RFC1035_TYPE_A) {
346
347 struct in_addr a;
348 char tmp[16];
349 memcpy(&a, answers[i].rdata, 4);
350 printf("A\t%d\t%s\n", answers[i].ttl, inet_ntop(AF_INET,&a,tmp,16));
351 } else if (answers[i].type == RFC1035_TYPE_AAAA) {
352
353 struct in6_addr a;
354 char tmp[INET6_ADDRSTRLEN];
355 memcpy(&a, answers[i].rdata, 16);
356 printf("AAAA\t%d\t%s\n", answers[i].ttl, inet_ntop(AF_INET6,&a,tmp,sizeof(tmp)));
357 } else if (answers[i].type == RFC1035_TYPE_PTR) {
358 char ptr[RFC1035_MAXHOSTNAMESZ];
359 strncpy(ptr, answers[i].rdata, answers[i].rdlength);
360 printf("PTR\t%d\t%s\n", answers[i].ttl, ptr);
361 } else if (answers[i].type == RFC1035_TYPE_CNAME) {
362 char ptr[RFC1035_MAXHOSTNAMESZ];
363 strncpy(ptr, answers[i].rdata, answers[i].rdlength);
364 printf("CNAME\t%d\t%s\n", answers[i].ttl, ptr);
365 } else {
366 fprintf(stderr, "can't print answer type %d\n",
367 (int) answers[i].type);
368 }
369 }
370 }
371 }
372 }
373
374 return 0;
375 }
376
377 #endif