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_private.h"
28
29 /*
30 * ares_cancel() cancels all ongoing requests/resolves that might be going on
31 * on the given channel. It does NOT kill the channel, use ares_destroy() for
32 * that.
33 */
ares_cancel(ares_channel_t * channel)34 void ares_cancel(ares_channel_t *channel)
35 {
36 if (channel == NULL) {
37 return;
38 }
39
40 ares_channel_lock(channel);
41
42 if (ares_llist_len(channel->all_queries) > 0) {
43 ares_llist_node_t *node = NULL;
44 ares_llist_node_t *next = NULL;
45
46 /* Swap list heads, so that only those queries which were present on entry
47 * into this function are cancelled. New queries added by callbacks of
48 * queries being cancelled will not be cancelled themselves.
49 */
50 ares_llist_t *list_copy = channel->all_queries;
51 channel->all_queries = ares_llist_create(NULL);
52
53 /* Out of memory, this function doesn't return a result code though so we
54 * can't report to caller */
55 if (channel->all_queries == NULL) {
56 channel->all_queries = list_copy; /* LCOV_EXCL_LINE: OutOfMemory */
57 goto done; /* LCOV_EXCL_LINE: OutOfMemory */
58 }
59
60 node = ares_llist_node_first(list_copy);
61 while (node != NULL) {
62 ares_query_t *query;
63
64 /* Cache next since this node is being deleted */
65 next = ares_llist_node_next(node);
66
67 query = ares_llist_node_claim(node);
68 query->node_all_queries = NULL;
69
70 /* NOTE: its possible this may enqueue new queries */
71 query->callback(query->arg, ARES_ECANCELLED, 0, NULL);
72 ares_free_query(query);
73
74 node = next;
75 }
76
77 ares_llist_destroy(list_copy);
78 }
79
80 /* See if the connections should be cleaned up */
81 ares_check_cleanup_conns(channel);
82
83 ares_queue_notify_empty(channel);
84
85 done:
86 ares_channel_unlock(channel);
87 }
88