]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - libares/ares_send.c
need automake as a build-dep, even though we don't use most of it
[1ts-debian.git] / libares / ares_send.c
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
3  * Permission to use, copy, modify, and distribute this
4  * software and its documentation for any purpose and without
5  * fee is hereby granted, provided that the above copyright
6  * notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting
8  * documentation, and that the name of M.I.T. not be used in
9  * advertising or publicity pertaining to distribution of the
10  * software without specific, written prior permission.
11  * M.I.T. makes no representations about the suitability of
12  * this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  */
15
16 static const char rcsid[] = "$Id: ares_send.c,v 1.3 1999/10/23 19:28:14 danw Exp $";
17
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include "ares.h"
25 #include "ares_dns.h"
26 #include "ares_private.h"
27
28 void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen,
29                ares_callback callback, void *arg)
30 {
31   struct query *query;
32   int i;
33   time_t now;
34
35   /* Verify that the query is at least long enough to hold the header. */
36   if (qlen < HFIXEDSZ || qlen >= (1 << 16))
37     {
38       callback(arg, ARES_EBADQUERY, NULL, 0);
39       return;
40     }
41
42   /* Allocate space for query and allocated fields. */
43   query = malloc(sizeof(struct query));
44   if (!query)
45     {
46       callback(arg, ARES_ENOMEM, NULL, 0);
47       return;
48     }
49   query->tcpbuf = malloc(qlen + 2);
50   if (!query->tcpbuf)
51     {
52       free(query);
53       callback(arg, ARES_ENOMEM, NULL, 0);
54       return;
55     }
56   query->skip_server = malloc(channel->nservers * sizeof(int));
57   if (!query->skip_server)
58     {
59       free(query->tcpbuf);
60       free(query);
61       callback(arg, ARES_ENOMEM, NULL, 0);
62       return;
63     }
64
65   /* Compute the query ID.  Start with no timeout. */
66   query->qid = DNS_HEADER_QID(qbuf);
67   query->timeout = 0;
68
69   /* Form the TCP query buffer by prepending qlen (as two
70    * network-order bytes) to qbuf.
71    */
72   query->tcpbuf[0] = (qlen >> 8) & 0xff;
73   query->tcpbuf[1] = qlen & 0xff;
74   memcpy(query->tcpbuf + 2, qbuf, qlen);
75   query->tcplen = qlen + 2;
76
77   /* Fill in query arguments. */
78   query->qbuf = query->tcpbuf + 2;
79   query->qlen = qlen;
80   query->callback = callback;
81   query->arg = arg;
82
83   /* Initialize query status. */
84   query->try = 0;
85   query->server = 0;
86   for (i = 0; i < channel->nservers; i++)
87     query->skip_server[i] = 0;
88   query->using_tcp = (channel->flags & ARES_FLAG_USEVC) || qlen > PACKETSZ;
89   query->error_status = ARES_ECONNREFUSED;
90
91   /* Chain the query into this channel's query list. */
92   query->next = channel->queries;
93   channel->queries = query;
94
95   /* Perform the first query action. */
96   time(&now);
97   ares__send_query(channel, query, now);
98 }