3 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
20 #if defined(WIN32) && !defined(WATT32)
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
26 #include <arpa/nameser_compat.h>
34 #include "ares_private.h"
36 /* Header format, from RFC 1035:
38 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
39 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
41 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
42 * |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
43 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
45 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
47 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
49 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
51 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
53 * AA, TC, RA, and RCODE are only set in responses. Brief description
54 * of the remaining fields:
55 * ID Identifier to match responses with queries
56 * QR Query (0) or response (1)
57 * Opcode For our purposes, always QUERY
58 * RD Recursion desired
60 * QDCOUNT Number of queries
61 * ANCOUNT Number of answers
62 * NSCOUNT Number of name server records
63 * ARCOUNT Number of additional records
65 * Question format, from RFC 1035:
67 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
68 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
72 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
74 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
76 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
78 * The query name is encoded as a series of labels, each represented
79 * as a one-byte length (maximum 63) followed by the text of the
80 * label. The list is terminated by a label of length zero (which can
81 * be thought of as the root domain).
84 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
85 int rd, unsigned char **buf, int *buflen)
91 /* Set our results early, in case we bail out early with an error. */
95 /* Compute the length of the encoded name so we can check buflen.
96 * Start counting at 1 for the zero-length label at the end. */
98 for (p = name; *p; p++)
100 if (*p == '\\' && *(p + 1) != 0)
104 /* If there are n periods in the name, there are n + 1 labels, and
105 * thus n + 1 length fields, unless the name is empty or ends with a
106 * period. So add 1 unless name is empty or ends with a period.
108 if (*name && *(p - 1) != '.')
111 /* Immediately reject names that are longer than the maximum of 255
112 * bytes that's specified in RFC 1035 ("To simplify implementations,
113 * the total length of a domain name (i.e., label octets and label
114 * length octets) is restricted to 255 octets or less."). We aren't
115 * doing this just to be a stickler about RFCs. For names that are
116 * too long, 'dnscache' closes its TCP connection to us immediately
117 * (when using TCP) and ignores the request when using UDP, and
118 * BIND's named returns ServFail (TCP or UDP). Sending a request
119 * that we know will cause 'dnscache' to close the TCP connection is
120 * painful, since that makes any other outstanding requests on that
121 * connection fail. And sending a UDP request that we know
122 * 'dnscache' will ignore is bad because resources will be tied up
123 * until we time-out the request.
126 return ARES_EBADNAME;
128 *buflen = len + HFIXEDSZ + QFIXEDSZ;
129 *buf = malloc(*buflen);
133 /* Set up the header. */
135 memset(q, 0, HFIXEDSZ);
136 DNS_HEADER_SET_QID(q, id);
137 DNS_HEADER_SET_OPCODE(q, QUERY);
139 DNS_HEADER_SET_RD(q, 1);
142 DNS_HEADER_SET_RD(q, 0);
144 DNS_HEADER_SET_QDCOUNT(q, 1);
146 /* A name of "." is a screw case for the loop below, so adjust it. */
147 if (strcmp(name, ".") == 0)
150 /* Start writing out the name after the header. */
155 return ARES_EBADNAME;
157 /* Count the number of bytes in this label. */
159 for (p = name; *p && *p != '.'; p++)
161 if (*p == '\\' && *(p + 1) != 0)
166 return ARES_EBADNAME;
168 /* Encode the length and copy the data. */
169 *q++ = (unsigned char)len;
170 for (p = name; *p && *p != '.'; p++)
172 if (*p == '\\' && *(p + 1) != 0)
177 /* Go to the next label and repeat, unless we hit the end. */
183 /* Add the zero-length label at the end. */
186 /* Finish off the question with the type and class. */
187 DNS_QUESTION_SET_TYPE(q, type);
188 DNS_QUESTION_SET_CLASS(q, dnsclass);