• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_srv_reply(const unsigned char * abuf,int alen_int,struct ares_srv_reply ** srv_out)32 int ares_parse_srv_reply(const unsigned char *abuf, int alen_int,
33                          struct ares_srv_reply **srv_out)
34 {
35   ares_status_t          status;
36   size_t                 alen;
37   struct ares_srv_reply *srv_head = NULL;
38   struct ares_srv_reply *srv_last = NULL;
39   struct ares_srv_reply *srv_curr;
40   ares_dns_record_t     *dnsrec = NULL;
41   size_t                 i;
42 
43   *srv_out = NULL;
44 
45   if (alen_int < 0) {
46     return ARES_EBADRESP;
47   }
48 
49   alen = (size_t)alen_int;
50 
51   status = ares_dns_parse(abuf, alen, 0, &dnsrec);
52   if (status != ARES_SUCCESS) {
53     goto done;
54   }
55 
56   if (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER) == 0) {
57     status = ARES_ENODATA;
58     goto done;
59   }
60 
61   for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
62     const ares_dns_rr_t *rr =
63       ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
64 
65     if (rr == NULL) {
66       /* Shouldn't be possible */
67       status = ARES_EBADRESP;
68       goto done;
69     }
70 
71     if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN ||
72         ares_dns_rr_get_type(rr) != ARES_REC_TYPE_SRV) {
73       continue;
74     }
75 
76     /* Allocate storage for this SRV answer appending it to the list */
77     srv_curr = ares_malloc_data(ARES_DATATYPE_SRV_REPLY);
78     if (srv_curr == NULL) {
79       status = ARES_ENOMEM;
80       goto done;
81     }
82 
83     /* Link in the record */
84     if (srv_last) {
85       srv_last->next = srv_curr;
86     } else {
87       srv_head = srv_curr;
88     }
89     srv_last = srv_curr;
90 
91 
92     srv_curr->priority = ares_dns_rr_get_u16(rr, ARES_RR_SRV_PRIORITY);
93     srv_curr->weight   = ares_dns_rr_get_u16(rr, ARES_RR_SRV_WEIGHT);
94     srv_curr->port     = ares_dns_rr_get_u16(rr, ARES_RR_SRV_PORT);
95 
96     srv_curr->host = ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_SRV_TARGET));
97 
98     if (srv_curr->host == NULL) {
99       status = ARES_ENOMEM;
100       goto done;
101     }
102   }
103 
104 done:
105   /* clean up on error */
106   if (status != ARES_SUCCESS) {
107     if (srv_head) {
108       ares_free_data(srv_head);
109     }
110   } else {
111     /* everything looks fine, return the data */
112     *srv_out = srv_head;
113   }
114   ares_dns_record_destroy(dnsrec);
115   return (int)status;
116 }
117