- Allow device probing again (this should fix the problem Arnaldo de Melo
authorKlaas Freitag <freitag@suse.de>
Tue, 17 Nov 1998 15:16:09 +0000 (15:16 +0000)
committerKlaas Freitag <freitag@suse.de>
Tue, 17 Nov 1998 15:16:09 +0000 (15:16 +0000)
reported)

- Various cleanups and bug fixes.

- Better error message for ENODEV.

- Proper exit codes for interface printing

- Add safe_strncpy and use it everywhere: this fixes quite a lot of
strncpy bugs (strncpy doesn't add a '\0' when the source string is too
long!)

22 files changed:
arp.c
ifconfig.c
interface.c
interface.h
lib/af.c
lib/arcnet.c
lib/ash.c
lib/ax25.c
lib/ddp.c
lib/ether.c
lib/fddi.c
lib/getroute.c
lib/hippi.c
lib/inet.c
lib/inet6.c
lib/inet_sr.c
lib/ipx.c
lib/loopback.c
lib/netrom.c
lib/unix.c
lib/util.c
lib/util.h

diff --git a/arp.c b/arp.c
index abfc72f..6fa3827 100644 (file)
--- a/arp.c
+++ b/arp.c
@@ -8,7 +8,7 @@
  *              NET-3 Networking Distribution for the LINUX operating
  *              system.
  *
- * Version:     $Id: arp.c,v 1.9 1998/11/15 20:07:31 freitag Exp $
+ * Version:     $Id: arp.c,v 1.10 1998/11/17 15:16:09 freitag Exp $
  *
  * Maintainer:  Bernd 'eckes' Eckenfels, <net-tools@lina.inka.de>
  *
@@ -69,6 +69,7 @@
 #include "version.h"
 #include "config.h"
 #include "intl.h"
+#include "util.h"
 
 #define DFLT_AF        "inet"
 #define DFLT_HW        "ether"
@@ -107,8 +108,7 @@ static int arp_del(char **args)
        fprintf(stderr, _("arp: need host name\n"));
        return (-1);
     }
-    host[(sizeof host) - 1] = 0;
-    strncpy(host, *args, (sizeof host) - 1);
+    safe_strncpy(host, *args, (sizeof host));
     if (ap->input(0, host, &sa) < 0) {
        ap->herror(host);
        return (-1);
@@ -165,8 +165,7 @@ static int arp_del(char **args)
        if (!strcmp(*args, "dev")) {
            if (*++args == NULL)
                usage();
-           strncpy(device, *args, sizeof(device) - 1);
-           device[sizeof(device) - 1] = '\0';
+           safe_strncpy(device, *args, sizeof(device));
            args++;
            continue;
        }
@@ -268,8 +267,7 @@ static int arp_set(char **args)
        fprintf(stderr, _("arp: need host name\n"));
        return (-1);
     }
-    host[(sizeof host) - 1] = 0;
-    strncpy(host, *args++, (sizeof host) - 1);
+    safe_strncpy(host, *args++, (sizeof host));
     if (ap->input(0, host, &sa) < 0) {
        ap->herror(host);
        return (-1);
@@ -336,8 +334,7 @@ static int arp_set(char **args)
        if (!strcmp(*args, "dev")) {
            if (*++args == NULL)
                usage();
-           strncpy(device, *args, sizeof(device) - 1);
-           device[sizeof(device) - 1] = '\0';
+           safe_strncpy(device, *args, sizeof(device));
            args++;
            continue;
        }
@@ -528,8 +525,7 @@ static int arp_show(char *name)
 
     if (name != NULL) {
        /* Resolve the host name. */
-       host[(sizeof host) - 1] = 0;
-       strncpy(host, name, (sizeof host) - 1);
+       safe_strncpy(host, name, (sizeof host));
        if (ap->input(0, host, &sa) < 0) {
            ap->herror(host);
            return (-1);
@@ -708,8 +704,7 @@ int main(int argc, char **argv)
            hw_set = 1;
            break;
        case 'i':
-           strncpy(device, optarg, sizeof(device) - 1);
-           device[sizeof(device) - 1] = '\0';
+           safe_strncpy(device, optarg, sizeof(device));
            break;
 
        case 'V':
index efebfca..fc00e69 100644 (file)
@@ -3,7 +3,7 @@
  *              that either displays or sets the characteristics of
  *              one or more of the system's networking interfaces.
  *
- * Version:     $Id: ifconfig.c,v 1.18 1998/11/15 20:07:42 freitag Exp $
+ * Version:     $Id: ifconfig.c,v 1.19 1998/11/17 15:16:14 freitag Exp $
  *
  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  *              and others.  Copyright 1993 MicroWalt Corporation
@@ -310,8 +310,8 @@ static void ife_print(struct interface *ptr)
     /* If needed, display the interface statistics. */
 
     if (ptr->statistics_valid) {
-       /* XXX: statistics are currently only printed for the original address,
-        *    not for the aliases, although strictly speaking they're shared
+       /* XXX: statistics are currently only printed for the primary address,
+        *      not for the aliases, although strictly speaking they're shared
         *      by all addresses.
         */
        printf("          ");
@@ -336,6 +336,7 @@ static void ife_print(struct interface *ptr)
            printf(_("txqueuelen:%d "), ptr->tx_queue_len);
        printf("\n");
     }
+
     if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
         ptr->map.base_addr)) {
        printf("          ");
@@ -354,37 +355,51 @@ static void ife_print(struct interface *ptr)
     printf("\n");
 }
 
+static int do_if_fetch(struct interface *ife)
+{ 
+    if (if_fetch(ife) < 0) {
+       char *errmsg; 
+       if (errno == ENODEV) { 
+           /* Give better error message for this case. */ 
+           errmsg = _("Device not found"); 
+       } else { 
+           errmsg = strerror(errno); 
+       }
+       fprintf(stderr, _("%s: error fetching interface information: %s\n"),
+               ife->name, errmsg);
+       return -1;
+    }
+    return 0; 
+}
+
 static int do_if_print(struct interface *ife, void *cookie)
 {
     int *opt_a = (int *) cookie;
+    int res; 
 
-    if (if_fetch(ife->name, ife) < 0) {
-       fprintf(stderr, _("%s: error fetching interface information: %s\n\n"),
-               ife->name, strerror(errno));
-       return -1;
+    res = do_if_fetch(ife); 
+    if (res >= 0) {   
+       if ((ife->flags & IFF_UP) || *opt_a)
+           ife_print(ife);
     }
-    if (!(ife->flags & IFF_UP) && !(*opt_a))
-       return 0;
-    ife_print(ife);
-    return 0;
+    return res;
 }
 
-static void if_print(char *ifname)
+static int if_print(char *ifname)
 {
-    struct interface *ife;
+    int res; 
 
     if (!ifname) {
-       for_all_interfaces(do_if_print, &opt_a);
+       res = for_all_interfaces(do_if_print, &opt_a);
     } else {
+       struct interface *ife;
+
        ife = lookup_interface(ifname);
-       if (!ife)
-           fprintf(stderr, _("%s: interface not found.\n"), ifname);
-       else if (if_fetch(ifname, ife) < 0)
-           fprintf(stderr, _("%s: error fetching interface information: %s"),
-                   ifname, strerror(errno));
-       else
+       res = do_if_fetch(ife); 
+       if (res >= 0) 
            ife_print(ife);
     }
+    return res; 
 }
 
 
@@ -540,17 +555,17 @@ int main(int argc, char **argv)
 
     /* Do we have to show the current setup? */
     if (argc == 0) {
-       if_print((char *) NULL);
+       int err = if_print((char *) NULL);
        (void) close(skfd);
-       exit(0);
+       exit(err < 0);
     }
     /* No. Fetch the interface name. */
     spp = argv;
-    strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
+    safe_strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
     if (*spp == (char *) NULL) {
-       if_print(ifr.ifr_name);
+       int err = if_print(ifr.ifr_name);
        (void) close(skfd);
-       exit(0);
+       exit(err < 0);
     }
     /* The next argument is either an address family name, or an option. */
     if ((ap = get_aftype(*spp)) == NULL)
@@ -739,8 +754,7 @@ int main(int argc, char **argv)
        }
        if (!strcmp(*spp, "broadcast")) {
            if (*++spp != NULL) {
-               host[(sizeof host) - 1] = 0;
-               strncpy(host, *spp, (sizeof host) - 1);
+               safe_strncpy(host, *spp, (sizeof host));
                if (ap->input(0, host, &sa) < 0) {
                    ap->herror(host);
                    goterr = 1;
@@ -762,8 +776,7 @@ int main(int argc, char **argv)
        if (!strcmp(*spp, "dstaddr")) {
            if (*++spp == NULL)
                usage();
-           host[(sizeof host) - 1] = 0;
-           strncpy(host, *spp, (sizeof host) - 1);
+           safe_strncpy(host, *spp, (sizeof host));
            if (ap->input(0, host, &sa) < 0) {
                ap->herror(host);
                goterr = 1;
@@ -783,8 +796,7 @@ int main(int argc, char **argv)
        if (!strcmp(*spp, "netmask")) {
            if (*++spp == NULL || didnetmask)
                usage();
-           host[(sizeof host) - 1] = 0;
-           strncpy(host, *spp, (sizeof host) - 1);
+           safe_strncpy(host, *spp, (sizeof host));
            if (ap->input(0, host, &sa) < 0) {
                ap->herror(host);
                goterr = 1;
@@ -863,8 +875,7 @@ int main(int argc, char **argv)
        if (!strcmp(*spp, "pointopoint")) {
            if (*(spp + 1) != NULL) {
                spp++;
-               host[(sizeof host) - 1] = 0;
-               strncpy(host, *spp, (sizeof host) - 1);
+               safe_strncpy(host, *spp, (sizeof host));
                if (ap->input(0, host, &sa)) {
                    ap->herror(host);
                    goterr = 1;
@@ -891,8 +902,7 @@ int main(int argc, char **argv)
                usage();
            if (*++spp == NULL)
                usage();
-           host[(sizeof host) - 1] = 0;
-           strncpy(host, *spp, (sizeof host) - 1);
+           safe_strncpy(host, *spp, (sizeof host));
            if (hw->input(host, &sa) < 0) {
                fprintf(stderr, _("%s: invalid %s address.\n"), host, hw->name);
                goterr = 1;
@@ -921,8 +931,7 @@ int main(int argc, char **argv)
            } else {
                prefix_len = 0;
            }
-           host[(sizeof host) - 1] = 0;
-           strncpy(host, *spp, (sizeof host) - 1);
+           safe_strncpy(host, *spp, (sizeof host));
            if (inet6_aftype.input(1, host, (struct sockaddr *) &sa6) < 0) {
                inet6_aftype.herror(host);
                goterr = 1;
@@ -965,8 +974,7 @@ int main(int argc, char **argv)
            } else {
                prefix_len = 0;
            }
-           host[(sizeof host) - 1] = 0;
-           strncpy(host, *spp, (sizeof host) - 1);
+           safe_strncpy(host, *spp, (sizeof host));
            if (inet6_aftype.input(1, host, (struct sockaddr *) &sa6) < 0) {
                inet6_aftype.herror(host);
                goterr = 1;
@@ -1014,8 +1022,7 @@ int main(int argc, char **argv)
            } else {
                prefix_len = 0;
            }
-           host[(sizeof host) - 1] = 0;
-           strncpy(host, *spp, (sizeof host) - 1);
+           safe_strncpy(host, *spp, (sizeof host));
            if (inet6_aftype.input(1, host, (struct sockaddr *) &sa6) < 0) {
                inet6_aftype.herror(host);
                goterr = 1;
@@ -1052,8 +1059,7 @@ int main(int argc, char **argv)
 #endif
 
        /* If the next argument is a valid hostname, assume OK. */
-       host[(sizeof host) - 1] = '\0';
-       strncpy(host, *spp, (sizeof host) - 1);
+       safe_strncpy(host, *spp, (sizeof host));
 
        /* FIXME: sa is too small for INET6 addresses, inet6 should use that too, 
           broadcast is unexpected */
index 6709fab..fd7d352 100644 (file)
@@ -4,7 +4,7 @@
    10/1998 partly rewriten by Andi Kleen to support an interface list.   
    I don't claim that the list operations are efficient @).  
 
-   $Id: interface.c,v 1.10 1998/11/15 20:07:52 freitag Exp $
+   $Id: interface.c,v 1.11 1998/11/17 15:16:20 freitag Exp $
  */
 
 #include "config.h"
@@ -67,13 +67,21 @@ void add_interface(struct interface *n)
 
 struct interface *lookup_interface(char *name)
 {
-    struct interface *ife;
-    if (!int_list && (if_readlist()) < 0)
-       return NULL;
-    for (ife = int_list; ife; ife = ife->next) {
-       if (!strcmp(ife->name, name))
-           break;
+    struct interface *ife = NULL;
+
+    if (int_list || if_readlist() >= 0) { 
+       for (ife = int_list; ife; ife = ife->next) {
+           if (!strcmp(ife->name, name))
+               break;
+       }
+    }
+
+    if (!ife) { 
+       new(ife);
+       safe_strncpy(ife->name, name, IFNAMSIZ-1);  
+       add_interface(ife);
     }
+
     return ife;
 }
 
@@ -99,14 +107,15 @@ static int if_readconf(void)
     int n, err = -1;
     int skfd;
 
-    /* SIOCGIFCONF seems to only work properly on AF_INET sockets
-       currently */
+    /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
+       (as of 2.1.128) */ 
     skfd = get_socket_for_af(AF_INET);
     if (skfd < 0) {
        fprintf(stderr, _("warning: no inet socket available: %s\n"),
                strerror(errno));
        return -1;
     }
+
     ifc.ifc_buf = NULL;
     for (;;) {
        ifc.ifc_len = sizeof(struct ifreq) * numreqs;
@@ -124,21 +133,14 @@ static int if_readconf(void)
        break;
     }
 
-    for (ifr = ifc.ifc_req, n = 0; n < ifc.ifc_len;
-        n += sizeof(struct ifreq), ifr++) {
-       struct interface *ife;
-
-       ife = lookup_interface(ifr->ifr_name);
-       if (ife)
-           continue;
-
-       new(ife);
-       strcpy(ife->name, ifr->ifr_name);
-       add_interface(ife);
+    ifr = ifc.ifc_req;
+    for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
+       lookup_interface(ifr->ifr_name);
+       ifr++;
     }
     err = 0;
 
-  out:
+out:
     free(ifc.ifc_buf);
     return err;
 }
@@ -320,10 +322,11 @@ static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
 #endif
 
 /* Fetch the interface configuration from the kernel. */
-int if_fetch(char *ifname, struct interface *ife)
+int if_fetch(struct interface *ife)
 {
     struct ifreq ifr;
     int fd;
+    char *ifname = ife->name; 
 
     strcpy(ifr.ifr_name, ifname);
     if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
index 51afb5c..10926b4 100644 (file)
@@ -62,7 +62,7 @@ struct interface {
     int outfill;               /* outfill value for SLIP */
 };
 
-extern int if_fetch(char *ifname, struct interface *ife);
+extern int if_fetch(struct interface *ife);
 
 extern int for_all_interfaces(int (*)(struct interface *, void *), void *);
 extern struct interface *lookup_interface(char *name);
index c373aa2..9b8efbe 100644 (file)
--- a/lib/af.c
+++ b/lib/af.c
@@ -2,7 +2,7 @@
  * lib/af.c   This file contains the top-level part of the protocol
  *              support functions module for the NET-2 base distribution.
  *
- * Version:     $Id: af.c,v 1.6 1998/11/15 20:08:59 freitag Exp $
+ * Version:     $Id: af.c,v 1.7 1998/11/17 15:16:26 freitag Exp $
  *
  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  *              Copyright 1993 MicroWalt Corporation
@@ -256,8 +256,7 @@ int aftrans_opt(const char *arg)
     char *tmp1, *tmp2;
     char buf[256];
 
-    strncpy(buf, arg, sizeof(buf));
-    buf[sizeof(buf) - 1] = '\0';
+    safe_strncpy(buf, arg, sizeof(buf));
 
     tmp1 = buf;
 
index 8b8a870..bb4ddce 100644 (file)
@@ -2,7 +2,7 @@
  * lib/arcnet.c       This file contains an implementation of the "ARCnet"
  *              support functions for the NET-2 base distribution.
  *
- * Version:     $Id: arcnet.c,v 1.3 1998/11/15 20:09:06 freitag Exp $
+ * Version:     $Id: arcnet.c,v 1.4 1998/11/17 15:16:29 freitag Exp $
  *
  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  *              Copyright 1993 MicroWalt Corporation
@@ -49,7 +49,7 @@ static char *pr_sarcnet(struct sockaddr *sap)
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (pr_arcnet(sap->sa_data));
 }
 
index 8542c27..c3a8447 100644 (file)
--- a/lib/ash.c
+++ b/lib/ash.c
@@ -1,7 +1,7 @@
 /*
  * lib/ash.c  This file contains an implementation of the Ash
  *              support functions for the NET-2 base distribution.
- * $Id: ash.c,v 1.6 1998/11/15 20:09:16 freitag Exp $
+ * $Id: ash.c,v 1.7 1998/11/17 15:16:31 freitag Exp $
  */
 
 #include "config.h"
@@ -52,7 +52,7 @@ static char *pr_sash(struct sockaddr *sap)
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, "[NONE SET]", 64);
+       return safe_strncpy(buf, "[NONE SET]", 64);
     return pr_ash(sap->sa_data);
 }
 
index ba5e1ad..5da08aa 100644 (file)
@@ -2,7 +2,7 @@
  * lib/ax25.c This file contains an implementation of the "AX.25"
  *              support functions.
  *
- * Version:     $Id: ax25.c,v 1.6 1998/11/15 20:09:19 freitag Exp $
+ * Version:     $Id: ax25.c,v 1.7 1998/11/17 15:16:32 freitag Exp $
  *
  * NOTE:        I will redo this module as soon as I got the libax25.a
  *              library sorted out.  This library contains some useful
@@ -71,7 +71,7 @@ static char *
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (AX25_print(((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call));
 }
 
@@ -93,8 +93,7 @@ static int AX25_input(int type, char *bufp, struct sockaddr *sap)
        if (islower(c))
            c = toupper(c);
        if (!(isupper(c) || isdigit(c))) {
-           strncpy(AX25_errmsg, _("Invalid callsign"), sizeof(AX25_errmsg)-2);
-           AX25_errmsg[sizeof(AX25_errmsg)-1]=0;
+           safe_strncpy(AX25_errmsg, _("Invalid callsign"), sizeof(AX25_errmsg));
 #ifdef DEBUG
            fprintf(stderr, "ax25_input(%s): %s !\n", AX25_errmsg, orig);
 #endif
@@ -154,7 +153,7 @@ static char *AX25_hprint(struct sockaddr *sap)
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (AX25_print(((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call));
 }
 
index d26eb70..b3ac453 100644 (file)
--- a/lib/ddp.c
+++ b/lib/ddp.c
@@ -4,7 +4,7 @@
  *
  *                      Alan Cox  <Alan.Cox@linux.org>
  *
- *             $Id: ddp.c,v 1.4 1998/11/15 20:09:25 freitag Exp $
+ *             $Id: ddp.c,v 1.5 1998/11/17 15:16:35 freitag Exp $
  *
  *              This program is free software; you can redistribute it
  *              and/or  modify it under  the terms of  the GNU General
@@ -46,7 +46,7 @@ static char *ddp_sprint(struct sockaddr *sap, int numeric)
     static char buf[64];
 
     if (sap->sa_family != AF_APPLETALK)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (ddp_print(sap->sa_data));
 }
 
index 56f8dc1..276dd6a 100644 (file)
@@ -2,7 +2,7 @@
  * lib/ether.c        This file contains an implementation of the "Ethernet"
  *              support functions.
  *
- * Version:     $Id: ether.c,v 1.4 1998/11/15 20:09:33 freitag Exp $
+ * Version:     $Id: ether.c,v 1.5 1998/11/17 15:16:37 freitag Exp $
  *
  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  *              Copyright 1993 MicroWalt Corporation
@@ -52,7 +52,7 @@ static char *pr_sether(struct sockaddr *sap)
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (pr_ether(sap->sa_data));
 }
 
index fadb8bd..38621ee 100644 (file)
@@ -2,7 +2,7 @@
  * lib/fddi.c This file contains an implementation of the "FDDI"
  *              support functions.
  *
- * Version:     $Id: fddi.c,v 1.4 1998/11/15 20:09:35 freitag Exp $
+ * Version:     $Id: fddi.c,v 1.5 1998/11/17 15:16:39 freitag Exp $
  *
  * Author:      Lawrence V. Stefani, <stefani@lkg.dec.com>
  *
@@ -63,7 +63,7 @@ static char *pr_sfddi(struct sockaddr *sap)
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (pr_fddi(sap->sa_data));
 }
 
index f4b91e7..fe93216 100644 (file)
@@ -7,7 +7,7 @@
  *              NET-3 Networking Distribution for the LINUX operating
  *              system. (net-tools, net-drivers)
  *
- * Version:     $Id: getroute.c,v 1.3 1998/11/15 20:09:47 freitag Exp $
+ * Version:     $Id: getroute.c,v 1.4 1998/11/17 15:16:42 freitag Exp $
  *
  * Author:      Bernd 'eckes' Eckenfels <net-tools@lina.inka.de>
  *              Copyright 1999 Bernd Eckenfels, Germany
@@ -75,8 +75,7 @@ int route_info(const char *afname, int options)
     int found = E_NOTFOUND, rc;
     char buf[256];
 
-    strncpy(buf, afname, sizeof(buf));
-    buf[sizeof(buf) - 1] = '\0';
+    safe_strncpy(buf, afname, sizeof(buf));
 
     tmp1 = buf;
 
index 0086a76..4bf15a5 100644 (file)
@@ -64,7 +64,7 @@ static char *
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return (strncpy(buf, _("[NONE SET]"), 64));
+       return (safe_strncpy(buf, _("[NONE SET]"), 64));
     return (pr_hippi(sap->sa_data));
 }
 
index 149a275..a8be57d 100644 (file)
@@ -3,7 +3,7 @@
  *              support functions for the net-tools.
  *              (NET-3 base distribution).
  *
- * Version:    $Id: inet.c,v 1.5 1998/11/15 20:10:27 freitag Exp $
+ * Version:    $Id: inet.c,v 1.6 1998/11/17 15:16:47 freitag Exp $
  *
  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  *              Copyright 1993 MicroWalt Corporation
@@ -189,7 +189,7 @@ static char *INET_sprint(struct sockaddr *sap, int numeric)
     static char buff[128];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buff, _("[NONE SET]"), sizeof(buff));
+       return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
     if (INET_rresolve(buff, (struct sockaddr_in *) sap, numeric) != 0)
        return (NULL);
     return (buff);
index 7c11528..c5e058f 100644 (file)
@@ -3,7 +3,7 @@
  *              support functions for the net-tools.
  *              (most of it copied from lib/inet.c 1.26).
  *
- * Version:     $Id: inet6.c,v 1.4 1998/11/15 20:10:37 freitag Exp $
+ * Version:     $Id: inet6.c,v 1.5 1998/11/17 15:16:49 freitag Exp $
  *
  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  *              Copyright 1993 MicroWalt Corporation
@@ -109,7 +109,7 @@ static char *INET6_sprint(struct sockaddr *sap, int numeric)
     static char buff[128];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buff, _("[NONE SET]"), sizeof(buff));
+       return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
     if (INET6_rresolve(buff, (struct sockaddr_in6 *) sap, numeric) != 0)
        return (NULL);
     return (buff);
index 6636e3a..fea1425 100644 (file)
@@ -71,8 +71,7 @@ static int INET_setroute(int action, int options, char **args)
     if (*args == NULL)
        return (usage());
 
-    target[(sizeof target) - 1] = 0;
-    strncpy(target, *args++, (sizeof target) - 1);
+    safe_strncpy(target, *args++, (sizeof target));
 
     /* Clean out the RTREQ structure. */
     memset((char *) &rt, 0, sizeof(struct rtentry));
@@ -136,8 +135,7 @@ static int INET_setroute(int action, int options, char **args)
            args++;
            if (!*args || mask_in_addr(rt))
                return (usage());
-           netmask[(sizeof netmask) - 1] = 0;
-           strncpy(netmask, *args, (sizeof netmask) - 1);
+           safe_strncpy(netmask, *args, (sizeof netmask));
            if ((isnet = inet_aftype.input(0, netmask, &mask)) < 0) {
                inet_aftype.herror(netmask);
                return (E_LOOKUP);
@@ -152,8 +150,7 @@ static int INET_setroute(int action, int options, char **args)
                return (usage());
            if (rt.rt_flags & RTF_GATEWAY)
                return (usage());
-           gateway[(sizeof gateway) - 1] = 0;
-           strncpy(gateway, *args, (sizeof gateway) - 1);
+           safe_strncpy(gateway, *args, (sizeof gateway));
            if ((isnet = inet_aftype.input(0, gateway, &rt.rt_gateway)) < 0) {
                inet_aftype.herror(gateway);
                return (E_LOOKUP);
index 668883e..07baa46 100644 (file)
--- a/lib/ipx.c
+++ b/lib/ipx.c
@@ -77,7 +77,7 @@ static char *IPX_sprint(struct sockaddr *sap, int numeric)
     static char buf[64];
 
     if (sap->sa_family != AF_IPX)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (IPX_print(sap->sa_data));
 }
 
index a96fd81..3395e28 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * lib/loopback.c     This file contains the general hardware types.
  *
- * Version:     $Id: loopback.c,v 1.3 1998/11/15 20:11:06 freitag Exp $
+ * Version:     $Id: loopback.c,v 1.4 1998/11/17 15:16:54 freitag Exp $
  *
  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  *              Copyright 1993 MicroWalt Corporation
@@ -52,7 +52,7 @@ static char *pr_sunspec(struct sockaddr *sap)
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (pr_unspec(sap->sa_data));
 }
 
index 0f8f957..c69723e 100644 (file)
@@ -2,7 +2,7 @@
  * lib/netrom.c       This file contains an implementation of the "NET/ROM"
  *              support functions for the NET-2 base distribution.
  *
- * Version:     $Id: netrom.c,v 1.5 1998/11/15 20:11:25 freitag Exp $
+ * Version:     $Id: netrom.c,v 1.6 1998/11/17 15:16:56 freitag Exp $
  *
  * NOTE:        I will redo this module as soon as I got the libax25.a
  *              library sorted out.  This library contains some useful
@@ -74,7 +74,7 @@ static char *NETROM_sprint(struct sockaddr *sap, int numeric)
 {
     char buf[64];
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (NETROM_print(((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call));
 }
 
@@ -96,7 +96,7 @@ static int NETROM_input(int type, char *bufp, struct sockaddr *sap)
        if (islower(c))
            c = toupper(c);
        if (!(isupper(c) || isdigit(c))) {
-           strncpy(netrom_errmsg, _("Invalid callsign"), sizeof(netrom_errmsg));
+           safe_strncpy(netrom_errmsg, _("Invalid callsign"), sizeof(netrom_errmsg));
 #ifdef DEBUG
            fprintf(stderr, "netrom_input(%s): %s !\n", netrom_errmsg, orig);
 #endif
@@ -109,7 +109,7 @@ static int NETROM_input(int type, char *bufp, struct sockaddr *sap)
 
     /* Callsign too long? */
     if ((i == 6) && (*bufp != '-') && (*bufp != '\0')) {
-       strncpy(netrom_errmsg, _("Callsign too long"), sizeof(netrom_errmsg));
+       safe_strncpy(netrom_errmsg, _("Callsign too long"), sizeof(netrom_errmsg));
 #ifdef DEBUG
        fprintf(stderr, "netrom_input(%s): %s !\n", netrom_errmsg, orig);
 #endif
index ec138fa..4eba894 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * lib/unix.c This file contains the general hardware types.
  *
- * Version:     $Id: unix.c,v 1.4 1998/11/15 20:12:31 freitag Exp $
+ * Version:     $Id: unix.c,v 1.5 1998/11/17 15:16:58 freitag Exp $
  *
  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  *              Copyright 1993 MicroWalt Corporation
@@ -52,7 +52,7 @@ static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (UNSPEC_print(sap->sa_data));
 }
 
@@ -72,7 +72,7 @@ static char *UNIX_sprint(struct sockaddr *sap, int numeric)
     static char buf[64];
 
     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
-       return strncpy(buf, _("[NONE SET]"), sizeof(buf));
+       return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
     return (UNIX_print(sap->sa_data));
 }
 
index 44d6d4e..ac932f4 100644 (file)
@@ -1,10 +1,13 @@
 /* Copyright 1998 by Andi Kleen. Subject to the GPL. */ 
-/* $Id: util.c,v 1.3 1998/11/15 20:12:35 freitag Exp $ */
+/* $Id: util.c,v 1.4 1998/11/17 15:17:02 freitag Exp $ */
 #include <stdio.h>
 #include <stdlib.h>
-#include "util.h"
+#include <string.h>
 #include <sys/utsname.h>
 
+#include "util.h"
+
+
 static void oom(void)
 {
     fprintf(stderr, "out of virtual memory\n");
@@ -38,3 +41,11 @@ int kernel_version(void)
        return -1;
     return KRELEASE(major, minor, patch);
 }
+
+
+/* Like strncpy but make sure the resulting string is always 0 terminated. */  
+char *safe_strncpy(char *dst, const char *src, size_t size)
+{   
+    dst[size-1] = '\0';
+    return strncpy(dst,src,size-1);   
+}
index 987a9fb..6acb158 100644 (file)
@@ -11,3 +11,6 @@ int kernel_version(void);
 
 
 int nstrcmp(const char *, const char *);
+
+char *safe_strncpy(char *dst, const char *src, size_t size); 
+