]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/rfc3596.c
Release notes update for EDNS
[thirdparty/squid.git] / lib / rfc3596.c
CommitLineData
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"
e210930b 82#include "rfc2671.h"
0710cbcd 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 */
96ssize_t
e210930b 97rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, int qtype, ssize_t edns_sz)
0710cbcd 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;
e210930b 107 h.arcount = (edns_sz > 0 ? 1 : 0);
0710cbcd 108 offset += rfc1035HeaderPack(buf + offset, sz - offset, &h);
109 offset += rfc1035QuestionPack(buf + offset,
110 sz - offset,
111 hostname,
112 qtype,
113 RFC1035_CLASS_IN);
e210930b
AJ
114 if (edns_sz > 0)
115 offset += rfc2671RROptPack(buf + offset, sz - offset, edns_sz);
0710cbcd 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 */
135ssize_t
e210930b 136rfc3596BuildAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
0710cbcd 137{
e210930b 138 return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_A, edns_sz);
0710cbcd 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 */
149ssize_t
e210930b 150rfc3596BuildAAAAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
0710cbcd 151{
e210930b 152 return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_AAAA, edns_sz);
0710cbcd 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 */
164ssize_t
e210930b 165rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
0710cbcd 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
e210930b 177 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
0710cbcd 178}
179
180ssize_t
e210930b 181rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
0710cbcd 182{
183 static char rev[RFC1035_MAXHOSTNAMESZ];
184 const uint8_t* r = addr.s6_addr;
185 char* p = rev;
b8031529 186 int i; /* NP: MUST allow signed for loop termination. */
0710cbcd 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. */
26ac0430 190 for (i = 15; i >= 0; i--, p+=4) {
0710cbcd 191 snprintf(p, 5, "%1x.%1x.", ((r[i]>>4)&0xf), (r[i])&0xf );
192 }
193
194 snprintf(p,10,"ip6.arpa.");
195
e210930b 196 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
0710cbcd 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>
0710cbcd 208
209int
210main(int argc, char *argv[])
211{
e210930b
AJ
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;
0710cbcd 217 unsigned short sid, sidb;
218 int s;
219 int rl;
e210930b 220 ssize_t edns_max = -1;
0710cbcd 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
26ac0430
AJ
233 if (argv[var][0] == '-') {
234 if (argv[var][1] == '4')
0710cbcd 235 prefer = AF_INET;
26ac0430 236 else if (argv[var][1] == '6')
0710cbcd 237 prefer = AF_INET6;
e210930b
AJ
238 else if (argv[var][1] == 'E')
239 edns_max = atoi(argv[var++]);
0710cbcd 240 else {
e210930b
AJ
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);
0710cbcd 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
e210930b 266 if ( ! inet_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}
273else
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
27bc2077 281 if ( ! inet_pton(AF_INET, argv[var], &((struct sockaddr_in *)S)->sin_addr.s_addr) )
26ac0430
AJ
282 perror("listen address");
283 return 1;
284}
285}
0710cbcd 286
e210930b 287while (fgets(input, PACKET_BUFSZ, stdin))
26ac0430 288{
0710cbcd 289
26ac0430 290 struct in6_addr junk6;
0710cbcd 291
26ac0430
AJ
292 struct in_addr junk4;
293 strtok(input, "\r\n");
e210930b
AJ
294 memset(buf, '\0', PACKET_BUFSZ);
295 sz = PACKET_BUFSZ;
0710cbcd 296
27bc2077 297 if (inet_pton(AF_INET6, input, &junk6)) {
e210930b 298 sid = rfc1035BuildPTRQuery6(junk6, buf, &sz, edns_max);
26ac0430 299 sidb=0;
27bc2077 300 } else if (inet_pton(AF_INET, input, &junk4)) {
e210930b 301 sid = rfc1035BuildPTRQuery4(junk4, buf, &sz, edns_max);
26ac0430
AJ
302 sidb=0;
303 } else {
e210930b
AJ
304 sid = rfc1035BuildAAAAQuery(input, buf, &sz, edns_max);
305 sidb = rfc1035BuildAQuery(input, buf, &sz, edns_max);
26ac0430 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
e210930b
AJ
326 memset(rbuf, '\0', PACKET_BUFSZ);
327 rl = recv(s, rbuf, PACKET_BUFSZ, 0);
26ac0430
AJ
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) {
42687bb2 339 printf("ERROR %d\n", -n);
26ac0430
AJ
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
376return 0;
377 }
378
0710cbcd 379#endif