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