• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* MIT License
2  *
3  * Copyright (c) 2009 Daniel Stenberg
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * SPDX-License-Identifier: MIT
25  */
26 
27 #include "ares_private.h"
28 #include <stddef.h>
29 #include <assert.h>
30 #include "ares_data.h"
31 
32 /*
33 ** ares_free_data() - c-ares external API function.
34 **
35 ** This function must be used by the application to free data memory that
36 ** has been internally allocated by some c-ares function and for which a
37 ** pointer has already been returned to the calling application. The list
38 ** of c-ares functions returning pointers that must be free'ed using this
39 ** function is:
40 **
41 **   ares_get_servers()
42 **   ares_parse_srv_reply()
43 **   ares_parse_txt_reply()
44 */
45 
ares_free_data(void * dataptr)46 void ares_free_data(void *dataptr)
47 {
48   while (dataptr != NULL) {
49     struct ares_data *ptr;
50     void             *next_data = NULL;
51 
52 #ifdef __INTEL_COMPILER
53 #  pragma warning(push)
54 #  pragma warning(disable : 1684)
55     /* 1684: conversion from pointer to same-sized integral type */
56 #endif
57 
58     ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data));
59 
60 #ifdef __INTEL_COMPILER
61 #  pragma warning(pop)
62 #endif
63 
64     if (ptr->mark != ARES_DATATYPE_MARK) {
65       return;
66     }
67 
68     switch (ptr->type) {
69       case ARES_DATATYPE_MX_REPLY:
70         next_data = ptr->data.mx_reply.next;
71         ares_free(ptr->data.mx_reply.host);
72         break;
73 
74       case ARES_DATATYPE_SRV_REPLY:
75         next_data = ptr->data.srv_reply.next;
76         ares_free(ptr->data.srv_reply.host);
77         break;
78 
79       case ARES_DATATYPE_URI_REPLY:
80         next_data = ptr->data.uri_reply.next;
81         ares_free(ptr->data.uri_reply.uri);
82         break;
83 
84       case ARES_DATATYPE_TXT_REPLY:
85       case ARES_DATATYPE_TXT_EXT:
86         next_data = ptr->data.txt_reply.next;
87         ares_free(ptr->data.txt_reply.txt);
88         break;
89 
90       case ARES_DATATYPE_ADDR_NODE:
91         next_data = ptr->data.addr_node.next;
92         break;
93 
94       case ARES_DATATYPE_ADDR_PORT_NODE:
95         next_data = ptr->data.addr_port_node.next;
96         break;
97 
98       case ARES_DATATYPE_NAPTR_REPLY:
99         next_data = ptr->data.naptr_reply.next;
100         ares_free(ptr->data.naptr_reply.flags);
101         ares_free(ptr->data.naptr_reply.service);
102         ares_free(ptr->data.naptr_reply.regexp);
103         ares_free(ptr->data.naptr_reply.replacement);
104         break;
105 
106       case ARES_DATATYPE_SOA_REPLY:
107         ares_free(ptr->data.soa_reply.nsname);
108         ares_free(ptr->data.soa_reply.hostmaster);
109         break;
110 
111       case ARES_DATATYPE_CAA_REPLY:
112         next_data = ptr->data.caa_reply.next;
113         ares_free(ptr->data.caa_reply.property);
114         ares_free(ptr->data.caa_reply.value);
115         break;
116 
117       default:
118         return;
119     }
120 
121     ares_free(ptr);
122     dataptr = next_data;
123   }
124 }
125 
126 /*
127 ** ares_malloc_data() - c-ares internal helper function.
128 **
129 ** This function allocates memory for a c-ares private ares_data struct
130 ** for the specified ares_datatype, initializes c-ares private fields
131 ** and zero initializes those which later might be used from the public
132 ** API. It returns an interior pointer which can be passed by c-ares
133 ** functions to the calling application, and that must be free'ed using
134 ** c-ares external API function ares_free_data().
135 */
136 
ares_malloc_data(ares_datatype type)137 void *ares_malloc_data(ares_datatype type)
138 {
139   struct ares_data *ptr;
140 
141   ptr = ares_malloc_zero(sizeof(*ptr));
142   if (!ptr) {
143     return NULL;
144   }
145 
146   switch (type) {
147     case ARES_DATATYPE_MX_REPLY:
148     case ARES_DATATYPE_SRV_REPLY:
149     case ARES_DATATYPE_URI_REPLY:
150     case ARES_DATATYPE_TXT_EXT:
151     case ARES_DATATYPE_TXT_REPLY:
152     case ARES_DATATYPE_CAA_REPLY:
153     case ARES_DATATYPE_ADDR_NODE:
154     case ARES_DATATYPE_ADDR_PORT_NODE:
155     case ARES_DATATYPE_NAPTR_REPLY:
156     case ARES_DATATYPE_SOA_REPLY:
157       break;
158 
159     default:
160       ares_free(ptr);
161       return NULL;
162   }
163 
164   ptr->mark = ARES_DATATYPE_MARK;
165   ptr->type = type;
166 
167   return &ptr->data;
168 }
169