1 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
16 static const char rcsid[] = "$Id: ares_send.c,v 1.3 1999/10/23 19:28:14 danw Exp $";
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
26 #include "ares_private.h"
28 void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen,
29 ares_callback callback, void *arg)
35 /* Verify that the query is at least long enough to hold the header. */
36 if (qlen < HFIXEDSZ || qlen >= (1 << 16))
38 callback(arg, ARES_EBADQUERY, NULL, 0);
42 /* Allocate space for query and allocated fields. */
43 query = malloc(sizeof(struct query));
46 callback(arg, ARES_ENOMEM, NULL, 0);
49 query->tcpbuf = malloc(qlen + 2);
53 callback(arg, ARES_ENOMEM, NULL, 0);
56 query->skip_server = malloc(channel->nservers * sizeof(int));
57 if (!query->skip_server)
61 callback(arg, ARES_ENOMEM, NULL, 0);
65 /* Compute the query ID. Start with no timeout. */
66 query->qid = DNS_HEADER_QID(qbuf);
69 /* Form the TCP query buffer by prepending qlen (as two
70 * network-order bytes) to qbuf.
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;
77 /* Fill in query arguments. */
78 query->qbuf = query->tcpbuf + 2;
80 query->callback = callback;
83 /* Initialize query status. */
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;
91 /* Chain the query into this channel's query list. */
92 query->next = channel->queries;
93 channel->queries = query;
95 /* Perform the first query action. */
97 ares__send_query(channel, query, now);