1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/* MIT License
*
* Copyright (c) 2009 Daniel Stenberg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* SPDX-License-Identifier: MIT
*/
#include "ares_setup.h"
#include <stddef.h>
#include "ares.h"
#include "ares_data.h"
#include "ares_private.h"
/*
** ares_free_data() - c-ares external API function.
**
** This function must be used by the application to free data memory that
** has been internally allocated by some c-ares function and for which a
** pointer has already been returned to the calling application. The list
** of c-ares functions returning pointers that must be free'ed using this
** function is:
**
** ares_get_servers()
** ares_parse_srv_reply()
** ares_parse_txt_reply()
*/
void ares_free_data(void *dataptr)
{
while (dataptr != NULL) {
struct ares_data *ptr;
void *next_data = NULL;
#ifdef __INTEL_COMPILER
# pragma warning(push)
# pragma warning(disable:1684)
/* 1684: conversion from pointer to same-sized integral type */
#endif
ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data));
#ifdef __INTEL_COMPILER
# pragma warning(pop)
#endif
if (ptr->mark != ARES_DATATYPE_MARK)
return;
switch (ptr->type)
{
case ARES_DATATYPE_MX_REPLY:
if (ptr->data.mx_reply.next)
next_data = ptr->data.mx_reply.next;
if (ptr->data.mx_reply.host)
ares_free(ptr->data.mx_reply.host);
break;
case ARES_DATATYPE_SRV_REPLY:
if (ptr->data.srv_reply.next)
next_data = ptr->data.srv_reply.next;
if (ptr->data.srv_reply.host)
ares_free(ptr->data.srv_reply.host);
break;
case ARES_DATATYPE_URI_REPLY:
if (ptr->data.uri_reply.next)
next_data = ptr->data.uri_reply.next;
if (ptr->data.uri_reply.uri)
ares_free(ptr->data.uri_reply.uri);
break;
case ARES_DATATYPE_TXT_REPLY:
case ARES_DATATYPE_TXT_EXT:
if (ptr->data.txt_reply.next)
next_data = ptr->data.txt_reply.next;
if (ptr->data.txt_reply.txt)
ares_free(ptr->data.txt_reply.txt);
break;
case ARES_DATATYPE_ADDR_NODE:
if (ptr->data.addr_node.next)
next_data = ptr->data.addr_node.next;
break;
case ARES_DATATYPE_ADDR_PORT_NODE:
if (ptr->data.addr_port_node.next)
next_data = ptr->data.addr_port_node.next;
break;
case ARES_DATATYPE_NAPTR_REPLY:
if (ptr->data.naptr_reply.next)
next_data = ptr->data.naptr_reply.next;
if (ptr->data.naptr_reply.flags)
ares_free(ptr->data.naptr_reply.flags);
if (ptr->data.naptr_reply.service)
ares_free(ptr->data.naptr_reply.service);
if (ptr->data.naptr_reply.regexp)
ares_free(ptr->data.naptr_reply.regexp);
if (ptr->data.naptr_reply.replacement)
ares_free(ptr->data.naptr_reply.replacement);
break;
case ARES_DATATYPE_SOA_REPLY:
if (ptr->data.soa_reply.nsname)
ares_free(ptr->data.soa_reply.nsname);
if (ptr->data.soa_reply.hostmaster)
ares_free(ptr->data.soa_reply.hostmaster);
break;
case ARES_DATATYPE_CAA_REPLY:
if (ptr->data.caa_reply.next)
next_data = ptr->data.caa_reply.next;
if (ptr->data.caa_reply.property)
ares_free(ptr->data.caa_reply.property);
if (ptr->data.caa_reply.value)
ares_free(ptr->data.caa_reply.value);
break;
default:
return;
}
ares_free(ptr);
dataptr = next_data;
}
}
/*
** ares_malloc_data() - c-ares internal helper function.
**
** This function allocates memory for a c-ares private ares_data struct
** for the specified ares_datatype, initializes c-ares private fields
** and zero initializes those which later might be used from the public
** API. It returns an interior pointer which can be passed by c-ares
** functions to the calling application, and that must be free'ed using
** c-ares external API function ares_free_data().
*/
void *ares_malloc_data(ares_datatype type)
{
struct ares_data *ptr;
ptr = ares_malloc(sizeof(struct ares_data));
if (!ptr)
return NULL;
switch (type)
{
case ARES_DATATYPE_MX_REPLY:
ptr->data.mx_reply.next = NULL;
ptr->data.mx_reply.host = NULL;
ptr->data.mx_reply.priority = 0;
break;
case ARES_DATATYPE_SRV_REPLY:
ptr->data.srv_reply.next = NULL;
ptr->data.srv_reply.host = NULL;
ptr->data.srv_reply.priority = 0;
ptr->data.srv_reply.weight = 0;
ptr->data.srv_reply.port = 0;
break;
case ARES_DATATYPE_URI_REPLY:
ptr->data.uri_reply.next = NULL;
ptr->data.uri_reply.priority = 0;
ptr->data.uri_reply.weight = 0;
ptr->data.uri_reply.uri = NULL;
ptr->data.uri_reply.ttl = 0;
break;
case ARES_DATATYPE_TXT_EXT:
ptr->data.txt_ext.record_start = 0;
/* FALLTHROUGH */
case ARES_DATATYPE_TXT_REPLY:
ptr->data.txt_reply.next = NULL;
ptr->data.txt_reply.txt = NULL;
ptr->data.txt_reply.length = 0;
break;
case ARES_DATATYPE_CAA_REPLY:
ptr->data.caa_reply.next = NULL;
ptr->data.caa_reply.plength = 0;
ptr->data.caa_reply.property = NULL;
ptr->data.caa_reply.length = 0;
ptr->data.caa_reply.value = NULL;
break;
case ARES_DATATYPE_ADDR_NODE:
ptr->data.addr_node.next = NULL;
ptr->data.addr_node.family = 0;
memset(&ptr->data.addr_node.addrV6, 0,
sizeof(ptr->data.addr_node.addrV6));
break;
case ARES_DATATYPE_ADDR_PORT_NODE:
ptr->data.addr_port_node.next = NULL;
ptr->data.addr_port_node.family = 0;
ptr->data.addr_port_node.udp_port = 0;
ptr->data.addr_port_node.tcp_port = 0;
memset(&ptr->data.addr_port_node.addrV6, 0,
sizeof(ptr->data.addr_port_node.addrV6));
break;
case ARES_DATATYPE_NAPTR_REPLY:
ptr->data.naptr_reply.next = NULL;
ptr->data.naptr_reply.flags = NULL;
ptr->data.naptr_reply.service = NULL;
ptr->data.naptr_reply.regexp = NULL;
ptr->data.naptr_reply.replacement = NULL;
ptr->data.naptr_reply.order = 0;
ptr->data.naptr_reply.preference = 0;
break;
case ARES_DATATYPE_SOA_REPLY:
ptr->data.soa_reply.nsname = NULL;
ptr->data.soa_reply.hostmaster = NULL;
ptr->data.soa_reply.serial = 0;
ptr->data.soa_reply.refresh = 0;
ptr->data.soa_reply.retry = 0;
ptr->data.soa_reply.expire = 0;
ptr->data.soa_reply.minttl = 0;
break;
default:
ares_free(ptr);
return NULL;
}
ptr->mark = ARES_DATATYPE_MARK;
ptr->type = type;
return &ptr->data;
}
|