1
2 /* Copyright (C) 2009-2013 by Daniel Stenberg
3 *
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in
10 * advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.
12 * M.I.T. makes no representations about the suitability of
13 * this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
15 */
16
17
18 #include "ares_setup.h"
19
20 #include <stddef.h>
21
22 #include "ares.h"
23 #include "ares_data.h"
24 #include "ares_private.h"
25
26
27 /*
28 ** ares_free_data() - c-ares external API function.
29 **
30 ** This function must be used by the application to free data memory that
31 ** has been internally allocated by some c-ares function and for which a
32 ** pointer has already been returned to the calling application. The list
33 ** of c-ares functions returning pointers that must be free'ed using this
34 ** function is:
35 **
36 ** ares_get_servers()
37 ** ares_parse_srv_reply()
38 ** ares_parse_txt_reply()
39 */
40
ares_free_data(void * dataptr)41 void ares_free_data(void *dataptr)
42 {
43 while (dataptr != NULL) {
44 struct ares_data *ptr;
45 void *next_data = NULL;
46
47 #ifdef __INTEL_COMPILER
48 # pragma warning(push)
49 # pragma warning(disable:1684)
50 /* 1684: conversion from pointer to same-sized integral type */
51 #endif
52
53 ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data));
54
55 #ifdef __INTEL_COMPILER
56 # pragma warning(pop)
57 #endif
58
59 if (ptr->mark != ARES_DATATYPE_MARK)
60 return;
61
62 switch (ptr->type)
63 {
64 case ARES_DATATYPE_MX_REPLY:
65
66 if (ptr->data.mx_reply.next)
67 next_data = ptr->data.mx_reply.next;
68 if (ptr->data.mx_reply.host)
69 ares_free(ptr->data.mx_reply.host);
70 break;
71
72 case ARES_DATATYPE_SRV_REPLY:
73
74 if (ptr->data.srv_reply.next)
75 next_data = ptr->data.srv_reply.next;
76 if (ptr->data.srv_reply.host)
77 ares_free(ptr->data.srv_reply.host);
78 break;
79
80 case ARES_DATATYPE_TXT_REPLY:
81 case ARES_DATATYPE_TXT_EXT:
82
83 if (ptr->data.txt_reply.next)
84 next_data = ptr->data.txt_reply.next;
85 if (ptr->data.txt_reply.txt)
86 ares_free(ptr->data.txt_reply.txt);
87 break;
88
89 case ARES_DATATYPE_ADDR_NODE:
90
91 if (ptr->data.addr_node.next)
92 next_data = ptr->data.addr_node.next;
93 break;
94
95 case ARES_DATATYPE_ADDR_PORT_NODE:
96
97 if (ptr->data.addr_port_node.next)
98 next_data = ptr->data.addr_port_node.next;
99 break;
100
101 case ARES_DATATYPE_NAPTR_REPLY:
102
103 if (ptr->data.naptr_reply.next)
104 next_data = ptr->data.naptr_reply.next;
105 if (ptr->data.naptr_reply.flags)
106 ares_free(ptr->data.naptr_reply.flags);
107 if (ptr->data.naptr_reply.service)
108 ares_free(ptr->data.naptr_reply.service);
109 if (ptr->data.naptr_reply.regexp)
110 ares_free(ptr->data.naptr_reply.regexp);
111 if (ptr->data.naptr_reply.replacement)
112 ares_free(ptr->data.naptr_reply.replacement);
113 break;
114
115 case ARES_DATATYPE_SOA_REPLY:
116 if (ptr->data.soa_reply.nsname)
117 ares_free(ptr->data.soa_reply.nsname);
118 if (ptr->data.soa_reply.hostmaster)
119 ares_free(ptr->data.soa_reply.hostmaster);
120 break;
121
122 case ARES_DATATYPE_CAA_REPLY:
123
124 if (ptr->data.caa_reply.next)
125 next_data = ptr->data.caa_reply.next;
126 if (ptr->data.caa_reply.property)
127 ares_free(ptr->data.caa_reply.property);
128 if (ptr->data.caa_reply.value)
129 ares_free(ptr->data.caa_reply.value);
130 break;
131
132 default:
133 return;
134 }
135
136 ares_free(ptr);
137 dataptr = next_data;
138 }
139 }
140
141
142 /*
143 ** ares_malloc_data() - c-ares internal helper function.
144 **
145 ** This function allocates memory for a c-ares private ares_data struct
146 ** for the specified ares_datatype, initializes c-ares private fields
147 ** and zero initializes those which later might be used from the public
148 ** API. It returns an interior pointer which can be passed by c-ares
149 ** functions to the calling application, and that must be free'ed using
150 ** c-ares external API function ares_free_data().
151 */
152
ares_malloc_data(ares_datatype type)153 void *ares_malloc_data(ares_datatype type)
154 {
155 struct ares_data *ptr;
156
157 ptr = ares_malloc(sizeof(struct ares_data));
158 if (!ptr)
159 return NULL;
160
161 switch (type)
162 {
163 case ARES_DATATYPE_MX_REPLY:
164 ptr->data.mx_reply.next = NULL;
165 ptr->data.mx_reply.host = NULL;
166 ptr->data.mx_reply.priority = 0;
167 break;
168
169 case ARES_DATATYPE_SRV_REPLY:
170 ptr->data.srv_reply.next = NULL;
171 ptr->data.srv_reply.host = NULL;
172 ptr->data.srv_reply.priority = 0;
173 ptr->data.srv_reply.weight = 0;
174 ptr->data.srv_reply.port = 0;
175 break;
176
177 case ARES_DATATYPE_TXT_EXT:
178 ptr->data.txt_ext.record_start = 0;
179 /* FALLTHROUGH */
180
181 case ARES_DATATYPE_TXT_REPLY:
182 ptr->data.txt_reply.next = NULL;
183 ptr->data.txt_reply.txt = NULL;
184 ptr->data.txt_reply.length = 0;
185 break;
186
187 case ARES_DATATYPE_CAA_REPLY:
188 ptr->data.caa_reply.next = NULL;
189 ptr->data.caa_reply.plength = 0;
190 ptr->data.caa_reply.property = NULL;
191 ptr->data.caa_reply.length = 0;
192 ptr->data.caa_reply.value = NULL;
193 break;
194
195 case ARES_DATATYPE_ADDR_NODE:
196 ptr->data.addr_node.next = NULL;
197 ptr->data.addr_node.family = 0;
198 memset(&ptr->data.addr_node.addrV6, 0,
199 sizeof(ptr->data.addr_node.addrV6));
200 break;
201
202 case ARES_DATATYPE_ADDR_PORT_NODE:
203 ptr->data.addr_port_node.next = NULL;
204 ptr->data.addr_port_node.family = 0;
205 ptr->data.addr_port_node.udp_port = 0;
206 ptr->data.addr_port_node.tcp_port = 0;
207 memset(&ptr->data.addr_port_node.addrV6, 0,
208 sizeof(ptr->data.addr_port_node.addrV6));
209 break;
210
211 case ARES_DATATYPE_NAPTR_REPLY:
212 ptr->data.naptr_reply.next = NULL;
213 ptr->data.naptr_reply.flags = NULL;
214 ptr->data.naptr_reply.service = NULL;
215 ptr->data.naptr_reply.regexp = NULL;
216 ptr->data.naptr_reply.replacement = NULL;
217 ptr->data.naptr_reply.order = 0;
218 ptr->data.naptr_reply.preference = 0;
219 break;
220
221 case ARES_DATATYPE_SOA_REPLY:
222 ptr->data.soa_reply.nsname = NULL;
223 ptr->data.soa_reply.hostmaster = NULL;
224 ptr->data.soa_reply.serial = 0;
225 ptr->data.soa_reply.refresh = 0;
226 ptr->data.soa_reply.retry = 0;
227 ptr->data.soa_reply.expire = 0;
228 ptr->data.soa_reply.minttl = 0;
229 break;
230
231 default:
232 ares_free(ptr);
233 return NULL;
234 }
235
236 ptr->mark = ARES_DATATYPE_MARK;
237 ptr->type = type;
238
239 return &ptr->data;
240 }
241