summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/getrrsetbyname.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/getrrsetbyname.c')
-rw-r--r--src/lib/libc/net/getrrsetbyname.c512
1 files changed, 512 insertions, 0 deletions
diff --git a/src/lib/libc/net/getrrsetbyname.c b/src/lib/libc/net/getrrsetbyname.c
new file mode 100644
index 0000000000..f00ac2b021
--- /dev/null
+++ b/src/lib/libc/net/getrrsetbyname.c
@@ -0,0 +1,512 @@
1/* $OpenBSD: getrrsetbyname.c,v 1.7 2003/03/07 07:34:14 itojun Exp $ */
2
3/*
4 * Copyright (c) 2001 Jakob Schlyter. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * Portions Copyright (c) 1999-2001 Internet Software Consortium.
31 *
32 * Permission to use, copy, modify, and distribute this software for any
33 * purpose with or without fee is hereby granted, provided that the above
34 * copyright notice and this permission notice appear in all copies.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
37 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
39 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
40 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
41 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
42 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 */
45
46#include <sys/types.h>
47#include <netinet/in.h>
48#include <arpa/nameser.h>
49#include <netdb.h>
50#include <resolv.h>
51#include <stdlib.h>
52#include <string.h>
53
54#include "thread_private.h"
55
56#define ANSWER_BUFFER_SIZE 1024*64
57
58struct dns_query {
59 char *name;
60 u_int16_t type;
61 u_int16_t class;
62 struct dns_query *next;
63};
64
65struct dns_rr {
66 char *name;
67 u_int16_t type;
68 u_int16_t class;
69 u_int16_t ttl;
70 u_int16_t size;
71 void *rdata;
72 struct dns_rr *next;
73};
74
75struct dns_response {
76 HEADER header;
77 struct dns_query *query;
78 struct dns_rr *answer;
79 struct dns_rr *authority;
80 struct dns_rr *additional;
81};
82
83static struct dns_response *parse_dns_response(const u_char *, int);
84static struct dns_query *parse_dns_qsection(const u_char *, int,
85 const u_char **, int);
86static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
87 int);
88
89static void free_dns_query(struct dns_query *);
90static void free_dns_rr(struct dns_rr *);
91static void free_dns_response(struct dns_response *);
92
93static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
94
95int
96getrrsetbyname(const char *hostname, unsigned int rdclass,
97 unsigned int rdtype, unsigned int flags,
98 struct rrsetinfo **res)
99{
100 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
101 int result;
102 struct rrsetinfo *rrset = NULL;
103 struct dns_response *response;
104 struct dns_rr *rr;
105 struct rdatainfo *rdata;
106 int length;
107 unsigned int index_ans, index_sig;
108 u_char answer[ANSWER_BUFFER_SIZE];
109
110 /* check for invalid class and type */
111 if (rdclass > 0xffff || rdtype > 0xffff) {
112 result = ERRSET_INVAL;
113 goto fail;
114 }
115
116 /* don't allow queries of class or type ANY */
117 if (rdclass == 0xff || rdtype == 0xff) {
118 result = ERRSET_INVAL;
119 goto fail;
120 }
121
122 /* don't allow flags yet, unimplemented */
123 if (flags) {
124 result = ERRSET_INVAL;
125 goto fail;
126 }
127
128 /* initialize resolver */
129 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
130 result = ERRSET_FAIL;
131 goto fail;
132 }
133
134#ifdef DEBUG
135 _resp->options |= RES_DEBUG;
136#endif /* DEBUG */
137
138#ifdef RES_USE_DNSSEC
139 /* turn on DNSSEC if EDNS0 is configured */
140 if (_resp->options & RES_USE_EDNS0)
141 _resp->options |= RES_USE_DNSSEC;
142#endif /* RES_USE_DNSEC */
143
144 /* make query */
145 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
146 answer, sizeof(answer));
147 if (length < 0) {
148 switch(h_errno) {
149 case HOST_NOT_FOUND:
150 result = ERRSET_NONAME;
151 goto fail;
152 case NO_DATA:
153 result = ERRSET_NODATA;
154 goto fail;
155 default:
156 result = ERRSET_FAIL;
157 goto fail;
158 }
159 }
160
161 /* parse result */
162 response = parse_dns_response(answer, length);
163 if (response == NULL) {
164 result = ERRSET_FAIL;
165 goto fail;
166 }
167
168 if (response->header.qdcount != 1) {
169 result = ERRSET_FAIL;
170 goto fail;
171 }
172
173 /* initialize rrset */
174 rrset = calloc(1, sizeof(struct rrsetinfo));
175 if (rrset == NULL) {
176 result = ERRSET_NOMEMORY;
177 goto fail;
178 }
179 rrset->rri_rdclass = response->query->class;
180 rrset->rri_rdtype = response->query->type;
181 rrset->rri_ttl = response->answer->ttl;
182 rrset->rri_nrdatas = response->header.ancount;
183
184 /* check for authenticated data */
185 if (response->header.ad == 1)
186 rrset->rri_flags |= RRSET_VALIDATED;
187
188 /* copy name from answer section */
189 length = strlen(response->answer->name);
190 rrset->rri_name = malloc(length + 1);
191 if (rrset->rri_name == NULL) {
192 result = ERRSET_NOMEMORY;
193 goto fail;
194 }
195 strlcpy(rrset->rri_name, response->answer->name, length + 1);
196
197 /* count answers */
198 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
199 rrset->rri_rdtype);
200 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
201 T_SIG);
202
203 /* allocate memory for answers */
204 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
205 sizeof(struct rdatainfo));
206 if (rrset->rri_rdatas == NULL) {
207 result = ERRSET_NOMEMORY;
208 goto fail;
209 }
210
211 /* allocate memory for signatures */
212 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
213 if (rrset->rri_sigs == NULL) {
214 result = ERRSET_NOMEMORY;
215 goto fail;
216 }
217
218 /* copy answers & signatures */
219 for (rr = response->answer, index_ans = 0, index_sig = 0;
220 rr; rr = rr->next) {
221
222 rdata = NULL;
223
224 if (rr->class == rrset->rri_rdclass &&
225 rr->type == rrset->rri_rdtype)
226 rdata = &rrset->rri_rdatas[index_ans++];
227
228 if (rr->class == rrset->rri_rdclass &&
229 rr->type == T_SIG)
230 rdata = &rrset->rri_sigs[index_sig++];
231
232 if (rdata) {
233 rdata->rdi_length = rr->size;
234 rdata->rdi_data = malloc(rr->size);
235
236 if (rdata->rdi_data == NULL) {
237 result = ERRSET_NOMEMORY;
238 goto fail;
239 }
240 memcpy(rdata->rdi_data, rr->rdata, rr->size);
241 }
242 }
243
244 *res = rrset;
245 return (ERRSET_SUCCESS);
246
247fail:
248 if (rrset != NULL)
249 freerrset(rrset);
250 return (result);
251}
252
253void
254freerrset(struct rrsetinfo *rrset)
255{
256 u_int16_t i;
257
258 if (rrset == NULL)
259 return;
260
261 if (rrset->rri_rdatas) {
262 for (i = 0; i < rrset->rri_nrdatas; i++) {
263 if (rrset->rri_rdatas[i].rdi_data == NULL)
264 break;
265 free(rrset->rri_rdatas[i].rdi_data);
266 }
267 free(rrset->rri_rdatas);
268 }
269
270 if (rrset->rri_sigs) {
271 for (i = 0; i < rrset->rri_nsigs; i++) {
272 if (rrset->rri_sigs[i].rdi_data == NULL)
273 break;
274 free(rrset->rri_sigs[i].rdi_data);
275 }
276 free(rrset->rri_sigs);
277 }
278
279 if (rrset->rri_name)
280 free(rrset->rri_name);
281 free(rrset);
282}
283
284/*
285 * DNS response parsing routines
286 */
287static struct dns_response *
288parse_dns_response(const u_char *answer, int size)
289{
290 struct dns_response *resp;
291 const u_char *cp;
292
293 /* allocate memory for the response */
294 resp = calloc(1, sizeof(*resp));
295 if (resp == NULL)
296 return (NULL);
297
298 /* initialize current pointer */
299 cp = answer;
300
301 /* copy header */
302 memcpy(&resp->header, cp, HFIXEDSZ);
303 cp += HFIXEDSZ;
304
305 /* fix header byte order */
306 resp->header.qdcount = ntohs(resp->header.qdcount);
307 resp->header.ancount = ntohs(resp->header.ancount);
308 resp->header.nscount = ntohs(resp->header.nscount);
309 resp->header.arcount = ntohs(resp->header.arcount);
310
311 /* there must be at least one query */
312 if (resp->header.qdcount < 1) {
313 free_dns_response(resp);
314 return (NULL);
315 }
316
317 /* parse query section */
318 resp->query = parse_dns_qsection(answer, size, &cp,
319 resp->header.qdcount);
320 if (resp->header.qdcount && resp->query == NULL) {
321 free_dns_response(resp);
322 return (NULL);
323 }
324
325 /* parse answer section */
326 resp->answer = parse_dns_rrsection(answer, size, &cp,
327 resp->header.ancount);
328 if (resp->header.ancount && resp->answer == NULL) {
329 free_dns_response(resp);
330 return (NULL);
331 }
332
333 /* parse authority section */
334 resp->authority = parse_dns_rrsection(answer, size, &cp,
335 resp->header.nscount);
336 if (resp->header.nscount && resp->authority == NULL) {
337 free_dns_response(resp);
338 return (NULL);
339 }
340
341 /* parse additional section */
342 resp->additional = parse_dns_rrsection(answer, size, &cp,
343 resp->header.arcount);
344 if (resp->header.arcount && resp->additional == NULL) {
345 free_dns_response(resp);
346 return (NULL);
347 }
348
349 return (resp);
350}
351
352static struct dns_query *
353parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
354{
355 struct dns_query *head, *curr, *prev;
356 int i, length;
357 char name[MAXDNAME];
358
359 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
360
361 /* allocate and initialize struct */
362 curr = calloc(1, sizeof(struct dns_query));
363 if (curr == NULL) {
364 free_dns_query(head);
365 return (NULL);
366 }
367 if (head == NULL)
368 head = curr;
369 if (prev != NULL)
370 prev->next = curr;
371
372 /* name */
373 length = dn_expand(answer, answer + size, *cp, name,
374 sizeof(name));
375 if (length < 0) {
376 free_dns_query(head);
377 return (NULL);
378 }
379 curr->name = strdup(name);
380 if (curr->name == NULL) {
381 free_dns_query(head);
382 return (NULL);
383 }
384 *cp += length;
385
386 /* type */
387 curr->type = _getshort(*cp);
388 *cp += INT16SZ;
389
390 /* class */
391 curr->class = _getshort(*cp);
392 *cp += INT16SZ;
393 }
394
395 return (head);
396}
397
398static struct dns_rr *
399parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count)
400{
401 struct dns_rr *head, *curr, *prev;
402 int i, length;
403 char name[MAXDNAME];
404
405 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
406
407 /* allocate and initialize struct */
408 curr = calloc(1, sizeof(struct dns_rr));
409 if (curr == NULL) {
410 free_dns_rr(head);
411 return (NULL);
412 }
413 if (head == NULL)
414 head = curr;
415 if (prev != NULL)
416 prev->next = curr;
417
418 /* name */
419 length = dn_expand(answer, answer + size, *cp, name,
420 sizeof(name));
421 if (length < 0) {
422 free_dns_rr(head);
423 return (NULL);
424 }
425 curr->name = strdup(name);
426 if (curr->name == NULL) {
427 free_dns_rr(head);
428 return (NULL);
429 }
430 *cp += length;
431
432 /* type */
433 curr->type = _getshort(*cp);
434 *cp += INT16SZ;
435
436 /* class */
437 curr->class = _getshort(*cp);
438 *cp += INT16SZ;
439
440 /* ttl */
441 curr->ttl = _getlong(*cp);
442 *cp += INT32SZ;
443
444 /* rdata size */
445 curr->size = _getshort(*cp);
446 *cp += INT16SZ;
447
448 /* rdata itself */
449 curr->rdata = malloc(curr->size);
450 if (curr->rdata == NULL) {
451 free_dns_rr(head);
452 return (NULL);
453 }
454 memcpy(curr->rdata, *cp, curr->size);
455 *cp += curr->size;
456 }
457
458 return (head);
459}
460
461static void
462free_dns_query(struct dns_query *p)
463{
464 if (p == NULL)
465 return;
466
467 if (p->name)
468 free(p->name);
469 free_dns_query(p->next);
470 free(p);
471}
472
473static void
474free_dns_rr(struct dns_rr *p)
475{
476 if (p == NULL)
477 return;
478
479 if (p->name)
480 free(p->name);
481 if (p->rdata)
482 free(p->rdata);
483 free_dns_rr(p->next);
484 free(p);
485}
486
487static void
488free_dns_response(struct dns_response *p)
489{
490 if (p == NULL)
491 return;
492
493 free_dns_query(p->query);
494 free_dns_rr(p->answer);
495 free_dns_rr(p->authority);
496 free_dns_rr(p->additional);
497 free(p);
498}
499
500static int
501count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
502{
503 int n = 0;
504
505 while(p) {
506 if (p->class == class && p->type == type)
507 n++;
508 p = p->next;
509 }
510
511 return (n);
512}