2 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
17 #include "ares_setup.h"
19 #ifdef HAVE_SYS_SOCKET_H
20 # include <sys/socket.h>
22 #ifdef HAVE_NETINET_IN_H
23 # include <netinet/in.h>
25 #ifdef HAVE_ARPA_NAMESER_H
26 # include <arpa/nameser.h>
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 # include <arpa/nameser_compat.h>
38 #include "ares_private.h"
40 /* Header format, from RFC 1035:
42 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
43 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
45 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
46 * |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
47 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
49 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
51 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
53 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
55 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
57 * AA, TC, RA, and RCODE are only set in responses. Brief description
58 * of the remaining fields:
59 * ID Identifier to match responses with queries
60 * QR Query (0) or response (1)
61 * Opcode For our purposes, always QUERY
62 * RD Recursion desired
64 * QDCOUNT Number of queries
65 * ANCOUNT Number of answers
66 * NSCOUNT Number of name server records
67 * ARCOUNT Number of additional records
69 * Question format, from RFC 1035:
71 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
72 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
76 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
78 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
80 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
82 * The query name is encoded as a series of labels, each represented
83 * as a one-byte length (maximum 63) followed by the text of the
84 * label. The list is terminated by a label of length zero (which can
85 * be thought of as the root domain).
88 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
89 int rd, unsigned char **buf, int *buflen)
95 /* Set our results early, in case we bail out early with an error. */
99 /* Compute the length of the encoded name so we can check buflen.
100 * Start counting at 1 for the zero-length label at the end. */
102 for (p = name; *p; p++)
104 if (*p == '\\' && *(p + 1) != 0)
108 /* If there are n periods in the name, there are n + 1 labels, and
109 * thus n + 1 length fields, unless the name is empty or ends with a
110 * period. So add 1 unless name is empty or ends with a period.
112 if (*name && *(p - 1) != '.')
115 /* Immediately reject names that are longer than the maximum of 255
116 * bytes that's specified in RFC 1035 ("To simplify implementations,
117 * the total length of a domain name (i.e., label octets and label
118 * length octets) is restricted to 255 octets or less."). We aren't
119 * doing this just to be a stickler about RFCs. For names that are
120 * too long, 'dnscache' closes its TCP connection to us immediately
121 * (when using TCP) and ignores the request when using UDP, and
122 * BIND's named returns ServFail (TCP or UDP). Sending a request
123 * that we know will cause 'dnscache' to close the TCP connection is
124 * painful, since that makes any other outstanding requests on that
125 * connection fail. And sending a UDP request that we know
126 * 'dnscache' will ignore is bad because resources will be tied up
127 * until we time-out the request.
130 return ARES_EBADNAME;
132 *buflen = len + HFIXEDSZ + QFIXEDSZ;
133 *buf = malloc(*buflen);
137 /* Set up the header. */
139 memset(q, 0, HFIXEDSZ);
140 DNS_HEADER_SET_QID(q, id);
141 DNS_HEADER_SET_OPCODE(q, QUERY);
143 DNS_HEADER_SET_RD(q, 1);
146 DNS_HEADER_SET_RD(q, 0);
148 DNS_HEADER_SET_QDCOUNT(q, 1);
150 /* A name of "." is a screw case for the loop below, so adjust it. */
151 if (strcmp(name, ".") == 0)
154 /* Start writing out the name after the header. */
159 return ARES_EBADNAME;
161 /* Count the number of bytes in this label. */
163 for (p = name; *p && *p != '.'; p++)
165 if (*p == '\\' && *(p + 1) != 0)
170 return ARES_EBADNAME;
172 /* Encode the length and copy the data. */
173 *q++ = (unsigned char)len;
174 for (p = name; *p && *p != '.'; p++)
176 if (*p == '\\' && *(p + 1) != 0)
181 /* Go to the next label and repeat, unless we hit the end. */
187 /* Add the zero-length label at the end. */
190 /* Finish off the question with the type and class. */
191 DNS_QUESTION_SET_TYPE(q, type);
192 DNS_QUESTION_SET_CLASS(q, dnsclass);