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 default:
123 return;
124 }
125
126 ares_free(ptr);
127 dataptr = next_data;
128 }
129 }
130
131
132 /*
133 ** ares_malloc_data() - c-ares internal helper function.
134 **
135 ** This function allocates memory for a c-ares private ares_data struct
136 ** for the specified ares_datatype, initializes c-ares private fields
137 ** and zero initializes those which later might be used from the public
138 ** API. It returns an interior pointer which can be passed by c-ares
139 ** functions to the calling application, and that must be free'ed using
140 ** c-ares external API function ares_free_data().
141 */
142
ares_malloc_data(ares_datatype type)143 void *ares_malloc_data(ares_datatype type)
144 {
145 struct ares_data *ptr;
146
147 ptr = ares_malloc(sizeof(struct ares_data));
148 if (!ptr)
149 return NULL;
150
151 switch (type)
152 {
153 case ARES_DATATYPE_MX_REPLY:
154 ptr->data.mx_reply.next = NULL;
155 ptr->data.mx_reply.host = NULL;
156 ptr->data.mx_reply.priority = 0;
157 break;
158
159 case ARES_DATATYPE_SRV_REPLY:
160 ptr->data.srv_reply.next = NULL;
161 ptr->data.srv_reply.host = NULL;
162 ptr->data.srv_reply.priority = 0;
163 ptr->data.srv_reply.weight = 0;
164 ptr->data.srv_reply.port = 0;
165 break;
166
167 case ARES_DATATYPE_TXT_EXT:
168 ptr->data.txt_ext.record_start = 0;
169 /* FALLTHROUGH */
170
171 case ARES_DATATYPE_TXT_REPLY:
172 ptr->data.txt_reply.next = NULL;
173 ptr->data.txt_reply.txt = NULL;
174 ptr->data.txt_reply.length = 0;
175 break;
176
177 case ARES_DATATYPE_ADDR_NODE:
178 ptr->data.addr_node.next = NULL;
179 ptr->data.addr_node.family = 0;
180 memset(&ptr->data.addr_node.addrV6, 0,
181 sizeof(ptr->data.addr_node.addrV6));
182 break;
183
184 case ARES_DATATYPE_ADDR_PORT_NODE:
185 ptr->data.addr_port_node.next = NULL;
186 ptr->data.addr_port_node.family = 0;
187 ptr->data.addr_port_node.udp_port = 0;
188 ptr->data.addr_port_node.tcp_port = 0;
189 memset(&ptr->data.addr_port_node.addrV6, 0,
190 sizeof(ptr->data.addr_port_node.addrV6));
191 break;
192
193 case ARES_DATATYPE_NAPTR_REPLY:
194 ptr->data.naptr_reply.next = NULL;
195 ptr->data.naptr_reply.flags = NULL;
196 ptr->data.naptr_reply.service = NULL;
197 ptr->data.naptr_reply.regexp = NULL;
198 ptr->data.naptr_reply.replacement = NULL;
199 ptr->data.naptr_reply.order = 0;
200 ptr->data.naptr_reply.preference = 0;
201 break;
202
203 case ARES_DATATYPE_SOA_REPLY:
204 ptr->data.soa_reply.nsname = NULL;
205 ptr->data.soa_reply.hostmaster = NULL;
206 ptr->data.soa_reply.serial = 0;
207 ptr->data.soa_reply.refresh = 0;
208 ptr->data.soa_reply.retry = 0;
209 ptr->data.soa_reply.expire = 0;
210 ptr->data.soa_reply.minttl = 0;
211 break;
212
213 default:
214 ares_free(ptr);
215 return NULL;
216 }
217
218 ptr->mark = ARES_DATATYPE_MARK;
219 ptr->type = type;
220
221 return &ptr->data;
222 }
223