X-Git-Url: http://git.subgeniuskitty.com/icmpmonitor/.git/blobdiff_plain/c9b16ece585295b83d84cc148d30071d49b87b2e..18bd6fd3133687aea12c6a0ce8ab1cfe92b8b72c:/icmpmonitor.c diff --git a/icmpmonitor.c b/icmpmonitor.c index 04aa91c..98b6f5e 100644 --- a/icmpmonitor.c +++ b/icmpmonitor.c @@ -46,16 +46,18 @@ struct monitor_host { unsigned int sentpackets; unsigned int recvdpackets; - /* linked list */ + /* Linked list */ struct monitor_host * next; }; -/* globals */ -static struct monitor_host ** hosts = NULL; -static int isVerbose = 0; -static int keepBanging = 0; -static unsigned short ident; -static int send_delay = 1; +/* Globals */ + /* Since the program is based around signals, a linked list of hosts is maintained here. */ + static struct monitor_host ** hosts = NULL; + /* Set by command line flags. */ + static bool verbose = false; + static bool retry_down_cmd = false; + /* TODO: Get rid of this global. */ + static int send_delay = 1; /* * Checksum routine for Internet Protocol family headers @@ -97,7 +99,7 @@ in_cksum(unsigned short * addr, int len) * Modifies out = out - in. */ static void -tvsub(register struct timeval * out, register struct timeval * in) +tv_sub(register struct timeval * out, register struct timeval * in) { if ((out->tv_usec -= in->tv_usec) < 0) { --out->tv_sec; @@ -127,11 +129,11 @@ pinger(int ignore) struct timeval now; gettimeofday(&now, (struct timezone *) NULL); - tvsub(&now, &p->last_ping_received); + tv_sub(&now, &p->last_ping_received); if (now.tv_sec > (p->max_delay + p->ping_interval)) { - if ((p->hostup) || keepBanging) { - if (isVerbose) printf("INFO: Host %s stopped responding. Executing DOWN command.\n", p->name); + if ((p->hostup) || retry_down_cmd) { + if (verbose) printf("INFO: Host %s stopped responding. Executing DOWN command.\n", p->name); p->hostup = false; if (!fork()) { system(p->downcmd); @@ -143,7 +145,7 @@ pinger(int ignore) } gettimeofday(&now, (struct timezone *) NULL); - tvsub(&now, &p->last_ping_sent); + tv_sub(&now, &p->last_ping_sent); if (now.tv_sec > p->ping_interval) { /* Time to send ping */ icp = (struct icmp *) outpack; @@ -151,9 +153,9 @@ pinger(int ignore) icp->icmp_code = 0; icp->icmp_cksum = 0; icp->icmp_seq = p->socket; - icp->icmp_id = ident; + icp->icmp_id = getpid() & 0xFFFF; - if (isVerbose) printf("INFO: Sending ICMP packet to %s.\n", p->name); + if (verbose) printf("INFO: Sending ICMP packet to %s.\n", p->name); gettimeofday((struct timeval *) &outpack[8], (struct timezone *) NULL); @@ -208,17 +210,17 @@ read_icmp_data(struct monitor_host * p) return; } - if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == ident && icmp->icmp_seq == p->socket) { + if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == (getpid() & 0xFFFF) && icmp->icmp_seq == p->socket) { p->recvdpackets++; memcpy(&p->last_ping_received, &tv, sizeof(tv)); - tvsub(&tv, (struct timeval *) &icmp->icmp_data[0]); + tv_sub(&tv, (struct timeval *) &icmp->icmp_data[0]); delay = tv.tv_sec * 1000 + (tv.tv_usec / 1000); - if (isVerbose) printf("INFO: Got ICMP reply from %s in %d ms.\n", p->name, delay); + if (verbose) printf("INFO: Got ICMP reply from %s in %d ms.\n", p->name, delay); if (!p->hostup) { - if (isVerbose) printf("INFO: Host %s started responding. Executing UP command.\n", p->name); + if (verbose) printf("INFO: Host %s started responding. Executing UP command.\n", p->name); p->hostup = true; if (!fork()) { system(p->upcmd); @@ -329,7 +331,7 @@ read_hosts(const char * cfg_file_name) } static int -gethostaddr(const char * name) +get_host_addr(const char * name) { static int res; struct hostent * he; @@ -365,7 +367,7 @@ init_hosts(void) while (p) { bzero(&p->dest, sizeof(p->dest)); p->dest.sin_family = AF_INET; - if ((p->dest.sin_addr.s_addr = gethostaddr(p->name)) <= 0) { + if ((p->dest.sin_addr.s_addr = get_host_addr(p->name)) <= 0) { fprintf(stderr, "WARN: Can't resolve host. Skipping client %s.\n", p->name); p->socket=-1; } else { @@ -398,10 +400,10 @@ main(int argc, char ** argv) while ((param = getopt(argc, argv, "rvf:")) != -1) { switch(param) { case 'v': - isVerbose = 1; + verbose = true; break; case 'r': - keepBanging = 1; + retry_down_cmd = true; break; case 'f': cfgfile=strdup(optarg); @@ -421,8 +423,6 @@ main(int argc, char ** argv) init_hosts(); - ident = getpid() & 0xFFFF; - signal(SIGALRM, pinger); alarm(send_delay);