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
ares_destroy_options(struct ares_options * options)25 void ares_destroy_options(struct ares_options *options)
26 {
27 int i;
28
29 if(options->servers)
30 ares_free(options->servers);
31 for (i = 0; i < options->ndomains; i++)
32 ares_free(options->domains[i]);
33 if(options->domains)
34 ares_free(options->domains);
35 if(options->sortlist)
36 ares_free(options->sortlist);
37 if(options->lookups)
38 ares_free(options->lookups);
39 if(options->resolvconf_path)
40 ares_free(options->resolvconf_path);
41 if(options->hosts_path)
42 ares_free(options->hosts_path);
43 }
44
ares_destroy(ares_channel channel)45 void ares_destroy(ares_channel channel)
46 {
47 int i;
48 struct query *query;
49 struct list_node* list_head;
50 struct list_node* list_node;
51
52 if (!channel)
53 return;
54
55 list_head = &(channel->all_queries);
56 for (list_node = list_head->next; list_node != list_head; )
57 {
58 query = list_node->data;
59 list_node = list_node->next; /* since we're deleting the query */
60 query->callback(query->arg, ARES_EDESTRUCTION, 0, NULL, 0);
61 ares__free_query(query);
62 }
63 #ifndef NDEBUG
64 /* Freeing the query should remove it from all the lists in which it sits,
65 * so all query lists should be empty now.
66 */
67 assert(ares__is_list_empty(&(channel->all_queries)));
68 for (i = 0; i < ARES_QID_TABLE_SIZE; i++)
69 {
70 assert(ares__is_list_empty(&(channel->queries_by_qid[i])));
71 }
72 for (i = 0; i < ARES_TIMEOUT_TABLE_SIZE; i++)
73 {
74 assert(ares__is_list_empty(&(channel->queries_by_timeout[i])));
75 }
76 #endif
77
78 ares__destroy_servers_state(channel);
79
80 if (channel->domains) {
81 for (i = 0; i < channel->ndomains; i++)
82 ares_free(channel->domains[i]);
83 ares_free(channel->domains);
84 }
85
86 if(channel->sortlist)
87 ares_free(channel->sortlist);
88
89 if (channel->lookups)
90 ares_free(channel->lookups);
91
92 if (channel->resolvconf_path)
93 ares_free(channel->resolvconf_path);
94
95 if (channel->hosts_path)
96 ares_free(channel->hosts_path);
97
98 if (channel->rand_state)
99 ares__destroy_rand_state(channel->rand_state);
100
101 ares_free(channel);
102 }
103
ares__destroy_servers_state(ares_channel channel)104 void ares__destroy_servers_state(ares_channel channel)
105 {
106 struct server_state *server;
107 int i;
108
109 if (channel->servers)
110 {
111 for (i = 0; i < channel->nservers; i++)
112 {
113 server = &channel->servers[i];
114 ares__close_sockets(channel, server);
115 assert(ares__is_list_empty(&server->queries_to_server));
116 }
117 ares_free(channel->servers);
118 channel->servers = NULL;
119 }
120 channel->nservers = -1;
121 }
122