1 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
16 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <arpa/nameser.h>
30 /* Header format, from RFC 1035:
32 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
33 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
35 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
36 * |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
37 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
39 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
41 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
43 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
45 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
47 * AA, TC, RA, and RCODE are only set in responses. Brief description
48 * of the remaining fields:
49 * ID Identifier to match responses with queries
50 * QR Query (0) or response (1)
51 * Opcode For our purposes, always QUERY
52 * RD Recursion desired
54 * QDCOUNT Number of queries
55 * ANCOUNT Number of answers
56 * NSCOUNT Number of name server records
57 * ARCOUNT Number of additional records
59 * Question format, from RFC 1035:
61 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
62 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
66 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
68 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
70 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
72 * The query name is encoded as a series of labels, each represented
73 * as a one-byte length (maximum 63) followed by the text of the
74 * label. The list is terminated by a label of length zero (which can
75 * be thought of as the root domain).
78 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
79 int rd, unsigned char **buf, int *buflen)
85 /* Compute the length of the encoded name so we can check buflen.
86 * Start counting at 1 for the zero-length label at the end. */
88 for (p = name; *p; p++)
90 if (*p == '\\' && *(p + 1) != 0)
94 /* If there are n periods in the name, there are n + 1 labels, and
95 * thus n + 1 length fields, unless the name is empty or ends with a
96 * period. So add 1 unless name is empty or ends with a period.
98 if (*name && *(p - 1) != '.')
101 *buflen = len + HFIXEDSZ + QFIXEDSZ;
102 *buf = malloc(*buflen);
106 /* Set up the header. */
108 memset(q, 0, HFIXEDSZ);
109 DNS_HEADER_SET_QID(q, id);
110 DNS_HEADER_SET_OPCODE(q, QUERY);
111 DNS_HEADER_SET_RD(q, (rd) ? 1 : 0);
112 DNS_HEADER_SET_QDCOUNT(q, 1);
114 /* A name of "." is a screw case for the loop below, so adjust it. */
115 if (strcmp(name, ".") == 0)
118 /* Start writing out the name after the header. */
123 return ARES_EBADNAME;
125 /* Count the number of bytes in this label. */
127 for (p = name; *p && *p != '.'; p++)
129 if (*p == '\\' && *(p + 1) != 0)
134 return ARES_EBADNAME;
136 /* Encode the length and copy the data. */
138 for (p = name; *p && *p != '.'; p++)
140 if (*p == '\\' && *(p + 1) != 0)
145 /* Go to the next label and repeat, unless we hit the end. */
151 /* Add the zero-length label at the end. */
154 /* Finish off the question with the type and class. */
155 DNS_QUESTION_SET_TYPE(q, type);
156 DNS_QUESTION_SET_CLASS(q, dnsclass);