include header file only when available
[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 #ifdef HAVE_NETINET_IN_H
24 #include <netinet/in.h>
25 #endif
26 #ifdef HAVE_ARPA_NAMESER_H
27 #include <arpa/nameser.h>
28 #endif
29 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
30 #include <arpa/nameser_compat.h>
31 #endif
32 #endif
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <time.h>
37 #include "ares.h"
38 #include "ares_dns.h"
39 #include "ares_private.h"
40
41 void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen,
42                ares_callback callback, void *arg)
43 {
44   struct query *query;
45   int i;
46   struct timeval now;
47
48   /* Verify that the query is at least long enough to hold the header. */
49   if (qlen < HFIXEDSZ || qlen >= (1 << 16))
50     {
51       callback(arg, ARES_EBADQUERY, 0, NULL, 0);
52       return;
53     }
54
55   /* Allocate space for query and allocated fields. */
56   query = malloc(sizeof(struct query));
57   if (!query)
58     {
59       callback(arg, ARES_ENOMEM, 0, NULL, 0);
60       return;
61     }
62   query->tcpbuf = malloc(qlen + 2);
63   if (!query->tcpbuf)
64     {
65       free(query);
66       callback(arg, ARES_ENOMEM, 0, NULL, 0);
67       return;
68     }
69   query->server_info = malloc(channel->nservers *
70                               sizeof(query->server_info[0]));
71   if (!query->server_info)
72     {
73       free(query->tcpbuf);
74       free(query);
75       callback(arg, ARES_ENOMEM, 0, NULL, 0);
76       return;
77     }
78
79   /* Compute the query ID.  Start with no timeout. */
80   query->qid = (unsigned short)DNS_HEADER_QID(qbuf);
81   query->timeout.tv_sec = 0;
82   query->timeout.tv_usec = 0;
83
84   /* Form the TCP query buffer by prepending qlen (as two
85    * network-order bytes) to qbuf.
86    */
87   query->tcpbuf[0] = (unsigned char)((qlen >> 8) & 0xff);
88   query->tcpbuf[1] = (unsigned char)(qlen & 0xff);
89   memcpy(query->tcpbuf + 2, qbuf, qlen);
90   query->tcplen = qlen + 2;
91
92   /* Fill in query arguments. */
93   query->qbuf = query->tcpbuf + 2;
94   query->qlen = qlen;
95   query->callback = callback;
96   query->arg = arg;
97
98   /* Initialize query status. */
99   query->try = 0;
100   query->server = 0;
101   for (i = 0; i < channel->nservers; i++)
102     {
103       query->server_info[i].skip_server = 0;
104       query->server_info[i].tcp_connection_generation = 0;
105     }
106   query->using_tcp = (channel->flags & ARES_FLAG_USEVC) || qlen > PACKETSZ;
107   query->error_status = ARES_ECONNREFUSED;
108   query->timeouts = 0;
109
110   /* Initialize our list nodes. */
111   ares__init_list_node(&(query->queries_by_qid),     query);
112   ares__init_list_node(&(query->queries_by_timeout), query);
113   ares__init_list_node(&(query->queries_to_server),  query);
114   ares__init_list_node(&(query->all_queries),        query);
115
116   /* Chain the query into the list of all queries. */
117   ares__insert_in_list(&(query->all_queries), &(channel->all_queries));
118   /* Keep track of queries bucketed by qid, so we can process DNS
119    * responses quickly.
120    */
121   ares__insert_in_list(
122     &(query->queries_by_qid),
123     &(channel->queries_by_qid[query->qid % ARES_QID_TABLE_SIZE]));
124
125   /* Perform the first query action. */
126   now = ares__tvnow();
127   ares__send_query(channel, query, &now);
128 }