udhcp: comment out unused domain compression code
[oweals/busybox.git] / networking / udhcp / domain_codec.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu>
4  *
5  * Loosely based on the isc-dhcpd implementation by dhankins@isc.org
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 #ifdef DNS_COMPR_TESTING
10 # define _GNU_SOURCE
11 # define FAST_FUNC /* nothing */
12 # define xmalloc malloc
13 # include <stdlib.h>
14 # include <stdint.h>
15 # include <string.h>
16 # include <stdio.h>
17 #else
18 # include "common.h"
19 #endif
20
21 #define NS_MAXDNAME  1025       /* max domain name length */
22 #define NS_MAXCDNAME  255       /* max compressed domain name length */
23 #define NS_MAXLABEL    63       /* max label length */
24 #define NS_MAXDNSRCH    6       /* max domains in search path */
25 #define NS_CMPRSFLGS 0xc0       /* name compression pointer flag */
26
27
28 /* Expand a RFC1035-compressed list of domain names "cstr", of length "clen";
29  * returns a newly allocated string containing the space-separated domains,
30  * prefixed with the contents of string pre, or NULL if an error occurs.
31  */
32 char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
33 {
34         char *ret = ret; /* for compiler */
35         char *dst = NULL;
36
37         /* We make two passes over the cstr string. First, we compute
38          * how long the resulting string would be. Then we allocate a
39          * new buffer of the required length, and fill it in with the
40          * expanded content. The advantage of this approach is not
41          * having to deal with requiring callers to supply their own
42          * buffer, then having to check if it's sufficiently large, etc.
43          */
44         while (1) {
45                 /* note: "return NULL" below are leak-safe since
46                  * dst isn't allocated yet */
47                 const uint8_t *c;
48                 unsigned crtpos, retpos, depth, len;
49
50                 crtpos = retpos = depth = len = 0;
51                 while (crtpos < clen) {
52                         c = cstr + crtpos;
53
54                         if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
55                                 /* pointer */
56                                 if (crtpos + 2 > clen) /* no offset to jump to? abort */
57                                         return NULL;
58                                 if (retpos == 0) /* toplevel? save return spot */
59                                         retpos = crtpos + 2;
60                                 depth++;
61                                 crtpos = ((c[0] & 0x3f) << 8) | c[1]; /* jump */
62                         } else if (*c) {
63                                 /* label */
64                                 if (crtpos + *c + 1 > clen) /* label too long? abort */
65                                         return NULL;
66                                 if (dst)
67                                         /* \3com ---> "com." */
68                                         ((char*)mempcpy(dst + len, c + 1, *c))[0] = '.';
69                                 len += *c + 1;
70                                 crtpos += *c + 1;
71                         } else {
72                                 /* NUL: end of current domain name */
73                                 if (retpos == 0) {
74                                         /* toplevel? keep going */
75                                         crtpos++;
76                                 } else {
77                                         /* return to toplevel saved spot */
78                                         crtpos = retpos;
79                                         retpos = depth = 0;
80                                 }
81                                 if (dst && len != 0)
82                                         /* \4host\3com\0\4host and we are at \0:
83                                          * \3com was converted to "com.", change dot to space.
84                                          */
85                                         dst[len - 1] = ' ';
86                         }
87
88                         if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
89                          || len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
90                         ) {
91                                 return NULL;
92                         }
93                 }
94
95                 if (!len) /* expanded string has 0 length? abort */
96                         return NULL;
97
98                 if (!dst) { /* first pass? */
99                         /* allocate dst buffer and copy pre */
100                         unsigned plen = strlen(pre);
101                         ret = xmalloc(plen + len);
102                         dst = stpcpy(ret, pre);
103                 } else {
104                         dst[len - 1] = '\0';
105                         break;
106                 }
107         }
108
109         return ret;
110 }
111
112 /* Convert a domain name (src) from human-readable "foo.BLAH.com" format into
113  * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
114  * NULL if an error occurs.
115  */
116 static uint8_t *convert_dname(const char *src, int *retlen)
117 {
118         uint8_t c, *res, *lenptr, *dst;
119         int len;
120
121         res = xmalloc(strlen(src) + 2);
122         dst = lenptr = res;
123         dst++;
124
125         for (;;) {
126                 c = (uint8_t)*src++;
127                 if (c == '.' || c == '\0') {  /* end of label */
128                         len = dst - lenptr - 1;
129                         /* label too long, too short, or two '.'s in a row? abort */
130                         if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
131                                 free(res);
132                                 *retlen = 0;
133                                 return NULL;
134                         }
135                         *lenptr = len;
136                         if (c == '\0' || *src == '\0')  /* "" or ".": end of src */
137                                 break;
138                         lenptr = dst++;
139                         continue;
140                 }
141                 if (c >= 'A' && c <= 'Z')  /* uppercase? convert to lower */
142                         c += ('a' - 'A');
143                 *dst++ = c;
144         }
145
146         if (dst - res >= NS_MAXCDNAME) {  /* dname too long? abort */
147                 free(res);
148                 *retlen = 0;
149                 return NULL;
150         }
151
152         *dst++ = 0;
153         *retlen = dst - res;
154         return res;
155 }
156
157 #if 0 //UNUSED
158 /* Returns the offset within cstr at which dname can be found, or -1 */
159 static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
160 {
161         const uint8_t *c, *d;
162         int off;
163
164         /* find all labels in cstr */
165         off = 0;
166         while (off < clen) {
167                 c = cstr + off;
168
169                 if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {  /* pointer, skip */
170                         off += 2;
171                         continue;
172                 }
173                 if (*c) {  /* label, try matching dname */
174                         d = dname;
175                         while (1) {
176                                 unsigned len1 = *c + 1;
177                                 if (memcmp(c, d, len1) != 0)
178                                         break;
179                                 if (len1 == 1)  /* at terminating NUL - match, return offset */
180                                         return off;
181                                 d += len1;
182                                 c += len1;
183                                 if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS)  /* pointer, jump */
184                                         c = cstr + (((c[0] & 0x3f) << 8) | c[1]);
185                         }
186                         off += cstr[off] + 1;
187                         continue;
188                 }
189                 /* NUL, skip */
190                 off++;
191         }
192
193         return -1;
194 }
195 #endif
196
197 uint8_t* FAST_FUNC dname_enc(/*const uint8_t *cstr, int clen,*/ const char *src, int *retlen)
198 {
199 #if 0 //UNUSED, was intended for long, repetitive DHCP_DOMAIN_SEARCH options?
200         uint8_t *d, *dname;
201 /* Computes string to be appended to cstr so that src would be added to
202  * the compression (best case, it's a 2-byte pointer to some offset within
203  * cstr; worst case, it's all of src, converted to <4>host<3>com<0> format).
204  * The computed string is returned directly; its length is returned via retlen;
205  * NULL and 0, respectively, are returned if an error occurs.
206  */
207         dname = convert_dname(src, retlen);
208         if (dname == NULL) {
209                 return NULL;
210         }
211
212         d = dname;
213         while (*d) {
214                 if (cstr) {
215                         int off = find_offset(cstr, clen, d);
216                         if (off >= 0) { /* found a match, add pointer and return */
217                                 *d++ = NS_CMPRSFLGS | (off >> 8);
218                                 *d = off;
219                                 break;
220                         }
221                 }
222                 d += *d + 1;
223         }
224
225         *retlen = d - dname + 1;
226         return dname;
227 #endif
228         return convert_dname(src, retlen);
229 }
230
231 #ifdef DNS_COMPR_TESTING
232 /* gcc -Wall -DDNS_COMPR_TESTING domain_codec.c -o domain_codec && ./domain_codec */
233 int main(int argc, char **argv)
234 {
235         int len;
236         uint8_t *encoded;
237
238         uint8_t str[6] = { 0x00, 0x00, 0x02, 0x65, 0x65, 0x00 };
239         printf("NUL:'%s'\n",   dname_dec(str, 6, ""));
240
241 #define DNAME_DEC(encoded,pre) dname_dec((uint8_t*)(encoded), sizeof(encoded), (pre))
242         printf("'%s'\n",       DNAME_DEC("\4host\3com\0", "test1:"));
243         printf("test2:'%s'\n", DNAME_DEC("\4host\3com\0\4host\3com\0", ""));
244         printf("test3:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\0", ""));
245         printf("test4:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5", ""));
246         printf("test5:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5\1z\xC0\xA", ""));
247
248 #define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp))
249         encoded = dname_enc(NULL, 0, "test.net", &len);
250         printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
251         encoded = DNAME_ENC("\3net\0", "test.net", &len);
252         printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
253         encoded = DNAME_ENC("\4test\3net\0", "test.net", &len);
254         printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
255         return 0;
256 }
257 #endif