install correct aliases file
[unix-history] / usr / src / usr.sbin / arp / arp.c
index 093eeb4..b90168c 100644 (file)
+/*
+ * Copyright (c) 1984 Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Sun Microsystems, Inc.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1984 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)arp.c       1.3 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)arp.c      5.8 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * arp - display, set, and delete arp table entries
  */
 
 
 /*
  * arp - display, set, and delete arp table entries
  */
 
-#include <stdio.h>
-#include <sys/types.h>
+#include <machine/pte.h>
+
+#include <sys/param.h>
+#include <sys/vmmac.h>
+#include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
 #include <sys/ioctl.h>
 #include <sys/ioctl.h>
-#include <errno.h>
+
 #include <netdb.h>
 #include <netdb.h>
-#include <nlist.h>
+#include <netinet/in.h>
 #include <net/if.h>
 #include <netinet/if_ether.h>
 
 #include <net/if.h>
 #include <netinet/if_ether.h>
 
+#include <errno.h>
+#include <nlist.h>
+#include <stdio.h>
+
 extern int errno;
 extern int errno;
+static int kflag;
 
 main(argc, argv)
 
 main(argc, argv)
+       int argc;
        char **argv;
 {
        char **argv;
 {
-       if (argc >= 2 && strcmp(argv[1], "-a") == 0) {
-               char *kernel = "/vmunix", *mem = "/dev/kmem";
+       int ch;
 
 
-               if (argc >= 3)
-                       kernel = argv[2];
-               if (argc >= 4)
-                       mem = argv[3];
-               dump(kernel, mem);
-               exit(0);
-       }
-       if (argc == 2) {
-               get(argv[1]);
-               exit(0);
-       }
-       if (argc >= 4 && strcmp(argv[1], "-s") == 0) {
-               set(argc-2, &argv[2]);
-               exit(0);
-       }
-       if (argc == 3 && strcmp(argv[1], "-d") == 0) {
-               delete(argv[2]);
-               exit(0);
-       }
-       if (argc == 3 && strcmp(argv[1], "-f") == 0) {
-               file(argv[2]);
-               exit(0);
-       }
-       usage();
-       exit(1);
+       while ((ch = getopt(argc, argv, "adsf")) != EOF)
+               switch((char)ch) {
+               case 'a': {
+                       char *mem;
+
+                       if (argc > 4)
+                               usage();
+                       if (argc == 4) {
+                               kflag = 1;
+                               mem = argv[3];
+                       }
+                       else
+                               mem = "/dev/kmem";
+                       dump((argc >= 3) ? argv[2] : "/vmunix", mem);
+                       exit(0);
+               }
+               case 'd':
+                       if (argc != 3)
+                               usage();
+                       delete(argv[2]);
+                       exit(0);
+               case 's':
+                       if (argc < 4 || argc > 7)
+                               usage();
+                       exit(set(argc-2, &argv[2]) ? 1 : 0);
+               case 'f':
+                       if (argc != 3)
+                               usage();
+                       exit (file(argv[2]) ? 1 : 0);
+               case '?':
+               default:
+                       usage();
+               }
+       if (argc != 2)
+               usage();
+       get(argv[1]);
+       exit(0);
 }
 
 /*
 }
 
 /*
@@ -59,8 +104,8 @@ file(name)
        char *name;
 {
        FILE *fp;
        char *name;
 {
        FILE *fp;
-       int i;
-       char line[100], arg[4][50], *args[4];
+       int i, retval;
+       char line[100], arg[5][50], *args[5];
 
        if ((fp = fopen(name, "r")) == NULL) {
                fprintf(stderr, "arp: cannot open %s\n", name);
 
        if ((fp = fopen(name, "r")) == NULL) {
                fprintf(stderr, "arp: cannot open %s\n", name);
@@ -70,21 +115,28 @@ file(name)
        args[1] = &arg[1][0];
        args[2] = &arg[2][0];
        args[3] = &arg[3][0];
        args[1] = &arg[1][0];
        args[2] = &arg[2][0];
        args[3] = &arg[3][0];
+       args[4] = &arg[4][0];
+       retval = 0;
        while(fgets(line, 100, fp) != NULL) {
        while(fgets(line, 100, fp) != NULL) {
-               i = sscanf(line, "%s %s %s %s", arg[0], arg[1], arg[2], arg[3]);
+               i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
+                   arg[3], arg[4]);
                if (i < 2) {
                        fprintf(stderr, "arp: bad line: %s\n", line);
                if (i < 2) {
                        fprintf(stderr, "arp: bad line: %s\n", line);
+                       retval = 1;
                        continue;
                }
                        continue;
                }
-               set(i, args);
+               if (set(i, args))
+                       retval = 1;
        }
        fclose(fp);
        }
        fclose(fp);
+       return (retval);
 }
 
 /*
  * Set an individual arp entry 
  */
 set(argc, argv)
 }
 
 /*
  * Set an individual arp entry 
  */
 set(argc, argv)
+       int argc;
        char **argv;
 {
        struct arpreq ar;
        char **argv;
 {
        struct arpreq ar;
@@ -96,40 +148,46 @@ set(argc, argv)
 
        argc -= 2;
        argv += 2;
 
        argc -= 2;
        argv += 2;
-       hp = gethostbyname(host);
-       if (hp == NULL) {
-               fprintf(stderr, "arp: %s: unknown host\n", host);
-               return;
-       }
        bzero((caddr_t)&ar, sizeof ar);
        bzero((caddr_t)&ar, sizeof ar);
-       ar.arp_pa.sa_family = AF_INET;
        sin = (struct sockaddr_in *)&ar.arp_pa;
        sin = (struct sockaddr_in *)&ar.arp_pa;
-       bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, sizeof sin->sin_addr);
+       sin->sin_family = AF_INET;
+       sin->sin_addr.s_addr = inet_addr(host);
+       if (sin->sin_addr.s_addr == -1) {
+               if (!(hp = gethostbyname(host))) {
+                       fprintf(stderr, "arp: %s: ", host);
+                       herror((char *)NULL);
+                       return (1);
+               }
+               bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
+                   sizeof sin->sin_addr);
+       }
        ea = (u_char *)ar.arp_ha.sa_data;
        if (ether_aton(eaddr, ea))
        ea = (u_char *)ar.arp_ha.sa_data;
        if (ether_aton(eaddr, ea))
-               return;
+               return (1);
        ar.arp_flags = ATF_PERM;
        ar.arp_flags = ATF_PERM;
-       while(argc-- > 0) {
+       while (argc-- > 0) {
                if (strncmp(argv[0], "temp", 4) == 0)
                        ar.arp_flags &= ~ATF_PERM;
                if (strncmp(argv[0], "temp", 4) == 0)
                        ar.arp_flags &= ~ATF_PERM;
-               if (strncmp(argv[0], "pub", 3) == 0)
+               else if (strncmp(argv[0], "pub", 3) == 0)
                        ar.arp_flags |= ATF_PUBL;
                        ar.arp_flags |= ATF_PUBL;
+               else if (strncmp(argv[0], "trail", 5) == 0)
+                       ar.arp_flags |= ATF_USETRAILERS;
                argv++;
        }
        
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s < 0) {
                argv++;
        }
        
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s < 0) {
-                perror("arp: socket");
-                exit(1);
-        }
+               perror("arp: socket");
+               exit(1);
+       }
        if (ioctl(s, SIOCSARP, (caddr_t)&ar) < 0) {
                perror(host);
                exit(1);
        }
        close(s);
        if (ioctl(s, SIOCSARP, (caddr_t)&ar) < 0) {
                perror(host);
                exit(1);
        }
        close(s);
+       return (0);
 }
 
 }
 
-
 /*
  * Display an individual arp entry
  */
 /*
  * Display an individual arp entry
  */
@@ -141,21 +199,27 @@ get(host)
        struct sockaddr_in *sin;
        u_char *ea;
        int s;
        struct sockaddr_in *sin;
        u_char *ea;
        int s;
+       char *inet_ntoa();
 
 
-       hp = gethostbyname(host);
-       if (hp == NULL) {
-               fprintf(stderr, "arp: %s: unknown host\n", host);
-               exit(1);
-       }
        bzero((caddr_t)&ar, sizeof ar);
        ar.arp_pa.sa_family = AF_INET;
        sin = (struct sockaddr_in *)&ar.arp_pa;
        bzero((caddr_t)&ar, sizeof ar);
        ar.arp_pa.sa_family = AF_INET;
        sin = (struct sockaddr_in *)&ar.arp_pa;
-       sin->sin_addr = *(struct in_addr *)hp->h_addr;
+       sin->sin_family = AF_INET;
+       sin->sin_addr.s_addr = inet_addr(host);
+       if (sin->sin_addr.s_addr == -1) {
+               if (!(hp = gethostbyname(host))) {
+                       fprintf(stderr, "arp: %s: ", host);
+                       herror((char *)NULL);
+                       exit(1);
+               }
+               bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
+                   sizeof sin->sin_addr);
+       }
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s < 0) {
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s < 0) {
-                perror("arp: socket");
-                exit(1);
-        }
+               perror("arp: socket");
+               exit(1);
+       }
        if (ioctl(s, SIOCGARP, (caddr_t)&ar) < 0) {
                if (errno == ENXIO)
                        printf("%s (%s) -- no entry\n",
        if (ioctl(s, SIOCGARP, (caddr_t)&ar) < 0) {
                if (errno == ENXIO)
                        printf("%s (%s) -- no entry\n",
@@ -171,8 +235,12 @@ get(host)
                ether_print(ea);
        else
                printf("(incomplete)");
                ether_print(ea);
        else
                printf("(incomplete)");
-       if (!(ar.arp_flags & ATF_PERM)) printf(" temporary");
-       if (ar.arp_flags & ATF_PUBL) printf(" published");
+       if (ar.arp_flags & ATF_PERM)
+               printf(" permanent");
+       if (ar.arp_flags & ATF_PUBL)
+               printf(" published");
+       if (ar.arp_flags & ATF_USETRAILERS)
+               printf(" trailers");
        printf("\n");
 }
 
        printf("\n");
 }
 
@@ -187,20 +255,25 @@ delete(host)
        struct sockaddr_in *sin;
        int s;
 
        struct sockaddr_in *sin;
        int s;
 
-       hp = gethostbyname(host);
-       if (hp == NULL) {
-               fprintf(stderr, "arp: %s: unknown host\n", host);
-               exit(1);
-       }
        bzero((caddr_t)&ar, sizeof ar);
        ar.arp_pa.sa_family = AF_INET;
        sin = (struct sockaddr_in *)&ar.arp_pa;
        bzero((caddr_t)&ar, sizeof ar);
        ar.arp_pa.sa_family = AF_INET;
        sin = (struct sockaddr_in *)&ar.arp_pa;
-       bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, sizeof sin->sin_addr);
+       sin->sin_family = AF_INET;
+       sin->sin_addr.s_addr = inet_addr(host);
+       if (sin->sin_addr.s_addr == -1) {
+               if (!(hp = gethostbyname(host))) {
+                       fprintf(stderr, "arp: %s: ", host);
+                       herror((char *)NULL);
+                       exit(1);
+               }
+               bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
+                   sizeof sin->sin_addr);
+       }
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s < 0) {
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s < 0) {
-                perror("arp: socket");
-                exit(1);
-        }
+               perror("arp: socket");
+               exit(1);
+       }
        if (ioctl(s, SIOCDARP, (caddr_t)&ar) < 0) {
                if (errno == ENXIO)
                        printf("%s (%s) -- no entry\n",
        if (ioctl(s, SIOCDARP, (caddr_t)&ar) < 0) {
                if (errno == ENXIO)
                        printf("%s (%s) -- no entry\n",
@@ -218,68 +291,115 @@ struct nlist nl[] = {
        { "_arptab" },
 #define        X_ARPTAB_SIZE   1
        { "_arptab_size" },
        { "_arptab" },
 #define        X_ARPTAB_SIZE   1
        { "_arptab_size" },
+#define        N_SYSMAP        2
+       { "_Sysmap" },
+#define        N_SYSSIZE       3
+       { "_Syssize" },
        { "" },
 };
 
        { "" },
 };
 
+static struct pte *Sysmap;
+
 /*
  * Dump the entire arp table
  */
 dump(kernel, mem)
        char *kernel, *mem;
 {
 /*
  * Dump the entire arp table
  */
 dump(kernel, mem)
        char *kernel, *mem;
 {
-       int mf, arptab_size, sz;
+       extern int h_errno;
        struct arptab *at;
        struct hostent *hp;
        struct arptab *at;
        struct hostent *hp;
-       char *host;
+       int bynumber, mf, arptab_size, sz;
+       char *host, *malloc();
+       off_t lseek();
 
 
-       nlist(kernel, nl);
-       if(nl[X_ARPTAB_SIZE].n_type == 0) {
+       if (nlist(kernel, nl) < 0 || nl[X_ARPTAB_SIZE].n_type == 0) {
                fprintf(stderr, "arp: %s: bad namelist\n", kernel);
                exit(1);
        }
                fprintf(stderr, "arp: %s: bad namelist\n", kernel);
                exit(1);
        }
-       mf = open(mem, 0);
-       if(mf < 0) {
-               fprintf(fprintf, "arp: cannot open %s\n", mem);
+       mf = open(mem, O_RDONLY);
+       if (mf < 0) {
+               fprintf(stderr, "arp: cannot open %s\n", mem);
                exit(1);
        }
                exit(1);
        }
-       lseek(mf, (long)nl[X_ARPTAB_SIZE].n_value, 0);
+       if (kflag) {
+               off_t off;
+
+               Sysmap = (struct pte *)
+                  malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
+               if (!Sysmap) {
+                       fputs("arp: can't get memory for Sysmap.\n", stderr);
+                       exit(1);
+               }
+               off = nl[N_SYSMAP].n_value & ~KERNBASE;
+               (void)lseek(mf, off, L_SET);
+               (void)read(mf, (char *)Sysmap,
+                   (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
+       }
+       klseek(mf, (long)nl[X_ARPTAB_SIZE].n_value, L_SET);
        read(mf, &arptab_size, sizeof arptab_size);
        read(mf, &arptab_size, sizeof arptab_size);
-       if (arptab_size <=0 || arptab_size > 1000) {
+       if (arptab_size <= 0 || arptab_size > 1000) {
                fprintf(stderr, "arp: %s: namelist wrong\n", kernel);
                exit(1);
        }
        sz = arptab_size * sizeof (struct arptab);
                fprintf(stderr, "arp: %s: namelist wrong\n", kernel);
                exit(1);
        }
        sz = arptab_size * sizeof (struct arptab);
-       at = (struct arptab *)malloc(sz);
+       at = (struct arptab *)malloc((u_int)sz);
        if (at == NULL) {
        if (at == NULL) {
-               fprintf(stderr, "arp: can't get memory for arptab\n");
+               fputs("arp: can't get memory for arptab.\n", stderr);
                exit(1);
        }
                exit(1);
        }
-       lseek(mf, (long)nl[X_ARPTAB].n_value, 0);
+       klseek(mf, (long)nl[X_ARPTAB].n_value, L_SET);
        if (read(mf, (char *)at, sz) != sz) {
                perror("arp: error reading arptab");
                exit(1);
        }
        close(mf);
        if (read(mf, (char *)at, sz) != sz) {
                perror("arp: error reading arptab");
                exit(1);
        }
        close(mf);
-       for (; arptab_size-- > 0; at++) {
+       for (bynumber = 0; arptab_size-- > 0; at++) {
                if (at->at_iaddr.s_addr == 0 || at->at_flags == 0)
                        continue;
                if (at->at_iaddr.s_addr == 0 || at->at_flags == 0)
                        continue;
-               hp = gethostbyaddr((caddr_t)&at->at_iaddr, sizeof at->at_iaddr,
-                       AF_INET);
+               if (bynumber == 0)
+                       hp = gethostbyaddr((caddr_t)&at->at_iaddr,
+                           sizeof at->at_iaddr, AF_INET);
+               else
+                       hp = 0;
                if (hp)
                        host = hp->h_name;
                if (hp)
                        host = hp->h_name;
-               else
+               else {
                        host = "?";
                        host = "?";
+                       if (h_errno == TRY_AGAIN)
+                               bynumber = 1;
+               }
                printf("%s (%s) at ", host, inet_ntoa(at->at_iaddr));
                if (at->at_flags & ATF_COM)
                        ether_print(at->at_enaddr);
                else
                        printf("(incomplete)");
                printf("%s (%s) at ", host, inet_ntoa(at->at_iaddr));
                if (at->at_flags & ATF_COM)
                        ether_print(at->at_enaddr);
                else
                        printf("(incomplete)");
-               if (!(at->at_flags & ATF_PERM)) printf(" temporary");
-               if (at->at_flags & ATF_PUBL) printf(" published");
+               if (at->at_flags & ATF_PERM)
+                       printf(" permanent");
+               if (at->at_flags & ATF_PUBL)
+                       printf(" published");
+               if (at->at_flags & ATF_USETRAILERS)
+                       printf(" trailers");
                printf("\n");
        }
 }
 
                printf("\n");
        }
 }
 
+/*
+ * Seek into the kernel for a value.
+ */
+klseek(fd, base, off)
+       int fd, off;
+       off_t base;
+{
+       off_t lseek();
+
+       if (kflag) {    /* get kernel pte */
+               base &= ~KERNBASE;
+               base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
+       }
+       (void)lseek(fd, base, off);
+}
+
 ether_print(cp)
        u_char *cp;
 {
 ether_print(cp)
        u_char *cp;
 {
@@ -305,9 +425,10 @@ ether_aton(a, n)
 
 usage()
 {
 
 usage()
 {
-       printf("Usage: arp hostname\n");
+       printf("usage: arp hostname\n");
        printf("       arp -a [/vmunix] [/dev/kmem]\n");
        printf("       arp -d hostname\n");
        printf("       arp -a [/vmunix] [/dev/kmem]\n");
        printf("       arp -d hostname\n");
-       printf("       arp -s hostname ether_addr [temp] [pub]\n");
+       printf("       arp -s hostname ether_addr [temp] [pub] [trail]\n");
        printf("       arp -f filename\n");
        printf("       arp -f filename\n");
+       exit(1);
 }
 }