ares__close_sockets.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* $Id$ */
  2. /* Copyright 1998 by the Massachusetts Institute of Technology.
  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 "setup.h"
  17. #include <stdlib.h>
  18. #ifdef HAVE_UNISTD_H
  19. #include <unistd.h>
  20. #endif
  21. #include "ares.h"
  22. #include "ares_private.h"
  23. void ares__close_sockets(ares_channel channel, struct server_state *server)
  24. {
  25. struct send_request *sendreq;
  26. /* Free all pending output buffers. */
  27. while (server->qhead)
  28. {
  29. /* Advance server->qhead; pull out query as we go. */
  30. sendreq = server->qhead;
  31. server->qhead = sendreq->next;
  32. if (sendreq->data_storage != NULL)
  33. free(sendreq->data_storage);
  34. free(sendreq);
  35. }
  36. server->qtail = NULL;
  37. /* Reset any existing input buffer. */
  38. if (server->tcp_buffer)
  39. free(server->tcp_buffer);
  40. server->tcp_buffer = NULL;
  41. server->tcp_lenbuf_pos = 0;
  42. /* Reset brokenness */
  43. server->is_broken = 0;
  44. /* Close the TCP and UDP sockets. */
  45. if (server->tcp_socket != ARES_SOCKET_BAD)
  46. {
  47. SOCK_STATE_CALLBACK(channel, server->tcp_socket, 0, 0);
  48. closesocket(server->tcp_socket);
  49. server->tcp_socket = ARES_SOCKET_BAD;
  50. server->tcp_connection_generation = ++channel->tcp_connection_generation;
  51. }
  52. if (server->udp_socket != ARES_SOCKET_BAD)
  53. {
  54. SOCK_STATE_CALLBACK(channel, server->udp_socket, 0, 0);
  55. closesocket(server->udp_socket);
  56. server->udp_socket = ARES_SOCKET_BAD;
  57. }
  58. }