Blame ares_getsock.c

Packit 514978
Packit 514978
/* Copyright (C) 2005 - 2010, Daniel Stenberg
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this software and its
Packit 514978
 * documentation for any purpose and without fee is hereby granted, provided
Packit 514978
 * that the above copyright notice appear in all copies and that both that
Packit 514978
 * copyright notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in advertising or
Packit 514978
 * publicity pertaining to distribution of the software without specific,
Packit 514978
 * written prior permission.  M.I.T. makes no representations about the
Packit 514978
 * suitability of this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
int ares_getsock(ares_channel channel,
Packit 514978
                 ares_socket_t *socks,
Packit 514978
                 int numsocks) /* size of the 'socks' array */
Packit 514978
{
Packit 514978
  struct server_state *server;
Packit 514978
  int i;
Packit 514978
  int sockindex=0;
Packit 514978
  int bitmap = 0;
Packit 514978
  unsigned int setbits = 0xffffffff;
Packit 514978
Packit 514978
  /* Are there any active queries? */
Packit 514978
  int active_queries = !ares__is_list_empty(&(channel->all_queries));
Packit 514978
Packit 514978
  for (i = 0; i < channel->nservers; i++)
Packit 514978
    {
Packit 514978
      server = &channel->servers[i];
Packit 514978
      /* We only need to register interest in UDP sockets if we have
Packit 514978
       * outstanding queries.
Packit 514978
       */
Packit 514978
      if (active_queries && server->udp_socket != ARES_SOCKET_BAD)
Packit 514978
        {
Packit 514978
          if(sockindex >= numsocks || sockindex >= ARES_GETSOCK_MAXNUM)
Packit 514978
            break;
Packit 514978
          socks[sockindex] = server->udp_socket;
Packit 514978
          bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
Packit 514978
          sockindex++;
Packit 514978
        }
Packit 514978
      /* We always register for TCP events, because we want to know
Packit 514978
       * when the other side closes the connection, so we don't waste
Packit 514978
       * time trying to use a broken connection.
Packit 514978
       */
Packit 514978
      if (server->tcp_socket != ARES_SOCKET_BAD)
Packit 514978
       {
Packit 514978
         if(sockindex >= numsocks || sockindex >= ARES_GETSOCK_MAXNUM)
Packit 514978
           break;
Packit 514978
         socks[sockindex] = server->tcp_socket;
Packit 514978
         bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
Packit 514978
Packit 514978
         if (server->qhead && active_queries)
Packit 514978
           /* then the tcp socket is also writable! */
Packit 514978
           bitmap |= ARES_GETSOCK_WRITABLE(setbits, sockindex);
Packit 514978
Packit 514978
         sockindex++;
Packit 514978
       }
Packit 514978
    }
Packit 514978
  return bitmap;
Packit 514978
}