ares_cancel.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright (C) 2004 by Daniel Stenberg et al
  2. *
  3. * Permission to use, copy, modify, and distribute this software and its
  4. * documentation for any purpose and without fee is hereby granted, provided
  5. * that the above copyright notice appear in all copies and that both that
  6. * copyright notice and this permission notice appear in supporting
  7. * documentation, and that the name of M.I.T. not be used in advertising or
  8. * publicity pertaining to distribution of the software without specific,
  9. * written prior permission. M.I.T. makes no representations about the
  10. * suitability of this software for any purpose. It is provided "as is"
  11. * without express or implied warranty.
  12. */
  13. #include "ares_setup.h"
  14. #include <assert.h>
  15. #include "ares.h"
  16. #include "ares_private.h"
  17. /*
  18. * ares_cancel() cancels all ongoing requests/resolves that might be going on
  19. * on the given channel. It does NOT kill the channel, use ares_destroy() for
  20. * that.
  21. */
  22. void ares_cancel(ares_channel channel)
  23. {
  24. struct query *query;
  25. struct list_node list_head_copy;
  26. struct list_node* list_head;
  27. struct list_node* list_node;
  28. int i;
  29. if (!ares__is_list_empty(&(channel->all_queries)))
  30. {
  31. /* Swap list heads, so that only those queries which were present on entry
  32. * into this function are cancelled. New queries added by callbacks of
  33. * queries being cancelled will not be cancelled themselves.
  34. */
  35. list_head = &(channel->all_queries);
  36. list_head_copy.prev = list_head->prev;
  37. list_head_copy.next = list_head->next;
  38. list_head_copy.prev->next = &list_head_copy;
  39. list_head_copy.next->prev = &list_head_copy;
  40. list_head->prev = list_head;
  41. list_head->next = list_head;
  42. for (list_node = list_head_copy.next; list_node != &list_head_copy; )
  43. {
  44. query = list_node->data;
  45. list_node = list_node->next; /* since we're deleting the query */
  46. query->callback(query->arg, ARES_ECANCELLED, 0, NULL, 0);
  47. ares__free_query(query);
  48. }
  49. }
  50. if (!(channel->flags & ARES_FLAG_STAYOPEN) && ares__is_list_empty(&(channel->all_queries)))
  51. {
  52. if (channel->servers)
  53. {
  54. for (i = 0; i < channel->nservers; i++)
  55. ares__close_sockets(channel, &channel->servers[i]);
  56. }
  57. }
  58. }