1 |
/* $Id: udns_rr_txt.c,v 1.15 2006/11/28 22:45:20 mjt Exp $ |
---|
2 |
parse/query TXT 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 <string.h> |
---|
25 |
#include <stdlib.h> |
---|
26 |
#include <assert.h> |
---|
27 |
#include "udns.h" |
---|
28 |
|
---|
29 |
int |
---|
30 |
dns_parse_txt(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end, |
---|
31 |
void **result) { |
---|
32 |
struct dns_rr_txt *ret; |
---|
33 |
struct dns_parse p; |
---|
34 |
struct dns_rr rr; |
---|
35 |
int r, l; |
---|
36 |
dnsc_t *sp; |
---|
37 |
dnscc_t *cp, *ep; |
---|
38 |
|
---|
39 |
assert(dns_get16(cur+0) == DNS_T_TXT); |
---|
40 |
|
---|
41 |
/* first, validate the answer and count size of the result */ |
---|
42 |
l = 0; |
---|
43 |
dns_initparse(&p, qdn, pkt, cur, end); |
---|
44 |
while((r = dns_nextrr(&p, &rr)) > 0) { |
---|
45 |
cp = rr.dnsrr_dptr; ep = rr.dnsrr_dend; |
---|
46 |
while(cp < ep) { |
---|
47 |
r = *cp++; |
---|
48 |
if (cp + r > ep) |
---|
49 |
return DNS_E_PROTOCOL; |
---|
50 |
l += r; |
---|
51 |
cp += r; |
---|
52 |
} |
---|
53 |
} |
---|
54 |
if (r < 0) |
---|
55 |
return DNS_E_PROTOCOL; |
---|
56 |
if (!p.dnsp_nrr) |
---|
57 |
return DNS_E_NODATA; |
---|
58 |
|
---|
59 |
/* next, allocate and set up result */ |
---|
60 |
l += (sizeof(struct dns_txt) + 1) * p.dnsp_nrr + dns_stdrr_size(&p); |
---|
61 |
ret = malloc(sizeof(*ret) + l); |
---|
62 |
if (!ret) |
---|
63 |
return DNS_E_NOMEM; |
---|
64 |
ret->dnstxt_nrr = p.dnsp_nrr; |
---|
65 |
ret->dnstxt_txt = (struct dns_txt *)(ret+1); |
---|
66 |
|
---|
67 |
/* and 3rd, fill in result, finally */ |
---|
68 |
sp = (dnsc_t*)(ret->dnstxt_txt + p.dnsp_nrr); |
---|
69 |
for(dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr) > 0; ++r) { |
---|
70 |
ret->dnstxt_txt[r].txt = sp; |
---|
71 |
cp = rr.dnsrr_dptr; ep = rr.dnsrr_dend; |
---|
72 |
while(cp < ep) { |
---|
73 |
l = *cp++; |
---|
74 |
memcpy(sp, cp, l); |
---|
75 |
sp += l; |
---|
76 |
cp += l; |
---|
77 |
} |
---|
78 |
ret->dnstxt_txt[r].len = sp - ret->dnstxt_txt[r].txt; |
---|
79 |
*sp++ = '\0'; |
---|
80 |
} |
---|
81 |
dns_stdrr_finish((struct dns_rr_null *)ret, (char*)sp, &p); |
---|
82 |
*result = ret; |
---|
83 |
return 0; |
---|
84 |
} |
---|
85 |
|
---|
86 |
struct dns_query * |
---|
87 |
dns_submit_txt(struct dns_ctx *ctx, const char *name, int qcls, int flags, |
---|
88 |
dns_query_txt_fn *cbck, void *data) { |
---|
89 |
return |
---|
90 |
dns_submit_p(ctx, name, qcls, DNS_T_TXT, flags, |
---|
91 |
dns_parse_txt, (dns_query_fn *)cbck, data); |
---|
92 |
} |
---|
93 |
|
---|
94 |
struct dns_rr_txt * |
---|
95 |
dns_resolve_txt(struct dns_ctx *ctx, const char *name, int qcls, int flags) { |
---|
96 |
return (struct dns_rr_txt *) |
---|
97 |
dns_resolve_p(ctx, name, qcls, DNS_T_TXT, flags, dns_parse_txt); |
---|
98 |
} |
---|