1968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
2968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold/* Copyright (C) 2005 - 2010, Daniel Stenberg
3968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold *
4968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * Permission to use, copy, modify, and distribute this software and its
5968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * documentation for any purpose and without fee is hereby granted, provided
6968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * that the above copyright notice appear in all copies and that both that
7968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * copyright notice and this permission notice appear in supporting
8968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * documentation, and that the name of M.I.T. not be used in advertising or
9968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * publicity pertaining to distribution of the software without specific,
10968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * written prior permission.  M.I.T. makes no representations about the
11968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * suitability of this software for any purpose.  It is provided "as is"
12968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * without express or implied warranty.
13968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold */
14968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
15968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares_setup.h"
16968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
17968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifdef HAVE_SYS_TIME_H
18968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <sys/time.h>
19968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif
20968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
21968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares.h"
22968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares_private.h"
23968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
24968bf19396ad404e89420f5d67900fce13f4186cGilad Arnoldint ares_getsock(ares_channel channel,
25968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold                 ares_socket_t *socks,
26968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold                 int numsocks) /* size of the 'socks' array */
27968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold{
28968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  struct server_state *server;
29968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  int i;
30968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  int sockindex=0;
31968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  int bitmap = 0;
32968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  unsigned int setbits = 0xffffffff;
33968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
34968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  /* Are there any active queries? */
35968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  int active_queries = !ares__is_list_empty(&(channel->all_queries));
36968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
37968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  for (i = 0;
38968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       (i < channel->nservers) && (sockindex < ARES_GETSOCK_MAXNUM);
39968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       i++)
40968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold    {
41968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold      server = &channel->servers[i];
42968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold      /* We only need to register interest in UDP sockets if we have
43968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       * outstanding queries.
44968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       */
45968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold      if (active_queries && server->udp_socket != ARES_SOCKET_BAD)
46968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold        {
47968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold          if(sockindex >= numsocks)
48968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold            break;
49968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold          socks[sockindex] = server->udp_socket;
50968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold          bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
51968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold          sockindex++;
52968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold        }
53968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold      /* We always register for TCP events, because we want to know
54968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       * when the other side closes the connection, so we don't waste
55968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       * time trying to use a broken connection.
56968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       */
57968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold      if (server->tcp_socket != ARES_SOCKET_BAD)
58968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       {
59968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold         if(sockindex >= numsocks)
60968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold           break;
61968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold         socks[sockindex] = server->tcp_socket;
62968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold         bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
63968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
64968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold         if (server->qhead && active_queries)
65968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold           /* then the tcp socket is also writable! */
66968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold           bitmap |= ARES_GETSOCK_WRITABLE(setbits, sockindex);
67968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold
68968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold         sockindex++;
69968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold       }
70968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold    }
71968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold  return bitmap;
72968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold}
73