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