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