1 /* MIT License
2 *
3 * Copyright (c) 2023 Brad House
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26
27 #include "ares_setup.h"
28
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
31 #endif
32 #ifdef HAVE_NETDB_H
33 # include <netdb.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
37 #endif
38
39 #include "ares.h"
40 #include "ares_private.h"
41
ares_parse_ns_reply(const unsigned char * abuf,int alen_int,struct hostent ** host)42 int ares_parse_ns_reply(const unsigned char *abuf, int alen_int,
43 struct hostent **host)
44 {
45 ares_status_t status;
46 size_t alen;
47 size_t nscount = 0;
48 struct hostent *hostent = NULL;
49 const char *hostname = NULL;
50 ares_dns_record_t *dnsrec = NULL;
51 size_t i;
52 size_t ancount;
53
54 *host = NULL;
55
56 if (alen_int < 0) {
57 return ARES_EBADRESP;
58 }
59
60 alen = (size_t)alen_int;
61
62 status = ares_dns_parse(abuf, alen, 0, &dnsrec);
63 if (status != ARES_SUCCESS) {
64 goto done;
65 }
66
67 ancount = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
68 if (ancount == 0) {
69 status = ARES_ENODATA;
70 goto done;
71 }
72
73 /* Response structure */
74 hostent = ares_malloc(sizeof(*hostent));
75 if (hostent == NULL) {
76 status = ARES_ENOMEM;
77 goto done;
78 }
79
80 memset(hostent, 0, sizeof(*hostent));
81
82 hostent->h_addr_list = ares_malloc(sizeof(*hostent->h_addr_list));
83 if (hostent->h_addr_list == NULL) {
84 status = ARES_ENOMEM;
85 goto done;
86 }
87 hostent->h_addr_list[0] = NULL;
88 hostent->h_addrtype = AF_INET;
89 hostent->h_length = sizeof(struct in_addr);
90
91 /* Fill in hostname */
92 status = ares_dns_record_query_get(dnsrec, 0, &hostname, NULL, NULL);
93 if (status != ARES_SUCCESS) {
94 goto done;
95 }
96 hostent->h_name = ares_strdup(hostname);
97 if (hostent->h_name == NULL) {
98 status = ARES_ENOMEM;
99 goto done;
100 }
101
102 /* Preallocate the maximum number + 1 */
103 hostent->h_aliases = ares_malloc((ancount + 1) * sizeof(*hostent->h_aliases));
104 if (hostent->h_aliases == NULL) {
105 status = ARES_ENOMEM;
106 goto done;
107 }
108 memset(hostent->h_aliases, 0, (ancount + 1) * sizeof(*hostent->h_aliases));
109
110 for (i = 0; i < ancount; i++) {
111 const ares_dns_rr_t *rr =
112 ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
113
114 if (rr == NULL) {
115 /* Shouldn't be possible */
116 status = ARES_EBADRESP;
117 goto done;
118 }
119
120 if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN ||
121 ares_dns_rr_get_type(rr) != ARES_REC_TYPE_NS) {
122 continue;
123 }
124
125 hostname = ares_dns_rr_get_str(rr, ARES_RR_NS_NSDNAME);
126 if (hostname == NULL) {
127 status = ARES_EBADRESP;
128 goto done;
129 }
130
131 hostent->h_aliases[nscount] = ares_strdup(hostname);
132 if (hostent->h_aliases[nscount] == NULL) {
133 status = ARES_ENOMEM;
134 goto done;
135 }
136 nscount++;
137 }
138
139 if (nscount == 0) {
140 status = ARES_ENODATA;
141 } else {
142 status = ARES_SUCCESS;
143 }
144
145 done:
146 if (status != ARES_SUCCESS) {
147 ares_free_hostent(hostent);
148 /* Compatibility */
149 if (status == ARES_EBADNAME) {
150 status = ARES_EBADRESP;
151 }
152 } else {
153 *host = hostent;
154 }
155 ares_dns_record_destroy(dnsrec);
156 return (int)status;
157 }
158