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