Blame ares_cancel.c

Packit 514978
Packit 514978
/* Copyright (C) 2004 by Daniel Stenberg et al
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this software and its
Packit 514978
 * documentation for any purpose and without fee is hereby granted, provided
Packit 514978
 * that the above copyright notice appear in all copies and that both that
Packit 514978
 * copyright notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in advertising or
Packit 514978
 * publicity pertaining to distribution of the software without specific,
Packit 514978
 * written prior permission.  M.I.T. makes no representations about the
Packit 514978
 * suitability of this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
#include <assert.h>
Packit 514978
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
/*
Packit 514978
 * ares_cancel() cancels all ongoing requests/resolves that might be going on
Packit 514978
 * on the given channel. It does NOT kill the channel, use ares_destroy() for
Packit 514978
 * that.
Packit 514978
 */
Packit 514978
void ares_cancel(ares_channel channel)
Packit 514978
{
Packit 514978
  struct query *query;
Packit 514978
  struct list_node list_head_copy;
Packit 514978
  struct list_node* list_head;
Packit 514978
  struct list_node* list_node;
Packit 514978
  int i;
Packit 514978
Packit 514978
  if (!ares__is_list_empty(&(channel->all_queries)))
Packit 514978
  {
Packit 514978
    /* Swap list heads, so that only those queries which were present on entry
Packit 514978
     * into this function are cancelled. New queries added by callbacks of
Packit 514978
     * queries being cancelled will not be cancelled themselves.
Packit 514978
     */
Packit 514978
    list_head = &(channel->all_queries);
Packit 514978
    list_head_copy.prev = list_head->prev;
Packit 514978
    list_head_copy.next = list_head->next;
Packit 514978
    list_head_copy.prev->next = &list_head_copy;
Packit 514978
    list_head_copy.next->prev = &list_head_copy;
Packit 514978
    list_head->prev = list_head;
Packit 514978
    list_head->next = list_head;
Packit 514978
    for (list_node = list_head_copy.next; list_node != &list_head_copy; )
Packit 514978
    {
Packit 514978
      query = list_node->data;
Packit 514978
      list_node = list_node->next;  /* since we're deleting the query */
Packit 514978
      query->callback(query->arg, ARES_ECANCELLED, 0, NULL, 0);
Packit 514978
      ares__free_query(query);
Packit 514978
    }
Packit 514978
  }
Packit 514978
  if (!(channel->flags & ARES_FLAG_STAYOPEN) && ares__is_list_empty(&(channel->all_queries)))
Packit 514978
  {
Packit 514978
    if (channel->servers)
Packit 514978
    {
Packit 514978
      for (i = 0; i < channel->nservers; i++)
Packit 514978
        ares__close_sockets(channel, &channel->servers[i]);
Packit 514978
    }
Packit 514978
  }
Packit 514978
}