Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares__close_sockets.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in
11  * advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.
13  * M.I.T. makes no representations about the suitability of
14  * this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  */
17
18 #include "setup.h"
19
20 #include <stdlib.h>
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #include "ares.h"
26 #include "ares_private.h"
27
28 void ares__close_sockets(ares_channel channel, struct server_state *server)
29 {
30   struct send_request *sendreq;
31
32   /* Free all pending output buffers. */
33   while (server->qhead)
34     {
35       /* Advance server->qhead; pull out query as we go. */
36       sendreq = server->qhead;
37       server->qhead = sendreq->next;
38       if (sendreq->data_storage != NULL)
39         free(sendreq->data_storage);
40       free(sendreq);
41     }
42   server->qtail = NULL;
43
44   /* Reset any existing input buffer. */
45   if (server->tcp_buffer)
46     free(server->tcp_buffer);
47   server->tcp_buffer = NULL;
48   server->tcp_lenbuf_pos = 0;
49
50   /* Reset brokenness */
51   server->is_broken = 0;
52
53   /* Close the TCP and UDP sockets. */
54   if (server->tcp_socket != ARES_SOCKET_BAD)
55     {
56       SOCK_STATE_CALLBACK(channel, server->tcp_socket, 0, 0);
57       closesocket(server->tcp_socket);
58       server->tcp_socket = ARES_SOCKET_BAD;
59       server->tcp_connection_generation = ++channel->tcp_connection_generation;
60     }
61   if (server->udp_socket != ARES_SOCKET_BAD)
62     {
63       SOCK_STATE_CALLBACK(channel, server->udp_socket, 0, 0);
64       closesocket(server->udp_socket);
65       server->udp_socket = ARES_SOCKET_BAD;
66     }
67 }