Three fixes in one commit (sorry): a) Take care of the tcpbuf if it ends while queued...
[platform/upstream/c-ares.git] / ares_destroy.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
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 #include "setup.h"
19 #include <stdlib.h>
20 #include "ares.h"
21 #include "ares_private.h"
22
23 void ares_destroy_options(struct ares_options *options)
24 {
25   int i;
26
27   free(options->servers);
28   for (i = 0; i < options->ndomains; i++)
29     free(options->domains[i]);
30   free(options->domains);
31   if(options->sortlist)
32     free(options->sortlist);
33   free(options->lookups);
34 }
35
36 void ares_destroy(ares_channel channel)
37 {
38   int i;
39   struct query *query;
40
41   if (!channel)
42     return;
43
44   if (channel->servers) {
45     for (i = 0; i < channel->nservers; i++)
46       ares__close_sockets(channel, &channel->servers[i]);
47     free(channel->servers);
48   }
49
50   if (channel->domains) {
51     for (i = 0; i < channel->ndomains; i++)
52       free(channel->domains[i]);
53     free(channel->domains);
54   }
55
56   if(channel->sortlist)
57     free(channel->sortlist);
58
59   if (channel->lookups)
60     free(channel->lookups);
61
62   while (channel->queries) {
63     query = channel->queries;
64     channel->queries = query->next;
65     query->callback(query->arg, ARES_EDESTRUCTION, NULL, 0);
66     if (query->tcpbuf)
67       free(query->tcpbuf);
68     if (query->server_info)
69       free(query->server_info);
70     free(query);
71   }
72
73   free(channel);
74 }