2 parse/query A/AAAA IN records
4 Copyright (C) 2005 Michael Tokarev <mjt@corpit.ru>
5 This file is part of UDNS library, an async DNS stub resolver.
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.
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.
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
28 # include <sys/types.h>
29 # include <netinet/in.h>
33 /* here, we use common routine to parse both IPv4 and IPv6 addresses.
36 /* this structure should match dns_rr_a[46] */
39 unsigned char *dnsa_addr;
43 dns_parse_a(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
44 void **result, unsigned dsize) {
50 /* first, validate and count number of addresses */
51 dns_initparse(&p, qdn, pkt, cur, end);
52 while((r = dns_nextrr(&p, &rr)) > 0)
53 if (rr.dnsrr_dsz != dsize)
54 return DNS_E_PROTOCOL;
56 return DNS_E_PROTOCOL;
60 ret = malloc(sizeof(*ret) + dsize * p.dnsp_nrr + dns_stdrr_size(&p));
64 ret->dnsa_nrr = p.dnsp_nrr;
65 ret->dnsa_addr = (unsigned char*)(ret+1);
68 for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r)
69 memcpy(ret->dnsa_addr + dsize * r, rr.dnsrr_dptr, dsize);
71 dns_stdrr_finish((struct dns_rr_null *)ret,
72 (char *)(ret->dnsa_addr + dsize * p.dnsp_nrr), &p);
78 dns_parse_a4(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
81 assert(sizeof(struct in_addr) == 4);
83 assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_A);
84 return dns_parse_a(qdn, pkt, cur, end, result, 4);
88 dns_submit_a4(struct dns_ctx *ctx, const char *name, int flags,
89 dns_query_a4_fn *cbck, void *data) {
91 dns_submit_p(ctx, name, DNS_C_IN, DNS_T_A, flags,
92 dns_parse_a4, (dns_query_fn*)cbck, data);
96 dns_resolve_a4(struct dns_ctx *ctx, const char *name, int flags) {
97 return (struct dns_rr_a4 *)
98 dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_A, flags, dns_parse_a4);
102 dns_parse_a6(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
105 assert(sizeof(struct in6_addr) == 16);
107 assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_AAAA);
108 return dns_parse_a(qdn, pkt, cur, end, result, 16);
112 dns_submit_a6(struct dns_ctx *ctx, const char *name, int flags,
113 dns_query_a6_fn *cbck, void *data) {
115 dns_submit_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags,
116 dns_parse_a6, (dns_query_fn*)cbck, data);
120 dns_resolve_a6(struct dns_ctx *ctx, const char *name, int flags) {
121 return (struct dns_rr_a6 *)
122 dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags, dns_parse_a6);