11cb0ef41Sopenharmony_ci/* MIT License
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Copyright (c) 2004 Daniel Stenberg
41cb0ef41Sopenharmony_ci *
51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights
81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
111cb0ef41Sopenharmony_ci *
121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next
131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
141cb0ef41Sopenharmony_ci * Software.
151cb0ef41Sopenharmony_ci *
161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
221cb0ef41Sopenharmony_ci * SOFTWARE.
231cb0ef41Sopenharmony_ci *
241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT
251cb0ef41Sopenharmony_ci */
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#include "ares_setup.h"
281cb0ef41Sopenharmony_ci#include <assert.h>
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci#include "ares.h"
311cb0ef41Sopenharmony_ci#include "ares_private.h"
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci/*
341cb0ef41Sopenharmony_ci * ares_cancel() cancels all ongoing requests/resolves that might be going on
351cb0ef41Sopenharmony_ci * on the given channel. It does NOT kill the channel, use ares_destroy() for
361cb0ef41Sopenharmony_ci * that.
371cb0ef41Sopenharmony_ci */
381cb0ef41Sopenharmony_civoid ares_cancel(ares_channel_t *channel)
391cb0ef41Sopenharmony_ci{
401cb0ef41Sopenharmony_ci  if (channel == NULL) {
411cb0ef41Sopenharmony_ci    return;
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  ares__channel_lock(channel);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  if (ares__llist_len(channel->all_queries) > 0) {
471cb0ef41Sopenharmony_ci    ares__llist_node_t *node = NULL;
481cb0ef41Sopenharmony_ci    ares__llist_node_t *next = NULL;
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    /* Swap list heads, so that only those queries which were present on entry
511cb0ef41Sopenharmony_ci     * into this function are cancelled. New queries added by callbacks of
521cb0ef41Sopenharmony_ci     * queries being cancelled will not be cancelled themselves.
531cb0ef41Sopenharmony_ci     */
541cb0ef41Sopenharmony_ci    ares__llist_t      *list_copy = channel->all_queries;
551cb0ef41Sopenharmony_ci    channel->all_queries          = ares__llist_create(NULL);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    /* Out of memory, this function doesn't return a result code though so we
581cb0ef41Sopenharmony_ci     * can't report to caller */
591cb0ef41Sopenharmony_ci    if (channel->all_queries == NULL) {
601cb0ef41Sopenharmony_ci      channel->all_queries = list_copy;
611cb0ef41Sopenharmony_ci      goto done;
621cb0ef41Sopenharmony_ci    }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    node = ares__llist_node_first(list_copy);
651cb0ef41Sopenharmony_ci    while (node != NULL) {
661cb0ef41Sopenharmony_ci      struct query             *query;
671cb0ef41Sopenharmony_ci      struct server_connection *conn;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci      /* Cache next since this node is being deleted */
701cb0ef41Sopenharmony_ci      next = ares__llist_node_next(node);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci      query                   = ares__llist_node_claim(node);
731cb0ef41Sopenharmony_ci      conn                    = query->conn;
741cb0ef41Sopenharmony_ci      query->node_all_queries = NULL;
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci      /* NOTE: its possible this may enqueue new queries */
771cb0ef41Sopenharmony_ci      query->callback(query->arg, ARES_ECANCELLED, 0, NULL, 0);
781cb0ef41Sopenharmony_ci      ares__free_query(query);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci      /* See if the connection should be cleaned up */
811cb0ef41Sopenharmony_ci      ares__check_cleanup_conn(channel, conn);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci      node = next;
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    ares__llist_destroy(list_copy);
871cb0ef41Sopenharmony_ci  }
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  ares_queue_notify_empty(channel);
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_cidone:
921cb0ef41Sopenharmony_ci  ares__channel_unlock(channel);
931cb0ef41Sopenharmony_ci}
94