X-Git-Url: http://git.subgeniuskitty.com/icmpmonitor/.git/blobdiff_plain/01ca0aa3b52541d320625fb868932c1339cf0702..fc42886b5ccafa67847a2ccad623b237333bfd4d:/icmpmonitor.c diff --git a/icmpmonitor.c b/icmpmonitor.c index a53538f..f7e4a47 100644 --- a/icmpmonitor.c +++ b/icmpmonitor.c @@ -39,20 +39,19 @@ #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 +#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 -/* 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 { @@ -76,10 +75,10 @@ struct host_entry { /* 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. */ - 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. @@ -247,7 +246,11 @@ get_response(void) } } -static void +/* + * Parse a configuration file using the `iniparser` library. + * See `icmpmonitor.ini` and `README.md` for examples and reference. + */ +void parse_config(const char * conf_file) { dictionary * conf = iniparser_load(conf_file); @@ -266,38 +269,37 @@ parse_config(const char * conf_file) for (int i=0; i < host_count; i++) { /* Allocate a reusable buffer large enough to hold the full 'section:key' string. */ int section_len = strlen(iniparser_getsecname(conf, i)); - char * key_buf = malloc(section_len + 1 + MAXCONFKEYLEN + 1); + char * key_buf = malloc(section_len + 1 + MAX_CONF_KEY_LEN + 1); /* +1 for ':' and '\0' */ strcpy(key_buf, iniparser_getsecname(conf, i)); key_buf[section_len++] = ':'; struct host_entry * cur_host = malloc(sizeof(struct host_entry)); key_buf[section_len] = '\0'; - strncat(key_buf, "host", MAXCONFKEYLEN); + strncat(key_buf, "host", MAX_CONF_KEY_LEN); cur_host->name = strdup(iniparser_getstring(conf, key_buf, NULL)); key_buf[section_len] = '\0'; - strncat(key_buf, "interval", MAXCONFKEYLEN); + strncat(key_buf, "interval", MAX_CONF_KEY_LEN); cur_host->ping_interval = iniparser_getint(conf, key_buf, -1); key_buf[section_len] = '\0'; - strncat(key_buf, "max_delay", MAXCONFKEYLEN); + strncat(key_buf, "max_delay", MAX_CONF_KEY_LEN); cur_host->max_delay = iniparser_getint(conf, key_buf, -1); key_buf[section_len] = '\0'; - strncat(key_buf, "up_cmd", MAXCONFKEYLEN); + strncat(key_buf, "up_cmd", MAX_CONF_KEY_LEN); cur_host->up_cmd = strdup(iniparser_getstring(conf, key_buf, NULL)); key_buf[section_len] = '\0'; - strncat(key_buf, "down_cmd", MAXCONFKEYLEN); + strncat(key_buf, "down_cmd", MAX_CONF_KEY_LEN); cur_host->down_cmd = strdup(iniparser_getstring(conf, key_buf, NULL)); key_buf[section_len] = '\0'; - /* TODO: Parse for up/down/auto in start_condition. */ - /* TODO: Do a host up/down check if necessary. */ - cur_host->host_up = true; + strncat(key_buf, "start_condition", MAX_CONF_KEY_LEN); + const char * value = iniparser_getstring(conf, key_buf, NULL); + if (value) cur_host->host_up = *value == 'u' ? true : false; - /* TODO: Do I want to make any checks for start_condition? */ if (cur_host->name == NULL || cur_host->ping_interval == -1 || cur_host->max_delay == -1) { fprintf(stderr, "ERROR: Problems parsing section %s.\n", iniparser_getsecname(conf, i)); exit(EXIT_FAILURE); @@ -340,14 +342,6 @@ get_host_addr(const char * name) 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) {