11cb0ef41Sopenharmony_ci/* MIT License 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Copyright (c) 2009 Daniel Stenberg 41cb0ef41Sopenharmony_ci * 51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights 81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 111cb0ef41Sopenharmony_ci * 121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next 131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 141cb0ef41Sopenharmony_ci * Software. 151cb0ef41Sopenharmony_ci * 161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 221cb0ef41Sopenharmony_ci * SOFTWARE. 231cb0ef41Sopenharmony_ci * 241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT 251cb0ef41Sopenharmony_ci */ 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci#include "ares_setup.h" 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci#include <stddef.h> 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci#include "ares.h" 331cb0ef41Sopenharmony_ci#include "ares_data.h" 341cb0ef41Sopenharmony_ci#include "ares_private.h" 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci/* 371cb0ef41Sopenharmony_ci** ares_free_data() - c-ares external API function. 381cb0ef41Sopenharmony_ci** 391cb0ef41Sopenharmony_ci** This function must be used by the application to free data memory that 401cb0ef41Sopenharmony_ci** has been internally allocated by some c-ares function and for which a 411cb0ef41Sopenharmony_ci** pointer has already been returned to the calling application. The list 421cb0ef41Sopenharmony_ci** of c-ares functions returning pointers that must be free'ed using this 431cb0ef41Sopenharmony_ci** function is: 441cb0ef41Sopenharmony_ci** 451cb0ef41Sopenharmony_ci** ares_get_servers() 461cb0ef41Sopenharmony_ci** ares_parse_srv_reply() 471cb0ef41Sopenharmony_ci** ares_parse_txt_reply() 481cb0ef41Sopenharmony_ci*/ 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_civoid ares_free_data(void *dataptr) 511cb0ef41Sopenharmony_ci{ 521cb0ef41Sopenharmony_ci while (dataptr != NULL) { 531cb0ef41Sopenharmony_ci struct ares_data *ptr; 541cb0ef41Sopenharmony_ci void *next_data = NULL; 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci#ifdef __INTEL_COMPILER 571cb0ef41Sopenharmony_ci# pragma warning(push) 581cb0ef41Sopenharmony_ci# pragma warning(disable : 1684) 591cb0ef41Sopenharmony_ci /* 1684: conversion from pointer to same-sized integral type */ 601cb0ef41Sopenharmony_ci#endif 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data)); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci#ifdef __INTEL_COMPILER 651cb0ef41Sopenharmony_ci# pragma warning(pop) 661cb0ef41Sopenharmony_ci#endif 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci if (ptr->mark != ARES_DATATYPE_MARK) { 691cb0ef41Sopenharmony_ci return; 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci switch (ptr->type) { 731cb0ef41Sopenharmony_ci case ARES_DATATYPE_MX_REPLY: 741cb0ef41Sopenharmony_ci next_data = ptr->data.mx_reply.next; 751cb0ef41Sopenharmony_ci ares_free(ptr->data.mx_reply.host); 761cb0ef41Sopenharmony_ci break; 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci case ARES_DATATYPE_SRV_REPLY: 791cb0ef41Sopenharmony_ci next_data = ptr->data.srv_reply.next; 801cb0ef41Sopenharmony_ci ares_free(ptr->data.srv_reply.host); 811cb0ef41Sopenharmony_ci break; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci case ARES_DATATYPE_URI_REPLY: 841cb0ef41Sopenharmony_ci next_data = ptr->data.uri_reply.next; 851cb0ef41Sopenharmony_ci ares_free(ptr->data.uri_reply.uri); 861cb0ef41Sopenharmony_ci break; 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci case ARES_DATATYPE_TXT_REPLY: 891cb0ef41Sopenharmony_ci case ARES_DATATYPE_TXT_EXT: 901cb0ef41Sopenharmony_ci next_data = ptr->data.txt_reply.next; 911cb0ef41Sopenharmony_ci ares_free(ptr->data.txt_reply.txt); 921cb0ef41Sopenharmony_ci break; 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci case ARES_DATATYPE_ADDR_NODE: 951cb0ef41Sopenharmony_ci next_data = ptr->data.addr_node.next; 961cb0ef41Sopenharmony_ci break; 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci case ARES_DATATYPE_ADDR_PORT_NODE: 991cb0ef41Sopenharmony_ci next_data = ptr->data.addr_port_node.next; 1001cb0ef41Sopenharmony_ci break; 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci case ARES_DATATYPE_NAPTR_REPLY: 1031cb0ef41Sopenharmony_ci next_data = ptr->data.naptr_reply.next; 1041cb0ef41Sopenharmony_ci ares_free(ptr->data.naptr_reply.flags); 1051cb0ef41Sopenharmony_ci ares_free(ptr->data.naptr_reply.service); 1061cb0ef41Sopenharmony_ci ares_free(ptr->data.naptr_reply.regexp); 1071cb0ef41Sopenharmony_ci ares_free(ptr->data.naptr_reply.replacement); 1081cb0ef41Sopenharmony_ci break; 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci case ARES_DATATYPE_SOA_REPLY: 1111cb0ef41Sopenharmony_ci ares_free(ptr->data.soa_reply.nsname); 1121cb0ef41Sopenharmony_ci ares_free(ptr->data.soa_reply.hostmaster); 1131cb0ef41Sopenharmony_ci break; 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci case ARES_DATATYPE_CAA_REPLY: 1161cb0ef41Sopenharmony_ci next_data = ptr->data.caa_reply.next; 1171cb0ef41Sopenharmony_ci ares_free(ptr->data.caa_reply.property); 1181cb0ef41Sopenharmony_ci ares_free(ptr->data.caa_reply.value); 1191cb0ef41Sopenharmony_ci break; 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci default: 1221cb0ef41Sopenharmony_ci return; 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci ares_free(ptr); 1261cb0ef41Sopenharmony_ci dataptr = next_data; 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci} 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci/* 1311cb0ef41Sopenharmony_ci** ares_malloc_data() - c-ares internal helper function. 1321cb0ef41Sopenharmony_ci** 1331cb0ef41Sopenharmony_ci** This function allocates memory for a c-ares private ares_data struct 1341cb0ef41Sopenharmony_ci** for the specified ares_datatype, initializes c-ares private fields 1351cb0ef41Sopenharmony_ci** and zero initializes those which later might be used from the public 1361cb0ef41Sopenharmony_ci** API. It returns an interior pointer which can be passed by c-ares 1371cb0ef41Sopenharmony_ci** functions to the calling application, and that must be free'ed using 1381cb0ef41Sopenharmony_ci** c-ares external API function ares_free_data(). 1391cb0ef41Sopenharmony_ci*/ 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_civoid *ares_malloc_data(ares_datatype type) 1421cb0ef41Sopenharmony_ci{ 1431cb0ef41Sopenharmony_ci struct ares_data *ptr; 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci ptr = ares_malloc_zero(sizeof(*ptr)); 1461cb0ef41Sopenharmony_ci if (!ptr) { 1471cb0ef41Sopenharmony_ci return NULL; 1481cb0ef41Sopenharmony_ci } 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci switch (type) { 1511cb0ef41Sopenharmony_ci case ARES_DATATYPE_MX_REPLY: 1521cb0ef41Sopenharmony_ci case ARES_DATATYPE_SRV_REPLY: 1531cb0ef41Sopenharmony_ci case ARES_DATATYPE_URI_REPLY: 1541cb0ef41Sopenharmony_ci case ARES_DATATYPE_TXT_EXT: 1551cb0ef41Sopenharmony_ci case ARES_DATATYPE_TXT_REPLY: 1561cb0ef41Sopenharmony_ci case ARES_DATATYPE_CAA_REPLY: 1571cb0ef41Sopenharmony_ci case ARES_DATATYPE_ADDR_NODE: 1581cb0ef41Sopenharmony_ci case ARES_DATATYPE_ADDR_PORT_NODE: 1591cb0ef41Sopenharmony_ci case ARES_DATATYPE_NAPTR_REPLY: 1601cb0ef41Sopenharmony_ci case ARES_DATATYPE_SOA_REPLY: 1611cb0ef41Sopenharmony_ci break; 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci default: 1641cb0ef41Sopenharmony_ci ares_free(ptr); 1651cb0ef41Sopenharmony_ci return NULL; 1661cb0ef41Sopenharmony_ci } 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci ptr->mark = ARES_DATATYPE_MARK; 1691cb0ef41Sopenharmony_ci ptr->type = type; 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci return &ptr->data; 1721cb0ef41Sopenharmony_ci} 173