Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares_mkquery.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  *
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.
16  */
17
18 #include "setup.h"
19
20 #if defined(WIN32) && !defined(WATT32)
21 #include "nameser.h"
22 #else
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
26 #include <arpa/nameser_compat.h>
27 #endif
28 #endif
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include "ares.h"
33 #include "ares_dns.h"
34 #include "ares_private.h"
35
36 /* Header format, from RFC 1035:
37  *                                  1  1  1  1  1  1
38  *    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
39  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
40  *  |                      ID                       |
41  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
42  *  |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
43  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
44  *  |                    QDCOUNT                    |
45  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
46  *  |                    ANCOUNT                    |
47  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
48  *  |                    NSCOUNT                    |
49  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
50  *  |                    ARCOUNT                    |
51  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
52  *
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
59  *      Z       Reserved (zero)
60  *      QDCOUNT Number of queries
61  *      ANCOUNT Number of answers
62  *      NSCOUNT Number of name server records
63  *      ARCOUNT Number of additional records
64  *
65  * Question format, from RFC 1035:
66  *                                  1  1  1  1  1  1
67  *    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
68  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
69  *  |                                               |
70  *  /                     QNAME                     /
71  *  /                                               /
72  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
73  *  |                     QTYPE                     |
74  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
75  *  |                     QCLASS                    |
76  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
77  *
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).
82  */
83
84 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
85                  int rd, unsigned char **buf, int *buflen)
86 {
87   int len;
88   unsigned char *q;
89   const char *p;
90
91   /* Set our results early, in case we bail out early with an error. */
92   *buflen = 0;
93   *buf = NULL;
94
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. */
97   len = 1;
98   for (p = name; *p; p++)
99     {
100       if (*p == '\\' && *(p + 1) != 0)
101         p++;
102       len++;
103     }
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.
107    */
108   if (*name && *(p - 1) != '.')
109     len++;
110
111   /* Immediately reject names that are longer than the maximum of 255
112    * bytes that's specified in RFC 1035 ("To simplify implementations,
113    * the total length of a domain name (i.e., label octets and label
114    * length octets) is restricted to 255 octets or less."). We aren't
115    * doing this just to be a stickler about RFCs. For names that are
116    * too long, 'dnscache' closes its TCP connection to us immediately
117    * (when using TCP) and ignores the request when using UDP, and
118    * BIND's named returns ServFail (TCP or UDP). Sending a request
119    * that we know will cause 'dnscache' to close the TCP connection is
120    * painful, since that makes any other outstanding requests on that
121    * connection fail. And sending a UDP request that we know
122    * 'dnscache' will ignore is bad because resources will be tied up
123    * until we time-out the request.
124    */
125   if (len > MAXCDNAME)
126     return ARES_EBADNAME;
127
128   *buflen = len + HFIXEDSZ + QFIXEDSZ;
129   *buf = malloc(*buflen);
130   if (!*buf)
131       return ARES_ENOMEM;
132
133   /* Set up the header. */
134   q = *buf;
135   memset(q, 0, HFIXEDSZ);
136   DNS_HEADER_SET_QID(q, id);
137   DNS_HEADER_SET_OPCODE(q, QUERY);
138   if (rd) {
139     DNS_HEADER_SET_RD(q, 1);
140   }
141   else {
142     DNS_HEADER_SET_RD(q, 0);
143   }
144   DNS_HEADER_SET_QDCOUNT(q, 1);
145
146   /* A name of "." is a screw case for the loop below, so adjust it. */
147   if (strcmp(name, ".") == 0)
148     name++;
149
150   /* Start writing out the name after the header. */
151   q += HFIXEDSZ;
152   while (*name)
153     {
154       if (*name == '.')
155         return ARES_EBADNAME;
156
157       /* Count the number of bytes in this label. */
158       len = 0;
159       for (p = name; *p && *p != '.'; p++)
160         {
161           if (*p == '\\' && *(p + 1) != 0)
162             p++;
163           len++;
164         }
165       if (len > MAXLABEL)
166         return ARES_EBADNAME;
167
168       /* Encode the length and copy the data. */
169       *q++ = (unsigned char)len;
170       for (p = name; *p && *p != '.'; p++)
171         {
172           if (*p == '\\' && *(p + 1) != 0)
173             p++;
174           *q++ = *p;
175         }
176
177       /* Go to the next label and repeat, unless we hit the end. */
178       if (!*p)
179         break;
180       name = p + 1;
181     }
182
183   /* Add the zero-length label at the end. */
184   *q++ = 0;
185
186   /* Finish off the question with the type and class. */
187   DNS_QUESTION_SET_TYPE(q, type);
188   DNS_QUESTION_SET_CLASS(q, dnsclass);
189
190   return ARES_SUCCESS;
191 }