]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc3596.c
4 * Low level DNS protocol routines
5 * AUTHOR: Amos Jeffries, Rafael Martinez Torres
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
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.
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.
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.
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.
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.
42 * UDP replies with TC set should be retried via TCP
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.
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.
59 #include "compat/inet_pton.h"
75 #include <netinet/in.h>
83 #ifndef SQUID_RFC1035_H
84 #error RFC3596 Library depends on RFC1035
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
96 rfc3596BuildHostQuery(const char *hostname
, char *buf
, size_t sz
, unsigned short qid
, rfc1035_query
* query
, int qtype
)
98 static rfc1035_message h
;
100 memset(&h
, '\0', sizeof(h
));
104 h
.opcode
= 0; /* QUERY */
105 h
.qdcount
= (unsigned int) 1;
106 offset
+= rfc1035HeaderPack(buf
+ offset
, sz
- offset
, &h
);
107 offset
+= rfc1035QuestionPack(buf
+ offset
,
114 query
->qtype
= qtype
;
115 query
->qclass
= RFC1035_CLASS_IN
;
116 xstrncpy(query
->name
, hostname
, sizeof(query
->name
));
119 assert(offset
<= sz
);
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
132 rfc3596BuildAQuery(const char *hostname
, char *buf
, size_t sz
, unsigned short qid
, rfc1035_query
* query
)
134 return rfc3596BuildHostQuery(hostname
, buf
, sz
, qid
, query
, RFC1035_TYPE_A
);
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
146 rfc3596BuildAAAAQuery(const char *hostname
, char *buf
, size_t sz
, unsigned short qid
, rfc1035_query
* query
)
148 return rfc3596BuildHostQuery(hostname
, buf
, sz
, qid
, query
, RFC1035_TYPE_AAAA
);
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
162 rfc3596BuildPTRQuery4(const struct in_addr addr
, char *buf
, size_t sz
, unsigned short qid
, rfc1035_query
* query
)
164 static char rev
[RFC1035_MAXHOSTNAMESZ
];
167 i
= (unsigned int) ntohl(addr
.s_addr
);
168 snprintf(rev
, RFC1035_MAXHOSTNAMESZ
, "%u.%u.%u.%u.in-addr.arpa.",
174 return rfc3596BuildHostQuery(rev
, buf
, sz
, qid
, query
, RFC1035_TYPE_PTR
);
179 rfc3596BuildPTRQuery6(const struct in6_addr addr
, char *buf
, size_t sz
, unsigned short qid
, rfc1035_query
* query
)
181 static char rev
[RFC1035_MAXHOSTNAMESZ
];
182 const uint8_t* r
= addr
.s6_addr
;
184 int i
; // NP: MUST allow signed for loop termination.
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 );
192 snprintf(p
,10,"ip6.arpa.");
194 return rfc3596BuildHostQuery(rev
, buf
, sz
, qid
, query
, RFC1035_TYPE_PTR
);
200 /* driver needs the rfc1035 code _without_ the main() */
201 # define main(a,b) rfc1035_main(a,b)
202 # include "rfc1035.c"
205 #include <sys/socket.h>
208 main(int argc
, char *argv
[])
214 unsigned short sid
, sidb
;
221 if ( argc
< 3 || argc
> 4) {
222 fprintf(stderr
, "usage: %s [-6|-4] ip port\n", argv
[0]);
226 setbuf(stdout
, NULL
);
227 setbuf(stderr
, NULL
);
229 if (argv
[var
][0] == '-') {
230 if (argv
[var
][1] == '4')
232 else if (argv
[var
][1] == '6')
235 fprintf(stderr
, "usage: %s [-6|-4] ip port\n", argv
[0]);
242 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
250 memset(&S
, '\0', sizeof(S
));
253 S
= (struct sockaddr
*) new sockaddr_in6
;
254 memset(S
,0,sizeof(struct sockaddr_in6
));
256 ((struct sockaddr_in6
*)S
)->sin6_family
= AF_INET6
;
257 ((struct sockaddr_in6
*)S
)->sin6_port
= htons(atoi(argv
[var
+1]));
259 if ( ! inet_pton(AF_INET6
, argv
[var
], &((struct sockaddr_in6
*)S
)->sin6_addr
.s_addr
) )
260 perror("listen address");
264 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
268 S
= (struct sockaddr
*) new sockaddr_in
;
269 memset(S
,0,sizeof(struct sockaddr_in
));
271 ((struct sockaddr_in
*)S
)->sin_family
= AF_INET
;
272 ((struct sockaddr_in
*)S
)->sin_port
= htons(atoi(argv
[var
+1]));
274 if ( ! inet_pton(AF_INET
, argv
[var
], &((struct sockaddr_in
*)S
)->sin_addr
.s_addr
) )
275 perror("listen address");
280 while (fgets(input
, 512, stdin
))
283 struct in6_addr junk6
;
285 struct in_addr junk4
;
286 strtok(input
, "\r\n");
287 memset(buf
, '\0', 512);
290 if (inet_pton(AF_INET6
, input
, &junk6
)) {
291 sid
= rfc1035BuildPTRQuery6(junk6
, buf
, &sz
);
293 } else if (inet_pton(AF_INET
, input
, &junk4
)) {
294 sid
= rfc1035BuildPTRQuery4(junk4
, buf
, &sz
);
297 sid
= rfc1035BuildAAAAQuery(input
, buf
, &sz
);
298 sidb
= rfc1035BuildAQuery(input
, buf
, &sz
);
301 sendto(s
, buf
, sz
, 0, S
, sizeof(*S
));
311 rl
= select(s
+ 1, &R
, NULL
, NULL
, &to
);
319 memset(rbuf
, '\0', 512);
320 rl
= recv(s
, rbuf
, 512, 0);
322 unsigned short rid
= 0;
325 rfc1035_rr
*answers
= NULL
;
326 n
= rfc1035AnswersUnpack(rbuf
,
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
);
337 printf("%d answers\n", n
);
339 for (i
= 0; i
< n
; i
++) {
340 if (answers
[i
].type
== RFC1035_TYPE_A
) {
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
) {
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
);
361 fprintf(stderr
, "can't print answer type %d\n",
362 (int) answers
[i
].type
);