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