]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Fix cleanmetas function and fix FreeBSD again
authorRoy Marples <roy@marples.name>
Fri, 15 Dec 2006 14:47:01 +0000 (14:47 +0000)
committerRoy Marples <roy@marples.name>
Fri, 15 Dec 2006 14:47:01 +0000 (14:47 +0000)
ChangeLog
Makefile
client.c
configure.c
dhcp.c
dhcp.h
signals.c
socket.c

index 6b49b0f67c784712cca60d0ffd386b41a88ab0cd..b9e77f96866aecd2059a7f2ebd1b6177a8a68376 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
 
index cb321062551ccb424e1eee15cd2c229093e1bc08..ca7fae6b59308d69f86336dc90046c62e5673702 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -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 @@ CFLAGS ?= -O2 -pipe
 # 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 @@ CFLAGS += -D_BSD_SOURCE -pedantic -std=c99 \
 # 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
index ea162ce5a5c89bbb40317e00b1804b2f8ee3baba..3d1e8763035afaf366cea942c226c9bf323b7691 100644 (file)
--- a/client.c
+++ b/client.c
@@ -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 @@ int dhcp_run (const options_t *options)
   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;
 
index 8ef35f5a84e7f68daa8d5945862a3f50ae0c123c..d86b8eba43894cff7ab2911e92b0f351eae6c04b 100644 (file)
 #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 @@ static void exec_script (const char *script, const char *infofile,
   pid_t pid;
   char *const argc[4] =
     { (char *) script, (char *) infofile, (char *) arg, NULL };
+
   if (! script || ! infofile || ! arg)
     return;
 
@@ -78,9 +92,9 @@ static void exec_script (const char *script, const char *infofile,
        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 @@ static int make_ntp (const char *ifname, const dhcp_t *dhcp)
       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 @@ static int make_nis (const char *ifname, const dhcp_t *dhcp)
     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 @@ static int write_info(const interface_t *iface, const dhcp_t *dhcp)
   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 @@ static int write_info(const interface_t *iface, const dhcp_t *dhcp)
        }
       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 @@ int configure (const options_t *options, interface_t *iface,
   char curhostname[HOSTNAME_MAX_LEN] = {0};
   char *dname = NULL;
   int dnamel = 0;
+
   if (! options || ! iface || ! dhcp)
     return -1;
 
@@ -369,10 +386,10 @@ int configure (const options_t *options, interface_t *iface,
                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 @@ int configure (const options_t *options, interface_t *iface,
   /* 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 @@ int configure (const options_t *options, interface_t *iface,
     {
       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 @@ int configure (const options_t *options, interface_t *iface,
            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 @@ int configure (const options_t *options, interface_t *iface,
     }
 
   gethostname (curhostname, sizeof (curhostname));
-  
+
   if (options->dohostname
       || strlen (curhostname) == 0
       || strcmp (curhostname, "(none)") == 0
@@ -523,9 +540,9 @@ int configure (const options_t *options, interface_t *iface,
        }
     }
 
-  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));
diff --git a/dhcp.c b/dhcp.c
index 0ca862fc21a1c273cc64ca14edff604e70780f3c..7f5cd0ee2cbeaf11edcca9dd5431b287a3302a58 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -464,9 +464,9 @@ static void dhcp_add_address(address_t *address, unsigned char *data, int length
 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,14 +474,10 @@ int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message)
   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];
 
-  memset (first_route, 0, sizeof (route_t));
+  end += sizeof (message->options);
 
-  /* The message back never has the class or client id's so we save them */
-  strcpy (classid, dhcp->classid);
-  strcpy (clientid, dhcp->clientid);
+  memset (first_route, 0, sizeof (route_t));
 
   dhcp->address.s_addr = message->yiaddr;
   strcpy (dhcp->servername, message->servername);
@@ -655,10 +651,6 @@ eexit:
        }
     }
 
-  /* 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;
 }
 
diff --git a/dhcp.h b/dhcp.h
index 7b923fc1858b612304bedb35460e8373e29e0df8..64de616169065654a0bc86c9469cfb8cb4ce8dac 100644 (file)
--- a/dhcp.h
+++ b/dhcp.h
@@ -123,9 +123,6 @@ typedef struct dhcp_t
   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;
index a292a0f15e497846e42cc3f643d23eb813ea3204..d3af679f75f4269dcc196a889e18f2886b320a67 100644 (file)
--- a/signals.c
+++ b/signals.c
@@ -38,7 +38,6 @@ static void signal_handler (int sig)
     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 @@ void signal_setup(void)
   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 @@ int signal_fd_set (fd_set *rfds, int extra_fd)
   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 signal_read (const fd_set *rfds)
 {
   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)
index 48c5fa0d90030045374cf0f893e3d75c9a1e4c09..0f7e0ef4eec3a6123fa98f48583c9f2e9ba8df30 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -117,7 +117,7 @@ void make_dhcp_packet(struct udp_dhcp_packet *packet,
   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 @@ static struct bpf_insn arp_bpf_filter [] = {
 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 @@ int open_socket (interface_t *iface, bool arp)
       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 @@ int open_socket (interface_t *iface, bool arp)
     }
   iface->buffer_length = buf;
 
+  flags = 1;
   if (ioctl (fd, BIOCIMMEDIATE, &flags) < 0)
     {
       logger (LOG_ERR, "ioctl BIOCIMMEDIATE: %s", strerror (errno));
@@ -267,6 +269,7 @@ int open_socket (interface_t *iface, bool arp)
   /* 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 @@ int get_packet (const interface_t *iface, unsigned char *data,
   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);