1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2 *
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.
14 */
15
16 static const char rcsid[] = "$Id: ares_mkquery.c,v 1.3 2000/09/21 19:15:51 ghudson Exp $";
17
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "ares.h"
24 #include "ares_dns.h"
25
26 /* Header format, from RFC 1035:
27 * 1 1 1 1 1 1
28 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
29 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
30 * | ID |
31 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
32 * |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
33 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
34 * | QDCOUNT |
35 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
36 * | ANCOUNT |
37 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
38 * | NSCOUNT |
39 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
40 * | ARCOUNT |
41 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
42 *
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
49 * Z Reserved (zero)
50 * QDCOUNT Number of queries
51 * ANCOUNT Number of answers
52 * NSCOUNT Number of name server records
53 * ARCOUNT Number of additional records
54 *
55 * Question format, from RFC 1035:
56 * 1 1 1 1 1 1
57 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
58 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
59 * | |
60 * / QNAME /
61 * / /
62 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
63 * | QTYPE |
64 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
65 * | QCLASS |
66 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
67 *
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).
72 */
73
ares_mkquery(const char * name,int dnsclass,int type,unsigned short id,int rd,unsigned char ** buf,int * buflen)74 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
75 int rd, unsigned char **buf, int *buflen)
76 {
77 int len;
78 unsigned char *q;
79 const char *p;
80
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. */
83 len = 1;
84 for (p = name; *p; p++)
85 {
86 if (*p == '\\' && *(p + 1) != 0)
87 p++;
88 len++;
89 }
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.
93 */
94 if (*name && *(p - 1) != '.')
95 len++;
96
97 *buflen = len + HFIXEDSZ + QFIXEDSZ;
98 *buf = malloc(*buflen);
99 if (!*buf)
100 return ARES_ENOMEM;
101
102 /* Set up the header. */
103 q = *buf;
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);
109
110 /* A name of "." is a screw case for the loop below, so adjust it. */
111 if (strcmp(name, ".") == 0)
112 name++;
113
114 /* Start writing out the name after the header. */
115 q += HFIXEDSZ;
116 while (*name)
117 {
118 if (*name == '.')
119 return ARES_EBADNAME;
120
121 /* Count the number of bytes in this label. */
122 len = 0;
123 for (p = name; *p && *p != '.'; p++)
124 {
125 if (*p == '\\' && *(p + 1) != 0)
126 p++;
127 len++;
128 }
129 if (len > MAXLABEL)
130 return ARES_EBADNAME;
131
132 /* Encode the length and copy the data. */
133 *q++ = len;
134 for (p = name; *p && *p != '.'; p++)
135 {
136 if (*p == '\\' && *(p + 1) != 0)
137 p++;
138 *q++ = *p;
139 }
140
141 /* Go to the next label and repeat, unless we hit the end. */
142 if (!*p)
143 break;
144 name = p + 1;
145 }
146
147 /* Add the zero-length label at the end. */
148 *q++ = 0;
149
150 /* Finish off the question with the type and class. */
151 DNS_QUESTION_SET_TYPE(q, type);
152 DNS_QUESTION_SET_CLASS(q, dnsclass);
153
154 return ARES_SUCCESS;
155 }
156