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