changeset 61:835ca00adaa9 draft

Stop using sleeps as it may mess with our signal handler. Display cidr when adding/deleting addreses.
author Roy Marples <roy@marples.name>
date Wed, 21 Feb 2007 10:30:50 +0000
parents 76274880aa25
children f4e33f87c803
files client.c interface.c interface.h socket.c
diffstat 4 files changed, 38 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/client.c	Tue Feb 20 22:57:10 2007 +0000
+++ b/client.c	Wed Feb 21 10:30:50 2007 +0000
@@ -515,7 +515,9 @@
 			  /* RFC 2131 says that we should wait for 10 seconds
 			     before doing anything else */
 			  logger (LOG_INFO, "sleeping for 10 seconds");
-			  sleep (10);
+			  tv.tv_sec = 10;
+			  tv.tv_usec = 0;
+			  select (0, NULL, NULL, NULL, &tv);
 			  continue;
 			}
 		    }
@@ -536,9 +538,8 @@
 				 "no lease time supplied, assuming %d seconds",
 				 dhcp->leasetime);
 			}
-		      else
-			logger (LOG_INFO, "leased %s for %u seconds",
-				inet_ntoa (dhcp->address), dhcp->leasetime);
+		      logger (LOG_INFO, "leased %s for %u seconds",
+			      inet_ntoa (dhcp->address), dhcp->leasetime);
 
 		      if (dhcp->rebindtime >= dhcp->leasetime)
 			{
--- a/interface.c	Tue Feb 20 22:57:10 2007 +0000
+++ b/interface.c	Wed Feb 21 10:30:50 2007 +0000
@@ -91,6 +91,19 @@
     }
 }
 
+int inet_ntocidr (struct in_addr address)
+{
+  int cidr = 0;
+  uint32_t mask = htonl (address.s_addr);
+  
+  while (mask)
+    {
+      cidr++;
+      mask <<= 1;
+    }
+
+  return (cidr);
+}
 
 char *hwaddr_ntoa (const unsigned char *hwaddr, int hwlen)
 {
@@ -337,23 +350,20 @@
   metric = 0;
 
   dstd = strdup (inet_ntoa (destination));
-  gend = strdup (inet_ntoa (netmask));
   if (gateway.s_addr == destination.s_addr)
-    logger (LOG_INFO, "%s route to %s (%s)",
+    logger (LOG_INFO, "%s route to %s/%d",
 	    change ? "changing" : del ? "removing" : "adding",
-	    dstd, gend);
+	    dstd, gend, inet_ntocidr (netmask));
   else if (destination.s_addr == INADDR_ANY && netmask.s_addr == INADDR_ANY)
     logger (LOG_INFO, "%s default route via %s",
 	    change ? "changing" : del ? "removing" : "adding",
 	    inet_ntoa (gateway));
   else
-    logger (LOG_INFO, "%s route to %s (%s) via %s",
+    logger (LOG_INFO, "%s route to %s/%d via %s",
 	    change ? "changing" : del ? "removing" : "adding",
-	    dstd, gend, inet_ntoa (gateway));
+	    dstd, inet_ntocidr (netmask), inet_ntoa (gateway));
   if (dstd)
     free (dstd);
-  if (gend)
-    free (gend);
 
   if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) 
     {
@@ -645,7 +655,6 @@
       char buffer[64];
     }
   nlm;
-  uint32_t mask = htonl (netmask.s_addr);
 
   if (!ifname)
     return -1;
@@ -660,13 +669,7 @@
   nlm.ifa.ifa_index = if_nametoindex (ifname);
   nlm.ifa.ifa_family = AF_INET;
 
-  /* Store the netmask in the prefix */
-  while (mask)
-    {
-      nlm.ifa.ifa_prefixlen++;
-      mask <<= 1;
-    }
-
+  nlm.ifa.ifa_prefixlen = inet_ntocidr (netmask);
   add_attr_l (&nlm.hdr, sizeof (nlm), IFA_LOCAL, &address.s_addr,
 	      sizeof (address.s_addr));
   if (! del)
@@ -691,7 +694,6 @@
       char buffer[256];
     }
   nlm;
-  uint32_t mask = htonl (netmask.s_addr);
 
   if (! ifname)
     return -1;
@@ -741,13 +743,7 @@
       nlm.rt.rtm_type = RTN_UNICAST;
     }
 
-  /* Store the netmask in the prefix */
-  while (mask)
-    {
-      nlm.rt.rtm_dst_len++;
-      mask <<= 1;
-    }
-
+  nlm.rt.rtm_dst_len = inet_ntocidr (netmask);
   add_attr_l (&nlm.hdr, sizeof (nlm), RTA_DST, &destination.s_addr,
 	      sizeof (destination.s_addr));
   if (gateway.s_addr != INADDR_ANY && gateway.s_addr != destination.s_addr)
@@ -771,10 +767,8 @@
 int add_address (const char *ifname, struct in_addr address,
 		 struct in_addr netmask, struct in_addr broadcast)
 {
-  char *daddress = strdup (inet_ntoa (address));
-  logger (LOG_INFO, "adding IP address %s netmask %s",
-	  daddress, inet_ntoa (netmask));
-  free (daddress);
+  logger (LOG_INFO, "adding IP address %s/%d",
+	  inet_ntoa (address), inet_ntocidr (netmask));
 
   return (do_address (ifname, address, netmask, broadcast, 0));
 }
@@ -783,11 +777,9 @@
 		 struct in_addr address, struct in_addr netmask)
 {
   struct in_addr t;
-  char *addr = strdup (inet_ntoa (address));
 
-  logger (LOG_INFO, "deleting IP address %s netmask %s", addr,
-	  inet_ntoa (netmask));
-  free (addr);
+  logger (LOG_INFO, "deleting IP address %s/%d",
+	  inet_ntoa (address), inet_ntocidr (netmask));
 
   memset (&t, 0, sizeof (t));
   return (do_address (ifname, address, netmask, t, 1));
--- a/interface.h	Tue Feb 20 22:57:10 2007 +0000
+++ b/interface.h	Wed Feb 21 10:30:50 2007 +0000
@@ -94,6 +94,7 @@
 int del_route (const char *ifname, struct in_addr destination,
 	       struct in_addr netmask, struct in_addr gateway, int metric);
 
+int inet_ntocidr (struct in_addr address);
 char *hwaddr_ntoa (const unsigned char *hwaddr, int hwlen);
 
 #endif
--- a/socket.c	Tue Feb 20 22:57:10 2007 +0000
+++ b/socket.c	Wed Feb 21 10:30:50 2007 +0000
@@ -21,6 +21,7 @@
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
+#include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/uio.h>
 #include <arpa/inet.h>
@@ -361,8 +362,11 @@
       *buffer_pos = 0;
       if (*buffer_len < 1)
 	{
+	  struct timeval tv;
 	  logger (LOG_ERR, "read: %s", strerror (errno));
-	  sleep (3);
+	  tv.tv_sec = 3;
+	  tv.tv_usec = 0;
+	  select (0, NULL, NULL, NULL, &tv);
 	  return -1;
 	}
     }
@@ -520,8 +524,11 @@
   bytes = read (iface->fd, buffer, iface->buffer_length);
   if (bytes < 0)
     {
+      struct timeval tv;
       logger (LOG_ERR, "read: %s", strerror (errno));
-      sleep (3);
+      tv.tv_sec = 3;
+      tv.tv_usec = 0;
+      select (0, NULL, NULL, NULL, &tv);
       return -1;
     }