]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/rfc3596.c
SourceFormat Enforcement
[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
f7f3304a 58#include "squid.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
0710cbcd 155/**
156 * Builds a message buffer with a QUESTION to lookup PTR records
157 * for an address. Caller must allocate 'buf' which should
158 * probably be at least 512 octets. The 'szp' initially
159 * specifies the size of the buffer, on return it contains
160 * the size of the message (i.e. how much to write).
161 * \return the size of the query
162 */
163ssize_t
e210930b 164rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
0710cbcd 165{
166 static char rev[RFC1035_MAXHOSTNAMESZ];
167 unsigned int i;
168
169 i = (unsigned int) ntohl(addr.s_addr);
170 snprintf(rev, RFC1035_MAXHOSTNAMESZ, "%u.%u.%u.%u.in-addr.arpa.",
171 i & 255,
172 (i >> 8) & 255,
173 (i >> 16) & 255,
174 (i >> 24) & 255);
175
e210930b 176 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
0710cbcd 177}
178
179ssize_t
e210930b 180rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
0710cbcd 181{
182 static char rev[RFC1035_MAXHOSTNAMESZ];
183 const uint8_t* r = addr.s6_addr;
184 char* p = rev;
b8031529 185 int i; /* NP: MUST allow signed for loop termination. */
0710cbcd 186
187 /* work from the raw addr field. anything else may have representation changes. */
188 /* The sin6_port and sin6_addr members shall be in network byte order. */
26ac0430 189 for (i = 15; i >= 0; i--, p+=4) {
5c4e391c 190 snprintf(p, 5, "%1x.%1x.", ((r[i])&0xf), (r[i]>>4)&0xf );
0710cbcd 191 }
192
193 snprintf(p,10,"ip6.arpa.");
194
e210930b 195 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
0710cbcd 196}
197
0710cbcd 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>
0710cbcd 206
207int
208main(int argc, char *argv[])
209{
e210930b
AJ
210#define PACKET_BUFSZ 1024
211 char input[PACKET_BUFSZ];
212 char buf[PACKET_BUFSZ];
213 char rbuf[PACKET_BUFSZ];
214 size_t sz = PACKET_BUFSZ;
0710cbcd 215 unsigned short sid, sidb;
216 int s;
217 int rl;
e210930b 218 ssize_t edns_max = -1;
0710cbcd 219
220 struct sockaddr* S;
221 int var = 1;
222
223 if ( argc < 3 || argc > 4) {
224 fprintf(stderr, "usage: %s [-6|-4] ip port\n", argv[0]);
225 return 1;
226 }
227
228 setbuf(stdout, NULL);
229 setbuf(stderr, NULL);
230
26ac0430
AJ
231 if (argv[var][0] == '-') {
232 if (argv[var][1] == '4')
0710cbcd 233 prefer = AF_INET;
26ac0430 234 else if (argv[var][1] == '6')
0710cbcd 235 prefer = AF_INET6;
e210930b
AJ
236 else if (argv[var][1] == 'E')
237 edns_max = atoi(argv[var++]);
0710cbcd 238 else {
e210930b
AJ
239 fprintf(stderr, "usage: %s [-6|-4] [-E packet-size] ip port\n", argv[0]);
240 fprintf(stderr, " EDNS packets my be up to %d\n", PACKET_BUFSZ);
0710cbcd 241 return 1;
242 }
243
244 var++;
245 }
246
247 s = socket(PF_INET, SOCK_DGRAM, 0);
248
249 if (s < 0) {
250 perror("socket");
251 return 1;
252 }
253
0710cbcd 254 memset(&S, '\0', sizeof(S));
255
26ac0430 256 if (prefer == 6) {
0710cbcd 257 S = (struct sockaddr *) new sockaddr_in6;
258 memset(S,0,sizeof(struct sockaddr_in6));
259
260 ((struct sockaddr_in6 *)S)->sin6_family = AF_INET6;
261 ((struct sockaddr_in6 *)S)->sin6_port = htons(atoi(argv[var+1]));
262
e210930b 263 if ( ! inet_pton(AF_INET6, argv[var], &((struct sockaddr_in6 *)S)->sin6_addr.s_addr) ) {
0710cbcd 264 perror("listen address");
24520b1b
AJ
265 return 1;
266 }
0710cbcd 267
24520b1b
AJ
268 s = socket(PF_INET6, SOCK_DGRAM, 0);
269 } else {
270 S = (struct sockaddr *) new sockaddr_in;
271 memset(S,0,sizeof(struct sockaddr_in));
0710cbcd 272
24520b1b
AJ
273 ((struct sockaddr_in *)S)->sin_family = AF_INET;
274 ((struct sockaddr_in *)S)->sin_port = htons(atoi(argv[var+1]));
0710cbcd 275
24520b1b
AJ
276 if ( ! inet_pton(AF_INET, argv[var], &((struct sockaddr_in *)S)->sin_addr.s_addr) )
277 perror("listen address");
278 return 1;
279 }
26ac0430 280}
0710cbcd 281
e210930b 282while (fgets(input, PACKET_BUFSZ, stdin))
26ac0430 283{
0710cbcd 284
26ac0430 285 struct in6_addr junk6;
0710cbcd 286
26ac0430
AJ
287 struct in_addr junk4;
288 strtok(input, "\r\n");
e210930b
AJ
289 memset(buf, '\0', PACKET_BUFSZ);
290 sz = PACKET_BUFSZ;
0710cbcd 291
27bc2077 292 if (inet_pton(AF_INET6, input, &junk6)) {
e210930b 293 sid = rfc1035BuildPTRQuery6(junk6, buf, &sz, edns_max);
26ac0430 294 sidb=0;
27bc2077 295 } else if (inet_pton(AF_INET, input, &junk4)) {
e210930b 296 sid = rfc1035BuildPTRQuery4(junk4, buf, &sz, edns_max);
26ac0430
AJ
297 sidb=0;
298 } else {
e210930b
AJ
299 sid = rfc1035BuildAAAAQuery(input, buf, &sz, edns_max);
300 sidb = rfc1035BuildAQuery(input, buf, &sz, edns_max);
26ac0430 301 }
0710cbcd 302
26ac0430 303 sendto(s, buf, sz, 0, S, sizeof(*S));
0710cbcd 304
26ac0430
AJ
305 do {
306 fd_set R;
0710cbcd 307
26ac0430
AJ
308 struct timeval to;
309 FD_ZERO(&R);
310 FD_SET(s, &R);
311 to.tv_sec = 10;
312 to.tv_usec = 0;
313 rl = select(s + 1, &R, NULL, NULL, &to);
314 } while (0);
0710cbcd 315
26ac0430
AJ
316 if (rl < 1) {
317 printf("TIMEOUT\n");
318 continue;
319 }
0710cbcd 320
e210930b
AJ
321 memset(rbuf, '\0', PACKET_BUFSZ);
322 rl = recv(s, rbuf, PACKET_BUFSZ, 0);
26ac0430
AJ
323 {
324 unsigned short rid = 0;
325 int i;
326 int n;
327 rfc1035_rr *answers = NULL;
328 n = rfc1035AnswersUnpack(rbuf,
329 rl,
330 &answers,
331 &rid);
332
333 if (n < 0) {
42687bb2 334 printf("ERROR %d\n", -n);
26ac0430
AJ
335 } else if (rid != sid && rid != sidb) {
336 printf("ERROR, ID mismatch (%#hx, %#hx)\n", sid, rid);
337 printf("ERROR, ID mismatch (%#hx, %#hx)\n", sidb, rid);
338 } else {
339 printf("%d answers\n", n);
340
341 for (i = 0; i < n; i++) {
342 if (answers[i].type == RFC1035_TYPE_A) {
343
344 struct in_addr a;
2f0b84f7 345 char tmp[16];
26ac0430 346 memcpy(&a, answers[i].rdata, 4);
2f0b84f7 347 printf("A\t%d\t%s\n", answers[i].ttl, inet_ntop(AF_INET,&a,tmp,16));
26ac0430
AJ
348 } else if (answers[i].type == RFC1035_TYPE_AAAA) {
349
350 struct in6_addr a;
2f0b84f7 351 char tmp[INET6_ADDRSTRLEN];
26ac0430 352 memcpy(&a, answers[i].rdata, 16);
2f0b84f7 353 printf("AAAA\t%d\t%s\n", answers[i].ttl, inet_ntop(AF_INET6,&a,tmp,sizeof(tmp)));
26ac0430
AJ
354 } else if (answers[i].type == RFC1035_TYPE_PTR) {
355 char ptr[RFC1035_MAXHOSTNAMESZ];
356 strncpy(ptr, answers[i].rdata, answers[i].rdlength);
357 printf("PTR\t%d\t%s\n", answers[i].ttl, ptr);
358 } else if (answers[i].type == RFC1035_TYPE_CNAME) {
359 char ptr[RFC1035_MAXHOSTNAMESZ];
360 strncpy(ptr, answers[i].rdata, answers[i].rdlength);
361 printf("CNAME\t%d\t%s\n", answers[i].ttl, ptr);
362 } else {
363 fprintf(stderr, "can't print answer type %d\n",
364 (int) answers[i].type);
0710cbcd 365 }
366 }
367 }
368 }
0710cbcd 369}
370
26ac0430 371return 0;
24520b1b 372}
26ac0430 373
0710cbcd 374#endif