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 #include "ares_setup.h"
27 #include "ares.h"
28 #include "ares_data.h"
29 #include "ares_private.h"
30
ares_parse_naptr_reply(const unsigned char * abuf,int alen_int,struct ares_naptr_reply ** naptr_out)31 int ares_parse_naptr_reply(const unsigned char *abuf, int alen_int,
32 struct ares_naptr_reply **naptr_out)
33 {
34 ares_status_t status;
35 size_t alen;
36 struct ares_naptr_reply *naptr_head = NULL;
37 struct ares_naptr_reply *naptr_last = NULL;
38 struct ares_naptr_reply *naptr_curr;
39 ares_dns_record_t *dnsrec = NULL;
40 size_t i;
41
42 *naptr_out = NULL;
43
44 if (alen_int < 0) {
45 return ARES_EBADRESP;
46 }
47
48 alen = (size_t)alen_int;
49
50 status = ares_dns_parse(abuf, alen, 0, &dnsrec);
51 if (status != ARES_SUCCESS) {
52 goto done;
53 }
54
55 if (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER) == 0) {
56 status = ARES_ENODATA;
57 goto done;
58 }
59
60 for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
61 const ares_dns_rr_t *rr =
62 ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
63
64 if (rr == NULL) {
65 /* Shouldn't be possible */
66 status = ARES_EBADRESP;
67 goto done;
68 }
69
70 if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN ||
71 ares_dns_rr_get_type(rr) != ARES_REC_TYPE_NAPTR) {
72 continue;
73 }
74
75 /* Allocate storage for this NAPTR answer appending it to the list */
76 naptr_curr = ares_malloc_data(ARES_DATATYPE_NAPTR_REPLY);
77 if (naptr_curr == NULL) {
78 status = ARES_ENOMEM;
79 goto done;
80 }
81
82 /* Link in the record */
83 if (naptr_last) {
84 naptr_last->next = naptr_curr;
85 } else {
86 naptr_head = naptr_curr;
87 }
88 naptr_last = naptr_curr;
89
90 naptr_curr->order = ares_dns_rr_get_u16(rr, ARES_RR_NAPTR_ORDER);
91 naptr_curr->preference = ares_dns_rr_get_u16(rr, ARES_RR_NAPTR_PREFERENCE);
92
93 /* XXX: Why is this unsigned char * ? */
94 naptr_curr->flags = (unsigned char *)ares_strdup(
95 ares_dns_rr_get_str(rr, ARES_RR_NAPTR_FLAGS));
96 if (naptr_curr->flags == NULL) {
97 status = ARES_ENOMEM;
98 goto done;
99 }
100 /* XXX: Why is this unsigned char * ? */
101 naptr_curr->service = (unsigned char *)ares_strdup(
102 ares_dns_rr_get_str(rr, ARES_RR_NAPTR_SERVICES));
103 if (naptr_curr->service == NULL) {
104 status = ARES_ENOMEM;
105 goto done;
106 }
107 /* XXX: Why is this unsigned char * ? */
108 naptr_curr->regexp = (unsigned char *)ares_strdup(
109 ares_dns_rr_get_str(rr, ARES_RR_NAPTR_REGEXP));
110 if (naptr_curr->regexp == NULL) {
111 status = ARES_ENOMEM;
112 goto done;
113 }
114 naptr_curr->replacement =
115 ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_NAPTR_REPLACEMENT));
116 if (naptr_curr->replacement == NULL) {
117 status = ARES_ENOMEM;
118 goto done;
119 }
120 }
121
122 done:
123 /* clean up on error */
124 if (status != ARES_SUCCESS) {
125 if (naptr_head) {
126 ares_free_data(naptr_head);
127 }
128 } else {
129 /* everything looks fine, return the data */
130 *naptr_out = naptr_head;
131 }
132 ares_dns_record_destroy(dnsrec);
133 return (int)status;
134 }
135