2 /* Copyright (C) 2009-2013 by Daniel Stenberg
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.
18 #include "ares_setup.h"
23 #include "ares_data.h"
24 #include "ares_private.h"
28 ** ares_free_data() - c-ares external API function.
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
37 ** ares_parse_srv_reply()
38 ** ares_parse_txt_reply()
41 void ares_free_data(void *dataptr)
43 struct ares_data *ptr;
48 #ifdef __INTEL_COMPILER
49 # pragma warning(push)
50 # pragma warning(disable:1684)
51 /* 1684: conversion from pointer to same-sized integral type */
54 ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data));
56 #ifdef __INTEL_COMPILER
60 if (ptr->mark != ARES_DATATYPE_MARK)
65 case ARES_DATATYPE_MX_REPLY:
67 if (ptr->data.mx_reply.next)
68 ares_free_data(ptr->data.mx_reply.next);
69 if (ptr->data.mx_reply.host)
70 free(ptr->data.mx_reply.host);
73 case ARES_DATATYPE_SRV_REPLY:
75 if (ptr->data.srv_reply.next)
76 ares_free_data(ptr->data.srv_reply.next);
77 if (ptr->data.srv_reply.host)
78 free(ptr->data.srv_reply.host);
81 case ARES_DATATYPE_TXT_REPLY:
83 if (ptr->data.txt_reply.next)
84 ares_free_data(ptr->data.txt_reply.next);
85 if (ptr->data.txt_reply.txt)
86 free(ptr->data.txt_reply.txt);
89 case ARES_DATATYPE_ADDR_NODE:
91 if (ptr->data.addr_node.next)
92 ares_free_data(ptr->data.addr_node.next);
95 case ARES_DATATYPE_NAPTR_REPLY:
97 if (ptr->data.naptr_reply.next)
98 ares_free_data(ptr->data.naptr_reply.next);
99 if (ptr->data.naptr_reply.flags)
100 free(ptr->data.naptr_reply.flags);
101 if (ptr->data.naptr_reply.service)
102 free(ptr->data.naptr_reply.service);
103 if (ptr->data.naptr_reply.regexp)
104 free(ptr->data.naptr_reply.regexp);
105 if (ptr->data.naptr_reply.replacement)
106 free(ptr->data.naptr_reply.replacement);
109 case ARES_DATATYPE_SOA_REPLY:
110 if (ptr->data.soa_reply.nsname)
111 free(ptr->data.soa_reply.nsname);
112 if (ptr->data.soa_reply.hostmaster)
113 free(ptr->data.soa_reply.hostmaster);
125 ** ares_malloc_data() - c-ares internal helper function.
127 ** This function allocates memory for a c-ares private ares_data struct
128 ** for the specified ares_datatype, initializes c-ares private fields
129 ** and zero initializes those which later might be used from the public
130 ** API. It returns an interior pointer which can be passed by c-ares
131 ** functions to the calling application, and that must be free'ed using
132 ** c-ares external API function ares_free_data().
135 void *ares_malloc_data(ares_datatype type)
137 struct ares_data *ptr;
139 ptr = malloc(sizeof(struct ares_data));
145 case ARES_DATATYPE_MX_REPLY:
146 ptr->data.mx_reply.next = NULL;
147 ptr->data.mx_reply.host = NULL;
148 ptr->data.mx_reply.priority = 0;
151 case ARES_DATATYPE_SRV_REPLY:
152 ptr->data.srv_reply.next = NULL;
153 ptr->data.srv_reply.host = NULL;
154 ptr->data.srv_reply.priority = 0;
155 ptr->data.srv_reply.weight = 0;
156 ptr->data.srv_reply.port = 0;
159 case ARES_DATATYPE_TXT_REPLY:
160 ptr->data.txt_reply.next = NULL;
161 ptr->data.txt_reply.txt = NULL;
162 ptr->data.txt_reply.length = 0;
165 case ARES_DATATYPE_ADDR_NODE:
166 ptr->data.addr_node.next = NULL;
167 ptr->data.addr_node.family = 0;
168 memset(&ptr->data.addr_node.addrV6, 0,
169 sizeof(ptr->data.addr_node.addrV6));
172 case ARES_DATATYPE_NAPTR_REPLY:
173 ptr->data.naptr_reply.next = NULL;
174 ptr->data.naptr_reply.flags = NULL;
175 ptr->data.naptr_reply.service = NULL;
176 ptr->data.naptr_reply.regexp = NULL;
177 ptr->data.naptr_reply.replacement = NULL;
178 ptr->data.naptr_reply.order = 0;
179 ptr->data.naptr_reply.preference = 0;
182 case ARES_DATATYPE_SOA_REPLY:
183 ptr->data.soa_reply.nsname = NULL;
184 ptr->data.soa_reply.hostmaster = NULL;
185 ptr->data.soa_reply.serial = 0;
186 ptr->data.soa_reply.refresh = 0;
187 ptr->data.soa_reply.retry = 0;
188 ptr->data.soa_reply.expire = 0;
189 ptr->data.soa_reply.minttl = 0;
197 ptr->mark = ARES_DATATYPE_MARK;