1 
2 /* Copyright (C) 2004 by Daniel Stenberg et al
3  *
4  * Permission to use, copy, modify, and distribute this software and its
5  * documentation for any purpose and without fee is hereby granted, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of M.I.T. not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  M.I.T. makes no representations about the
11  * suitability of this software for any purpose.  It is provided "as is"
12  * without express or implied warranty.
13  */
14 
15 #include "ares_setup.h"
16 #include <assert.h>
17 #include <stdlib.h>
18 #include "ares.h"
19 #include "ares_private.h"
20 
21 /*
22  * ares_cancel() cancels all ongoing requests/resolves that might be going on
23  * on the given channel. It does NOT kill the channel, use ares_destroy() for
24  * that.
25  */
ares_cancel(ares_channel channel)26 void ares_cancel(ares_channel channel)
27 {
28   struct query *query;
29   struct list_node* list_head;
30   struct list_node* list_node;
31   int i;
32 
33   list_head = &(channel->all_queries);
34   for (list_node = list_head->next; list_node != list_head; )
35   {
36     query = list_node->data;
37     list_node = list_node->next;  /* since we're deleting the query */
38     query->callback(query->arg, ARES_ECANCELLED, 0, NULL, 0);
39     ares__free_query(query);
40   }
41 #ifndef NDEBUG
42   /* Freeing the query should remove it from all the lists in which it sits,
43    * so all query lists should be empty now.
44    */
45   assert(ares__is_list_empty(&(channel->all_queries)));
46   for (i = 0; i < ARES_QID_TABLE_SIZE; i++)
47     {
48       assert(ares__is_list_empty(&(channel->queries_by_qid[i])));
49     }
50   for (i = 0; i < ARES_TIMEOUT_TABLE_SIZE; i++)
51     {
52       assert(ares__is_list_empty(&(channel->queries_by_timeout[i])));
53     }
54 #endif
55   if (!(channel->flags & ARES_FLAG_STAYOPEN))
56   {
57     if (channel->servers)
58     {
59       for (i = 0; i < channel->nservers; i++)
60         ares__close_sockets(channel, &channel->servers[i]);
61     }
62   }
63 }
64