3 /* Copyright (C) 2005 - 2007, Daniel Stenberg
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation for any purpose and without fee is hereby granted, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. M.I.T. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
18 #ifdef HAVE_SYS_TIME_H
23 #include "ares_private.h"
25 int ares_getsock(ares_channel channel,
27 int numsocks) /* size of the 'socks' array */
29 struct server_state *server;
33 unsigned int setbits = 0xffffffff;
35 ares_socket_t *socks = (ares_socket_t *)s;
37 /* Are there any active queries? */
38 int active_queries = !ares__is_list_empty(&(channel->all_queries));
41 (i < channel->nservers) && (sockindex < ARES_GETSOCK_MAXNUM);
44 server = &channel->servers[i];
45 /* We only need to register interest in UDP sockets if we have
46 * outstanding queries.
48 if (active_queries && server->udp_socket != ARES_SOCKET_BAD)
50 if(sockindex >= numsocks)
52 socks[sockindex] = server->udp_socket;
53 bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
56 /* We always register for TCP events, because we want to know
57 * when the other side closes the connection, so we don't waste
58 * time trying to use a broken connection.
60 if (server->tcp_socket != ARES_SOCKET_BAD)
62 if(sockindex >= numsocks)
64 socks[sockindex] = server->tcp_socket;
65 bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
67 if (server->qhead && active_queries)
68 /* then the tcp socket is also writable! */
69 bitmap |= ARES_GETSOCK_WRITABLE(setbits, sockindex);