changeset 32:59e2c67f7e9b draft

Fix cleanmetas function and fix FreeBSD again
author Roy Marples <roy@marples.name>
date Fri, 15 Dec 2006 14:47:01 +0000
parents 0e37362216a1
children ad8baa1bbf62
files ChangeLog Makefile client.c configure.c dhcp.c dhcp.h signals.c socket.c
diffstat 8 files changed, 69 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Dec 15 11:44:23 2006 +0000
+++ b/ChangeLog	Fri Dec 15 14:47:01 2006 +0000
@@ -1,3 +1,5 @@
+cleanmetas now inserts a \ when it finds a ' so we get the proper
+values in our .info files when read by a shell.
 Add new CFLAGS to ensure that the code quality is good.
 Use const more in public functions.
 
--- a/Makefile	Fri Dec 15 11:44:23 2006 +0000
+++ b/Makefile	Fri Dec 15 14:47:01 2006 +0000
@@ -1,6 +1,6 @@
 # Should work for both GNU make and BSD make
 
-VERSION = 3.0.7_pre2
+VERSION = 3.0.7_pre3
 
 INSTALL ?= install
 
@@ -9,7 +9,7 @@
 # Loads of nice flags to ensure our code is good
 # We define _BSD_SOURCE for maximum portability
 CFLAGS += -D_BSD_SOURCE -pedantic -std=c99 \
-    -Wall -Werror -Wunused -Wimplicit -Wshadow -Wformat=2 \
+    -Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \
     -Wmissing-declarations -Wno-missing-prototypes -Wwrite-strings \
     -Wbad-function-cast -Wnested-externs -Wcomment -Winline \
     -Wchar-subscripts -Wcast-align -Wno-format-nonliteral
@@ -18,6 +18,10 @@
 # this line out
 CFLAGS += -Wsequence-point -Wextra -Wdeclaration-after-statement
 
+# -Werrror is a good flag to use for development, but some platforms may
+#  have buggy headers from time to time, so you may need to comment this out
+#CFLAGS += -Werror
+
 DESTDIR =
 SBINDIR = $(DESTDIR)/sbin
 MANDIR = $(DESTDIR)/usr/share/man
--- a/client.c	Fri Dec 15 11:44:23 2006 +0000
+++ b/client.c	Fri Dec 15 14:47:01 2006 +0000
@@ -81,7 +81,7 @@
   send_message (iface, dhcp, xid, _type, options); \
 }
 
-static int daemonise (char *pidfile)
+static int daemonise (const char *pidfile)
 {
   logger (LOG_DEBUG, "forking to background");
   if (daemon (0, 0) < 0)
@@ -159,12 +159,6 @@
   dhcp = xmalloc (sizeof (dhcp_t));
   memset (dhcp, 0, sizeof (dhcp_t));
 
-  strcpy (dhcp->classid, options->classid);
-  if (options->clientid[0])
-    strcpy (dhcp->clientid, options->clientid);
-  else
-    sprintf (dhcp->clientid, "%s", ether_ntoa (&iface->ethernet_address));
-
   if (options->requestaddress.s_addr != 0)
     dhcp->address.s_addr = options->requestaddress.s_addr;
 
--- a/configure.c	Fri Dec 15 11:44:23 2006 +0000
+++ b/configure.c	Fri Dec 15 14:47:01 2006 +0000
@@ -46,19 +46,33 @@
 #include "logger.h"
 #include "socket.h"
 
-static char *cleanmetas (char *cstr)
+static char *cleanmetas (const char *cstr)
 {
-  register char *c = cstr;
-  
-  if (! cstr)
-    return NULL;
-  
+  /* The largest single element we can have is 256 bytes according to the RFC,
+     so this buffer size should be safe even if it's all ' */
+  char buffer[1024] = {0};
+  register char *c = (char *) cstr;
+  register char *b = buffer;
+
+  if (! cstr || strlen (cstr) == 0)
+      return b;
+
   do
     if (*c == 39)
-      *c = ' ';
+      {
+	*b++ = '\'';
+	*b++ = '\\';
+	*b++ = '\'';
+	*b++ = '\'';
+      }
+    else
+      *b++ = *c;
   while (*c++);
-  
-  return cstr;
+
+  *b++ = 0;
+  b = buffer;
+
+  return b;
 }
 
 static void exec_script (const char *script, const char *infofile,
@@ -68,7 +82,7 @@
   pid_t pid;
   char *const argc[4] =
     { (char *) script, (char *) infofile, (char *) arg, NULL };
- 
+
   if (! script || ! infofile || ! arg)
     return;
 
@@ -78,9 +92,9 @@
 	logger (LOG_ERR, "`%s': %s", script, strerror (ENOENT));
       return;
     }
-  
+
   logger (LOG_DEBUG, "exec \"%s %s %s\"", script, infofile, arg);
- 
+
   /* We don't wait for the user script to finish - do we trust it? */
   /* Don't use vfork as we lose our memory when dhcpcd exits
      causing the script to fail */
@@ -183,7 +197,7 @@
       logger (LOG_ERR, "fopen `%s': %s", NTPFILE, strerror (errno));
       return -1;
     }
-	  
+
   fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname);
   fprintf (f, "restrict default noquery notrust nomodify\n");
   fprintf (f, "restrict 127.0.0.1\n");
@@ -230,11 +244,12 @@
     fprintf (f, "%s %s\n", prefix, inet_ntoa (address->address));
 
   fclose (f);
-  
+
   return 0;
 }
 
-static int write_info(const interface_t *iface, const dhcp_t *dhcp)
+static int write_info(const interface_t *iface, const dhcp_t *dhcp,
+		      const options_t *options)
 {
   FILE *f;
   route_t *route;
@@ -252,7 +267,7 @@
   fprintf (f, "BROADCAST='%s'\n", inet_ntoa (dhcp->broadcast));
   if (dhcp->mtu > 0)
     fprintf (f, "MTU='%d'\n", dhcp->mtu);
-  
+
   if (dhcp->routes)
     {
       fprintf (f, "ROUTES='");
@@ -322,20 +337,22 @@
 	}
       fprintf (f, "'\n");
     }
- 
+
   if (dhcp->rootpath)
     fprintf (f, "ROOTPATH='%s'\n", cleanmetas (dhcp->rootpath));
 
   fprintf (f, "DHCPSID='%s'\n", inet_ntoa (dhcp->serveraddress));
-  fprintf (f, "DHCPCHADDR='%s'\n", ether_ntoa (&iface->ethernet_address));
   fprintf (f, "DHCPSNAME='%s'\n", cleanmetas (dhcp->servername));
   fprintf (f, "LEASETIME='%u'\n", dhcp->leasetime);
   fprintf (f, "RENEWALTIME='%u'\n", dhcp->renewaltime);
   fprintf (f, "REBINDTIME='%u'\n", dhcp->rebindtime);
   fprintf (f, "INTERFACE='%s'\n", iface->name);
-  fprintf (f, "CLASSID='%s'\n", cleanmetas (dhcp->classid));
-  fprintf (f, "CLIENTID='%s'\n", cleanmetas (dhcp->clientid));
-
+  fprintf (f, "CLASSID='%s'\n", cleanmetas (options->classid));
+  if (options->clientid[0])
+    fprintf (f, "CLIENTID='%s'\n", cleanmetas (options->clientid));
+  else
+    fprintf (f, "CLIENTID='%s'\n", ether_ntoa (&iface->ethernet_address));
+  fprintf (f, "DHCPCHADDR='%s'\n", ether_ntoa (&iface->ethernet_address));
   fclose (f);
   return 0;
 }
@@ -351,7 +368,7 @@
   char curhostname[HOSTNAME_MAX_LEN] = {0};
   char *dname = NULL;
   int dnamel = 0;
- 
+
   if (! options || ! iface || ! dhcp)
     return -1;
 
@@ -369,10 +386,10 @@
 		if (new_route->destination.s_addr == route->destination.s_addr
 		    && new_route->netmask.s_addr == route->netmask.s_addr
 		    && new_route->gateway.s_addr == route->gateway.s_addr)
-		   {
-		     have = 1;
-		     break;
-		   }
+		  {
+		    have = 1;
+		    break;
+		  }
 	    if (! have)
 	      del_route (iface->name, route->destination, route->netmask,
 			 route->gateway, options->metric);
@@ -409,7 +426,7 @@
   /* Now delete the old address if different */
   if (iface->previous_address.s_addr != dhcp->address.s_addr
       && iface->previous_address.s_addr != 0)
-  	del_address (iface->name, iface->previous_address);
+    del_address (iface->name, iface->previous_address);
 
 #ifdef __linux__
   /* On linux, we need to change the subnet route to have our metric. */
@@ -431,7 +448,7 @@
     {
       route_t *new_routes = NULL;
       int remember;
-      
+
       for (route = dhcp->routes; route; route = route->next)
 	{
 	  /* Don't set default routes if not asked to */
@@ -440,8 +457,8 @@
 	    continue;
 
 	  remember = add_route (iface->name, route->destination,
-				    route->netmask,  route->gateway,
-				    options->metric);
+				route->netmask,  route->gateway,
+				options->metric);
 	  /* If we failed to add the route, we may have already added it
 	     ourselves. If so, remember it again. */
 	  if (remember < 0)
@@ -507,7 +524,7 @@
     }
 
   gethostname (curhostname, sizeof (curhostname));
-  
+
   if (options->dohostname
       || strlen (curhostname) == 0
       || strcmp (curhostname, "(none)") == 0
@@ -523,9 +540,9 @@
 	}
     }
 
-  write_info (iface, dhcp);
+  write_info (iface, dhcp, options);
 
-   if (iface->previous_address.s_addr != dhcp->address.s_addr)
+  if (iface->previous_address.s_addr != dhcp->address.s_addr)
     {
       memcpy (&iface->previous_address,
 	      &dhcp->address, sizeof (struct in_addr));
--- a/dhcp.c	Fri Dec 15 11:44:23 2006 +0000
+++ b/dhcp.c	Fri Dec 15 14:47:01 2006 +0000
@@ -464,9 +464,9 @@
 int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message)
 {
   unsigned char *p = message->options;
+  unsigned char *end = message->options; /* Add size later for gcc-3 issue */
   unsigned char option;
   unsigned char length;
-  unsigned char *end = message->options + sizeof (message->options);
   unsigned int len = 0;
   int i;
   int retval = -1;
@@ -474,15 +474,11 @@
   route_t *route = first_route;
   route_t *last_route = NULL;
   route_t *csr = NULL;
-  char classid[CLASS_ID_MAX_LEN];
-  char clientid[CLIENT_ID_MAX_LEN];
+
+  end += sizeof (message->options);
 
   memset (first_route, 0, sizeof (route_t));
 
-  /* The message back never has the class or client id's so we save them */
-  strcpy (classid, dhcp->classid);
-  strcpy (clientid, dhcp->clientid);
-
   dhcp->address.s_addr = message->yiaddr;
   strcpy (dhcp->servername, message->servername);
 
@@ -655,10 +651,6 @@
 	}
     }
 
-  /* The message back never has the class or client id's so we restore them */
-  strcpy (dhcp->classid, classid);
-  strcpy (dhcp->clientid, clientid);
-
   return retval;
 }
 
--- a/dhcp.h	Fri Dec 15 11:44:23 2006 +0000
+++ b/dhcp.h	Fri Dec 15 14:47:01 2006 +0000
@@ -123,9 +123,6 @@
   struct in_addr serveraddress;
   char serverhw[IF_NAMESIZE];
   char servername[64];
-
-  char classid[CLASS_ID_MAX_LEN];
-  char clientid[CLIENT_ID_MAX_LEN];
   
   struct in_addr address;
   struct in_addr netmask;
--- a/signals.c	Fri Dec 15 11:44:23 2006 +0000
+++ b/signals.c	Fri Dec 15 14:47:01 2006 +0000
@@ -38,7 +38,6 @@
     logger (LOG_ERR, "Could not send signal: %s", strerror (errno));
 }
 
-
 /* Call this before doing anything else. Sets up the socket pair
  * and installs the signal handler */
 void signal_setup(void)
@@ -60,7 +59,6 @@
   signal (SIGINT, signal_handler);
 }
 
-
 /* Quick little function to setup the rfds. Will return the
  * max_fd for use with select. Limited in that you can only pass
  * one extra fd */
@@ -73,7 +71,6 @@
   return signal_pipe[0] > extra_fd ? signal_pipe[0] : extra_fd;
 }
 
-
 /* Read a signal from the signal pipe. Returns 0 if there is
  * no signal, -1 on error (and sets errno appropriately), and
  * your signal on success */
@@ -81,7 +78,7 @@
 {
   int sig;
 
-  if (!FD_ISSET (signal_pipe[0], rfds))
+  if (! FD_ISSET (signal_pipe[0], rfds))
     return 0;
 
   if (read (signal_pipe[0], &sig, sizeof (sig)) < 0)
--- a/socket.c	Fri Dec 15 11:44:23 2006 +0000
+++ b/socket.c	Fri Dec 15 14:47:01 2006 +0000
@@ -117,7 +117,7 @@
   ip->ip_sum = checksum ((unsigned char *) ip, sizeof (struct ip));
 }
 
-static int valid_dhcp_packet (unsigned char * data)
+static int valid_dhcp_packet (unsigned char *data)
 {
   struct udp_dhcp_packet *packet = (struct udp_dhcp_packet *) data;
   uint16_t bytes = ntohs (packet->ip.ip_len);
@@ -212,7 +212,7 @@
 int open_socket (interface_t *iface, bool arp)
 {
   int n = 0;
-  int fd = 0;
+  int fd = -1;
   char device[PATH_MAX];
   int flags;
   struct ifreq ifr;
@@ -239,6 +239,7 @@
       return -1;
     }
 
+  memset (&ifr, 0, sizeof (struct ifreq));
   strncpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name));
   if (ioctl (fd, BIOCSETIF, &ifr) < 0)
     {
@@ -257,6 +258,7 @@
     }
   iface->buffer_length = buf;
 
+  flags = 1;
   if (ioctl (fd, BIOCIMMEDIATE, &flags) < 0)
     {
       logger (LOG_ERR, "ioctl BIOCIMMEDIATE: %s", strerror (errno));
@@ -267,6 +269,7 @@
   /* Install the DHCP filter */
   if (arp)
     {
+      printf ("arp!");
       p.bf_insns = arp_bpf_filter;
       p.bf_len = sizeof (arp_bpf_filter) / sizeof (struct bpf_insn);
     }
@@ -466,7 +469,7 @@
   long bytes;
   struct udp_dhcp_packet *dhcp;
 
- /* We don't use the given buffer, but we need to rewind the position */
+  /* We don't use the given buffer, but we need to rewind the position */
   *buffer_pos = 0;
 
   memset (buffer, 0, iface->buffer_length);