Clearer GCD function in icmpmonitor.c.
[icmpmonitor] / icmpmonitor.c
index f2c10a2..f0fbc09 100644 (file)
@@ -4,86 +4,34 @@
  * © 2019 Aaron Taylor <ataylor at subgeniuskitty dot com>
  * © 1999 Vadim Zaliva <lord@crocodile.org>
  * © 1989 The Regents of the University of California & Mike Muuss
  * © 2019 Aaron Taylor <ataylor at subgeniuskitty dot com>
  * © 1999 Vadim Zaliva <lord@crocodile.org>
  * © 1989 The Regents of the University of California & Mike Muuss
- * See LICENSE.txt file for copyright and license details.
+ * See LICENSE file for copyright and license details.
  */
 
 #include <sys/param.h>
 #include <stdio.h>
 #include <stdlib.h>
  */
 
 #include <sys/param.h>
 #include <stdio.h>
 #include <stdlib.h>
-#ifdef HAVE_SYSLOG_H
-# include <syslog.h>
-#endif
 #include <stdarg.h>
 #include <signal.h>
 #include <string.h>
 #include <stdarg.h>
 #include <signal.h>
 #include <string.h>
-#ifdef HAVE_SYS_TIME_H
-# include <sys/time.h>
-# include <sys/wait.h>
-#endif
+#include <sys/time.h>
+#include <sys/wait.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/types.h>
-#ifdef HAVE_FCNTL_H
-# include <fcntl.h>
-#endif
-#ifdef HAVE_SYS_FCNTL_H
-# include <sys/fcntl.h>
-#endif
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
+#include <fcntl.h>
+#include <sys/fcntl.h>
+#include <unistd.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
-
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <netinet/in_systm.h>
 #include <netinet/ip.h>
 #include <netinet/ip_icmp.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <netinet/in_systm.h>
 #include <netinet/ip.h>
 #include <netinet/ip_icmp.h>
-
-/* Workaround for broken ICMP header on Slackware 4.x */
-#ifdef _LINUX_ICMP_H
-# warning "Broken Slackware 4.x 'netinet/ip_icmp.h' header detected. Using replacement 'struct icmp' definition."
-# define ICMP_MINLEN    8
-struct icmp
-{
-    u_int8_t  icmp_type;
-    u_int8_t  icmp_code;
-    u_int16_t icmp_cksum;
-    union
-    {
-        struct ih_idseq
-        { 
-            u_int16_t icd_id;
-            u_int16_t icd_seq;
-        } ih_idseq;
-    } icmp_hun;
-    
-# define icmp_id         icmp_hun.ih_idseq.icd_id
-# define icmp_seq        icmp_hun.ih_idseq.icd_seq
-    
-    union {
-        u_int8_t    id_data[1];
-    } icmp_dun;
-    
-# define icmp_data       icmp_dun.id_data
-    
-};
-#endif /* _LINUX_ICMP_H */
-
 #include <stddef.h>
 #include <errno.h>
 
 #include "cfg.h"
 
 #include <stddef.h>
 #include <errno.h>
 
 #include "cfg.h"
 
-/* defines */
-
-/* #define DEBUG */
-
-#ifndef nil
-# define nil NULL
-#endif
-
 /* return codes */
 #define RET_OK         0
 #define RET_NO_HOSTS   1
 /* return codes */
 #define RET_OK         0
 #define RET_NO_HOSTS   1
@@ -125,8 +73,6 @@ typedef struct monitor_host
 } monitor_host_t;
 
 /* protos */
 } monitor_host_t;
 
 /* protos */
-static void logopen(void);
-static void logclose(void);
 static void log(int type, char *format, ...);
 static int  gethostaddr(const char *name);
 static void read_hosts(const char *cfg_file_name);
 static void log(int type, char *format, ...);
 static int  gethostaddr(const char *name);
 static void read_hosts(const char *cfg_file_name);
@@ -137,13 +83,11 @@ static int  in_cksum(u_short *addr, int len);
 static void read_icmp_data(monitor_host_t *p);
 static void tvsub(struct timeval *out, struct timeval *in);
 static void done(int code);
 static void read_icmp_data(monitor_host_t *p);
 static void tvsub(struct timeval *out, struct timeval *in);
 static void done(int code);
-static void start_daemon(void);
 static int gcd(int x, int y);
 
 /* globals */
 
 static int gcd(int x, int y);
 
 /* globals */
 
-static monitor_host_t **hosts      = nil;
-static int             isDaemon    = 0; 
+static monitor_host_t **hosts      = NULL;
 static int             isVerbose   = 0;
 static int             keepBanging = 0;
 static unsigned short  ident;
 static int             isVerbose   = 0;
 static int             keepBanging = 0;
 static unsigned short  ident;
@@ -153,21 +97,17 @@ int main(int ac, char **av)
 {
     extern char* optarg;
     extern int   optind;
 {
     extern char* optarg;
     extern int   optind;
-    char         *cfgfile=nil;
+    char         *cfgfile=NULL;
     int          param;
     
     int          param;
     
-    logopen();
     log(LOG_INFO, VERSION " is starting.");
 
     log(LOG_INFO, VERSION " is starting.");
 
-    while((param = getopt(ac, av, "rvdf:")) != -1)
+    while((param = getopt(ac, av, "rvf:")) != -1)
        switch(param)
         { 
        case 'v':
             isVerbose = 1;
            break; 
        switch(param)
         { 
        case 'v':
             isVerbose = 1;
            break; 
-       case 'd':
-            isDaemon = 1;
-           break; 
        case 'r':
             keepBanging = 1;
            break; 
        case 'r':
             keepBanging = 1;
            break; 
@@ -175,7 +115,7 @@ int main(int ac, char **av)
            cfgfile=strdup(optarg);  
            break;  
        default: 
            cfgfile=strdup(optarg);  
            break;  
        default: 
-           fprintf(stderr,"Usage: icmpmonitor [-d] [-v] [-r] [-f cfgfile]\n");
+           fprintf(stderr,"Usage: icmpmonitor [-v] [-r] [-f cfgfile]\n");
             done(RET_BAD_OPT);
        } 
     
             done(RET_BAD_OPT);
        } 
     
@@ -185,11 +125,7 @@ int main(int ac, char **av)
        cfgfile="icmpmonitor.cfg";
     }
 
        cfgfile="icmpmonitor.cfg";
     }
 
-    read_hosts(cfgfile); /* we do this before becoming daemon,
-                            to be able process relative path */
-
-    if(isDaemon)
-        start_daemon();
+    read_hosts(cfgfile);
     
     init_hosts();
     
     
     init_hosts();
     
@@ -280,7 +216,7 @@ static void pinger(int ignore)
                         exit(0);
                     } else
                     {
                         exit(0);
                     } else
                     {
-                        wait(nil);
+                        wait(NULL);
                     }
                 }
             }
                     }
                 }
             }
@@ -354,7 +290,7 @@ static void get_response(void)
             p=p->next;
         }
 
             p=p->next;
         }
 
-        retval = select(maxd+1, &rfds, nil, nil, nil);
+        retval = select(maxd+1, &rfds, NULL, NULL, NULL);
         if(retval<0)
         {
             /* we get her in case we are interrupted by signal.
         if(retval<0)
         {
             /* we get her in case we are interrupted by signal.
@@ -444,7 +380,7 @@ static void read_icmp_data(monitor_host_t *p)
                 exit(0);
             } else
             {
                 exit(0);
             } else
             {
-                wait(nil);
+                wait(NULL);
             }
         }
     } else
             }
         }
     } else
@@ -562,7 +498,7 @@ static void init_hosts(void)
     struct protoent   *proto;
     int ok=0;
 
     struct protoent   *proto;
     int ok=0;
 
-    if((proto=getprotobyname("icmp"))==nil)
+    if((proto=getprotobyname("icmp"))==NULL)
     {
         log(LOG_ERR,"Unknown protocol: icmp. Exiting.");
         done(RET_INIT_ERROR);
     {
         log(LOG_ERR,"Unknown protocol: icmp. Exiting.");
         done(RET_INIT_ERROR);
@@ -620,44 +556,9 @@ tvsub(register struct timeval *out, register struct timeval *in)
 
 void done(int code)
 {
 
 void done(int code)
 {
-    logclose();
     exit(code);
 }
 
     exit(code);
 }
 
-void start_daemon(void)
-{
-    if(fork())
-       exit(0);
-    
-    chdir("/");
-    umask(0);
-    (void) close(0);
-    (void) close(1);
-    (void) close(2);
-    (void) open("/", O_RDONLY);
-    (void) dup2(0, 1);
-    (void) dup2(0, 2);
-    setsid();
-}
-
-static void logopen(void)
-{
-#if HAVE_OPENLOG
-    if(isDaemon)
-        openlog("icmpmonitor", LOG_PID| LOG_CONS|LOG_NOWAIT, LOG_USER);
-#else
-    log(LOG_WARNING,"Compiled without syslog. Syslog can't be used.");
-#endif
-}
-
-static void logclose(void)
-{
-#if HAVE_CLOSELOG
-    if(isDaemon)
-        closelog();
-#endif
-}
-
 /**
  * This function should be used as central logging facility.
  * 'type' argument should be one of following:
 /**
  * This function should be used as central logging facility.
  * 'type' argument should be one of following:
@@ -682,40 +583,15 @@ static void log(int type, char *format, ...)
     
     va_start(ap, format);
 
     
     va_start(ap, format);
 
-    if(isDaemon)
-    {
-        char buffer[MAX_LOG_MSG_SIZE];
-            
-#if HAVE_VSNPRINTF    
-        (void)vsnprintf(buffer, MAX_LOG_MSG_SIZE, format, ap);
-#else
-# if HAVE_VSPRINTF
-#  warning "Using VSPRINTF. Buffer overflow could happen!"
-        (void)vsprintf(buffer, format, ap);
-# else
-#  error "Your standard libabry have neither vsnprintf nor vsprintf defined. One of them is reqired!"
-# endif
-#endif
-#if HAVE_SYSLOG            
-        syslog(type,buffer);
-#endif
-    } else
-    {
-        (void)  fprintf(stderr, "icmpmonitor[%d]:", (int)getpid());
-        (void) vfprintf(stderr, format, ap);
-        (void)  fprintf(stderr, "\n");
-    }
+    fprintf(stderr, "icmpmonitor[%d]:", (int)getpid());
+    vfprintf(stderr, format, ap);
+    fprintf(stderr, "\n");
     va_end(ap);
 }
 
 static int gcd(int x, int y)
 {
     va_end(ap);
 }
 
 static int gcd(int x, int y)
 {
-    while(x!=y)
-    {
-        if(x<y)
-            y-=x;
-        else 
-            x-=y;
-    }
-    return x;
+    int remainder = x % y;
+    if (remainder == 0) return y;
+    return gcd(y, remainder);
 }
 }