Eliminated send_delay global (file) variable.
[icmpmonitor] / icmpmonitor.c
index 52709af..f2bf43e 100644 (file)
@@ -27,6 +27,7 @@
 #include <netinet/ip.h>
 #include <netinet/ip_icmp.h>
 #include <errno.h>
 #include <netinet/ip.h>
 #include <netinet/ip_icmp.h>
 #include <errno.h>
+#include <assert.h>
 
 #include "iniparser/iniparser.h"
 
 
 #include "iniparser/iniparser.h"
 
 #define ICMP_ECHO_DATA_BYTES    sizeof(struct timeval)
 #define ICMP_ECHO_PACKET_BYTES  ICMP_ECHO_HEADER_BYTES + ICMP_ECHO_DATA_BYTES
 
 #define ICMP_ECHO_DATA_BYTES    sizeof(struct timeval)
 #define ICMP_ECHO_PACKET_BYTES  ICMP_ECHO_HEADER_BYTES + ICMP_ECHO_DATA_BYTES
 
+/* Minimum time in seconds between pings. If this value is increased above the */
+/* `ping_interval` for a given host, some pings to that host may not be sent.  */
+#define TIMER_RESOLUTION        1
+
 /* Must be larger than the length of the longest configuration key (currently 'start_condition'). */
 #define MAXCONFKEYLEN 20
 
 /* Must be larger than the length of the longest configuration key (currently 'start_condition'). */
 #define MAXCONFKEYLEN 20
 
@@ -66,7 +71,6 @@ struct monitor_host {
 /* Globals */
     /* Since the program is based around signals, a linked list of hosts is maintained here. */
     static struct monitor_host * hosts          = NULL;
 /* Globals */
     /* Since the program is based around signals, a linked list of hosts is maintained here. */
     static struct monitor_host * hosts          = NULL;
-    static int                   send_delay     = 1;
     /* Set by command line flags. */
     static bool                  verbose        = false;
     static bool                  retry_down_cmd = false;
     /* Set by command line flags. */
     static bool                  verbose        = false;
     static bool                  retry_down_cmd = false;
@@ -90,18 +94,13 @@ checksum(uint16_t * data)
 }
 
 /*
 }
 
 /*
- * Subtracts two timeval structs.
- * Ensure out >= in.
- * Modifies out = out - in.
+ * Calculate difference between two timeval structs to within one second.
  */
  */
-static void
-tv_sub(register struct timeval * out, register struct timeval * in)
+void
+timeval_diff(struct timeval * a, const struct timeval * b)
 {
 {
-    if ((out->tv_usec -= in->tv_usec) < 0) {
-        --out->tv_sec;
-        out->tv_usec += 1000000;
-    }
-    out->tv_sec -= in->tv_sec;
+    assert(a->tv_sec >= b->tv_sec);
+    a->tv_sec -= b->tv_sec;
 }
 
 /*
 }
 
 /*
@@ -124,7 +123,7 @@ pinger(int ignore)
             struct timeval now;
 
             gettimeofday(&now, (struct timezone *) NULL);
             struct timeval now;
 
             gettimeofday(&now, (struct timezone *) NULL);
-            tv_sub(&now, &p->last_ping_received);
+            timeval_diff(&now, &p->last_ping_received);
 
             if (now.tv_sec > (p->max_delay + p->ping_interval)) {
                 if ((p->host_up) || retry_down_cmd) {
 
             if (now.tv_sec > (p->max_delay + p->ping_interval)) {
                 if ((p->host_up) || retry_down_cmd) {
@@ -140,7 +139,7 @@ pinger(int ignore)
             }
 
             gettimeofday(&now, (struct timezone *) NULL);
             }
 
             gettimeofday(&now, (struct timezone *) NULL);
-            tv_sub(&now, &p->last_ping_sent);
+            timeval_diff(&now, &p->last_ping_sent);
 
             if (now.tv_sec > p->ping_interval) { /* Time to send ping */
                 icp = (struct icmp *) outpack;
 
             if (now.tv_sec > p->ping_interval) { /* Time to send ping */
                 icp = (struct icmp *) outpack;
@@ -171,7 +170,7 @@ pinger(int ignore)
     }
 
     signal(SIGALRM, pinger); /* restore handler */
     }
 
     signal(SIGALRM, pinger); /* restore handler */
-    alarm(send_delay);
+    alarm(TIMER_RESOLUTION);
 }
 
 static void
 }
 
 static void
@@ -207,10 +206,10 @@ read_icmp_data(struct monitor_host * p)
 
         memcpy(&p->last_ping_received, &tv, sizeof(tv));
 
 
         memcpy(&p->last_ping_received, &tv, sizeof(tv));
 
-        tv_sub(&tv, (struct timeval *) &icmp->icmp_data[0]);
+        timeval_diff(&tv, (struct timeval *) &icmp->icmp_data[0]);
         delay = tv.tv_sec * 1000 + (tv.tv_usec / 1000);
 
         delay = tv.tv_sec * 1000 + (tv.tv_usec / 1000);
 
-        if (verbose) printf("INFO: Got ICMP reply from %s in %d ms.\n", p->name, delay);
+        if (verbose) printf("INFO: Got ICMP reply from %s.\n", p->name);
         if (!p->host_up) {
             if (verbose) printf("INFO: Host %s started responding. Executing UP command.\n", p->name);
             p->host_up = true;
         if (!p->host_up) {
             if (verbose) printf("INFO: Host %s started responding. Executing UP command.\n", p->name);
             p->host_up = true;
@@ -379,8 +378,8 @@ init_hosts(void)
                 fprintf(stderr, "WARN: Can't create socket. Skipping client %s.\n", p->name);
                 p->socket=-1;
             } else {
                 fprintf(stderr, "WARN: Can't create socket. Skipping client %s.\n", p->name);
                 p->socket=-1;
             } else {
-                if (ok == 0) send_delay = p->ping_interval;
-                else send_delay = gcd(send_delay, p->ping_interval);
+                //if (ok == 0) send_delay = p->ping_interval;
+                //else send_delay = gcd(send_delay, p->ping_interval);
                 ok++;
             }
         }
                 ok++;
             }
         }
@@ -442,7 +441,7 @@ main(int argc, char ** argv)
     init_hosts();
 
     signal(SIGALRM, pinger);
     init_hosts();
 
     signal(SIGALRM, pinger);
-    alarm(send_delay);
+    alarm(TIMER_RESOLUTION);
 
     get_response();
 
 
     get_response();