| 1 |
/* $Id: udns_rr_ptr.c,v 1.15 2005/09/12 11:21:06 mjt Exp $ |
|---|
| 2 |
parse/query PTR records |
|---|
| 3 |
|
|---|
| 4 |
Copyright (C) 2005 Michael Tokarev <mjt@corpit.ru> |
|---|
| 5 |
This file is part of UDNS library, an async DNS stub resolver. |
|---|
| 6 |
|
|---|
| 7 |
This library is free software; you can redistribute it and/or |
|---|
| 8 |
modify it under the terms of the GNU Lesser General Public |
|---|
| 9 |
License as published by the Free Software Foundation; either |
|---|
| 10 |
version 2.1 of the License, or (at your option) any later version. |
|---|
| 11 |
|
|---|
| 12 |
This library is distributed in the hope that it will be useful, |
|---|
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 15 |
Lesser General Public License for more details. |
|---|
| 16 |
|
|---|
| 17 |
You should have received a copy of the GNU Lesser General Public |
|---|
| 18 |
License along with this library, in file named COPYING.LGPL; if not, |
|---|
| 19 |
write to the Free Software Foundation, Inc., 59 Temple Place, |
|---|
| 20 |
Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 |
|
|---|
| 22 |
*/ |
|---|
| 23 |
|
|---|
| 24 |
#include <stdlib.h> |
|---|
| 25 |
#include <assert.h> |
|---|
| 26 |
#include "udns.h" |
|---|
| 27 |
|
|---|
| 28 |
int |
|---|
| 29 |
dns_parse_ptr(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end, |
|---|
| 30 |
void **result) { |
|---|
| 31 |
struct dns_rr_ptr *ret; |
|---|
| 32 |
struct dns_parse p; |
|---|
| 33 |
struct dns_rr rr; |
|---|
| 34 |
int r, l, c; |
|---|
| 35 |
char *sp; |
|---|
| 36 |
dnsc_t ptr[DNS_MAXDN]; |
|---|
| 37 |
|
|---|
| 38 |
assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_PTR); |
|---|
| 39 |
|
|---|
| 40 |
/* first, validate the answer and count size of the result */ |
|---|
| 41 |
l = c = 0; |
|---|
| 42 |
dns_initparse(&p, qdn, pkt, cur, end); |
|---|
| 43 |
while((r = dns_nextrr(&p, &rr)) > 0) { |
|---|
| 44 |
cur = rr.dnsrr_dptr; |
|---|
| 45 |
r = dns_getdn(pkt, &cur, end, ptr, sizeof(ptr)); |
|---|
| 46 |
if (r <= 0 || cur != rr.dnsrr_dend) |
|---|
| 47 |
return DNS_E_PROTOCOL; |
|---|
| 48 |
l += dns_dntop_size(ptr); |
|---|
| 49 |
++c; |
|---|
| 50 |
} |
|---|
| 51 |
if (r < 0) |
|---|
| 52 |
return DNS_E_PROTOCOL; |
|---|
| 53 |
if (!c) |
|---|
| 54 |
return DNS_E_NODATA; |
|---|
| 55 |
|
|---|
| 56 |
/* next, allocate and set up result */ |
|---|
| 57 |
ret = malloc(sizeof(*ret) + sizeof(char **) * c + l + dns_stdrr_size(&p)); |
|---|
| 58 |
if (!ret) |
|---|
| 59 |
return DNS_E_NOMEM; |
|---|
| 60 |
ret->dnsptr_nrr = c; |
|---|
| 61 |
ret->dnsptr_ptr = (char **)(ret+1); |
|---|
| 62 |
|
|---|
| 63 |
/* and 3rd, fill in result, finally */ |
|---|
| 64 |
sp = (char*)(ret->dnsptr_ptr + c); |
|---|
| 65 |
c = 0; |
|---|
| 66 |
dns_rewind(&p, qdn); |
|---|
| 67 |
while((r = dns_nextrr(&p, &rr)) > 0) { |
|---|
| 68 |
ret->dnsptr_ptr[c] = sp; |
|---|
| 69 |
cur = rr.dnsrr_dptr; |
|---|
| 70 |
dns_getdn(pkt, &cur, end, ptr, sizeof(ptr)); |
|---|
| 71 |
sp += dns_dntop(ptr, sp, DNS_MAXNAME); |
|---|
| 72 |
++c; |
|---|
| 73 |
} |
|---|
| 74 |
dns_stdrr_finish((struct dns_rr_null *)ret, sp, &p); |
|---|
| 75 |
*result = ret; |
|---|
| 76 |
return 0; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
struct dns_query * |
|---|
| 80 |
dns_submit_a4ptr(struct dns_ctx *ctx, const struct in_addr *addr, |
|---|
| 81 |
dns_query_ptr_fn *cbck, void *data) { |
|---|
| 82 |
dnsc_t dn[DNS_A4RSIZE]; |
|---|
| 83 |
dns_a4todn(addr, 0, dn, sizeof(dn)); |
|---|
| 84 |
return |
|---|
| 85 |
dns_submit_dn(ctx, dn, DNS_C_IN, DNS_T_PTR, DNS_NOSRCH, |
|---|
| 86 |
dns_parse_ptr, (dns_query_fn *)cbck, data); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
struct dns_rr_ptr * |
|---|
| 90 |
dns_resolve_a4ptr(struct dns_ctx *ctx, const struct in_addr *addr) { |
|---|
| 91 |
return (struct dns_rr_ptr *) |
|---|
| 92 |
dns_resolve(ctx, dns_submit_a4ptr(ctx, addr, NULL, NULL)); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
struct dns_query * |
|---|
| 96 |
dns_submit_a6ptr(struct dns_ctx *ctx, const struct in6_addr *addr, |
|---|
| 97 |
dns_query_ptr_fn *cbck, void *data) { |
|---|
| 98 |
dnsc_t dn[DNS_A6RSIZE]; |
|---|
| 99 |
dns_a6todn(addr, 0, dn, sizeof(dn)); |
|---|
| 100 |
return |
|---|
| 101 |
dns_submit_dn(ctx, dn, DNS_C_IN, DNS_T_PTR, DNS_NOSRCH, |
|---|
| 102 |
dns_parse_ptr, (dns_query_fn *)cbck, data); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
struct dns_rr_ptr * |
|---|
| 106 |
dns_resolve_a6ptr(struct dns_ctx *ctx, const struct in6_addr *addr) { |
|---|
| 107 |
return (struct dns_rr_ptr *) |
|---|
| 108 |
dns_resolve(ctx, dns_submit_a6ptr(ctx, addr, NULL, NULL)); |
|---|
| 109 |
} |
|---|