ares_data.c
Go to the documentation of this file.
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 
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  {
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 
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 
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 
90 
91  if (ptr->data.addr_node.next)
92  next_data = ptr->data.addr_node.next;
93  break;
94 
96 
97  if (ptr->data.addr_port_node.next)
98  next_data = ptr->data.addr_port_node.next;
99  break;
100 
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 
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 
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 
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  {
164  ptr->data.mx_reply.next = NULL;
165  ptr->data.mx_reply.host = NULL;
166  ptr->data.mx_reply.priority = 0;
167  break;
168 
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 
178  ptr->data.txt_ext.record_start = 0;
179  /* FALLTHROUGH */
180 
182  ptr->data.txt_reply.next = NULL;
183  ptr->data.txt_reply.txt = NULL;
184  ptr->data.txt_reply.length = 0;
185  break;
186 
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 
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 
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 
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 
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 }
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
memset
return memset(p, 0, total)
ares.h
ARES_DATATYPE_CAA_REPLY
@ ARES_DATATYPE_CAA_REPLY
Definition: ares_data.h:33
ARES_DATATYPE_TXT_REPLY
@ ARES_DATATYPE_TXT_REPLY
Definition: ares_data.h:20
ares_free_data
void ares_free_data(void *dataptr)
Definition: ares_data.c:41
ares_malloc
void *(* ares_malloc)(size_t size)=default_malloc
Definition: ares_library_init.c:58
ARES_DATATYPE_NAPTR_REPLY
@ ARES_DATATYPE_NAPTR_REPLY
Definition: ares_data.h:24
ares_data.h
ares_data
Definition: ares_data.h:57
ARES_DATATYPE_MX_REPLY
@ ARES_DATATYPE_MX_REPLY
Definition: ares_data.h:23
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
ares_datatype
ares_datatype
Definition: ares_data.h:17
ARES_DATATYPE_MARK
#define ARES_DATATYPE_MARK
Definition: ares_data.h:37
ares_setup.h
ARES_DATATYPE_ADDR_PORT_NODE
@ ARES_DATATYPE_ADDR_PORT_NODE
Definition: ares_data.h:32
ARES_DATATYPE_TXT_EXT
@ ARES_DATATYPE_TXT_EXT
Definition: ares_data.h:21
ares_malloc_data
void * ares_malloc_data(ares_datatype type)
Definition: ares_data.c:153
ares_free
void(* ares_free)(void *ptr)=default_free
Definition: ares_library_init.c:60
ARES_DATATYPE_ADDR_NODE
@ ARES_DATATYPE_ADDR_NODE
Definition: ares_data.h:22
ares_private.h
ARES_DATATYPE_SRV_REPLY
@ ARES_DATATYPE_SRV_REPLY
Definition: ares_data.h:19
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
ARES_DATATYPE_SOA_REPLY
@ ARES_DATATYPE_SOA_REPLY
Definition: ares_data.h:25


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:32