ares_getsock.c 2.2 KB

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