Source-Changes-HG archive

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

[src/netbsd-1-4]: src/usr.sbin/arp Pull up revision 1.29 (via patch, requeste...



details:   https://anonhg.NetBSD.org/src/rev/10712671b1f8
branches:  netbsd-1-4
changeset: 471263:10712671b1f8
user:      he <he%NetBSD.org@localhost>
date:      Sun Apr 01 15:01:19 2001 +0000

description:
Pull up revision 1.29 (via patch, requested by fair):
  Add interface names to ``arp -a'' output.

diffstat:

 usr.sbin/arp/arp.c |  84 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 76 insertions(+), 8 deletions(-)

diffs (153 lines):

diff -r 3d958191fe1a -r 10712671b1f8 usr.sbin/arp/arp.c
--- a/usr.sbin/arp/arp.c        Tue Mar 13 23:16:49 2001 +0000
+++ b/usr.sbin/arp/arp.c        Sun Apr 01 15:01:19 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arp.c,v 1.23.2.1 2000/10/19 17:04:49 he Exp $ */
+/*     $NetBSD: arp.c,v 1.23.2.2 2001/04/01 15:01:19 he Exp $ */
 
 /*
  * Copyright (c) 1984, 1993
@@ -46,7 +46,7 @@
 #if 0
 static char sccsid[] = "@(#)arp.c      8.3 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: arp.c,v 1.23.2.1 2000/10/19 17:04:49 he Exp $");
+__RCSID("$NetBSD: arp.c,v 1.23.2.2 2001/04/01 15:01:19 he Exp $");
 #endif
 #endif /* not lint */
 
@@ -58,6 +58,7 @@
 #include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/sysctl.h>
+#include <sys/ioctl.h>
 
 #include <net/if.h>
 #include <net/if_dl.h>
@@ -81,6 +82,7 @@
 int    delete __P((const char *, const char *));
 void   dump __P((u_long));
 void   sdl_print __P((const struct sockaddr_dl *));
+int    getifname __P((u_int16_t, char *));
 int    atosdl __P((const char *s, struct sockaddr_dl *sdl));
 int    file __P((char *));
 void   get __P((const char *));
@@ -94,6 +96,11 @@
 static int pid;
 static int nflag, vflag;
 static int s = -1;
+static int is = -1;
+
+static struct ifconf ifc;
+static char ifconfbuf[8192];
+static char *progname;
 
 int    delete __P((const char *, const char *));
 void   dump __P((u_long));
@@ -115,6 +122,7 @@
        int op = 0;
 
        pid = getpid();
+       progname = ((progname = strrchr(argv[0], '/')) ? progname + 1 : argv[0]);
 
        while ((ch = getopt(argc, argv, "andsfv")) != -1)
                switch((char)ch) {
@@ -390,6 +398,7 @@
 {
        int mib[6];
        size_t needed;
+       char ifname[IFNAMSIZ];
        char *host, *lim, *buf, *next;
        struct rt_msghdr *rtm;
        struct sockaddr_inarp *sin;
@@ -438,6 +447,12 @@
                        sdl_print(sdl);
                else
                        (void)printf("(incomplete)");
+
+               if (sdl->sdl_index) {
+                       if (getifname(sdl->sdl_index, ifname) == 0)
+                               printf(" on %s", ifname);
+               }
+
                if (rtm->rtm_rmx.rmx_expire == 0)
                        (void)printf(" permanent");
                if (sin->sin_other & SIN_PROXY)
@@ -508,14 +523,13 @@
 void
 usage()
 {
-       extern char *__progname;
 
-       (void)fprintf(stderr, "usage: %s [-n] hostname\n", __progname);
-       (void)fprintf(stderr, "usage: %s [-n] -a\n", __progname);
-       (void)fprintf(stderr, "usage: %s -d hostname\n", __progname);
+       (void)fprintf(stderr, "usage: %s [-n] hostname\n", progname);
+       (void)fprintf(stderr, "usage: %s [-n] -a\n", progname);
+       (void)fprintf(stderr, "usage: %s -d hostname\n", progname);
        (void)fprintf(stderr,
-           "usage: %s -s hostname ether_addr [temp] [pub]\n", __progname);
-       (void)fprintf(stderr, "usage: %s -f filename\n", __progname);
+           "usage: %s -s hostname ether_addr [temp] [pub]\n", progname);
+       (void)fprintf(stderr, "usage: %s -f filename\n", progname);
        exit(1);
 }
 
@@ -604,3 +618,57 @@
        (void)memcpy(inap, hp->h_addr, sizeof(*inap));
        return (0);
 }
+
+int
+getifname(ifindex, ifname)
+       u_int16_t ifindex;
+       char* ifname;
+{
+       int i, idx, siz;
+       char ifrbuf[8192];
+       struct ifreq ifreq, *ifr;
+       const struct sockaddr_dl *sdl = NULL;
+
+       if (is < 0) {
+               is = socket(PF_INET, SOCK_DGRAM, 0);
+               if (is < 0)
+                       err(1, "socket");
+
+               ifc.ifc_len = sizeof(ifconfbuf);
+               ifc.ifc_buf = ifconfbuf;
+
+               if (ioctl(is, SIOCGIFCONF, &ifc) < 0) {
+                       close(is);
+                       err(1, "SIOCGIFCONF");
+                       is = -1;
+               }
+       }
+
+       ifr = ifc.ifc_req;
+       ifreq.ifr_name[0] = '\0';
+       for (i = 0, idx = 0; i < ifc.ifc_len; ) {
+               ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
+               siz = sizeof(ifr->ifr_name) +
+                       (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
+                               ? ifr->ifr_addr.sa_len
+                               : sizeof(struct sockaddr));
+               i += siz;
+               /* avoid alignment issue */
+               if (sizeof(ifrbuf) < siz)
+                       errx(1, "ifr too big");
+
+               memcpy(ifrbuf, ifr, siz);
+               ifr = (struct ifreq *)ifrbuf;
+
+               if (ifr->ifr_addr.sa_family != AF_LINK)
+                       continue;
+
+               sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
+               if (sdl && sdl->sdl_index == ifindex) {
+                       (void) strncpy(ifname, ifr->ifr_name, IFNAMSIZ);
+                       return 0;
+               }
+       }
+
+       return -1;
+}



Home | Main Index | Thread Index | Old Index