1
/* $Id: ares_data.c,v 1.2 2009-11-20 09:06:33 yangtse Exp $ */
3
/* Copyright (C) 2009 by Daniel Stenberg
5
* Permission to use, copy, modify, and distribute this
6
* software and its documentation for any purpose and without
7
* fee is hereby granted, provided that the above copyright
8
* notice appear in all copies and that both that copyright
9
* notice and this permission notice appear in supporting
10
* documentation, and that the name of M.I.T. not be used in
11
* advertising or publicity pertaining to distribution of the
12
* software without specific, written prior permission.
13
* M.I.T. makes no representations about the suitability of
14
* this software for any purpose. It is provided "as is"
15
* without express or implied warranty.
23
#include "ares_data.h"
26
** ares_free_data() - c-ares external API function.
28
** This function must be used by the application to free data memory that
29
** has been internally allocated by some c-ares function and for which a
30
** pointer has already been returned to the calling application. The list
31
** of c-ares functions returning pointers that must be free'ed using this
34
** ares_parse_srv_reply()
35
** ares_parse_txt_reply()
38
void _ares_free_data(void *dataptr)
40
struct ares_data *ptr;
45
ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data));
47
if (ptr->mark != ARES_DATATYPE_MARK)
52
case ARES_DATATYPE_SRV_REPLY:
54
if (ptr->data.srv_reply.next)
55
_ares_free_data(ptr->data.srv_reply.next);
56
if (ptr->data.srv_reply.host)
57
free(ptr->data.srv_reply.host);
60
case ARES_DATATYPE_TXT_REPLY:
62
if (ptr->data.txt_reply.next)
63
_ares_free_data(ptr->data.txt_reply.next);
64
if (ptr->data.txt_reply.txt)
65
free(ptr->data.txt_reply.txt);
77
** ares_malloc_data() - c-ares internal helper function.
79
** This function allocates memory for a c-ares private ares_data struct
80
** for the specified ares_datatype, initializes c-ares private fields
81
** and zero initializes those which later might be used from the public
82
** API. It returns an interior pointer which can be passed by c-ares
83
** functions to the calling application, and that must be free'ed using
84
** c-ares external API function ares_free_data().
87
void *_ares_malloc_data(ares_datatype type)
89
struct ares_data *ptr;
91
ptr = malloc(sizeof(struct ares_data));
97
case ARES_DATATYPE_SRV_REPLY:
98
ptr->data.srv_reply.next = NULL;
99
ptr->data.srv_reply.host = NULL;
100
ptr->data.srv_reply.priority = 0;
101
ptr->data.srv_reply.weight = 0;
102
ptr->data.srv_reply.port = 0;
105
case ARES_DATATYPE_TXT_REPLY:
106
ptr->data.txt_reply.next = NULL;
107
ptr->data.txt_reply.txt = NULL;
108
ptr->data.txt_reply.length = 0;
116
ptr->mark = ARES_DATATYPE_MARK;
124
** ares_get_datatype() - c-ares internal helper function.
126
** This function returns the ares_datatype of the data stored in a
127
** private ares_data struct when given the public API pointer.
130
ares_datatype ares_get_datatype(void * dataptr)
132
struct ares_data *ptr;
134
ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data));
136
if (ptr->mark == ARES_DATATYPE_MARK)
139
return ARES_DATATYPE_UNKNOWN;