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