]> git.mxchange.org Git - simgear.git/blob - 3rdparty/udns/udns_rr_a.c
HTTP: Always use absolute paths for hashes
[simgear.git] / 3rdparty / udns / udns_rr_a.c
1 /* udns_rr_a.c
2    parse/query A/AAAA IN 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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <string.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #ifndef WINDOWS
31 # include <sys/types.h>
32 # include <netinet/in.h>
33 #endif
34 #include "udns.h"
35
36 /* here, we use common routine to parse both IPv4 and IPv6 addresses.
37  */
38
39 /* this structure should match dns_rr_a[46] */
40 struct dns_rr_a {
41   dns_rr_common(dnsa);
42   unsigned char *dnsa_addr;
43 };
44
45 static int
46 dns_parse_a(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
47             void **result, unsigned dsize) {
48   struct dns_rr_a *ret;
49   struct dns_parse p;
50   struct dns_rr rr;
51   int r;
52
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;
58   if (r < 0)
59     return DNS_E_PROTOCOL;
60   else if (!p.dnsp_nrr)
61     return DNS_E_NODATA;
62
63   ret = malloc(sizeof(*ret) + dsize * p.dnsp_nrr + dns_stdrr_size(&p));
64   if (!ret)
65     return DNS_E_NOMEM;
66
67   ret->dnsa_nrr = p.dnsp_nrr;
68   ret->dnsa_addr = (unsigned char*)(ret+1);
69
70   /* copy the RRs */
71   for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r)
72     memcpy(ret->dnsa_addr + dsize * r, rr.dnsrr_dptr, dsize);
73
74   dns_stdrr_finish((struct dns_rr_null *)ret,
75                    (char *)(ret->dnsa_addr + dsize * p.dnsp_nrr), &p);
76   *result = ret;
77   return 0;
78 }
79
80 int
81 dns_parse_a4(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
82              void **result) {
83 #ifdef AF_INET
84   assert(sizeof(struct in_addr) == 4);
85 #endif
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);
88 }
89
90 struct dns_query *
91 dns_submit_a4(struct dns_ctx *ctx, const char *name, int flags,
92               dns_query_a4_fn *cbck, void *data) {
93   return
94     dns_submit_p(ctx, name, DNS_C_IN, DNS_T_A, flags,
95                  dns_parse_a4, (dns_query_fn*)cbck, data);
96 }
97
98 struct dns_rr_a4 *
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);
102 }
103
104 int
105 dns_parse_a6(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
106              void **result) {
107 #ifdef AF_INET6
108   assert(sizeof(struct in6_addr) == 16);
109 #endif
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);
112 }
113
114 struct dns_query *
115 dns_submit_a6(struct dns_ctx *ctx, const char *name, int flags,
116               dns_query_a6_fn *cbck, void *data) {
117   return
118     dns_submit_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags,
119                  dns_parse_a6, (dns_query_fn*)cbck, data);
120 }
121
122 struct dns_rr_a6 *
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);
126 }