netstat memory leak fixed.
[platform/upstream/net-tools.git] / arp.c
diff --git a/arp.c b/arp.c
index b02c74b..72e87d6 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.24 2005/05/16 04:30:17 ecki Exp $
+ * Version:     $Id: arp.c,v 1.27 2009/09/06 22:50:11 vapier Exp $
  *
  * Maintainer:  Bernd 'eckes' Eckenfels, <net-tools@lina.inka.de>
  *
@@ -100,7 +100,8 @@ static int arp_del(char **args)
 {
     char host[128];
     struct arpreq req;
-    struct sockaddr sa;
+    struct sockaddr_storage ss;
+    struct sockaddr *sa;
     int flags = 0;
     int deleted = 0;
 
@@ -112,12 +113,13 @@ static int arp_del(char **args)
        return (-1);
     }
     safe_strncpy(host, *args, (sizeof host));
-    if (ap->input(0, host, &sa) < 0) {
+    sa = (struct sockaddr *)&ss;
+    if (ap->input(0, host, sa) < 0) {
        ap->herror(host);
        return (-1);
     }
     /* If a host has more than one address, use the correct one! */
-    memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct sockaddr));
+    memcpy((char *) &req.arp_pa, (char *) sa, sizeof(struct sockaddr));
 
     if (hw_set)
        req.arp_ha.sa_family = hw->type;
@@ -177,11 +179,11 @@ static int arp_del(char **args)
                usage();
            if (strcmp(*args, "255.255.255.255") != 0) {
                strcpy(host, *args);
-               if (ap->input(0, host, &sa) < 0) {
+               if (ap->input(0, host, sa) < 0) {
                    ap->herror(host);
                    return (-1);
                }
-               memcpy((char *) &req.arp_netmask, (char *) &sa,
+               memcpy((char *) &req.arp_netmask, (char *) sa,
                       sizeof(struct sockaddr));
                req.arp_flags |= ATF_NETMASK;
            }
@@ -241,7 +243,7 @@ static int arp_getdevhw(char *ifname, struct sockaddr *sa, struct hwtype *hw)
     struct ifreq ifr;
     struct hwtype *xhw;
 
-    strcpy(ifr.ifr_name, ifname);
+    strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
     if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
        fprintf(stderr, _("arp: cant get HW-Address for `%s': %s.\n"), ifname, strerror(errno));
        return (-1);
@@ -266,7 +268,8 @@ static int arp_set(char **args)
 {
     char host[128];
     struct arpreq req;
-    struct sockaddr sa;
+    struct sockaddr_storage ss;
+    struct sockaddr *sa;
     int flags;
 
     memset((char *) &req, 0, sizeof(req));
@@ -277,12 +280,13 @@ static int arp_set(char **args)
        return (-1);
     }
     safe_strncpy(host, *args++, (sizeof host));
-    if (ap->input(0, host, &sa) < 0) {
+    sa = (struct sockaddr *)&ss;
+    if (ap->input(0, host, sa) < 0) {
        ap->herror(host);
        return (-1);
     }
     /* If a host has more than one address, use the correct one! */
-    memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct sockaddr));
+    memcpy((char *) &req.arp_pa, (char *) sa, sizeof(struct sockaddr));
 
     /* Fetch the hardware address. */
     if (*args == NULL) {
@@ -352,11 +356,11 @@ static int arp_set(char **args)
                usage();
            if (strcmp(*args, "255.255.255.255") != 0) {
                strcpy(host, *args);
-               if (ap->input(0, host, &sa) < 0) {
+               if (ap->input(0, host, sa) < 0) {
                    ap->herror(host);
                    return (-1);
                }
-               memcpy((char *) &req.arp_netmask, (char *) &sa,
+               memcpy((char *) &req.arp_netmask, (char *) sa,
                       sizeof(struct sockaddr));
                flags |= ATF_NETMASK;
            }
@@ -430,7 +434,7 @@ static int arp_file(char *name)
 
 
 /* Print the contents of an ARP request block. */
-static void arp_disp_2(char *name, int type, int arp_flags, char *hwa, char *mask, char *dev)
+static void arp_disp_2(const char *name, int type, int arp_flags, const char *hwa, const char *mask, const char *dev)
 {
     static int title = 0;
     struct hwtype *xhw;
@@ -480,7 +484,7 @@ static void arp_disp_2(char *name, int type, int arp_flags, char *hwa, char *mas
 }
 
 /* Print the contents of an ARP request block. */
-static void arp_disp(char *name, char *ip, int type, int arp_flags, char *hwa, char *mask, char *dev)
+static void arp_disp(const char *name, const char *ip, int type, int arp_flags, const char *hwa, const char *mask, const char *dev)
 {
     struct hwtype *xhw;
 
@@ -525,7 +529,8 @@ static void arp_disp(char *name, char *ip, int type, int arp_flags, char *hwa, c
 static int arp_show(char *name)
 {
     char host[100];
-    struct sockaddr sa;
+    struct sockaddr_storage ss;
+    struct sockaddr *sa;
     char ip[100];
     char hwa[100];
     char mask[100];
@@ -533,19 +538,20 @@ static int arp_show(char *name)
     char dev[100];
     int type, flags;
     FILE *fp;
-    char *hostname;
+    const char *hostname;
     int num, entries = 0, showed = 0;
 
     host[0] = '\0';
 
+    sa = (struct sockaddr *)&ss;
     if (name != NULL) {
        /* Resolve the host name. */
        safe_strncpy(host, name, (sizeof host));
-       if (ap->input(0, host, &sa) < 0) {
+       if (ap->input(0, host, sa) < 0) {
            ap->herror(host);
            return (-1);
        }
-       safe_strncpy(host, ap->sprint(&sa, 1), sizeof(host));
+       safe_strncpy(host, ap->sprint(sa, 1), sizeof(host));
     }
     /* Open the PROCps kernel table. */
     if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL) {
@@ -558,7 +564,7 @@ static int arp_show(char *name)
        strcpy(dev, "-");
        /* Read the ARP cache entries. */
        for (; fgets(line, sizeof(line), fp);) {
-           num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n",
+           num = sscanf(line, "%s 0x%x 0x%x %99s %99s %99s\n",
                         ip, &type, &flags, hwa, mask, dev);
            if (num < 4)
                break;
@@ -581,10 +587,10 @@ static int arp_show(char *name)
            if (opt_n)
                hostname = "?";
            else {
-               if (ap->input(0, ip, &sa) < 0)
+               if (ap->input(0, ip, sa) < 0)
                    hostname = ip;
                else
-                   hostname = ap->sprint(&sa, opt_n | 0x8000);
+                   hostname = ap->sprint(sa, opt_n | 0x8000);
                if (strcmp(hostname, ip) == 0)
                    hostname = "?";
            }
@@ -624,6 +630,7 @@ static void usage(void)
     fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -Ds <host> <if> [netmask <nm>] pub          <-''-\n\n"));
     
     fprintf(stderr, _("        -a                       display (all) hosts in alternative (BSD) style\n"));
+    fprintf(stderr, _("        -e                       display (all) hosts in default (Linux) style\n"));
     fprintf(stderr, _("        -s, --set                set a new ARP entry\n"));
     fprintf(stderr, _("        -d, --delete             delete a specified entry\n"));
     fprintf(stderr, _("        -v, --verbose            be verbose\n"));