1 /* vi: set sw=4 ts=4: */
3 /* RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu>
5 * Loosely based on the isc-dhcpd implementation by dhankins@isc.org
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 #if ENABLE_FEATURE_UDHCP_RFC3397
15 #define NS_MAXDNAME 1025 /* max domain name length */
16 #define NS_MAXCDNAME 255 /* max compressed domain name length */
17 #define NS_MAXLABEL 63 /* max label length */
18 #define NS_MAXDNSRCH 6 /* max domains in search path */
19 #define NS_CMPRSFLGS 0xc0 /* name compression pointer flag */
22 /* expand a RFC1035-compressed list of domain names "cstr", of length "clen";
23 * returns a newly allocated string containing the space-separated domains,
24 * prefixed with the contents of string pre, or NULL if an error occurs.
26 char* FAST_FUNC
dname_dec(const uint8_t *cstr
, int clen
, const char *pre
)
29 int crtpos
, retpos
, depth
, plen
= 0, len
= 0;
38 /* We make two passes over the cstr string. First, we compute
39 * how long the resulting string would be. Then we allocate a
40 * new buffer of the required length, and fill it in with the
41 * expanded content. The advantage of this approach is not
42 * having to deal with requiring callers to supply their own
43 * buffer, then having to check if it's sufficiently large, etc.
48 if (len
> 0) { /* second pass? allocate dst buffer and copy pre */
49 dst
= xmalloc(len
+ plen
);
50 memcpy(dst
, pre
, plen
);
53 crtpos
= retpos
= depth
= len
= 0;
55 while (crtpos
< clen
) {
58 if ((*c
& NS_CMPRSFLGS
) != 0) { /* pointer */
59 if (crtpos
+ 2 > clen
) /* no offset to jump to? abort */
61 if (retpos
== 0) /* toplevel? save return spot */
64 crtpos
= ((*c
& 0x3f) << 8) | (*(c
+ 1) & 0xff); /* jump */
65 } else if (*c
) { /* label */
66 if (crtpos
+ *c
+ 1 > clen
) /* label too long? abort */
69 memcpy(dst
+ plen
+ len
, c
+ 1, *c
);
73 *(dst
+ plen
+ len
- 1) = '.';
74 } else { /* null: end of current domain name */
75 if (retpos
== 0) { /* toplevel? keep going */
77 } else { /* return to toplevel saved spot */
82 *(dst
+ plen
+ len
- 1) = ' ';
85 if (depth
> NS_MAXDNSRCH
|| /* too many jumps? abort, it's a loop */
86 len
> NS_MAXDNAME
* NS_MAXDNSRCH
) /* result too long? abort */
90 if (!len
) /* expanded string has 0 length? abort */
94 *(dst
+ plen
+ len
- 1) = '\0';
100 /* Convert a domain name (src) from human-readable "foo.blah.com" format into
101 * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
102 * NULL if an error occurs.
104 static uint8_t *convert_dname(const char *src
)
106 uint8_t c
, *res
, *lp
, *rp
;
109 res
= xmalloc(strlen(src
) + 2);
115 if (c
== '.' || c
== '\0') { /* end of label */
117 /* label too long, too short, or two '.'s in a row? abort */
118 if (len
> NS_MAXLABEL
|| len
== 0 || (c
== '.' && *src
== '.')) {
124 if (c
== '\0' || *src
== '\0') /* end of dname */
127 if (c
>= 0x41 && c
<= 0x5A) /* uppercase? convert to lower */
134 if (rp
- res
> NS_MAXCDNAME
) { /* dname too long? abort */
141 /* returns the offset within cstr at which dname can be found, or -1
143 static int find_offset(const uint8_t *cstr
, int clen
, const uint8_t *dname
)
145 const uint8_t *c
, *d
;
148 /* find all labels in cstr */
153 if ((*c
& NS_CMPRSFLGS
) != 0) { /* pointer, skip */
155 } else if (*c
) { /* label, try matching dname */
158 while (*c
== *d
&& memcmp(c
+ 1, d
+ 1, *c
) == 0) {
159 if (*c
== 0) /* match, return offset */
163 if ((*c
& NS_CMPRSFLGS
) != 0) /* pointer, jump */
164 c
= cstr
+ (((*c
& 0x3f) << 8) | (*(c
+ 1) & 0xff));
167 } else { /* null, skip */
175 /* computes string to be appended to cstr so that src would be added to
176 * the compression (best case, it's a 2-byte pointer to some offset within
177 * cstr; worst case, it's all of src, converted to rfc3011 format).
178 * The computed string is returned directly; its length is returned via retlen;
179 * NULL and 0, respectively, are returned if an error occurs.
181 uint8_t* FAST_FUNC
dname_enc(const uint8_t *cstr
, int clen
, const char *src
, int *retlen
)
186 dname
= convert_dname(src
);
192 for (d
= dname
; *d
!= 0; d
+= *d
+ 1) {
193 off
= find_offset(cstr
, clen
, d
);
194 if (off
>= 0) { /* found a match, add pointer and terminate string */
201 *retlen
= d
- dname
+ 1;
205 #endif /* ENABLE_FEATURE_UDHCP_RFC3397 */