ares_fds.c 2.0 KB

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