Makefile.am: increment -version-info for 1.10.0 release
[platform/upstream/c-ares.git] / ares_destroy.c
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  * Copyright (C) 2004-2011 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 #include "ares_setup.h"
19
20 #include <assert.h>
21
22 #include "ares.h"
23 #include "ares_private.h"
24
25 void ares_destroy_options(struct ares_options *options)
26 {
27   int i;
28
29   if(options->servers)
30     free(options->servers);
31   for (i = 0; i < options->ndomains; i++)
32     free(options->domains[i]);
33   if(options->domains)
34     free(options->domains);
35   if(options->sortlist)
36     free(options->sortlist);
37   if(options->lookups)
38     free(options->lookups);
39 }
40
41 void ares_destroy(ares_channel channel)
42 {
43   int i;
44   struct query *query;
45   struct list_node* list_head;
46   struct list_node* list_node;
47   
48   if (!channel)
49     return;
50
51   list_head = &(channel->all_queries);
52   for (list_node = list_head->next; list_node != list_head; )
53     {
54       query = list_node->data;
55       list_node = list_node->next;  /* since we're deleting the query */
56       query->callback(query->arg, ARES_EDESTRUCTION, 0, NULL, 0);
57       ares__free_query(query);
58     }
59 #ifndef NDEBUG
60   /* Freeing the query should remove it from all the lists in which it sits,
61    * so all query lists should be empty now.
62    */
63   assert(ares__is_list_empty(&(channel->all_queries)));
64   for (i = 0; i < ARES_QID_TABLE_SIZE; i++)
65     {
66       assert(ares__is_list_empty(&(channel->queries_by_qid[i])));
67     }
68   for (i = 0; i < ARES_TIMEOUT_TABLE_SIZE; i++)
69     {
70       assert(ares__is_list_empty(&(channel->queries_by_timeout[i])));
71     }
72 #endif
73
74   ares__destroy_servers_state(channel);
75
76   if (channel->domains) {
77     for (i = 0; i < channel->ndomains; i++)
78       free(channel->domains[i]);
79     free(channel->domains);
80   }
81
82   if(channel->sortlist)
83     free(channel->sortlist);
84
85   if (channel->lookups)
86     free(channel->lookups);
87
88   free(channel);
89 }
90
91 void ares__destroy_servers_state(ares_channel channel)
92 {
93   struct server_state *server;
94   int i;
95
96   if (channel->servers)
97     {
98       for (i = 0; i < channel->nservers; i++)
99         {
100           server = &channel->servers[i];
101           ares__close_sockets(channel, server);
102           assert(ares__is_list_empty(&server->queries_to_server));
103         }
104       free(channel->servers);
105       channel->servers = NULL;
106     }
107   channel->nservers = -1;
108 }