udhcpc size reduction

Paul Fox pgf at brightstareng.com
Wed Sep 21 20:41:30 UTC 2005


udhcpc and udhcpd share some source files.  i noticed this morning
that there were a couple of routines in the shared options.c file
that are only called from udhcpd's file.c.  so i've moved those
routines, for a savings of 288 bytes.  (if udhcpd is configured,
there's no savings, of course.)

i've built all three combinations of applets, they all build.  i'll
commit soon.

paul
=---------------------
 paul fox, pgf at brightstareng.com

Index: networking/udhcp/options.h
===================================================================
--- networking/udhcp/options.h	(revision 11511)
+++ networking/udhcp/options.h	(working copy)
@@ -34,7 +34,5 @@
 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);
-struct option_set *find_option(struct option_set *opt_list, char code);
-void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length);
 
 #endif
Index: networking/udhcp/options.c
===================================================================
--- networking/udhcp/options.c	(revision 11511)
+++ networking/udhcp/options.c	(working copy)
@@ -7,8 +7,8 @@
 #include <string.h>
 
 #include "dhcpd.h"
-#include "files.h"
 #include "options.h"
+#include "files.h"
 #include "common.h"
 
 
@@ -171,49 +171,3 @@
 	return 0;
 }
 
-
-/* find option 'code' in opt_list */
-struct option_set *find_option(struct option_set *opt_list, char code)
-{
-	while (opt_list && opt_list->data[OPT_CODE] < code)
-		opt_list = opt_list->next;
-
-	if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
-	else return NULL;
-}
-
-
-/* add an option to the opt_list */
-void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
-{
-	struct option_set *existing, *new, **curr;
-
-	/* add it to an existing option */
-	if ((existing = find_option(*opt_list, option->code))) {
-		DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
-		if (option->flags & OPTION_LIST) {
-			if (existing->data[OPT_LEN] + length <= 255) {
-				existing->data = realloc(existing->data,
-						existing->data[OPT_LEN] + length + 2);
-				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 */
-		} /* else, ignore the new data */
-	} else {
-		DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
-
-		/* 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;
-		memcpy(new->data + 2, buffer, length);
-
-		curr = opt_list;
-		while (*curr && (*curr)->data[OPT_CODE] < option->code)
-			curr = &(*curr)->next;
-
-		new->next = *curr;
-		*curr = new;
-	}
-}
Index: networking/udhcp/files.h
===================================================================
--- networking/udhcp/files.h	(revision 11511)
+++ networking/udhcp/files.h	(working copy)
@@ -14,4 +14,7 @@
 void write_leases(void);
 void read_leases(const char *file);
 
+struct option_set *find_option(struct option_set *opt_list, char code);
+void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length);
+
 #endif
Index: networking/udhcp/files.c
===================================================================
--- networking/udhcp/files.c	(revision 11511)
+++ networking/udhcp/files.c	(working copy)
@@ -15,8 +15,8 @@
 #include "static_leases.h"
 
 #include "dhcpd.h"
+#include "options.h"
 #include "files.h"
-#include "options.h"
 #include "common.h"
 
 /*
@@ -93,6 +93,52 @@
 }
 
 
+/* find option 'code' in opt_list */
+struct option_set *find_option(struct option_set *opt_list, char code)
+{
+	while (opt_list && opt_list->data[OPT_CODE] < code)
+		opt_list = opt_list->next;
+
+	if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
+	else return NULL;
+}
+
+
+/* add an option to the opt_list */
+void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
+{
+	struct option_set *existing, *new, **curr;
+
+	/* add it to an existing option */
+	if ((existing = find_option(*opt_list, option->code))) {
+		DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
+		if (option->flags & OPTION_LIST) {
+			if (existing->data[OPT_LEN] + length <= 255) {
+				existing->data = realloc(existing->data,
+						existing->data[OPT_LEN] + length + 2);
+				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 */
+		} /* else, ignore the new data */
+	} else {
+		DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
+
+		/* 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;
+		memcpy(new->data + 2, buffer, length);
+
+		curr = opt_list;
+		while (*curr && (*curr)->data[OPT_CODE] < option->code)
+			curr = &(*curr)->next;
+
+		new->next = *curr;
+		*curr = new;
+	}
+}
+
 /* read a dhcp option and add it to opt_list */
 static int read_opt(const char *const_line, void *arg)
 {



More information about the busybox mailing list