Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares_fds.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 #ifdef HAVE_SYS_TIME_H
21 #include <sys/time.h>
22 #endif
23
24 #include "ares.h"
25 #include "ares_private.h"
26
27 int ares_fds(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
28 {
29   struct server_state *server;
30   ares_socket_t nfds;
31   int i;
32
33   /* Are there any active queries? */
34   int active_queries = !ares__is_list_empty(&(channel->all_queries));
35
36   nfds = 0;
37   for (i = 0; i < channel->nservers; i++)
38     {
39       server = &channel->servers[i];
40       /* We only need to register interest in UDP sockets if we have
41        * outstanding queries.
42        */
43       if (active_queries && server->udp_socket != ARES_SOCKET_BAD)
44         {
45           FD_SET(server->udp_socket, read_fds);
46           if (server->udp_socket >= nfds)
47             nfds = server->udp_socket + 1;
48         }
49       /* We always register for TCP events, because we want to know
50        * when the other side closes the connection, so we don't waste
51        * time trying to use a broken connection.
52        */
53       if (server->tcp_socket != ARES_SOCKET_BAD)
54        {
55          FD_SET(server->tcp_socket, read_fds);
56          if (server->qhead)
57            FD_SET(server->tcp_socket, write_fds);
58          if (server->tcp_socket >= nfds)
59            nfds = server->tcp_socket + 1;
60         }
61     }
62   return (int)nfds;
63 }