remove rcsid stuff from c files, it serves no useful purpose
[platform/upstream/c-ares.git] / ares_mkquery.c
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 #include <sys/types.h>
17
18 #ifdef WIN32
19 #include "nameser.h"
20 #else
21 #include <netinet/in.h>
22 #include <arpa/nameser.h>
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include "ares.h"
28 #include "ares_dns.h"
29
30 /* Header format, from RFC 1035:
31  *                                  1  1  1  1  1  1
32  *    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
33  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
34  *  |                      ID                       |
35  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
36  *  |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
37  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
38  *  |                    QDCOUNT                    |
39  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
40  *  |                    ANCOUNT                    |
41  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
42  *  |                    NSCOUNT                    |
43  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
44  *  |                    ARCOUNT                    |
45  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
46  *
47  * AA, TC, RA, and RCODE are only set in responses.  Brief description
48  * of the remaining fields:
49  *      ID      Identifier to match responses with queries
50  *      QR      Query (0) or response (1)
51  *      Opcode  For our purposes, always QUERY
52  *      RD      Recursion desired
53  *      Z       Reserved (zero)
54  *      QDCOUNT Number of queries
55  *      ANCOUNT Number of answers
56  *      NSCOUNT Number of name server records
57  *      ARCOUNT Number of additional records
58  *
59  * Question format, from RFC 1035:
60  *                                  1  1  1  1  1  1
61  *    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
62  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
63  *  |                                               |
64  *  /                     QNAME                     /
65  *  /                                               /
66  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
67  *  |                     QTYPE                     |
68  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
69  *  |                     QCLASS                    |
70  *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
71  *
72  * The query name is encoded as a series of labels, each represented
73  * as a one-byte length (maximum 63) followed by the text of the
74  * label.  The list is terminated by a label of length zero (which can
75  * be thought of as the root domain).
76  */
77
78 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
79                  int rd, unsigned char **buf, int *buflen)
80 {
81   int len;
82   unsigned char *q;
83   const char *p;
84
85   /* Compute the length of the encoded name so we can check buflen.
86    * Start counting at 1 for the zero-length label at the end. */
87   len = 1;
88   for (p = name; *p; p++)
89     {
90       if (*p == '\\' && *(p + 1) != 0)
91         p++;
92       len++;
93     }
94   /* If there are n periods in the name, there are n + 1 labels, and
95    * thus n + 1 length fields, unless the name is empty or ends with a
96    * period.  So add 1 unless name is empty or ends with a period.
97    */
98   if (*name && *(p - 1) != '.')
99     len++;
100
101   *buflen = len + HFIXEDSZ + QFIXEDSZ;
102   *buf = malloc(*buflen);
103   if (!*buf)
104       return ARES_ENOMEM;
105
106   /* Set up the header. */
107   q = *buf;
108   memset(q, 0, HFIXEDSZ);
109   DNS_HEADER_SET_QID(q, id);
110   DNS_HEADER_SET_OPCODE(q, QUERY);
111   DNS_HEADER_SET_RD(q, (rd) ? 1 : 0);
112   DNS_HEADER_SET_QDCOUNT(q, 1);
113
114   /* A name of "." is a screw case for the loop below, so adjust it. */
115   if (strcmp(name, ".") == 0)
116     name++;
117
118   /* Start writing out the name after the header. */
119   q += HFIXEDSZ;
120   while (*name)
121     {
122       if (*name == '.')
123         return ARES_EBADNAME;
124
125       /* Count the number of bytes in this label. */
126       len = 0;
127       for (p = name; *p && *p != '.'; p++)
128         {
129           if (*p == '\\' && *(p + 1) != 0)
130             p++;
131           len++;
132         }
133       if (len > MAXLABEL)
134         return ARES_EBADNAME;
135
136       /* Encode the length and copy the data. */
137       *q++ = len;
138       for (p = name; *p && *p != '.'; p++)
139         {
140           if (*p == '\\' && *(p + 1) != 0)
141             p++;
142           *q++ = *p;
143         }
144
145       /* Go to the next label and repeat, unless we hit the end. */
146       if (!*p)
147         break;
148       name = p + 1;
149     }
150
151   /* Add the zero-length label at the end. */
152   *q++ = 0;
153
154   /* Finish off the question with the type and class. */
155   DNS_QUESTION_SET_TYPE(q, type);
156   DNS_QUESTION_SET_CLASS(q, dnsclass);
157
158   return ARES_SUCCESS;
159 }