Header inclusion clean-up
[platform/upstream/c-ares.git] / ares_create_query.c
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in
10  * advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.
12  * M.I.T. makes no representations about the suitability of
13  * this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  */
16
17 #include "ares_setup.h"
18
19 #ifdef HAVE_SYS_SOCKET_H
20 #  include <sys/socket.h>
21 #endif
22 #ifdef HAVE_NETINET_IN_H
23 #  include <netinet/in.h>
24 #endif
25 #ifdef HAVE_ARPA_NAMESER_H
26 #  include <arpa/nameser.h>
27 #else
28 #  include "nameser.h"
29 #endif
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 #  include <arpa/nameser_compat.h>
32 #endif
33
34 #include "ares.h"
35 #include "ares_dns.h"
36 #include "ares_private.h"
37
38 #ifndef T_OPT
39 #  define T_OPT  41 /* EDNS0 option (meta-RR) */
40 #endif
41
42 /* Header format, from RFC 1035:
43  *                                  1  1  1  1  1  1
44  *    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
45  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
46  *  |                      ID                       |
47  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
48  *  |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
49  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
50  *  |                    QDCOUNT                    |
51  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
52  *  |                    ANCOUNT                    |
53  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
54  *  |                    NSCOUNT                    |
55  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
56  *  |                    ARCOUNT                    |
57  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
58  *
59  * AA, TC, RA, and RCODE are only set in responses.  Brief description
60  * of the remaining fields:
61  *      ID      Identifier to match responses with queries
62  *      QR      Query (0) or response (1)
63  *      Opcode  For our purposes, always QUERY
64  *      RD      Recursion desired
65  *      Z       Reserved (zero)
66  *      QDCOUNT Number of queries
67  *      ANCOUNT Number of answers
68  *      NSCOUNT Number of name server records
69  *      ARCOUNT Number of additional records
70  *
71  * Question format, from RFC 1035:
72  *                                  1  1  1  1  1  1
73  *    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
74  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
75  *  |                                               |
76  *  /                     QNAME                     /
77  *  /                                               /
78  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
79  *  |                     QTYPE                     |
80  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
81  *  |                     QCLASS                    |
82  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
83  *
84  * The query name is encoded as a series of labels, each represented
85  * as a one-byte length (maximum 63) followed by the text of the
86  * label.  The list is terminated by a label of length zero (which can
87  * be thought of as the root domain).
88  */
89
90 int ares_create_query(const char *name, int dnsclass, int type,
91                       unsigned short id, int rd, unsigned char **buf,
92                       int *buflen, int max_udp_size)
93 {
94   int len;
95   unsigned char *q;
96   const char *p;
97
98   /* Set our results early, in case we bail out early with an error. */
99   *buflen = 0;
100   *buf = NULL;
101
102   /* Compute the length of the encoded name so we can check buflen.
103    * Start counting at 1 for the zero-length label at the end. */
104   len = 1;
105   for (p = name; *p; p++)
106     {
107       if (*p == '\\' && *(p + 1) != 0)
108         p++;
109       len++;
110     }
111   /* If there are n periods in the name, there are n + 1 labels, and
112    * thus n + 1 length fields, unless the name is empty or ends with a
113    * period.  So add 1 unless name is empty or ends with a period.
114    */
115   if (*name && *(p - 1) != '.')
116     len++;
117
118   /* Immediately reject names that are longer than the maximum of 255
119    * bytes that's specified in RFC 1035 ("To simplify implementations,
120    * the total length of a domain name (i.e., label octets and label
121    * length octets) is restricted to 255 octets or less."). We aren't
122    * doing this just to be a stickler about RFCs. For names that are
123    * too long, 'dnscache' closes its TCP connection to us immediately
124    * (when using TCP) and ignores the request when using UDP, and
125    * BIND's named returns ServFail (TCP or UDP). Sending a request
126    * that we know will cause 'dnscache' to close the TCP connection is
127    * painful, since that makes any other outstanding requests on that
128    * connection fail. And sending a UDP request that we know
129    * 'dnscache' will ignore is bad because resources will be tied up
130    * until we time-out the request.
131    */
132   if (len > MAXCDNAME)
133     return ARES_EBADNAME;
134
135   *buflen = len + HFIXEDSZ + QFIXEDSZ + (max_udp_size ? EDNSFIXEDSZ : 0);
136   *buf = malloc(*buflen);
137   if (!*buf)
138       return ARES_ENOMEM;
139
140   /* Set up the header. */
141   q = *buf;
142   memset(q, 0, HFIXEDSZ);
143   DNS_HEADER_SET_QID(q, id);
144   DNS_HEADER_SET_OPCODE(q, QUERY);
145   if (rd) {
146     DNS_HEADER_SET_RD(q, 1);
147   }
148   else {
149     DNS_HEADER_SET_RD(q, 0);
150   }
151   DNS_HEADER_SET_QDCOUNT(q, 1);
152
153   if (max_udp_size) {
154       DNS_HEADER_SET_ARCOUNT(q, 1);
155   }
156
157   /* A name of "." is a screw case for the loop below, so adjust it. */
158   if (strcmp(name, ".") == 0)
159     name++;
160
161   /* Start writing out the name after the header. */
162   q += HFIXEDSZ;
163   while (*name)
164     {
165       if (*name == '.')
166         return ARES_EBADNAME;
167
168       /* Count the number of bytes in this label. */
169       len = 0;
170       for (p = name; *p && *p != '.'; p++)
171         {
172           if (*p == '\\' && *(p + 1) != 0)
173             p++;
174           len++;
175         }
176       if (len > MAXLABEL)
177         return ARES_EBADNAME;
178
179       /* Encode the length and copy the data. */
180       *q++ = (unsigned char)len;
181       for (p = name; *p && *p != '.'; p++)
182         {
183           if (*p == '\\' && *(p + 1) != 0)
184             p++;
185           *q++ = *p;
186         }
187
188       /* Go to the next label and repeat, unless we hit the end. */
189       if (!*p)
190         break;
191       name = p + 1;
192     }
193
194   /* Add the zero-length label at the end. */
195   *q++ = 0;
196
197   /* Finish off the question with the type and class. */
198   DNS_QUESTION_SET_TYPE(q, type);
199   DNS_QUESTION_SET_CLASS(q, dnsclass);
200
201   if (max_udp_size)
202   {
203       q += QFIXEDSZ;
204       memset(q, 0, EDNSFIXEDSZ);
205       q++;
206       DNS_RR_SET_TYPE(q, T_OPT);
207       DNS_RR_SET_CLASS(q, max_udp_size);
208   }
209
210   return ARES_SUCCESS;
211 }