svn commit: trunk/busybox/networking/udhcp

vda at busybox.net vda at busybox.net
Sun Sep 30 17:54:12 UTC 2007


Author: vda
Date: 2007-09-30 10:54:10 -0700 (Sun, 30 Sep 2007)
New Revision: 20148

Log:
dhcpreplay: code shrink, -40 bytes



Modified:
   trunk/busybox/networking/udhcp/dhcprelay.c


Changeset:
Modified: trunk/busybox/networking/udhcp/dhcprelay.c
===================================================================
--- trunk/busybox/networking/udhcp/dhcprelay.c	2007-09-30 16:36:02 UTC (rev 20147)
+++ trunk/busybox/networking/udhcp/dhcprelay.c	2007-09-30 17:54:10 UTC (rev 20148)
@@ -11,17 +11,15 @@
  */
 
 #include "common.h"
-#include "dhcpd.h"
 #include "options.h"
 
 /* constants */
-#define SELECT_TIMEOUT 5 /* select timeout in sec. */
-#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
-#define MAX_INTERFACES 9
+#define SELECT_TIMEOUT    5 /* select timeout in sec. */
+#define MAX_LIFETIME   2*60 /* lifetime of an xid entry in sec. */
 
 /* This list holds information about clients. The xid_* functions manipulate this list. */
 struct xid_item {
-	time_t timestamp;
+	unsigned timestamp;
 	int client;
 	uint32_t xid;
 	struct sockaddr_in ip;
@@ -41,7 +39,7 @@
 	item->ip = *ip;
 	item->xid = xid;
 	item->client = client;
-	item->timestamp = time(NULL);
+	item->timestamp = monotonic_sec();
 	item->next = dhcprelay_xid_list.next;
 	dhcprelay_xid_list.next = item;
 
@@ -52,7 +50,7 @@
 {
 	struct xid_item *item = dhcprelay_xid_list.next;
 	struct xid_item *last = &dhcprelay_xid_list;
-	time_t current_time = time(NULL);
+	unsigned current_time = monotonic_sec();
 
 	while (item != NULL) {
 		if ((current_time - item->timestamp) > MAX_LIFETIME) {
@@ -120,31 +118,32 @@
  */
 static char **get_client_devices(char *dev_list, int *client_number)
 {
-	char *s, *list, **client_dev;
+	char *s, **client_dev;
 	int i, cn;
 
 	/* copy list */
-	list = xstrdup(dev_list);
-	if (list == NULL) return NULL;
+	dev_list = xstrdup(dev_list);
 
-	/* get number of items */
-	for (s = dev_list, cn = 1; *s; s++)
-		if (*s == ',')
+	/* get number of items, replace ',' with NULs */
+	s = dev_list;
+	cn = 1;
+	while (*s) {
+		if (*s == ',') {
+			*s = '\0';
 			cn++;
+		}
+		s++;
+	}
+	*client_number = cn;
 
+	/* create vector of pointers */
 	client_dev = xzalloc(cn * sizeof(*client_dev));
-
-	/* parse list */
-	s = strtok(list, ",");
-	i = 0;
-	while (s != NULL) {
-		client_dev[i++] = xstrdup(s);
-		s = strtok(NULL, ",");
+	client_dev[0] = dev_list;
+	i = 1;
+	while (i != cn) {
+		client_dev[i] = client_dev[i - 1] + strlen(client_dev[i - 1]) + 1;
+		i++;
 	}
-
-	/* free copy and exit */
-	free(list);
-	*client_number = cn;
 	return client_dev;
 }
 
@@ -161,7 +160,7 @@
 
 	for (i = 1; i < num_clients; i++) {
 		/* listen for clients on bootps */
-		fds[i] = listen_socket(/*NADDR_ANY,*/ 67, client[i-1]);
+		fds[i] = listen_socket(/*INADDR_ANY,*/ 67, client[i-1]);
 		if (fds[i] > n)
 			n = fds[i];
 	}
@@ -224,8 +223,6 @@
 
 	if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
 		item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
-	if (item->client > MAX_INTERFACES)
-		return;
 	res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
 				sizeof(item->ip));
 	if (res != packet_len) {
@@ -285,7 +282,8 @@
 int dhcprelay_main(int argc, char **argv);
 int dhcprelay_main(int argc, char **argv)
 {
-	int num_sockets, max_socket, fds[MAX_INTERFACES];
+	int num_sockets, max_socket;
+	int *fds;
 	uint32_t gw_ip;
 	char **clients;
 	struct sockaddr_in server_addr;
@@ -300,10 +298,10 @@
 	} else {
 		bb_show_usage();
 	}
-	clients = get_client_devices(argv[1], &num_sockets);
-	if (!clients) return 0;
 
+	clients = get_client_devices(argv[1], &num_sockets);
 	num_sockets++; /* for server socket at fds[0] */
+	fds = xmalloc(num_sockets * sizeof(fds[0]));
 	max_socket = init_sockets(clients, num_sockets, argv[2], fds);
 
 	if (read_interface(argv[2], NULL, &gw_ip, NULL))




More information about the busybox-cvs mailing list