blob: 7f4c52d41dfbf45f80addd9611c8665f7e543b73 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001
2/* Copyright 1998 by the Massachusetts Institute of Technology.
3 *
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.
15 */
16
17#include "ares_setup.h"
18
19#ifdef HAVE_NETINET_IN_H
20# include <netinet/in.h>
21#endif
22#ifdef HAVE_ARPA_NAMESER_H
23# include <arpa/nameser.h>
24#else
25# include "nameser.h"
26#endif
27#ifdef HAVE_ARPA_NAMESER_COMPAT_H
28# include <arpa/nameser_compat.h>
29#endif
30
31#include "ares.h"
32#include "ares_dns.h"
33#include "ares_private.h"
34
35#ifndef T_OPT
36# define T_OPT 41 /* EDNS0 option (meta-RR) */
37#endif
38
39/* Header format, from RFC 1035:
40 * 1 1 1 1 1 1
41 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
42 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
43 * | ID |
44 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
45 * |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
46 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
47 * | QDCOUNT |
48 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
49 * | ANCOUNT |
50 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
51 * | NSCOUNT |
52 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
53 * | ARCOUNT |
54 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
55 *
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
62 * Z Reserved (zero)
63 * QDCOUNT Number of queries
64 * ANCOUNT Number of answers
65 * NSCOUNT Number of name server records
66 * ARCOUNT Number of additional records
67 *
68 * Question format, from RFC 1035:
69 * 1 1 1 1 1 1
70 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
71 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
72 * | |
73 * / QNAME /
74 * / /
75 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
76 * | QTYPE |
77 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
78 * | QCLASS |
79 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
80 *
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).
85 */
86
87int 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)
90{
91 size_t len;
92 unsigned char *q;
93 const char *p;
94 size_t buflen;
95 unsigned char *buf;
96
97 /* Set our results early, in case we bail out early with an error. */
98 *buflenp = 0;
99 *bufp = NULL;
100
101 /* Allocate a memory area for the maximum size this packet might need. +2
102 * is for the length byte and zero termination if no dots or ecscaping is
103 * used.
104 */
105 len = strlen(name) + 2 + HFIXEDSZ + QFIXEDSZ +
106 (max_udp_size ? EDNSFIXEDSZ : 0);
107 buf = ares_malloc(len);
108 if (!buf)
109 return ARES_ENOMEM;
110
111 /* Set up the header. */
112 q = buf;
113 memset(q, 0, HFIXEDSZ);
114 DNS_HEADER_SET_QID(q, id);
115 DNS_HEADER_SET_OPCODE(q, QUERY);
116 if (rd) {
117 DNS_HEADER_SET_RD(q, 1);
118 }
119 else {
120 DNS_HEADER_SET_RD(q, 0);
121 }
122 DNS_HEADER_SET_QDCOUNT(q, 1);
123
124 if (max_udp_size) {
125 DNS_HEADER_SET_ARCOUNT(q, 1);
126 }
127
128 /* A name of "." is a screw case for the loop below, so adjust it. */
129 if (strcmp(name, ".") == 0)
130 name++;
131
132 /* Start writing out the name after the header. */
133 q += HFIXEDSZ;
134 while (*name)
135 {
136 if (*name == '.') {
137 free (buf);
138 return ARES_EBADNAME;
139 }
140
141 /* Count the number of bytes in this label. */
142 len = 0;
143 for (p = name; *p && *p != '.'; p++)
144 {
145 if (*p == '\\' && *(p + 1) != 0)
146 p++;
147 len++;
148 }
149 if (len > MAXLABEL) {
150 free (buf);
151 return ARES_EBADNAME;
152 }
153
154 /* Encode the length and copy the data. */
155 *q++ = (unsigned char)len;
156 for (p = name; *p && *p != '.'; p++)
157 {
158 if (*p == '\\' && *(p + 1) != 0)
159 p++;
160 *q++ = *p;
161 }
162
163 /* Go to the next label and repeat, unless we hit the end. */
164 if (!*p)
165 break;
166 name = p + 1;
167 }
168
169 /* Add the zero-length label at the end. */
170 *q++ = 0;
171
172 /* Finish off the question with the type and class. */
173 DNS_QUESTION_SET_TYPE(q, type);
174 DNS_QUESTION_SET_CLASS(q, dnsclass);
175
176 q += QFIXEDSZ;
177 if (max_udp_size)
178 {
179 memset(q, 0, EDNSFIXEDSZ);
180 q++;
181 DNS_RR_SET_TYPE(q, T_OPT);
182 DNS_RR_SET_CLASS(q, max_udp_size);
183 q += (EDNSFIXEDSZ-1);
184 }
185 buflen = (q - buf);
186
187 /* Reject names that are longer than the maximum of 255 bytes that's
188 * specified in RFC 1035 ("To simplify implementations, the total length of
189 * a domain name (i.e., label octets and label length octets) is restricted
190 * to 255 octets or less."). */
191 if (buflen > (MAXCDNAME + HFIXEDSZ + QFIXEDSZ +
192 (max_udp_size ? EDNSFIXEDSZ : 0))) {
193 free (buf);
194 return ARES_EBADNAME;
195 }
196
197 /* we know this fits in an int at this point */
198 *buflenp = (int) buflen;
199 *bufp = buf;
200
201 return ARES_SUCCESS;
202}