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 static const char rcsid[] = "$Id: ares_mkquery.c,v 1.3 2000/09/21 19:15:51 ghudson Exp $";
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
26 /* Header format, from RFC 1035:
28 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
29 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
31 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
32 * |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
33 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
35 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
37 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
39 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
41 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
43 * AA, TC, RA, and RCODE are only set in responses. Brief description
44 * of the remaining fields:
45 * ID Identifier to match responses with queries
46 * QR Query (0) or response (1)
47 * Opcode For our purposes, always QUERY
48 * RD Recursion desired
50 * QDCOUNT Number of queries
51 * ANCOUNT Number of answers
52 * NSCOUNT Number of name server records
53 * ARCOUNT Number of additional records
55 * Question format, from RFC 1035:
57 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
58 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
62 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
64 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
66 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
68 * The query name is encoded as a series of labels, each represented
69 * as a one-byte length (maximum 63) followed by the text of the
70 * label. The list is terminated by a label of length zero (which can
71 * be thought of as the root domain).
74 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
75 int rd, unsigned char **buf, int *buflen)
81 /* Compute the length of the encoded name so we can check buflen.
82 * Start counting at 1 for the zero-length label at the end. */
84 for (p = name; *p; p++)
86 if (*p == '\\' && *(p + 1) != 0)
90 /* If there are n periods in the name, there are n + 1 labels, and
91 * thus n + 1 length fields, unless the name is empty or ends with a
92 * period. So add 1 unless name is empty or ends with a period.
94 if (*name && *(p - 1) != '.')
97 *buflen = len + HFIXEDSZ + QFIXEDSZ;
98 *buf = malloc(*buflen);
102 /* Set up the header. */
104 memset(q, 0, HFIXEDSZ);
105 DNS_HEADER_SET_QID(q, id);
106 DNS_HEADER_SET_OPCODE(q, QUERY);
107 DNS_HEADER_SET_RD(q, (rd) ? 1 : 0);
108 DNS_HEADER_SET_QDCOUNT(q, 1);
110 /* A name of "." is a screw case for the loop below, so adjust it. */
111 if (strcmp(name, ".") == 0)
114 /* Start writing out the name after the header. */
119 return ARES_EBADNAME;
121 /* Count the number of bytes in this label. */
123 for (p = name; *p && *p != '.'; p++)
125 if (*p == '\\' && *(p + 1) != 0)
130 return ARES_EBADNAME;
132 /* Encode the length and copy the data. */
134 for (p = name; *p && *p != '.'; p++)
136 if (*p == '\\' && *(p + 1) != 0)
141 /* Go to the next label and repeat, unless we hit the end. */
147 /* Add the zero-length label at the end. */
150 /* Finish off the question with the type and class. */
151 DNS_QUESTION_SET_TYPE(q, type);
152 DNS_QUESTION_SET_CLASS(q, dnsclass);