1 /* MIT License
2 *
3 * Copyright (c) 1998 Massachusetts Institute of Technology
4 * Copyright (c) 2005 Dominick Meglio
5 * Copyright (c) 2019 Andrew Selivanov
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 * SPDX-License-Identifier: MIT
27 */
28
29 #include "ares_setup.h"
30
31 #ifdef HAVE_NETINET_IN_H
32 # include <netinet/in.h>
33 #endif
34 #ifdef HAVE_NETDB_H
35 # include <netdb.h>
36 #endif
37 #ifdef HAVE_ARPA_INET_H
38 # include <arpa/inet.h>
39 #endif
40
41 #include "ares_nameser.h"
42
43 #ifdef HAVE_STRINGS_H
44 # include <strings.h>
45 #endif
46
47 #ifdef HAVE_LIMITS_H
48 # include <limits.h>
49 #endif
50
51 #include "ares.h"
52 #include "ares_dns.h"
53 #include "ares_inet_net_pton.h"
54 #include "ares_private.h"
55
ares_parse_aaaa_reply(const unsigned char * abuf,int alen,struct hostent ** host,struct ares_addr6ttl * addrttls,int * naddrttls)56 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
57 struct hostent **host, struct ares_addr6ttl *addrttls,
58 int *naddrttls)
59 {
60 struct ares_addrinfo ai;
61 char *question_hostname = NULL;
62 ares_status_t status;
63 size_t req_naddrttls = 0;
64
65 if (alen < 0) {
66 return ARES_EBADRESP;
67 }
68
69 if (naddrttls) {
70 req_naddrttls = (size_t)*naddrttls;
71 *naddrttls = 0;
72 }
73
74 memset(&ai, 0, sizeof(ai));
75
76 status = ares__parse_into_addrinfo(abuf, (size_t)alen, 0, 0, &ai);
77 if (status != ARES_SUCCESS && status != ARES_ENODATA) {
78 goto fail;
79 }
80
81 if (host != NULL) {
82 status = ares__addrinfo2hostent(&ai, AF_INET6, host);
83 if (status != ARES_SUCCESS && status != ARES_ENODATA) {
84 goto fail;
85 }
86 }
87
88 if (addrttls != NULL && req_naddrttls) {
89 size_t temp_naddrttls = 0;
90 ares__addrinfo2addrttl(&ai, AF_INET6, req_naddrttls, NULL, addrttls,
91 &temp_naddrttls);
92 *naddrttls = (int)temp_naddrttls;
93 }
94
95 fail:
96 ares__freeaddrinfo_cnames(ai.cnames);
97 ares__freeaddrinfo_nodes(ai.nodes);
98 ares_free(question_hostname);
99 ares_free(ai.name);
100
101 return (int)status;
102 }
103