ares_destroy.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright 1998 by the Massachusetts Institute of Technology.
  2. * Copyright (C) 2004-2011 by Daniel Stenberg
  3. *
  4. * Permission to use, copy, modify, and distribute this
  5. * software and its documentation for any purpose and without
  6. * fee is hereby granted, provided that the above copyright
  7. * notice appear in all copies and that both that copyright
  8. * notice and this permission notice appear in supporting
  9. * documentation, and that the name of M.I.T. not be used in
  10. * advertising or publicity pertaining to distribution of the
  11. * software without specific, written prior permission.
  12. * M.I.T. makes no representations about the suitability of
  13. * this software for any purpose. It is provided "as is"
  14. * without express or implied warranty.
  15. */
  16. #include "ares_setup.h"
  17. #include <assert.h>
  18. #include "ares.h"
  19. #include "ares_private.h"
  20. void ares_destroy_options(struct ares_options *options)
  21. {
  22. int i;
  23. if(options->servers)
  24. ares_free(options->servers);
  25. for (i = 0; i < options->ndomains; i++)
  26. ares_free(options->domains[i]);
  27. if(options->domains)
  28. ares_free(options->domains);
  29. if(options->sortlist)
  30. ares_free(options->sortlist);
  31. if(options->lookups)
  32. ares_free(options->lookups);
  33. if(options->resolvconf_path)
  34. ares_free(options->resolvconf_path);
  35. }
  36. void ares_destroy(ares_channel channel)
  37. {
  38. int i;
  39. struct query *query;
  40. struct list_node* list_head;
  41. struct list_node* list_node;
  42. if (!channel)
  43. return;
  44. list_head = &(channel->all_queries);
  45. for (list_node = list_head->next; list_node != list_head; )
  46. {
  47. query = list_node->data;
  48. list_node = list_node->next; /* since we're deleting the query */
  49. query->callback(query->arg, ARES_EDESTRUCTION, 0, NULL, 0);
  50. ares__free_query(query);
  51. }
  52. #ifndef NDEBUG
  53. /* Freeing the query should remove it from all the lists in which it sits,
  54. * so all query lists should be empty now.
  55. */
  56. assert(ares__is_list_empty(&(channel->all_queries)));
  57. for (i = 0; i < ARES_QID_TABLE_SIZE; i++)
  58. {
  59. assert(ares__is_list_empty(&(channel->queries_by_qid[i])));
  60. }
  61. for (i = 0; i < ARES_TIMEOUT_TABLE_SIZE; i++)
  62. {
  63. assert(ares__is_list_empty(&(channel->queries_by_timeout[i])));
  64. }
  65. #endif
  66. ares__destroy_servers_state(channel);
  67. if (channel->domains) {
  68. for (i = 0; i < channel->ndomains; i++)
  69. ares_free(channel->domains[i]);
  70. ares_free(channel->domains);
  71. }
  72. if(channel->sortlist)
  73. ares_free(channel->sortlist);
  74. if (channel->lookups)
  75. ares_free(channel->lookups);
  76. if (channel->resolvconf_path)
  77. ares_free(channel->resolvconf_path);
  78. ares_free(channel);
  79. }
  80. void ares__destroy_servers_state(ares_channel channel)
  81. {
  82. struct server_state *server;
  83. int i;
  84. if (channel->servers)
  85. {
  86. for (i = 0; i < channel->nservers; i++)
  87. {
  88. server = &channel->servers[i];
  89. ares__close_sockets(channel, server);
  90. assert(ares__is_list_empty(&server->queries_to_server));
  91. }
  92. ares_free(channel->servers);
  93. channel->servers = NULL;
  94. }
  95. channel->nservers = -1;
  96. }