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
31 # include <sys/types.h>
32 # include <netinet/in.h>
36 /* here, we use common routine to parse both IPv4 and IPv6 addresses.
39 /* this structure should match dns_rr_a[46] */
42 unsigned char *dnsa_addr;
46 dns_parse_a(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
47 void **result, unsigned dsize) {
53 /* first, validate and count number of addresses */
54 dns_initparse(&p, qdn, pkt, cur, end);
55 while((r = dns_nextrr(&p, &rr)) > 0)
56 if (rr.dnsrr_dsz != dsize)
57 return DNS_E_PROTOCOL;
59 return DNS_E_PROTOCOL;
63 ret = malloc(sizeof(*ret) + dsize * p.dnsp_nrr + dns_stdrr_size(&p));
67 ret->dnsa_nrr = p.dnsp_nrr;
68 ret->dnsa_addr = (unsigned char*)(ret+1);
71 for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r)
72 memcpy(ret->dnsa_addr + dsize * r, rr.dnsrr_dptr, dsize);
74 dns_stdrr_finish((struct dns_rr_null *)ret,
75 (char *)(ret->dnsa_addr + dsize * p.dnsp_nrr), &p);
81 dns_parse_a4(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
84 assert(sizeof(struct in_addr) == 4);
86 assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_A);
87 return dns_parse_a(qdn, pkt, cur, end, result, 4);
91 dns_submit_a4(struct dns_ctx *ctx, const char *name, int flags,
92 dns_query_a4_fn *cbck, void *data) {
94 dns_submit_p(ctx, name, DNS_C_IN, DNS_T_A, flags,
95 dns_parse_a4, (dns_query_fn*)cbck, data);
99 dns_resolve_a4(struct dns_ctx *ctx, const char *name, int flags) {
100 return (struct dns_rr_a4 *)
101 dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_A, flags, dns_parse_a4);
105 dns_parse_a6(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
108 assert(sizeof(struct in6_addr) == 16);
110 assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_AAAA);
111 return dns_parse_a(qdn, pkt, cur, end, result, 16);
115 dns_submit_a6(struct dns_ctx *ctx, const char *name, int flags,
116 dns_query_a6_fn *cbck, void *data) {
118 dns_submit_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags,
119 dns_parse_a6, (dns_query_fn*)cbck, data);
123 dns_resolve_a6(struct dns_ctx *ctx, const char *name, int flags) {
124 return (struct dns_rr_a6 *)
125 dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags, dns_parse_a6);