Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/traceroute use err(3) and warn(3) for error and war...



details:   https://anonhg.NetBSD.org/src/rev/479a7a43d875
branches:  trunk
changeset: 764919:479a7a43d875
user:      christos <christos%NetBSD.org@localhost>
date:      Thu May 12 01:59:16 2011 +0000

description:
use err(3) and warn(3) for error and warning printing, tiny knf.

diffstat:

 usr.sbin/traceroute/traceroute.c |  271 ++++++++++++++------------------------
 1 files changed, 99 insertions(+), 172 deletions(-)

diffs (truncated from 560 to 300 lines):

diff -r 72cfdadf0cf5 -r 479a7a43d875 usr.sbin/traceroute/traceroute.c
--- a/usr.sbin/traceroute/traceroute.c  Wed May 11 23:20:46 2011 +0000
+++ b/usr.sbin/traceroute/traceroute.c  Thu May 12 01:59:16 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: traceroute.c,v 1.77 2011/05/10 01:52:49 christos Exp $ */
+/*     $NetBSD: traceroute.c,v 1.78 2011/05/12 01:59:16 christos Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
@@ -29,7 +29,7 @@
 #else
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997\
  The Regents of the University of California.  All rights reserved.");
-__RCSID("$NetBSD: traceroute.c,v 1.77 2011/05/10 01:52:49 christos Exp $");
+__RCSID("$NetBSD: traceroute.c,v 1.78 2011/05/12 01:59:16 christos Exp $");
 #endif
 #endif
 
@@ -453,10 +453,8 @@
        if (prog_init && prog_init() == -1)
                err(1, "init failed");
 
-       if ((s = prog_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
-               Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno));
-               exit(1);
-       }
+       if ((s = prog_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
+               err(1, "icmp socket");
 
        /*
         * XXX 'useicmp' will always be zero here. I think the HP-UX users
@@ -468,10 +466,8 @@
        sndsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW
            useicmp ? IPPROTO_ICMP : IPPROTO_UDP);
 #endif
-       if (sndsock < 0) {
-               Fprintf(stderr, "%s: raw socket: %s\n", prog, strerror(errno));
-               exit(1);
-       }
+       if (sndsock < 0)
+               err(1, "raw socket");
 
        /* Revert to non-privileged user after opening sockets */
        setuid(getuid());
@@ -509,12 +505,8 @@
                        break;
 
                case 'g':
-                       if (lsrr >= NGATEWAYS) {
-                               Fprintf(stderr,
-                                   "%s: No more than %d gateways\n",
-                                   prog, NGATEWAYS);
-                               exit(1);
-                       }
+                       if (lsrr >= NGATEWAYS)
+                               errx(1, "more than %d gateways", NGATEWAYS);
                        getaddr(gwlist + lsrr, optarg);
                        ++lsrr;
                        break;
@@ -589,15 +581,12 @@
                        usage();
                }
 
-       if (first_ttl > max_ttl) {
-               Fprintf(stderr,
-                   "%s: first ttl (%d) may not be greater than max ttl (%d)\n",
-                   prog, first_ttl, max_ttl);
-               exit(1);
-       }
+       if (first_ttl > max_ttl)
+               errx(1, "first ttl (%d) may not be greater than max ttl (%d)",
+                   first_ttl, max_ttl);
 
        if (!docksum)
-               Fprintf(stderr, "%s: Warning: ckecksums disabled\n", prog);
+               warnx("ckecksums disabled");
 
        if (lsrr > 0)
                optlen = (lsrr + 1) * sizeof(gwlist[0]);
@@ -608,11 +597,9 @@
                minpacket += sizeof(*outudp);
        if (packlen == 0)
                packlen = minpacket;            /* minimum sized packet */
-       else if (minpacket > packlen || packlen > maxpacket) {
-               Fprintf(stderr, "%s: packet size must be %d <= s <= %d\n",
-                   prog, minpacket, maxpacket);
-               exit(1);
-       }
+       else if (minpacket > packlen || packlen > maxpacket)
+               errx(1, "packet size must be %d <= s <= %d",
+                   minpacket, maxpacket);
 
        if (mtudisc)
                packlen = *mtuptr++;
@@ -630,9 +617,8 @@
                hi = gethostinfo(hostname);
                setsin(to, hi->addrs[0]);
                if (hi->n > 1)
-                       Fprintf(stderr,
-                   "%s: Warning: %s has multiple addresses; using %s\n",
-                               prog, hostname, inet_ntoa(to->sin_addr));
+                       warnx("%s has multiple addresses; using %s",
+                           hostname, inet_ntoa(to->sin_addr));
                hostname = hi->name;
                hi->name = NULL;
                freehostinfo(hi);
@@ -648,12 +634,10 @@
        setvbuf(stdout, NULL, _IOLBF, 0);
 #endif
 
-       outip = (struct ip *)malloc((unsigned)packlen);
-       if (outip == NULL) {
-               Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno));
-               exit(1);
-       }
-       memset((char *)outip, 0, packlen);
+       outip = malloc((unsigned)packlen);
+       if (outip == NULL)
+               err(1, "malloc");
+       memset(outip, 0, packlen);
 
        outip->ip_v = IPVERSION;
        if (settos)
@@ -798,67 +782,51 @@
                memcpy(optlist + 4, gwlist, i);
 
                if ((prog_setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS, optlist,
-                   i + sizeof(gwlist[0]))) < 0) {
-                       Fprintf(stderr, "%s: IP_OPTIONS: %s\n",
-                           prog, strerror(errno));
-                       exit(1);
-                   }
+                   i + sizeof(gwlist[0]))) < 0)
+                       err(1, "IP_OPTIONS");
        }
 #endif
 
 #ifdef SO_SNDBUF
        if (prog_setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&packlen,
-           sizeof(packlen)) < 0) {
-               Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno));
-               exit(1);
-       }
+           sizeof(packlen)) < 0)
+               err(1, "SO_SNDBUF");
 #endif
 #ifdef IP_HDRINCL
        if (prog_setsockopt(sndsock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
-           sizeof(on)) < 0) {
-               Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog, strerror(errno));
-               exit(1);
-       }
+           sizeof(on)) < 0)
+               err(1, "IP_HDRINCL");
 #else
 #ifdef IP_TOS
        if (settos && prog_setsockopt(sndsock, IPPROTO_IP, IP_TOS,
-           (char *)&tos, sizeof(tos)) < 0) {
-               Fprintf(stderr, "%s: setsockopt tos %d: %s\n",
-                   prog, tos, strerror(errno));
-               exit(1);
-       }
+           &tos, sizeof(tos)) < 0)
+               err(1, "setsockopt tos %d", tos);
 #endif
 #endif
        if (options & SO_DEBUG)
-               (void)prog_setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, (char *)&on,
-                   sizeof(on));
+               if (prog_setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, &on,
+                   sizeof(on)) < 0)
+                       err(1, "setsockopt debug %d", tos);
        if (options & SO_DONTROUTE)
-               (void)prog_setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
-                   sizeof(on));
+               if (prog_setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, &on,
+                   sizeof(on)) < 0)
+                       err(1, "setsockopt dontroute %d", tos);
 
        /* Get the interface address list */
        n = ifaddrlist(&al, errbuf, sizeof errbuf);
        al2 = al;
-       if (n < 0) {
-               Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf);
-               exit(1);
-       }
-       if (n == 0) {
-               Fprintf(stderr,
-                   "%s: Can't find any network interfaces\n", prog);
-               exit(1);
-       }
+       if (n < 0)
+               errx(1, "ifaddrlist (%s)", errbuf);
+       if (n == 0)
+               errx(1, "Can't find any network interfaces");
 
        /* Look for a specific device */
        if (device != NULL) {
                for (i = n; i > 0; --i, ++al2)
                        if (strcmp(device, al2->device) == 0)
                                break;
-               if (i <= 0) {
-                       Fprintf(stderr, "%s: Can't find interface %s\n",
-                           prog, device);
-                       exit(1);
-               }
+               if (i <= 0)
+                       errx(1, "Can't find interface %s", device);
        }
 
        /* Determine our source address */
@@ -870,9 +838,8 @@
                 */
                setsin(from, al2->addr);
                if (n > 1 && device == NULL && !find_local_ip(from, to)) {
-                       Fprintf(stderr,
-                   "%s: Warning: Multiple interfaces found; using %s @ %s\n",
-                           prog, inet_ntoa(from->sin_addr), al2->device);
+                       warnx("Multiple interfaces found; using %s @ %s",
+                           inet_ntoa(from->sin_addr), al2->device);
                }
        } else {
                hi = gethostinfo(source);
@@ -885,9 +852,8 @@
                         */
                        setsin(from, hi->addrs[0]);
                        if (hi->n > 1)
-                               Fprintf(stderr,
-                       "%s: Warning: %s has multiple addresses; using %s\n",
-                                   prog, source, inet_ntoa(from->sin_addr));
+                               warnx("%s has multiple addresses; using %s",
+                                   source, inet_ntoa(from->sin_addr));
                } else {
                        /*
                         * Make sure the source specified matches the
@@ -896,12 +862,9 @@
                        for (i = hi->n, ap = hi->addrs; i > 0; --i, ++ap)
                                if (*ap == al2->addr)
                                        break;
-                       if (i <= 0) {
-                               Fprintf(stderr,
-                                   "%s: %s is not on interface %s\n",
-                                   prog, source, device);
-                               exit(1);
-                       }
+                       if (i <= 0)
+                               errx(1, "%s is not on interface %s",
+                                   source, device);
                        setsin(from, *ap);
                }
                freehostinfo(hi);
@@ -917,28 +880,22 @@
                for (i = n; i > 0; --i, ++al2)
                        if (from->sin_addr.s_addr == al2->addr)
                            break;
-               if (i <= 0) {
-                       Fprintf(stderr, "%s: %s is not a valid local address "
-                           "and you are not superuser.\n", prog,
+               if (i <= 0)
+                       errx(1, "%s is not a valid local address "
+                           "and you are not superuser.",
                            inet_ntoa(from->sin_addr));
-                       exit(1);
-               }
        }
 
        outip->ip_src = from->sin_addr;
 #ifndef IP_HDRINCL
-       if (bind(sndsock, (struct sockaddr *)from, sizeof(*from)) < 0) {
-               Fprintf(stderr, "%s: bind: %s\n",
-                   prog, strerror(errno));
-               exit (1);
-       }
+       if (bind(sndsock, (struct sockaddr *)from, sizeof(*from)) < 0)
+               err(1, "bind");
 #endif
 
        if (as_path) {
                asn = as_setup(as_server);
                if (asn == NULL) {
-                       Fprintf(stderr, "%s: as_setup failed, AS# lookups disabled\n", 
-                               prog);
+                       warnx("as_setup failed, AS# lookups disabled");
                        (void)fflush(stderr);
                        as_path = 0;
                }
@@ -1101,11 +1058,9 @@
        }
 
        retval = prog_poll(set, 1, wait.tv_sec * 1000 + wait.tv_usec / 1000);
-       if (retval < 0)  {
+       if (retval < 0) 
                /* If we continue, we probably just flood the remote host. */
-               Fprintf(stderr, "%s: poll: %s\n", prog, strerror(errno));
-               exit(1);
-       }
+               err(1, "poll");
        if (retval > 0)  {
                cc = prog_recvfrom(s, (char *)packet, sizeof(packet), 0,
                            (struct sockaddr *)fromp, &fromlen);



Home | Main Index | Thread Index | Old Index