In ares_mkquery, make sure we set buflen and buf to reasonable values if there's...
[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   *buflen = len + HFIXEDSZ + QFIXEDSZ;
112   *buf = malloc(*buflen);
113   if (!*buf)
114       return ARES_ENOMEM;
115
116   /* Set up the header. */
117   q = *buf;
118   memset(q, 0, HFIXEDSZ);
119   DNS_HEADER_SET_QID(q, id);
120   DNS_HEADER_SET_OPCODE(q, QUERY);
121   if (rd) {
122     DNS_HEADER_SET_RD(q, 1);
123   }
124   else {
125     DNS_HEADER_SET_RD(q, 0);
126   }
127   DNS_HEADER_SET_QDCOUNT(q, 1);
128
129   /* A name of "." is a screw case for the loop below, so adjust it. */
130   if (strcmp(name, ".") == 0)
131     name++;
132
133   /* Start writing out the name after the header. */
134   q += HFIXEDSZ;
135   while (*name)
136     {
137       if (*name == '.')
138         return ARES_EBADNAME;
139
140       /* Count the number of bytes in this label. */
141       len = 0;
142       for (p = name; *p && *p != '.'; p++)
143         {
144           if (*p == '\\' && *(p + 1) != 0)
145             p++;
146           len++;
147         }
148       if (len > MAXLABEL)
149         return ARES_EBADNAME;
150
151       /* Encode the length and copy the data. */
152       *q++ = (unsigned char)len;
153       for (p = name; *p && *p != '.'; p++)
154         {
155           if (*p == '\\' && *(p + 1) != 0)
156             p++;
157           *q++ = *p;
158         }
159
160       /* Go to the next label and repeat, unless we hit the end. */
161       if (!*p)
162         break;
163       name = p + 1;
164     }
165
166   /* Add the zero-length label at the end. */
167   *q++ = 0;
168
169   /* Finish off the question with the type and class. */
170   DNS_QUESTION_SET_TYPE(q, type);
171   DNS_QUESTION_SET_CLASS(q, dnsclass);
172
173   return ARES_SUCCESS;
174 }