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 28#include "ares_setup.h" 29 30#include <stddef.h> 31 32#include "ares.h" 33#include "ares_data.h" 34#include "ares_private.h" 35 36/* 37** ares_free_data() - c-ares external API function. 38** 39** This function must be used by the application to free data memory that 40** has been internally allocated by some c-ares function and for which a 41** pointer has already been returned to the calling application. The list 42** of c-ares functions returning pointers that must be free'ed using this 43** function is: 44** 45** ares_get_servers() 46** ares_parse_srv_reply() 47** ares_parse_txt_reply() 48*/ 49 50void ares_free_data(void *dataptr) 51{ 52 while (dataptr != NULL) { 53 struct ares_data *ptr; 54 void *next_data = NULL; 55 56#ifdef __INTEL_COMPILER 57# pragma warning(push) 58# pragma warning(disable : 1684) 59 /* 1684: conversion from pointer to same-sized integral type */ 60#endif 61 62 ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data)); 63 64#ifdef __INTEL_COMPILER 65# pragma warning(pop) 66#endif 67 68 if (ptr->mark != ARES_DATATYPE_MARK) { 69 return; 70 } 71 72 switch (ptr->type) { 73 case ARES_DATATYPE_MX_REPLY: 74 next_data = ptr->data.mx_reply.next; 75 ares_free(ptr->data.mx_reply.host); 76 break; 77 78 case ARES_DATATYPE_SRV_REPLY: 79 next_data = ptr->data.srv_reply.next; 80 ares_free(ptr->data.srv_reply.host); 81 break; 82 83 case ARES_DATATYPE_URI_REPLY: 84 next_data = ptr->data.uri_reply.next; 85 ares_free(ptr->data.uri_reply.uri); 86 break; 87 88 case ARES_DATATYPE_TXT_REPLY: 89 case ARES_DATATYPE_TXT_EXT: 90 next_data = ptr->data.txt_reply.next; 91 ares_free(ptr->data.txt_reply.txt); 92 break; 93 94 case ARES_DATATYPE_ADDR_NODE: 95 next_data = ptr->data.addr_node.next; 96 break; 97 98 case ARES_DATATYPE_ADDR_PORT_NODE: 99 next_data = ptr->data.addr_port_node.next; 100 break; 101 102 case ARES_DATATYPE_NAPTR_REPLY: 103 next_data = ptr->data.naptr_reply.next; 104 ares_free(ptr->data.naptr_reply.flags); 105 ares_free(ptr->data.naptr_reply.service); 106 ares_free(ptr->data.naptr_reply.regexp); 107 ares_free(ptr->data.naptr_reply.replacement); 108 break; 109 110 case ARES_DATATYPE_SOA_REPLY: 111 ares_free(ptr->data.soa_reply.nsname); 112 ares_free(ptr->data.soa_reply.hostmaster); 113 break; 114 115 case ARES_DATATYPE_CAA_REPLY: 116 next_data = ptr->data.caa_reply.next; 117 ares_free(ptr->data.caa_reply.property); 118 ares_free(ptr->data.caa_reply.value); 119 break; 120 121 default: 122 return; 123 } 124 125 ares_free(ptr); 126 dataptr = next_data; 127 } 128} 129 130/* 131** ares_malloc_data() - c-ares internal helper function. 132** 133** This function allocates memory for a c-ares private ares_data struct 134** for the specified ares_datatype, initializes c-ares private fields 135** and zero initializes those which later might be used from the public 136** API. It returns an interior pointer which can be passed by c-ares 137** functions to the calling application, and that must be free'ed using 138** c-ares external API function ares_free_data(). 139*/ 140 141void *ares_malloc_data(ares_datatype type) 142{ 143 struct ares_data *ptr; 144 145 ptr = ares_malloc_zero(sizeof(*ptr)); 146 if (!ptr) { 147 return NULL; 148 } 149 150 switch (type) { 151 case ARES_DATATYPE_MX_REPLY: 152 case ARES_DATATYPE_SRV_REPLY: 153 case ARES_DATATYPE_URI_REPLY: 154 case ARES_DATATYPE_TXT_EXT: 155 case ARES_DATATYPE_TXT_REPLY: 156 case ARES_DATATYPE_CAA_REPLY: 157 case ARES_DATATYPE_ADDR_NODE: 158 case ARES_DATATYPE_ADDR_PORT_NODE: 159 case ARES_DATATYPE_NAPTR_REPLY: 160 case ARES_DATATYPE_SOA_REPLY: 161 break; 162 163 default: 164 ares_free(ptr); 165 return NULL; 166 } 167 168 ptr->mark = ARES_DATATYPE_MARK; 169 ptr->type = type; 170 171 return &ptr->data; 172} 173