X-Git-Url: http://git.subgeniuskitty.com/icmpmonitor/.git/blobdiff_plain/fc361e474b55944ca0817df904904a1be77ccc20..3900fd4332290428dea7ffaa693887e5f23f1a89:/icmpmonitor.c diff --git a/icmpmonitor.c b/icmpmonitor.c index cc141b5..f2bf43e 100644 --- a/icmpmonitor.c +++ b/icmpmonitor.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "iniparser/iniparser.h" @@ -35,6 +36,15 @@ #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 + +/* 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 @@ -61,58 +71,36 @@ 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; - static int send_delay = 1; /* Set by command line flags. */ static bool verbose = false; static bool retry_down_cmd = false; /* - * Checksum routine for Internet Protocol family headers + * Generate an Internet Checksum per RFC 1071. + * + * This is not a general purpose implementation of RFC 1071. Since we only + * send ICMP echo packets, we assume 'data' will contain a specific number of + * bytes. */ -static int -in_cksum(unsigned short * addr, int len) +uint16_t +checksum(uint16_t * data) { - int nleft = len; - unsigned short * w = addr; - int sum = 0; - unsigned short answer = 0; - - /* - * Our algorithm is simple, using a 32 bit accumulator (sum), we add - * sequential 16 bit words to it, and at the end, fold back all the - * carry bits from the top 16 bits into the lower 16 bits. - */ - while (nleft > 1) { - sum += *w++; - nleft -= 2; - } - - /* mop up an odd byte, if necessary */ - if (nleft == 1) { - *(u_char *)(&answer) = *(u_char *)w; - sum += answer; + uint32_t accumulator = 0; + for (size_t i = 0; i < ICMP_ECHO_PACKET_BYTES / 2; i++) { + accumulator += ntohs(data[i]); + if (accumulator > 0xffff) accumulator -= 0xffff; } - - /* add back carry outs from top 16 bits to low 16 bits */ - sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ - sum += (sum >> 16); /* add carry */ - answer = ~sum; /* truncate to 16 bits */ - return(answer); + return htons(~accumulator); } /* - * 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; } /* @@ -128,14 +116,14 @@ pinger(int ignore) int i; struct icmp * icp; struct monitor_host * p = hosts; - u_char outpack[MAXPACKETSIZE]; + unsigned char outpack[MAXPACKETSIZE]; /* Use char so this can be aliased later. */ while (p) { if (p->socket != -1) { 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) { @@ -151,7 +139,7 @@ pinger(int ignore) } 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; @@ -167,8 +155,7 @@ pinger(int ignore) int cc = DEFAULTDATALEN + 8; /* skips ICMP portion */ - /* compute ICMP checksum */ - icp->icmp_cksum = in_cksum((unsigned short *) icp, cc); + icp->icmp_cksum = checksum((uint16_t *) outpack); i = sendto(p->socket, (char *) outpack, cc, 0, (const struct sockaddr *) (&p->dest), sizeof(struct sockaddr)); @@ -183,7 +170,7 @@ pinger(int ignore) } signal(SIGALRM, pinger); /* restore handler */ - alarm(send_delay); + alarm(TIMER_RESOLUTION); } static void @@ -219,10 +206,10 @@ read_icmp_data(struct monitor_host * p) 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); - 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; @@ -391,8 +378,8 @@ init_hosts(void) 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++; } } @@ -454,7 +441,7 @@ main(int argc, char ** argv) init_hosts(); signal(SIGALRM, pinger); - alarm(send_delay); + alarm(TIMER_RESOLUTION); get_response();