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"
17 #ifdef HAVE_SYS_TIME_H
22 #include "ares_private.h"
24 int ares_getsock(ares_channel channel,
26 int numsocks) /* size of the 'socks' array */
28 struct server_state *server;
32 unsigned int setbits = 0xffffffff;
34 /* Are there any active queries? */
35 int active_queries = !ares__is_list_empty(&(channel->all_queries));
38 (i < channel->nservers) && (sockindex < ARES_GETSOCK_MAXNUM);
41 server = &channel->servers[i];
42 /* We only need to register interest in UDP sockets if we have
43 * outstanding queries.
45 if (active_queries && server->udp_socket != ARES_SOCKET_BAD)
47 if(sockindex >= numsocks)
49 socks[sockindex] = server->udp_socket;
50 bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
53 /* We always register for TCP events, because we want to know
54 * when the other side closes the connection, so we don't waste
55 * time trying to use a broken connection.
57 if (server->tcp_socket != ARES_SOCKET_BAD)
59 if(sockindex >= numsocks)
61 socks[sockindex] = server->tcp_socket;
62 bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
64 if (server->qhead && active_queries)
65 /* then the tcp socket is also writable! */
66 bitmap |= ARES_GETSOCK_WRITABLE(setbits, sockindex);