RELEASE-NOTES: two more bug fixes
[platform/upstream/c-ares.git] / ares_cancel.c
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
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  */
26 void ares_cancel(ares_channel channel)
27 {
28   struct query *query;
29   struct list_node list_head_copy;
30   struct list_node* list_head;
31   struct list_node* list_node;
32   int i;
33
34   if (!ares__is_list_empty(&(channel->all_queries)))
35   {
36     /* Swap list heads, so that only those queries which were present on entry
37      * into this function are cancelled. New queries added by callbacks of
38      * queries being cancelled will not be cancelled themselves.
39      */
40     list_head = &(channel->all_queries);
41     list_head_copy.prev = list_head->prev;
42     list_head_copy.next = list_head->next;
43     list_head_copy.prev->next = &list_head_copy;
44     list_head_copy.next->prev = &list_head_copy;
45     list_head->prev = list_head;
46     list_head->next = list_head;
47     for (list_node = list_head_copy.next; list_node != &list_head_copy; )
48     {
49       query = list_node->data;
50       list_node = list_node->next;  /* since we're deleting the query */
51       query->callback(query->arg, ARES_ECANCELLED, 0, NULL, 0);
52       ares__free_query(query);
53     }
54   }
55   if (!(channel->flags & ARES_FLAG_STAYOPEN) && ares__is_list_empty(&(channel->all_queries)))
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 }