update copyright year
[platform/upstream/c-ares.git] / ares_getsock.c
1 /* $Id$ */
2
3 /* Copyright (C) 2005 - 2007, Daniel Stenberg
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation for any purpose and without fee is hereby granted, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  M.I.T. makes no representations about the
12  * suitability of this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  */
15
16 #include "setup.h"
17
18 #ifdef HAVE_SYS_TIME_H
19 #include <sys/time.h>
20 #endif
21
22 #include "ares.h"
23 #include "ares_private.h"
24
25 int ares_getsock(ares_channel channel,
26                  int *s,
27                  int numsocks) /* size of the 'socks' array */
28 {
29   struct server_state *server;
30   int i;
31   int sockindex=0;
32   int bitmap = 0;
33   unsigned int setbits = 0xffffffff;
34
35   ares_socket_t *socks = (ares_socket_t *)s;
36
37   /* No queries, no file descriptors. */
38   if (!channel->queries)
39     return 0;
40
41   for (i = 0;
42        (i < channel->nservers) && (sockindex < ARES_GETSOCK_MAXNUM);
43        i++)
44     {
45       server = &channel->servers[i];
46       if (server->udp_socket != ARES_SOCKET_BAD)
47         {
48           if(sockindex >= numsocks)
49             break;
50           socks[sockindex] = server->udp_socket;
51           bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
52           sockindex++;
53         }
54       if (server->tcp_socket != ARES_SOCKET_BAD)
55        {
56          if(sockindex >= numsocks)
57            break;
58          socks[sockindex] = server->tcp_socket;
59          bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
60
61          if (server->qhead)
62            /* then the tcp socket is also writable! */
63            bitmap |= ARES_GETSOCK_WRITABLE(setbits, sockindex);
64
65          sockindex++;
66        }
67     }
68   return bitmap;
69 }