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 #include "ares.h"
29 #include "ares_data.h"
30 #include "ares_private.h"
31
ares_parse_soa_reply(const unsigned char * abuf,int alen_int,struct ares_soa_reply ** soa_out)32 int ares_parse_soa_reply(const unsigned char *abuf, int alen_int,
33 struct ares_soa_reply **soa_out)
34 {
35 ares_status_t status;
36 size_t alen;
37 struct ares_soa_reply *soa = NULL;
38 ares_dns_record_t *dnsrec = NULL;
39 size_t i;
40
41 *soa_out = NULL;
42
43 if (alen_int < 0) {
44 return ARES_EBADRESP;
45 }
46
47 alen = (size_t)alen_int;
48
49 status = ares_dns_parse(abuf, alen, 0, &dnsrec);
50 if (status != ARES_SUCCESS) {
51 goto done;
52 }
53
54 if (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER) == 0) {
55 status = ARES_EBADRESP; /* ENODATA might make more sense */
56 goto done;
57 }
58
59 for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
60 const ares_dns_rr_t *rr =
61 ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
62
63 if (rr == NULL) {
64 /* Shouldn't be possible */
65 status = ARES_EBADRESP;
66 goto done;
67 }
68
69 if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN ||
70 ares_dns_rr_get_type(rr) != ARES_REC_TYPE_SOA) {
71 continue;
72 }
73
74 /* allocate result struct */
75 soa = ares_malloc_data(ARES_DATATYPE_SOA_REPLY);
76 if (soa == NULL) {
77 status = ARES_ENOMEM;
78 goto done;
79 }
80
81 soa->serial = ares_dns_rr_get_u32(rr, ARES_RR_SOA_SERIAL);
82 soa->refresh = ares_dns_rr_get_u32(rr, ARES_RR_SOA_REFRESH);
83 soa->retry = ares_dns_rr_get_u32(rr, ARES_RR_SOA_RETRY);
84 soa->expire = ares_dns_rr_get_u32(rr, ARES_RR_SOA_EXPIRE);
85 soa->minttl = ares_dns_rr_get_u32(rr, ARES_RR_SOA_MINIMUM);
86 soa->nsname = ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_SOA_MNAME));
87 if (soa->nsname == NULL) {
88 status = ARES_ENOMEM;
89 goto done;
90 }
91 soa->hostmaster = ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_SOA_RNAME));
92 if (soa->hostmaster == NULL) {
93 status = ARES_ENOMEM;
94 goto done;
95 }
96 break;
97 }
98
99 if (soa == NULL) {
100 status = ARES_EBADRESP;
101 }
102
103 done:
104 /* clean up on error */
105 if (status != ARES_SUCCESS) {
106 ares_free_data(soa);
107 /* Compatibility */
108 if (status == ARES_EBADNAME) {
109 status = ARES_EBADRESP;
110 }
111 } else {
112 /* everything looks fine, return the data */
113 *soa_out = soa;
114 }
115 ares_dns_record_destroy(dnsrec);
116 return (int)status;
117 }
118