~ubuntu-branches/ubuntu/saucy/sssd/saucy

« back to all changes in this revision

Viewing changes to server/resolv/ares/ares_data.c

  • Committer: Stéphane Graber
  • Date: 2011-06-15 16:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: stgraber@ubuntu.com-20110615162314-rbhoppnpaxfqo5q7
Merge 1.5.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: ares_data.c,v 1.2 2009-11-20 09:06:33 yangtse Exp $ */
2
 
 
3
 
/* Copyright (C) 2009 by Daniel Stenberg
4
 
 *
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.
16
 
 */
17
 
 
18
 
 
19
 
#include <stddef.h>
20
 
#include <stdlib.h>
21
 
 
22
 
#include "ares.h"
23
 
#include "ares_data.h"
24
 
 
25
 
/*
26
 
** ares_free_data() - c-ares external API function.
27
 
**
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
32
 
** function is:
33
 
**
34
 
**   ares_parse_srv_reply()
35
 
**   ares_parse_txt_reply()
36
 
*/
37
 
 
38
 
void _ares_free_data(void *dataptr)
39
 
{
40
 
  struct ares_data *ptr;
41
 
 
42
 
  if (!dataptr)
43
 
    return;
44
 
 
45
 
  ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data));
46
 
 
47
 
  if (ptr->mark != ARES_DATATYPE_MARK)
48
 
    return;
49
 
 
50
 
  switch (ptr->type)
51
 
    {
52
 
      case ARES_DATATYPE_SRV_REPLY:
53
 
 
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);
58
 
        break;
59
 
 
60
 
      case ARES_DATATYPE_TXT_REPLY:
61
 
 
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);
66
 
        break;
67
 
 
68
 
      default:
69
 
        return;
70
 
    }
71
 
 
72
 
  free(ptr);
73
 
}
74
 
 
75
 
 
76
 
/*
77
 
** ares_malloc_data() - c-ares internal helper function.
78
 
**
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().
85
 
*/
86
 
 
87
 
void *_ares_malloc_data(ares_datatype type)
88
 
{
89
 
  struct ares_data *ptr;
90
 
 
91
 
  ptr = malloc(sizeof(struct ares_data));
92
 
  if (!ptr)
93
 
    return NULL;
94
 
 
95
 
  switch (type)
96
 
    {
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;
103
 
        break;
104
 
 
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;
109
 
        break;
110
 
 
111
 
      default:
112
 
        free(ptr);
113
 
        return NULL;
114
 
    }
115
 
 
116
 
  ptr->mark = ARES_DATATYPE_MARK;
117
 
  ptr->type = type;
118
 
 
119
 
  return &ptr->data;
120
 
}
121
 
 
122
 
 
123
 
/*
124
 
** ares_get_datatype() - c-ares internal helper function.
125
 
**
126
 
** This function returns the ares_datatype of the data stored in a
127
 
** private ares_data struct when given the public API pointer.
128
 
*/
129
 
 
130
 
ares_datatype ares_get_datatype(void * dataptr)
131
 
{
132
 
  struct ares_data *ptr;
133
 
 
134
 
  ptr = (void *)((char *)dataptr - offsetof(struct ares_data, data));
135
 
 
136
 
  if (ptr->mark == ARES_DATATYPE_MARK)
137
 
    return ptr->type;
138
 
 
139
 
  return ARES_DATATYPE_UNKNOWN;
140
 
}