Tested on OpenBSD 6.5.
[icmpmonitor] / icmpmonitor.c
index 462c920..5dd5064 100644 (file)
 
 #define VERSION 2
 
 
 #define VERSION 2
 
-#define MAXPACKETSIZE  (65536 - 60 - 8) /* TODO: What are the magic numbers? */
-#define DEFAULTDATALEN (64 - 8)         /* TODO: What are the magic numbers? */
-
 /* ICMP header contains: type, code, checksum, identifier and sequence number. */
 #define ICMP_ECHO_HEADER_BYTES  8
 #define ICMP_ECHO_DATA_BYTES    sizeof(struct timeval)
 #define ICMP_ECHO_PACKET_BYTES  ICMP_ECHO_HEADER_BYTES + ICMP_ECHO_DATA_BYTES
 /* ICMP header contains: type, code, checksum, identifier and sequence number. */
 #define ICMP_ECHO_HEADER_BYTES  8
 #define ICMP_ECHO_DATA_BYTES    sizeof(struct timeval)
 #define ICMP_ECHO_PACKET_BYTES  ICMP_ECHO_HEADER_BYTES + ICMP_ECHO_DATA_BYTES
+#define IP_PACKET_MAX_BYTES     65535
 
 /* 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
 
 
 /* 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 (not value). */
+/* For example: MAX_CONF_KEY_LEN > strlen("start_condition")                    */
+#define MAX_CONF_KEY_LEN       20
 
 /* One struct per host as listed in the config file. */
 struct host_entry {
 
 /* One struct per host as listed in the config file. */
 struct host_entry {
@@ -76,10 +75,10 @@ struct host_entry {
 
 /* Globals */
     /* Since the program is based around signals, a linked list of hosts is maintained here. */
 
 /* Globals */
     /* Since the program is based around signals, a linked list of hosts is maintained here. */
-    static struct host_entry * first_host_in_list = NULL;
+    struct host_entry * first_host_in_list = NULL;
     /* Set by command line flags. */
     /* Set by command line flags. */
-    static bool                  verbose        = false;
-    static bool                  retry_down_cmd = false;
+    bool                verbose            = false;
+    bool                retry_down_cmd     = false;
 
 /*
  * Generate an Internet Checksum per RFC 1071.
 
 /*
  * Generate an Internet Checksum per RFC 1071.
@@ -343,14 +342,6 @@ get_host_addr(const char * name)
     return result;
 }
 
     return result;
 }
 
-size_t
-gcd(const size_t x, const size_t y)
-{
-    size_t remainder = x % y;
-    if (remainder == 0) return y;
-    return gcd(y, remainder);
-}
-
 void
 remove_host_from_list(struct host_entry * host)
 {
 void
 remove_host_from_list(struct host_entry * host)
 {
@@ -441,6 +432,7 @@ parse_params(int argc, char ** argv)
     }
     if (first_host_in_list == NULL) {
         fprintf(stderr, "ERROR: Unable to parse a config file.\n");
     }
     if (first_host_in_list == NULL) {
         fprintf(stderr, "ERROR: Unable to parse a config file.\n");
+        print_usage(argv);
         exit(EXIT_FAILURE);
     }
 }
         exit(EXIT_FAILURE);
     }
 }
@@ -448,14 +440,22 @@ parse_params(int argc, char ** argv)
 int
 main(int argc, char ** argv)
 {
 int
 main(int argc, char ** argv)
 {
+    /* Parse the command line options, load and parse the config file. */
     parse_params(argc, argv);
 
     parse_params(argc, argv);
 
+    /* Process config for each host, generating/verifying any necessary information. */
     init_hosts();
 
     init_hosts();
 
+    /* Make sure initialization left us with something useful. */
+    assert(first_host_in_list);
+
+    /* Pings are sent asynchronously. */
     signal(SIGALRM, pinger);
     alarm(TIMER_RESOLUTION);
 
     signal(SIGALRM, pinger);
     alarm(TIMER_RESOLUTION);
 
+    /* The main program loop listens for ping responses. */
     get_response();
 
     get_response();
 
+    /* Should be unreachable. */
     exit(EXIT_SUCCESS);
 }
     exit(EXIT_SUCCESS);
 }