Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares_send.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in
11  * advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.
13  * M.I.T. makes no representations about the suitability of
14  * this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  */
17
18 #include "setup.h"
19
20 #if defined(WIN32) && !defined(WATT32)
21 #include "nameser.h"
22 #else
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
26 #include <arpa/nameser_compat.h>
27 #endif
28 #endif
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include "ares.h"
34 #include "ares_dns.h"
35 #include "ares_private.h"
36
37 void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen,
38                ares_callback callback, void *arg)
39 {
40   struct query *query;
41   int i;
42   struct timeval now;
43
44   /* Verify that the query is at least long enough to hold the header. */
45   if (qlen < HFIXEDSZ || qlen >= (1 << 16))
46     {
47       callback(arg, ARES_EBADQUERY, 0, NULL, 0);
48       return;
49     }
50
51   /* Allocate space for query and allocated fields. */
52   query = malloc(sizeof(struct query));
53   if (!query)
54     {
55       callback(arg, ARES_ENOMEM, 0, NULL, 0);
56       return;
57     }
58   query->tcpbuf = malloc(qlen + 2);
59   if (!query->tcpbuf)
60     {
61       free(query);
62       callback(arg, ARES_ENOMEM, 0, NULL, 0);
63       return;
64     }
65   query->server_info = malloc(channel->nservers *
66                               sizeof(query->server_info[0]));
67   if (!query->server_info)
68     {
69       free(query->tcpbuf);
70       free(query);
71       callback(arg, ARES_ENOMEM, 0, NULL, 0);
72       return;
73     }
74
75   /* Compute the query ID.  Start with no timeout. */
76   query->qid = (unsigned short)DNS_HEADER_QID(qbuf);
77   query->timeout.tv_sec = 0;
78   query->timeout.tv_usec = 0;
79
80   /* Form the TCP query buffer by prepending qlen (as two
81    * network-order bytes) to qbuf.
82    */
83   query->tcpbuf[0] = (unsigned char)((qlen >> 8) & 0xff);
84   query->tcpbuf[1] = (unsigned char)(qlen & 0xff);
85   memcpy(query->tcpbuf + 2, qbuf, qlen);
86   query->tcplen = qlen + 2;
87
88   /* Fill in query arguments. */
89   query->qbuf = query->tcpbuf + 2;
90   query->qlen = qlen;
91   query->callback = callback;
92   query->arg = arg;
93
94   /* Initialize query status. */
95   query->try = 0;
96   query->server = 0;
97   for (i = 0; i < channel->nservers; i++)
98     {
99       query->server_info[i].skip_server = 0;
100       query->server_info[i].tcp_connection_generation = 0;
101     }
102   query->using_tcp = (channel->flags & ARES_FLAG_USEVC) || qlen > PACKETSZ;
103   query->error_status = ARES_ECONNREFUSED;
104   query->timeouts = 0;
105
106   /* Initialize our list nodes. */
107   ares__init_list_node(&(query->queries_by_qid),     query);
108   ares__init_list_node(&(query->queries_by_timeout), query);
109   ares__init_list_node(&(query->queries_to_server),  query);
110   ares__init_list_node(&(query->all_queries),        query);
111
112   /* Chain the query into the list of all queries. */
113   ares__insert_in_list(&(query->all_queries), &(channel->all_queries));
114   /* Keep track of queries bucketed by qid, so we can process DNS
115    * responses quickly.
116    */
117   ares__insert_in_list(
118     &(query->queries_by_qid),
119     &(channel->queries_by_qid[query->qid % ARES_QID_TABLE_SIZE]));
120
121   /* Perform the first query action. */
122   now = ares__tvnow();
123   ares__send_query(channel, query, &now);
124 }