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