• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* MIT License
2  *
3  * Copyright (c) 2004 Daniel Stenberg
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * SPDX-License-Identifier: MIT
25  */
26 
27 #include "ares_setup.h"
28 #include <assert.h>
29 
30 #include "ares.h"
31 #include "ares_private.h"
32 
33 /*
34  * ares_cancel() cancels all ongoing requests/resolves that might be going on
35  * on the given channel. It does NOT kill the channel, use ares_destroy() for
36  * that.
37  */
ares_cancel(ares_channel_t * channel)38 void ares_cancel(ares_channel_t *channel)
39 {
40   if (channel == NULL) {
41     return;
42   }
43 
44   ares__channel_lock(channel);
45 
46   if (ares__llist_len(channel->all_queries) > 0) {
47     ares__llist_node_t *node = NULL;
48     ares__llist_node_t *next = NULL;
49 
50     /* Swap list heads, so that only those queries which were present on entry
51      * into this function are cancelled. New queries added by callbacks of
52      * queries being cancelled will not be cancelled themselves.
53      */
54     ares__llist_t      *list_copy = channel->all_queries;
55     channel->all_queries          = ares__llist_create(NULL);
56 
57     /* Out of memory, this function doesn't return a result code though so we
58      * can't report to caller */
59     if (channel->all_queries == NULL) {
60       channel->all_queries = list_copy;
61       goto done;
62     }
63 
64     node = ares__llist_node_first(list_copy);
65     while (node != NULL) {
66       struct query             *query;
67       struct server_connection *conn;
68 
69       /* Cache next since this node is being deleted */
70       next = ares__llist_node_next(node);
71 
72       query                   = ares__llist_node_claim(node);
73       conn                    = query->conn;
74       query->node_all_queries = NULL;
75 
76       /* NOTE: its possible this may enqueue new queries */
77       query->callback(query->arg, ARES_ECANCELLED, 0, NULL, 0);
78       ares__free_query(query);
79 
80       /* See if the connection should be cleaned up */
81       ares__check_cleanup_conn(channel, conn);
82 
83       node = next;
84     }
85 
86     ares__llist_destroy(list_copy);
87   }
88 
89   ares_queue_notify_empty(channel);
90 
91 done:
92   ares__channel_unlock(channel);
93 }
94