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_NETINET_IN_H
20 # include <netinet/in.h>
22 #ifdef HAVE_ARPA_NAMESER_H
23 # include <arpa/nameser.h>
27 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
28 # include <arpa/nameser_compat.h>
33 #include "ares_private.h"
36 # define T_OPT 41 /* EDNS0 option (meta-RR) */
39 /* Header format, from RFC 1035:
41 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
42 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
44 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
45 * |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
46 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
48 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
50 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
52 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
54 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
56 * AA, TC, RA, and RCODE are only set in responses. Brief description
57 * of the remaining fields:
58 * ID Identifier to match responses with queries
59 * QR Query (0) or response (1)
60 * Opcode For our purposes, always QUERY
61 * RD Recursion desired
63 * QDCOUNT Number of queries
64 * ANCOUNT Number of answers
65 * NSCOUNT Number of name server records
66 * ARCOUNT Number of additional records
68 * Question format, from RFC 1035:
70 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
71 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
75 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
77 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
79 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
81 * The query name is encoded as a series of labels, each represented
82 * as a one-byte length (maximum 63) followed by the text of the
83 * label. The list is terminated by a label of length zero (which can
84 * be thought of as the root domain).
87 int ares_create_query(const char *name, int dnsclass, int type,
88 unsigned short id, int rd, unsigned char **bufp,
89 int *buflenp, int max_udp_size)
97 /* Per RFC 7686, reject queries for ".onion" domain names with NXDOMAIN. */
98 if (ares__is_onion_domain(name))
99 return ARES_ENOTFOUND;
101 /* Set our results early, in case we bail out early with an error. */
105 /* Allocate a memory area for the maximum size this packet might need. +2
106 * is for the length byte and zero termination if no dots or ecscaping is
109 len = strlen(name) + 2 + HFIXEDSZ + QFIXEDSZ +
110 (max_udp_size ? EDNSFIXEDSZ : 0);
111 buf = ares_malloc(len);
115 /* Set up the header. */
117 memset(q, 0, HFIXEDSZ);
118 DNS_HEADER_SET_QID(q, id);
119 DNS_HEADER_SET_OPCODE(q, QUERY);
121 DNS_HEADER_SET_RD(q, 1);
124 DNS_HEADER_SET_RD(q, 0);
126 DNS_HEADER_SET_QDCOUNT(q, 1);
129 DNS_HEADER_SET_ARCOUNT(q, 1);
132 /* A name of "." is a screw case for the loop below, so adjust it. */
133 if (strcmp(name, ".") == 0)
136 /* Start writing out the name after the header. */
142 return ARES_EBADNAME;
145 /* Count the number of bytes in this label. */
147 for (p = name; *p && *p != '.'; p++)
149 if (*p == '\\' && *(p + 1) != 0)
153 if (len > MAXLABEL) {
155 return ARES_EBADNAME;
158 /* Encode the length and copy the data. */
159 *q++ = (unsigned char)len;
160 for (p = name; *p && *p != '.'; p++)
162 if (*p == '\\' && *(p + 1) != 0)
167 /* Go to the next label and repeat, unless we hit the end. */
173 /* Add the zero-length label at the end. */
176 /* Finish off the question with the type and class. */
177 DNS_QUESTION_SET_TYPE(q, type);
178 DNS_QUESTION_SET_CLASS(q, dnsclass);
183 memset(q, 0, EDNSFIXEDSZ);
185 DNS_RR_SET_TYPE(q, T_OPT);
186 DNS_RR_SET_CLASS(q, max_udp_size);
187 q += (EDNSFIXEDSZ-1);
191 /* Reject names that are longer than the maximum of 255 bytes that's
192 * specified in RFC 1035 ("To simplify implementations, the total length of
193 * a domain name (i.e., label octets and label length octets) is restricted
194 * to 255 octets or less."). */
195 if (buflen > (size_t)(MAXCDNAME + HFIXEDSZ + QFIXEDSZ +
196 (max_udp_size ? EDNSFIXEDSZ : 0))) {
198 return ARES_EBADNAME;
201 /* we know this fits in an int at this point */
202 *buflenp = (int) buflen;