]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
usage: use error(3) error-reporting function
authorSami Kerola <kerolasa@iki.fi>
Sun, 21 Aug 2016 17:13:53 +0000 (18:13 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 21 Aug 2016 19:01:34 +0000 (20:01 +0100)
There are two benefits of using error(3).  First of all this function
unifies error printing nice way, and when done this way a programmer cannot
accidentally mix stderr and stdout error print outs.  Secondly the error(3)
appends program name to errors, making it clear what printed an error.  To
demonstrate this see below for old and new output, in which the old error
could have in theory been reported by sudo(8).

$ sudo mtr -u -T # old
-u , -T and -S are mutually exclusive.

$ sudo ./mtr -u -T # new
./mtr: -u , -T and -S are mutually exclusive

asn.c
dns.c
mtr.c
net.c
select.c

diff --git a/asn.c b/asn.c
index fb0a9612ba3da07e37683f8118823f36fa688c05..833ec4fdc0c86d32626dbe7a2732bfaaa2110aab 100644 (file)
--- a/asn.c
+++ b/asn.c
@@ -22,6 +22,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
+#include <error.h>
+#include <errno.h>
 
 #ifdef __APPLE__
 #define BIND_8_COMPAT
@@ -80,7 +82,7 @@ char *ipinfo_lookup(const char *domain) {
 
 
     if(res_init() < 0) {
-        fprintf(stderr,"@res_init failed\n");
+        error(0, 0, "@res_init failed");
         return NULL;
     }
 
@@ -281,7 +283,7 @@ void asn_open(void) {
     if (ipinfo_no >= 0) {
         DEB_syslog(LOG_INFO, "hcreate(%d)", IIHASH_HI);
         if (!(iihash = hcreate(IIHASH_HI)))
-            perror("ipinfo hash");
+            error(0, errno, "ipinfo hash");
     }
 }
 
diff --git a/dns.c b/dns.c
index d10e702e0617f05d7282ae5e794a10adf9e14b2b..a7ab5d6398ca574fd82a588bac931552de706794 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -46,6 +46,8 @@
 //#endif
 //#include <netdb.h>
 //#include <resolv.h>
+#include <error.h>
+#include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
 //#include <ctype.h>
@@ -162,20 +164,17 @@ void dns_open(void)
   int pid; 
  
   if (pipe (todns) < 0) {
-    perror ("can't make a pipe for DNS process");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "can't make a pipe for DNS process");
   }
 
   if (pipe (fromdns) < 0) {
-    perror ("can't make a pipe for DNS process");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "can't make a pipe for DNS process");
   }
   fflush (stdout);
   pid = fork ();
   //pid = 1;
   if (pid < 0) {
-    perror ("can't fork for DNS process");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "can't fork for DNS process");
   }
   if (pid == 0) {
     char buf[2048];
@@ -184,8 +183,7 @@ void dns_open(void)
 
     // Automatically reap children. 
     if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
-      perror("signal");
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "signal");
     }
 
 #if 0
@@ -228,7 +226,8 @@ void dns_open(void)
           sprintf (result, "%s %s\n", strlongip (&host), hostname);
           //printf ("resolved: %s -> %s (%d)\n", strlongip (&host), hostname, rv);
           rv = write (fromdns[1], result, strlen (result));
-          if (rv < 0) perror ("write DNS lookup result");
+          if (rv < 0)
+            error (0, errno, "write DNS lookup result");
         }
 
         exit(EXIT_SUCCESS);
@@ -269,7 +268,7 @@ void dns_ack(void)
     if (r)  
       r->name = strdup (name);
     else 
-      fprintf (stderr, "dns_ack: Couldn't find host %s\n", host);
+      error (0, 0, "dns_ack: Couldn't find host %s", host);
   }
 }
 
@@ -315,7 +314,8 @@ char *dns_lookup2(ip_t * ip)
 
      sprintf (buf, "%s\n", strlongip (ip));
      rv = write  (todns[1], buf, strlen (buf));
-     if (rv < 0) perror ("couldn't write to resolver process");
+     if (rv < 0)
+       error (0, errno, "couldn't write to resolver process");
   }
   return strlongip (ip);
 }
diff --git a/mtr.c b/mtr.c
index f600682f6a28019eccab6c2b4c56f5b8e5eacc7e..83b57bcd4910500f018bc9d08c288ec9935dbb35 100644 (file)
--- a/mtr.c
+++ b/mtr.c
@@ -227,12 +227,11 @@ trim(char * s) {
 }
 
 static void
-append_to_names(const char* progname, const char* item) {
+append_to_names(const char* item) {
 
   names_t* name = calloc(1, sizeof(names_t));
   if (name == NULL) {
-    fprintf(stderr, "%s: memory allocation failure\n", progname);
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "memory allocation failure");
   }
   name->name = strdup(item);
   name->next = names;
@@ -240,7 +239,7 @@ append_to_names(const char* progname, const char* item) {
 }
 
 static void
-read_from_file(const char* progname, const char *filename) {
+read_from_file(const char *filename) {
 
   FILE *in;
   char line[512];
@@ -251,19 +250,17 @@ read_from_file(const char* progname, const char *filename) {
   } else {
     in = fopen(filename, "r");
     if (! in) {
-      fprintf(stderr, "%s: fopen: %s\n", progname, strerror(errno));
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "open %s", filename);
     }
   }
 
   while (fgets(line, sizeof(line), in)) {
     char* name = trim(line);
-    append_to_names(progname, name);
+    append_to_names(name);
   }
 
   if (ferror(in)) {
-    fprintf(stderr, "%s: ferror: %s\n", progname, strerror(errno));
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "ferror %s", filename);
   }
 
   if (in != stdin) fclose(in);
@@ -277,7 +274,7 @@ read_from_file(const char* progname, const char *filename) {
  */
 
 static void
-lock(const char* progname, FILE *f) {
+lock(FILE *f) {
     int fd;
     struct stat buf;
     static struct flock lock;
@@ -293,8 +290,7 @@ lock(const char* progname, FILE *f) {
     fd = fileno(f);
     if ((fstat(fd, &buf) == 0) && S_ISREG(buf.st_mode)) {
       if (fcntl(fd, F_SETLKW, &lock) == -1) {
-          fprintf(stderr, "%s: fcntl: %s (ignored)\n",
-            progname, strerror(errno));
+        error(0, errno, "fcntl (ignored)");
       }
     }
 }
@@ -305,7 +301,7 @@ lock(const char* progname, FILE *f) {
  */
 
 static void
-unlock(const char* progname, FILE *f) {
+unlock(FILE *f) {
     int fd;
     struct stat buf;
     static struct flock lock;
@@ -321,8 +317,7 @@ unlock(const char* progname, FILE *f) {
     fd = fileno(f);
     if ((fstat(fd, &buf) == 0) && S_ISREG(buf.st_mode)) {
       if (fcntl(fd, F_SETLKW, &lock) == -1) {
-          fprintf(stderr, "%s: fcntl: %s (ignored)\n",
-            progname, strerror(errno));
+        error(0, errno, "fcntl (ignored)");
       }
     }
 }
@@ -502,12 +497,10 @@ void parse_arg (int argc, char **argv)
     case 'i':
       WaitTime = strtofloat_or_err(optarg, "invalid argument");
       if (WaitTime <= 0.0) {
-       fprintf (stderr, "mtr: wait time must be positive\n");
-       exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, 0, "wait time must be positive");
       }
       if (getuid() != 0 && WaitTime < 1.0) {
-        fprintf (stderr, "non-root users cannot request an interval < 1.0 seconds\r\n");
-       exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, 0, "non-root users cannot request an interval < 1.0 seconds");
       }
       break;
     case 'f':
@@ -520,7 +513,7 @@ void parse_arg (int argc, char **argv)
       }
       break;
     case 'F':
-      read_from_file(argv[0], optarg);
+      read_from_file(optarg);
       break;
     case 'm':
       maxTTL = strtoint_or_err(optarg, "invalid argument");
@@ -543,13 +536,11 @@ void parse_arg (int argc, char **argv)
     case 'o':
       /* Check option before passing it on to fld_active. */
       if (strlen (optarg) > MAXFLD) {
-       fprintf (stderr, "Too many fields: %s\n", optarg);
-        exit(EXIT_FAILURE);
+       error(EXIT_FAILURE, 0, "Too many fields: %s", optarg);
       }
       for (i=0; optarg[i]; i++) {
         if(!strchr (available_options, optarg[i])) {
-          fprintf (stderr, "Unknown field identifier: %c\n", optarg[i]);
-          exit(EXIT_FAILURE);
+          error(EXIT_FAILURE, 0, "Unknown field identifier: %c", optarg[i]);
         }
       }
       strcpy ((char*)fld_active, optarg);
@@ -562,8 +553,7 @@ void parse_arg (int argc, char **argv)
     case 'G':
       GraceTime = strtofloat_or_err(optarg, "invalid argument");
       if (GraceTime <= 0.0) {
-        fprintf (stderr, "mtr: wait time must be positive\n");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, 0, "wait time must be positive");
       }
       break;
     case 'Q':
@@ -576,15 +566,13 @@ void parse_arg (int argc, char **argv)
       break;
     case 'u':
       if (mtrtype != IPPROTO_ICMP) {
-        fprintf(stderr, "-u , -T and -S are mutually exclusive.\n");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, 0, "-u , -T and -S are mutually exclusive");
       }
       mtrtype = IPPROTO_UDP;
       break;
     case 'T':
       if (mtrtype != IPPROTO_ICMP) {
-        fprintf(stderr, "-u , -T and -S are mutually exclusive.\n");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, 0, "-u , -T and -S are mutually exclusive");
       }
       if (!remoteport) {
         remoteport = 80;
@@ -594,16 +582,14 @@ void parse_arg (int argc, char **argv)
     case 'S':
 #ifdef HAS_SCTP
       if (mtrtype != IPPROTO_ICMP) {
-        fprintf(stderr, "-u , -T and -S are mutually exclusive.\n");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, 0, "-u , -T and -S are mutually exclusive");
       }
       if (!remoteport) {
         remoteport = 80;
       }
       mtrtype = IPPROTO_SCTP;
 #else
-      fprintf (stderr, "No SCTP support found at compiletime\n");
-      exit (EXIT_FAILURE);
+      error(EXIT_FAILURE, 0, "No SCTP support found at compiletime");
 #endif
       break;
     case 'b':
@@ -612,15 +598,13 @@ void parse_arg (int argc, char **argv)
     case 'P':
       remoteport = strtoint_or_err(optarg, "invalid argument");
       if (remoteport > 65535 || remoteport < 1) {
-        fprintf(stderr, "Illegal port number.\n");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, 0, "Illegal port number: %d", remoteport);
       }
       break;
     case 'L':
       localport = strtoint_or_err(optarg, "invalid argument");
       if (localport > 65535 || localport < MinPort) {
-        fprintf(stderr, "Illegal local port number.\n");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, 0, "Illegal port number: %d", localport);
       }
       break;
     case 'Z':
@@ -635,7 +619,7 @@ void parse_arg (int argc, char **argv)
       af = AF_INET6;
       break;
 #else
-      fprintf( stderr, "IPv6 not enabled.\n" );
+      error(EXIT_FAILURE, 0, "IPv6 not enabled");
       break;
 #endif
 #ifdef HAVE_IPINFO
@@ -650,7 +634,7 @@ void parse_arg (int argc, char **argv)
 #else
     case 'y':
     case 'z':
-      fprintf( stderr, "IPINFO not enabled.\n" );
+      error(EXIT_FAILURE, 0, "IPINFO not enabled");
       break;
 #endif
 #ifdef SO_MARK
@@ -659,7 +643,7 @@ void parse_arg (int argc, char **argv)
       break;
 #else
     case 'M':
-      fprintf( stderr, "SO_MARK not enabled.\n" );
+      error(EXIT_FAILURE, 0, "SO_MARK not enabled");
       break;
 #endif
     }
@@ -694,7 +678,7 @@ void parse_mtr_options (char *string)
     p = strtok (NULL, " \t");
   }
   if (p != NULL) {
-    fprintf (stderr, "Warning: extra arguments ignored: %s", p);
+    error(0, 0, "Warning: extra arguments ignored: %s", p);
   }
 
   parse_arg (argc, argv);
@@ -707,7 +691,7 @@ int main(int argc, char **argv)
   struct hostent *  host                = NULL;
   int               net_preopen_result;
   struct addrinfo       hints, *res;
-  int                   error;
+  int                   gai_error;
   struct hostent        trhost;
   char *                alptr[2];
   struct sockaddr_in *  sa4;
@@ -718,20 +702,17 @@ int main(int argc, char **argv)
   /*  Get the raw sockets first thing, so we can drop to user euid immediately  */
 
   if ( ( net_preopen_result = net_preopen () ) ) {
-    fprintf( stderr, "mtr: unable to get raw sockets.\n" );
-    exit( EXIT_FAILURE );
+    error(EXIT_FAILURE, errno, "Unable to get raw sockets");
   }
 
   /*  Now drop to user permissions  */
   if (setgid(getgid()) || setuid(getuid())) {
-    fprintf (stderr, "mtr: Unable to drop permissions.\n");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "Unable to drop permissions");
   }
 
   /*  Double check, just in case  */
   if ((geteuid() != getuid()) || (getegid() != getgid())) {
-    fprintf (stderr, "mtr: Unable to drop permissions.\n");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "Unable to drop permissions");
   }
 
   /* reset the random seed */
@@ -750,13 +731,12 @@ int main(int argc, char **argv)
 
   while (optind < argc) {
     char* name = argv[optind++];
-    append_to_names(argv[0], name);
+    append_to_names(name);
   }
 
   /* Now that we know mtrtype we can select which socket to use */
   if (net_selectsocket() != 0) {
-    fprintf( stderr, "mtr: Couldn't determine raw socket type.\n" );
-    exit( EXIT_FAILURE );
+    error(EXIT_FAILURE, 0, "Couldn't determine raw socket type");
   }
 
   if (PrintVersion) {
@@ -770,7 +750,7 @@ int main(int argc, char **argv)
 
   time_t now = time(NULL);
 
-  if (!names) append_to_names (argv[0], "localhost"); // default: localhost. 
+  if (!names) append_to_names ("localhost"); // default: localhost. 
 
   names_t* head = names;
   while (names != NULL) {
@@ -782,7 +762,7 @@ int main(int argc, char **argv)
     }
 
     if (net_preopen_result != 0) {
-      fprintf(stderr, "mtr: Unable to get raw socket.  (Executable not suid?)\n");
+      error(0, 0, "Unable to get raw socket.  (Executable not suid?)");
       if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE);
       else {
         names = names->next;
@@ -794,12 +774,12 @@ int main(int argc, char **argv)
     memset( &hints, 0, sizeof hints );
     hints.ai_family = af;
     hints.ai_socktype = SOCK_DGRAM;
-    error = getaddrinfo( Hostname, NULL, &hints, &res );
-    if ( error ) {
-      if (error == EAI_SYSTEM)
-         perror ("Failed to resolve host");
+    gai_error = getaddrinfo( Hostname, NULL, &hints, &res );
+    if ( gai_error ) {
+      if (gai_error == EAI_SYSTEM)
+         error(0, 0, "Failed to resolve host: %s", Hostname);
       else
-         fprintf (stderr, "Failed to resolve host: %s\n", gai_strerror(error));
+         error(0, 0, "Failed to resolve host: %s: %s", Hostname, gai_strerror(gai_error));
 
       if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE);
       else {
@@ -828,7 +808,7 @@ int main(int argc, char **argv)
       break;
 #endif
     default:
-      fprintf( stderr, "mtr unknown address type\n" );
+      error(0, 0, "unknown address type");
       if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE);
       else {
         names = names->next;
@@ -838,7 +818,7 @@ int main(int argc, char **argv)
     alptr[1] = NULL;
 
     if (net_open(host) != 0) {
-      fprintf(stderr, "mtr: Unable to start net module.\n");
+      error(0, 0, "Unable to start net module");
       if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE);
       else {
         names = names->next;
@@ -847,7 +827,7 @@ int main(int argc, char **argv)
     }
 
     if (net_set_interfaceaddress (InterfaceAddress) != 0) {
-      fprintf( stderr, "mtr: Couldn't set interface address.\n" );
+      error(0, 0, "Couldn't set interface address: %s", InterfaceAddress);
       if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE);
       else {
         names = names->next;
@@ -856,7 +836,7 @@ int main(int argc, char **argv)
     }
 
 
-    lock(argv[0], stdout);
+    lock(stdout);
       display_open();
       dns_open();
 
@@ -864,7 +844,7 @@ int main(int argc, char **argv)
 
       net_end_transit();
       display_close(now);
-    unlock(argv[0], stdout);
+    unlock(stdout);
 
     if ( DisplayMode != DisplayCSV ) break;
     else names = names->next;
diff --git a/net.c b/net.c
index c94abb400775fbafd80851be2a1455d410fdd446..fd96b960789c0882dd9473edbec751ac4c7ac626 100644 (file)
--- a/net.c
+++ b/net.c
@@ -38,6 +38,8 @@
 #include <math.h>
 #include <errno.h>
 #include <string.h>
+#include <error.h>
+
 
 #include "mtr.h"
 #include "net.h"
@@ -365,28 +367,24 @@ void net_send_tcp(int index)
 
   if (bind(s, (struct sockaddr *) &local, len)) {
     display_clear();
-    perror("bind()");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "bind()");
   }
 
   if (getsockname(s, (struct sockaddr *) &local, &len)) {
     display_clear();
-    perror("getsockname()");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "getsockname()");
   }
 
   //  opt = 1;
   flags = fcntl(s, F_GETFL, 0);
   if (flags < 0) {
     display_clear();
-    perror("ioctl FIONBIO");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "fcntl(F_GETFL)");
   }
 
   if (fcntl (s, F_SETFL, flags | O_NONBLOCK) < 0) {
     display_clear();
-    perror("ioctl FIONBIO");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "fcntl(F_SETFL, O_NONBLOCK)");
   }
 
 
@@ -394,21 +392,18 @@ void net_send_tcp(int index)
   case AF_INET:
     if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl))) {
       display_clear();
-      perror("setsockopt IP_TTL");
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "setsockopt IP_TTL");
     }
     if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof (tos))) {
       display_clear();
-      perror("setsockopt IP_TOS");
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "setsockopt IP_TOS");
     }
     break;
 #ifdef ENABLE_IPV6
   case AF_INET6:
     if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl))) {
       display_clear();
-      perror("setsockopt IP_TTL");
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "setsockopt IPPROTO_IPV6 ttl");
     }
     break;
 #endif
@@ -416,8 +411,7 @@ void net_send_tcp(int index)
 
 #ifdef SO_MARK
     if (mark >= 0 && setsockopt( s, SOL_SOCKET, SO_MARK, &mark, sizeof mark ) ) {
-      perror( "setsockopt SO_MARK" );
-      exit( EXIT_FAILURE );
+      error(EXIT_FAILURE, errno, "setsockopt SO_MARK");
     }
 #endif
 
@@ -432,8 +426,7 @@ void net_send_tcp(int index)
 #endif
   default:
     display_clear();
-    perror("unknown AF?");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, 0, "unknown address family");
   }
 
   save_sequence(index, port);
@@ -465,8 +458,7 @@ void net_send_sctp(int index)
   s = socket(af, SOCK_STREAM, IPPROTO_SCTP);
   if (s < 0) {
     display_clear();
-    perror("socket()");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "socket()");
   }
 
   memset(&local, 0, sizeof (local));
@@ -493,42 +485,36 @@ void net_send_sctp(int index)
 
   if (bind(s, (struct sockaddr *) &local, len)) {
     display_clear();
-    perror("bind()");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "bind()");
   }
 
   if (getsockname(s, (struct sockaddr *) &local, &len)) {
     display_clear();
-    perror("getsockname()");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "getsockname()");
   }
 
   opt = 1;
   if (ioctl(s, FIONBIO, &opt)) {
     display_clear();
-    perror("ioctl FIONBIO");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "ioctl FIONBIO");
   }
 
   switch (af) {
   case AF_INET:
     if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl))) {
       display_clear();
-      perror("setsockopt IP_TTL");
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "setsockopt IP_TTL");
     }
     if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof (tos))) {
       display_clear();
-      perror("setsockopt IP_TOS");
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "setsockopt IP_TOS");
     }
     break;
 #ifdef ENABLE_IPV6
   case AF_INET6:
     if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl))) {
       display_clear();
-      perror("setsockopt IP_TTL");
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "setsockopt IPPROTO_IPV6 ttl");
     }
     break;
 #endif
@@ -536,8 +522,7 @@ void net_send_sctp(int index)
 
 #ifdef SO_MARK
     if (mark >= 0 && setsockopt( s, SOL_SOCKET, SO_MARK, &mark, sizeof mark ) ) {
-      perror( "setsockopt SO_MARK" );
-      exit( EXIT_FAILURE );
+      error(EXIT_FAILURE, errno, "setsockopt SO_MARK");
     }
 #endif
 
@@ -552,8 +537,7 @@ void net_send_sctp(int index)
 #endif
   default:
     display_clear();
-    perror("unknown AF?");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, 0, "unknown address family");
   }
 
   save_sequence(index, port);
@@ -612,12 +596,10 @@ void net_send_query(int index)
 #if !defined(IP_HDRINCL) && defined(IP_TOS) && defined(IP_TTL)
     iphsize = 0;
     if ( setsockopt( sendsock, IPPROTO_IP, IP_TOS, &tos, sizeof tos ) ) {
-      perror( "setsockopt IP_TOS" );
-      exit( EXIT_FAILURE );
+      error(EXIT_FAILURE, errno, "setsockopt IP_TOS");
     }    
     if ( setsockopt( sendsock, IPPROTO_IP, IP_TTL, &ttl, sizeof ttl ) ) {
-      perror( "setsockopt IP_TTL" );
-      exit( EXIT_FAILURE );
+      error(EXIT_FAILURE, errno, "setsockopt IP_TTL");
     }    
 #else
     iphsize = sizeof (struct IPHeader);
@@ -643,8 +625,7 @@ void net_send_query(int index)
     iphsize = 0;
     if ( setsockopt( sendsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
                      &ttl, sizeof ttl ) ) {
-      perror( "setsockopt IPV6_UNICAST_HOPS" );
-      exit( EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "setsockopt IPV6_UNICAST_HOPS");
     }
     echotype = ICMP6_ECHO_REQUEST;
     salen = sizeof (struct sockaddr_in6);
@@ -654,8 +635,7 @@ void net_send_query(int index)
 
 #ifdef SO_MARK
     if (mark >= 0 && setsockopt( sendsock, SOL_SOCKET, SO_MARK, &mark, sizeof mark ) ) {
-      perror( "setsockopt SO_MARK" );
-      exit( EXIT_FAILURE );
+      error(EXIT_FAILURE, errno, "setsockopt SO_MARK");
     }
 #endif
 
@@ -733,8 +713,7 @@ void net_send_query(int index)
         offset = sizeof(struct UDPHeader);
       }
       if ( setsockopt(sendsock, IPPROTO_IPV6, IPV6_CHECKSUM, &offset, sizeof(offset)) ) {
-        perror( "setsockopt IPV6_CHECKSUM" );
-        exit( EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "setsockopt IPV6_CHECKSUM");
       }
       break;
     }
@@ -929,8 +908,7 @@ void net_process_return(void)
   num = recvfrom(recvsock, packet, MAXPACKET, 0, 
                 fromsockaddr, &fromsockaddrsize);
   if(num < 0) {
-    perror("recvfrom failed");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "recvfrom failed");
   }
 
   switch ( af ) {
@@ -1353,11 +1331,11 @@ static void set_fd_flags(int fd)
 
   oldflags = fcntl(fd, F_GETFD);
   if (oldflags == -1) {
-    perror("Couldn't get fd's flags");
+    error(0, errno, "Couldn't get fd's flags");
     return;
   }
   if (fcntl(fd, F_SETFD, oldflags | FD_CLOEXEC))
-    perror("Couldn't set fd's flags");
+    error(0, errno, "Couldn't set fd's flags");
 #endif
 }
 
@@ -1382,7 +1360,7 @@ int net_preopen(void)
   /*  FreeBSD wants this to avoid sending out packets with protocol type RAW
       to the network.  */
   if (setsockopt(sendsock4, SOL_IP, IP_HDRINCL, &trueopt, sizeof(trueopt))) {
-    perror("setsockopt(IP_HDRINCL,1)");
+    error(0, errno, "setsockopt IP_HDRINCL");
     return -1;
   }
 #endif /* IP_HDRINCL */
@@ -1457,8 +1435,7 @@ int net_open(struct hostent * hostent)
 #ifdef ENABLE_IPV6
   case AF_INET6:
     if (sendsock6 < 0 || recvsock6 < 0) {
-      fprintf( stderr, "Could not open IPv6 socket\n" );
-      exit( EXIT_FAILURE );
+      error(EXIT_FAILURE, errno, "Could not open IPv6 socket");
     }
     sendsock = sendsock6;
     recvsock = recvsock6;
@@ -1468,8 +1445,7 @@ int net_open(struct hostent * hostent)
     break;
 #endif
   default:
-    fprintf( stderr, "net_open bad address type\n" );
-    exit( EXIT_FAILURE );
+    error(EXIT_FAILURE, 0, "net_open bad address type");
   }
 
   len = sizeof name_struct; 
@@ -1504,8 +1480,7 @@ void net_reopen(struct hostent * addr)
     break;
 #endif
   default:
-    fprintf( stderr, "net_reopen bad address type\n" );
-    exit( EXIT_FAILURE );
+    error(EXIT_FAILURE, 0, "net_reopen bad address type");
   }
 
   net_reset ();
@@ -1590,13 +1565,11 @@ int net_set_interfaceaddress_udp(void)
 
   s = socket (af, SOCK_DGRAM, 0);
   if (s < 0) {
-    perror("udp socket()");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "udp socket()");
   }
 
   if (connect(s, (struct sockaddr *) &remote, len)) {
-    perror("udp connect() failed");
-    exit(EXIT_FAILURE);
+    error(EXIT_FAILURE, errno, "udp connect()");
   }
 
   getsockname(s, name, &len);
@@ -1639,7 +1612,7 @@ int net_set_interfaceaddress (char *InterfaceAddress)
   case AF_INET:
     ssa4->sin_port = 0;
     if ( inet_aton( InterfaceAddress, &(ssa4->sin_addr) ) < 1 ) {
-      fprintf( stderr, "mtr: bad interface address: %s\n", InterfaceAddress );
+      error(0, 0, "bad interface address: %s", InterfaceAddress);
       return( 1 );
   }
     len = sizeof (struct sockaddr);
@@ -1648,7 +1621,7 @@ int net_set_interfaceaddress (char *InterfaceAddress)
   case AF_INET6:
     ssa6->sin6_port = 0;
     if ( inet_pton( af, InterfaceAddress, &(ssa6->sin6_addr) ) < 1 ) {
-      fprintf( stderr, "mtr: bad interface address: %s\n", InterfaceAddress );
+      error(0, 0, "bad interface address: %s", InterfaceAddress);
       return( 1 );
     }
     len = sizeof (struct sockaddr_in6);
@@ -1657,7 +1630,7 @@ int net_set_interfaceaddress (char *InterfaceAddress)
   }
 
   if ( bind( sendsock, sourcesockaddr, len ) == -1 ) {
-    perror("mtr: failed to bind to interface");
+    error(0, 0, "failed to bind to interface: %s", InterfaceAddress);
       return( 1 );
   }
   getsockname (sendsock, name, &len);
@@ -1744,6 +1717,7 @@ void sockaddrtop( struct sockaddr * saddr, char * strptr, size_t len ) {
 #endif
   default:
     fprintf( stderr, "sockaddrtop unknown address type\n" );
+    error(0, 0, "sockaddrtop unknown address type");
     strptr[0] = '\0';
     return;
   }
index 64aa8734ae1d6cfcb3cc08171d775b67f3b7b600..52d81e106f4b7312fe4217d4967bc46071bd5477 100644 (file)
--- a/select.c
+++ b/select.c
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <math.h>
 #include <errno.h>
+#include <error.h>
 
 #include "mtr.h"
 #include "dns.h"
@@ -178,8 +179,7 @@ void select_loop(void) {
     } while ((rv < 0) && (errno == EINTR));
 
     if (rv < 0) {
-      perror ("Select failed");
-      exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "Select failed");
     }
     anyset = 0;