1
2 /* Copyright (C) 2005 - 2010, Daniel Stenberg
3 *
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.
13 */
14
15 #include "ares_setup.h"
16
17 #include "ares.h"
18 #include "ares_private.h"
19
ares_getsock(ares_channel channel,ares_socket_t * socks,int numsocks)20 int ares_getsock(ares_channel channel,
21 ares_socket_t *socks,
22 int numsocks) /* size of the 'socks' array */
23 {
24 struct server_state *server;
25 int i;
26 int sockindex=0;
27 int bitmap = 0;
28 unsigned int setbits = 0xffffffff;
29
30 /* Are there any active queries? */
31 int active_queries = !ares__is_list_empty(&(channel->all_queries));
32
33 for (i = 0; i < channel->nservers; i++)
34 {
35 server = &channel->servers[i];
36 /* We only need to register interest in UDP sockets if we have
37 * outstanding queries.
38 */
39 if (active_queries && server->udp_socket != ARES_SOCKET_BAD)
40 {
41 if(sockindex >= numsocks || sockindex >= ARES_GETSOCK_MAXNUM)
42 break;
43 socks[sockindex] = server->udp_socket;
44 bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
45 sockindex++;
46 }
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.
50 */
51 if (server->tcp_socket != ARES_SOCKET_BAD)
52 {
53 if(sockindex >= numsocks || sockindex >= ARES_GETSOCK_MAXNUM)
54 break;
55 socks[sockindex] = server->tcp_socket;
56 bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
57
58 if (server->qhead && active_queries)
59 /* then the tcp socket is also writable! */
60 bitmap |= ARES_GETSOCK_WRITABLE(setbits, sockindex);
61
62 sockindex++;
63 }
64 }
65 return bitmap;
66 }
67