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 *buflen = len + HFIXEDSZ + QFIXEDSZ;
112 *buf = malloc(*buflen);
116 /* Set up the header. */
118 memset(q, 0, HFIXEDSZ);
119 DNS_HEADER_SET_QID(q, id);
120 DNS_HEADER_SET_OPCODE(q, QUERY);
122 DNS_HEADER_SET_RD(q, 1);
125 DNS_HEADER_SET_RD(q, 0);
127 DNS_HEADER_SET_QDCOUNT(q, 1);
129 /* A name of "." is a screw case for the loop below, so adjust it. */
130 if (strcmp(name, ".") == 0)
133 /* Start writing out the name after the header. */
138 return ARES_EBADNAME;
140 /* Count the number of bytes in this label. */
142 for (p = name; *p && *p != '.'; p++)
144 if (*p == '\\' && *(p + 1) != 0)
149 return ARES_EBADNAME;
151 /* Encode the length and copy the data. */
152 *q++ = (unsigned char)len;
153 for (p = name; *p && *p != '.'; p++)
155 if (*p == '\\' && *(p + 1) != 0)
160 /* Go to the next label and repeat, unless we hit the end. */
166 /* Add the zero-length label at the end. */
169 /* Finish off the question with the type and class. */
170 DNS_QUESTION_SET_TYPE(q, type);
171 DNS_QUESTION_SET_CLASS(q, dnsclass);