PATCH: RFC3397 domain search support

Gabriel L. Somlo somlo at cmu.edu
Tue Feb 27 17:50:41 UTC 2007


Denis,

Enclosed below is a patch that adds RFC3397 support to udhcp. It adds
an 'option search' to the udhcpd.conf file on the server side, and
fills the 'search' environment variable on the client side before
udhcpc.script is called. Both client and server need to comply for
this to be useful (e.g., isc dhcp version 3.1.0 or later), so the
default setting for this in the build script is off.

I tested it with isc 3.1.0 alpha, and it seems to work.

Please let me know what you think. Apply if you like.

Regards,
Gabriel

On Mon, Jan 29, 2007 at 09:06:14PM -0600, Jason Schoon wrote:
> Great idea, but if you need this functionality you should add support to
> udhcp for option 119, specified in RFC3397.  This is the exact problem that
> was solved by that new option.



diff -NarU5 busybox-svn-17929.orig/networking/udhcp/Config.in busybox-svn-17929/networking/udhcp/Config.in
--- busybox-svn-17929.orig/networking/udhcp/Config.in	2007-02-19 15:33:43.000000000 -0500
+++ busybox-svn-17929/networking/udhcp/Config.in	2007-02-27 12:03:52.000000000 -0500
@@ -63,5 +63,13 @@
 	  If selected, udhcpd will output extra debugging output.  If using
 	  this option, compile uDHCP with "-g", and do not fork the daemon to
 	  the background.
 
 	  See http://udhcp.busybox.net for further details.
+
+config FEATURE_RFC3397
+	bool "Support for RFC3397 domain search (experimental)"
+	default n
+	depends on APP_UDHCPD || APP_UDHCPC
+	help
+	  If selected, both client and server will support passing of domain
+	  search lists via option 119, specified in RFC3397.
diff -NarU5 busybox-svn-17929.orig/networking/udhcp/Kbuild busybox-svn-17929/networking/udhcp/Kbuild
--- busybox-svn-17929.orig/networking/udhcp/Kbuild	2007-02-19 15:33:43.000000000 -0500
+++ busybox-svn-17929/networking/udhcp/Kbuild	2007-02-24 09:41:22.000000000 -0500
@@ -14,5 +14,6 @@
 				   script.o
 lib-$(CONFIG_APP_UDHCPD)	+= dhcpd.o arpping.o files.o leases.o \
 				   serverpacket.o static_leases.o
 lib-$(CONFIG_APP_DUMPLEASES)	+= dumpleases.o
 lib-$(CONFIG_APP_DHCPRELAY)     += dhcprelay.o
+lib-$(CONFIG_FEATURE_RFC3397)   += domain_codec.o
diff -NarU5 busybox-svn-17929.orig/networking/udhcp/domain_codec.c busybox-svn-17929/networking/udhcp/domain_codec.c
--- busybox-svn-17929.orig/networking/udhcp/domain_codec.c	1969-12-31 19:00:00.000000000 -0500
+++ busybox-svn-17929/networking/udhcp/domain_codec.c	2007-02-26 18:01:49.000000000 -0500
@@ -0,0 +1,205 @@
+/* vi: set sw=4 ts=4: */
+
+/* RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu>
+ *
+ * Loosely based on the isc-dhcpd implementation by dhankins at isc.org
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#if ENABLE_FEATURE_RFC3397
+
+#include "common.h"
+#include "options.h"
+
+#define NS_MAXDNAME  1025	/* max domain name length */
+#define NS_MAXCDNAME  255	/* max compressed domain name length */
+#define NS_MAXLABEL    63	/* max label length */
+#define NS_MAXDNSRCH    6	/* max domains in search path */
+#define NS_CMPRSFLGS 0xc0	/* name compression pointer flag */
+
+
+/* expand a RFC1035-compressed list of domain names "cstr", of length "clen";
+ * returns a newly allocated string containing the space-separated domains,
+ * prefixed with the contents of string pre, or NULL if an error occurs.
+ */
+char *dname_dec(const uint8_t *cstr, int clen, const char *pre)
+{
+	const uint8_t *c;
+	int crtpos, retpos, depth, plen = 0, len = 0;
+	char *dst = NULL;
+
+	if (!cstr)
+		return NULL;
+
+	if (pre)
+		plen = strlen(pre);
+
+	/* We make two passes over the cstr string. First, we compute
+	 * how long the resulting string would be. Then we allocate a
+	 * new buffer of the required length, and fill it in with the
+	 * expanded content. The advantage of this approach is not
+	 * having to deal with requiring callers to supply their own
+	 * buffer, then having to check if it's sufficiently large, etc.
+	 */
+
+	while (!dst) {
+
+		if (len > 0) {	/* second pass? allocate dst buffer and copy pre */
+			dst = xmalloc(len + plen);
+			memcpy(dst, pre, plen);
+		}
+
+		crtpos = retpos = depth = len = 0;
+
+		while (crtpos < clen) {
+			c = cstr + crtpos;
+
+			if ((*c & NS_CMPRSFLGS) != 0) {	/* pointer */
+				if (crtpos + 2 > clen)		/* no offset to jump to? abort */
+					return NULL;
+				if (retpos == 0)			/* toplevel? save return spot */
+					retpos = crtpos + 2;
+				depth++;
+				crtpos = ((*c & 0x3f) << 8) | (*(c + 1) & 0xff); /* jump */
+			} else if (*c) {			/* label */
+				if (crtpos + *c + 1 > clen)		/* label too long? abort */
+					return NULL;
+				if (dst)
+					memcpy(dst + plen + len, c + 1, *c);
+				len += *c + 1;
+				crtpos += *c + 1;
+				if (dst)
+					*(dst + plen + len - 1) = '.';
+			} else {					/* null: end of current domain name */
+				if (retpos == 0) {			/* toplevel? keep going */
+					crtpos++;
+				} else {					/* return to toplevel saved spot */
+					crtpos = retpos;
+					retpos = depth = 0;
+				}
+				if (dst)
+					*(dst + plen + len - 1) = ' ';
+			}
+
+			if (depth > NS_MAXDNSRCH || /* too many jumps? abort, it's a loop */
+				len > NS_MAXDNAME * NS_MAXDNSRCH) /* result too long? abort */
+				return NULL;
+		}
+
+		if (!len)			/* expanded string has 0 length? abort */
+			return NULL;
+
+		if (dst)
+			*(dst + plen + len - 1) = '\0';
+	}
+
+	return dst;
+}
+
+/* Convert a domain name (src) from human-readable "foo.blah.com" format into
+ * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
+ * NULL if an error occurs.
+ */
+static uint8_t *convert_dname(const char *src)
+{
+	uint8_t c, *res, *lp, *rp;
+	int len;
+
+	res = xmalloc(strlen(src) + 2);
+	rp = lp = res;
+	rp++;
+
+	for (;;) {
+		c = (uint8_t)*src++;
+		if (c == '.' || c == '\0') {	/* end of label */
+			len = rp - lp - 1;
+			/* label too long, too short, or two '.'s in a row? abort */
+			if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
+				free(res);
+				return NULL;
+			}
+			*lp = len;
+			lp = rp++;
+			if (c == '\0' || *src == '\0')	/* end of dname */
+				break;
+		} else {
+			if (c >= 0x41 && c <= 0x5A)		/* uppercase? convert to lower */
+				c += 0x20;
+			*rp++ = c;
+		}
+	}
+
+	*lp = 0;
+	if (rp - res > NS_MAXCDNAME) {	/* dname too long? abort */
+		free(res);
+		return NULL;
+	}
+	return res;
+}
+
+/* returns the offset within cstr at which dname can be found, or -1
+ */
+static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
+{
+	const uint8_t *c, *d;
+	int off, inc;
+
+	/* find all labels in cstr */
+	off = 0;
+	while (off < clen) {
+		c = cstr + off;
+
+		if ((*c & NS_CMPRSFLGS) != 0) {	/* pointer, skip */
+			off += 2;
+		} else if (*c) {	/* label, try matching dname */
+			inc = *c + 1;
+			d = dname;
+			while (*c == *d && memcmp(c + 1, d + 1, *c) == 0) {
+				if (*c == 0)	/* match, return offset */
+					return off;
+				d += *c + 1;
+				c += *c + 1;
+				if ((*c & NS_CMPRSFLGS) != 0)	/* pointer, jump */
+					c = cstr + (((*c & 0x3f) << 8) | (*(c + 1) & 0xff));
+			}
+			off += inc;
+		} else {	/* null, skip */
+			off++;
+		}
+	}
+
+	return -1;
+}
+
+/* computes string to be appended to cstr so that src would be added to
+ * the compression (best case, it's a 2-byte pointer to some offset within
+ * cstr; worst case, it's all of src, converted to rfc3011 format).
+ * The computed string is returned directly; its length is returned via retlen;
+ * NULL and 0, respectively, are returned if an error occurs.
+ */
+uint8_t *dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen)
+{
+	uint8_t *d, *dname;
+	int off;
+
+	dname = convert_dname(src);
+	if (dname == NULL) {
+		*retlen = 0;
+		return NULL;
+	}
+
+	for (d = dname; *d != 0; d += *d + 1) {
+		off = find_offset(cstr, clen, d);
+		if (off >= 0) {	/* found a match, add pointer and terminate string */
+			*d++ = NS_CMPRSFLGS;
+			*d = off;
+			break;
+		}
+	}
+
+	*retlen = d - dname + 1;
+	return dname;
+}		
+
+#endif /* ENABLE_FEATURE_RFC3397 */
diff -NarU5 busybox-svn-17929.orig/networking/udhcp/files.c busybox-svn-17929/networking/udhcp/files.c
--- busybox-svn-17929.orig/networking/udhcp/files.c	2007-02-19 15:33:43.000000000 -0500
+++ busybox-svn-17929/networking/udhcp/files.c	2007-02-24 09:41:22.000000000 -0500
@@ -102,10 +102,16 @@
 
 	existing = find_option(*opt_list, option->code);
 	if (!existing) {
 		DEBUG("Attaching option %s to list", option->name);
 
+#if ENABLE_FEATURE_RFC3397
+		if ((option->flags & TYPE_MASK) == OPTION_STR1035)
+			/* reuse buffer and length for RFC1035-formatted string */
+			buffer = dname_enc(NULL, 0, buffer, &length);
+#endif
+
 		/* make a new option */
 		new = xmalloc(sizeof(struct option_set));
 		new->data = xmalloc(length + 2);
 		new->data[OPT_CODE] = option->code;
 		new->data[OPT_LEN] = length;
@@ -115,16 +121,26 @@
 		while (*curr && (*curr)->data[OPT_CODE] < option->code)
 			curr = &(*curr)->next;
 
 		new->next = *curr;
 		*curr = new;
+#if ENABLE_FEATURE_RFC3397
+		if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
+			free(buffer);
+#endif
 		return;
 	}
 
 	/* add it to an existing option */
 	DEBUG("Attaching option %s to existing member of list", option->name);
 	if (option->flags & OPTION_LIST) {
+#if ENABLE_FEATURE_RFC3397
+		if ((option->flags & TYPE_MASK) == OPTION_STR1035)
+			/* reuse buffer and length for RFC1035-formatted string */
+			buffer = dname_enc(existing->data + 2,
+									existing->data[OPT_LEN], buffer, &length);
+#endif
 		if (existing->data[OPT_LEN] + length <= 255) {
 			existing->data = xrealloc(existing->data,
 					existing->data[OPT_LEN] + length + 3);
 			if ((option->flags & TYPE_MASK) == OPTION_STRING) {
 				/* ' ' can bring us to 256 - bad */
@@ -135,10 +151,14 @@
 				existing->data[OPT_LEN]++;
 			}
 			memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
 			existing->data[OPT_LEN] += length;
 		} /* else, ignore the data, we could put this in a second option in the future */
+#if ENABLE_FEATURE_RFC3397
+		if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
+			free(buffer);
+#endif
 	} /* else, ignore the new data */
 }
 
 
 /* read a dhcp option and add it to opt_list */
@@ -181,10 +201,13 @@
 			retval = read_ip(val, buffer);
 			if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
 			if (retval) retval = read_ip(val, buffer + 4);
 			break;
 		case OPTION_STRING:
+#if ENABLE_FEATURE_RFC3397
+		case OPTION_STR1035:
+#endif
 			length = strlen(val);
 			if (length > 0) {
 				if (length > 254) length = 254;
 				opt = val;
 				retval = 1;
diff -NarU5 busybox-svn-17929.orig/networking/udhcp/options.c busybox-svn-17929/networking/udhcp/options.c
--- busybox-svn-17929.orig/networking/udhcp/options.c	2007-02-19 15:33:43.000000000 -0500
+++ busybox-svn-17929/networking/udhcp/options.c	2007-02-24 09:41:22.000000000 -0500
@@ -9,11 +9,11 @@
 #include "options.h"
 
 
 /* supported options are easily added here */
 const struct dhcp_option dhcp_options[] = {
-	/* name[10]     flags                                   code */
+	/* name[12]     flags                                   code */
 	{"subnet",      OPTION_IP | OPTION_REQ,                 0x01},
 	{"timezone",    OPTION_S32,                             0x02},
 	{"router",      OPTION_IP | OPTION_LIST | OPTION_REQ,   0x03},
 	{"timesvr",     OPTION_IP | OPTION_LIST,                0x04},
 	{"namesvr",     OPTION_IP | OPTION_LIST,                0x05},
@@ -41,10 +41,13 @@
 	{"vendorclass", OPTION_STRING,                          0x3C},
 	{"clientid",    OPTION_STRING,                          0x3D},
 	{"tftp",        OPTION_STRING,                          0x42},
 	{"bootfile",    OPTION_STRING,                          0x43},
 	{"userclass",   OPTION_STRING,                          0x4D},
+#if ENABLE_FEATURE_RFC3397
+	{"search",      OPTION_STR1035 | OPTION_LIST | OPTION_REQ, 0x77},
+#endif
 	/* MSIE's "Web Proxy Autodiscovery Protocol" support */
 	{"wpad",        OPTION_STRING,                          0xfc},
 	{"",            0x00,                                   0x00}
 };
 
@@ -52,10 +55,13 @@
 const unsigned char option_lengths[] = {
 	[OPTION_IP] =      4,
 	[OPTION_IP_PAIR] = 8,
 	[OPTION_BOOLEAN] = 1,
 	[OPTION_STRING] =  1,
+#if ENABLE_FEATURE_RFC3397
+	[OPTION_STR1035] = 1,
+#endif
 	[OPTION_U8] =      1,
 	[OPTION_U16] =     2,
 	[OPTION_S16] =     2,
 	[OPTION_U32] =     4,
 	[OPTION_S32] =     4
diff -NarU5 busybox-svn-17929.orig/networking/udhcp/options.h busybox-svn-17929/networking/udhcp/options.h
--- busybox-svn-17929.orig/networking/udhcp/options.h	2007-02-19 15:33:43.000000000 -0500
+++ busybox-svn-17929/networking/udhcp/options.h	2007-02-24 09:41:22.000000000 -0500
@@ -7,10 +7,13 @@
 
 enum {
 	OPTION_IP=1,
 	OPTION_IP_PAIR,
 	OPTION_STRING,
+#if ENABLE_FEATURE_RFC3397
+	OPTION_STR1035,	/* RFC1035 compressed domain name list */
+#endif
 	OPTION_BOOLEAN,
 	OPTION_U8,
 	OPTION_U16,
 	OPTION_S16,
 	OPTION_U32,
@@ -31,7 +34,11 @@
 
 uint8_t *get_option(struct dhcpMessage *packet, int code);
 int end_option(uint8_t *optionptr);
 int add_option_string(uint8_t *optionptr, uint8_t *string);
 int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data);
+#if ENABLE_FEATURE_RFC3397
+char *dname_dec(const uint8_t *cstr, int clen, const char *pre);
+uint8_t *dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen);
+#endif
 
 #endif
diff -NarU5 busybox-svn-17929.orig/networking/udhcp/script.c busybox-svn-17929/networking/udhcp/script.c
--- busybox-svn-17929.orig/networking/udhcp/script.c	2007-02-19 15:33:43.000000000 -0500
+++ busybox-svn-17929/networking/udhcp/script.c	2007-02-24 09:41:22.000000000 -0500
@@ -17,10 +17,13 @@
 /* get a rough idea of how long an option will be (rounding up...) */
 static const int max_option_length[] = {
 	[OPTION_IP] =		sizeof("255.255.255.255 "),
 	[OPTION_IP_PAIR] =	sizeof("255.255.255.255 ") * 2,
 	[OPTION_STRING] =	1,
+#if ENABLE_FEATURE_RFC3397
+	[OPTION_STR1035] =  1,
+#endif
 	[OPTION_BOOLEAN] =	sizeof("yes "),
 	[OPTION_U8] =		sizeof("255 "),
 	[OPTION_U16] =		sizeof("65535 "),
 	[OPTION_S16] =		sizeof("-32768 "),
 	[OPTION_U32] =		sizeof("4294967295 "),
@@ -51,25 +54,27 @@
 	for (i = 0; i < 32 && !((bits >> i) & 1); i++);
 	return 32 - i;
 }
 
 
-/* Fill dest with the text of option 'option'. */
-static void fill_options(char *dest, uint8_t *option,
-			const struct dhcp_option *type_p)
+/* Allocate and fill with the text of option 'option'. */
+static char *alloc_fill_opts(uint8_t *option, const struct dhcp_option *type_p)
 {
-	int type, optlen;
+	int len, type, optlen;
 	uint16_t val_u16;
 	int16_t val_s16;
 	uint32_t val_u32;
 	int32_t val_s32;
-	int len = option[OPT_LEN - 2];
-
-	dest += sprintf(dest, "%s=", type_p->name);
+	char *dest, *ret;
 
+	len = option[OPT_LEN - 2];
 	type = type_p->flags & TYPE_MASK;
 	optlen = option_lengths[type];
+
+	dest = ret = xmalloc(upper_length(len, type) + strlen(type_p->name) + 2);
+	dest += sprintf(ret, "%s=", type_p->name);
+
 	for (;;) {
 		switch (type) {
 		case OPTION_IP_PAIR:
 			dest += sprintip(dest, "", option);
 			*(dest++) = '/';
@@ -101,17 +106,25 @@
 			dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
 			break;
 		case OPTION_STRING:
 			memcpy(dest, option, len);
 			dest[len] = '\0';
-			return;	 /* Short circuit this case */
+			return ret;	 /* Short circuit this case */
+#if ENABLE_FEATURE_RFC3397
+		case OPTION_STR1035:
+			/* unpack option into dest; use ret for prefix (i.e., "optname=") */
+			dest = dname_dec(option, len, ret);
+			free(ret);
+			return dest;
+#endif
 		}
 		option += optlen;
 		len -= optlen;
 		if (len <= 0) break;
 		dest += sprintf(dest, " ");
 	}
+	return ret;
 }
 
 
 /* put all the parameters into an environment */
 static char **fill_envp(struct dhcpMessage *packet)
@@ -153,13 +166,11 @@
 
 	for (i = 0; dhcp_options[i].code; i++) {
 		temp = get_option(packet, dhcp_options[i].code);
 		if (!temp)
 			continue;
-		envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2],
-			dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2);
-		fill_options(envp[j++], temp, &dhcp_options[i]);
+		envp[j++] = alloc_fill_opts(temp, &dhcp_options[i]);
 
 		/* Fill in a subnet bits option for things like /24 */
 		if (dhcp_options[i].code == DHCP_SUBNET) {
 			memcpy(&subnet, temp, 4);
 			envp[j++] = xasprintf("mask=%d", mton(&subnet));



More information about the busybox mailing list