home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / networks / arpdump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  6.0 KB  |  247 lines

  1. 25-Oct-85 16:15:29-MDT,6354;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Fri 25 Oct 85 16:15:05-MDT
  4. Received: from usenet by TGR.BRL.ARPA id a009691; 25 Oct 85 17:40 EDT
  5. From: Dennis Bednar <dennis@rlgvax.uucp>
  6. Newsgroups: net.unix-wizards,net.sources
  7. Subject: RE: Ethernet Problems
  8. Message-ID: <809@rlgvax.UUCP>
  9. Date: 25 Oct 85 05:51:26 GMT
  10. Xref: seismo net.unix-wizards:15493 net.sources:3725
  11. To:       unix-sources@BRL-TGR.ARPA
  12.  
  13.  
  14. The arpdump.c program posted by Barry Shien, has been enhanced
  15. to also print Timer and Flags for the ARP entries.
  16. All of the mods I made are ifdefed by CCI and DPB.
  17.  
  18. Here is the new version:
  19.  
  20. -------------- arpdump.c ---------------------
  21. #define CCI    /* CCI needs this because of nlist() problems */
  22. #define DPB    /* new personal mods added to the original */
  23.  
  24. #ifdef DPB
  25. /*
  26.  *    arpdump.c
  27.  *    Barry Shein, Boston University original 10 21 85
  28.  *    Dennis Bednar, CCI Reston VA, added options, Timer, Flags, 10 25 85
  29.  *
  30.  *    usage: arpdump [-a] [-v] [seconds]
  31.  *        -a = print old timed-out slots too
  32.  *            ie slots containing non-zero etheraddr && IP addr == 0
  33.  *        -v = verbose, prints every entry in the table
  34.  *        seconds = decimal seconds to print again
  35.  *
  36.  */
  37. #else
  38. /*
  39.     usage: arpdump [seconds]
  40. Newsgroups: net.unix-wizards
  41. Subject: Re: Re: Ethernet problems
  42.  
  43.  
  44. Re: Hedrick's suggestion that the problem may be with your ARP tables:
  45.  
  46. Here's a hack I use to examine the arp tables on my systems, I think
  47. half of it or more is from something else, but it seems to do the job
  48. (4.2bsd):
  49.  
  50. */
  51. #endif
  52.  
  53. #include <stdio.h>
  54. #include <nlist.h>
  55. #include <sys/types.h>
  56. #include <sys/socket.h>
  57. #include <netinet/in.h>
  58. #include <netdb.h>
  59.  
  60. char *fcore = "/dev/kmem", *fnlist = "/vmunix" ;
  61. struct nlist nl[] = {
  62. #define ARPTAB    0
  63.     { "_arptab" },
  64.     { "" },
  65. } ;
  66. #ifdef DPB
  67. /*    The arptab structure is defined in netinet/if_ether.h        */
  68. #endif
  69. struct    arptab {
  70.     struct    in_addr at_iaddr;    /* internet address */
  71.     u_char    at_enaddr[6];        /* ethernet address */
  72.     struct    mbuf *at_hold;        /* last packet until resolved/timeout */
  73.     u_char    at_timer;        /* minutes since last reference */
  74.     u_char    at_flags;        /* flags */
  75. };
  76. /* at_flags field values */
  77. #ifdef DPB
  78. /*    The at_flags are defined in net/if.h        */
  79. #endif
  80. #define    ATF_INUSE    1        /* entry in use */
  81. #define ATF_COM        2        /* completed entry (enaddr valid) */
  82. #ifdef DPB
  83. #define    ATF_PERM    4    /* permanent entry */
  84. #define    ATF_PUBL    8    /* publish entry (respond for other host) */
  85. #endif
  86.  
  87. #ifdef DPB
  88. /*    bucket stuff defined in netinet/if_ether.c    */
  89. #endif
  90. #define    ARPTAB_BSIZ    5        /* bucket size */
  91. #define    ARPTAB_NB    19        /* number of buckets */
  92. #define    ARPTAB_SIZE    (ARPTAB_BSIZ * ARPTAB_NB)
  93.  
  94. struct    arptab *arptab, atab ;
  95.  
  96. #ifdef DPB
  97. int    verbose;    /* -v verbose flag */
  98. int    allflag;    /* -a to print all timed out ones */
  99. #endif
  100.  
  101. main(argc,argv) int argc ; char **argv ;
  102. {
  103.     int fc ;
  104.     int i,j ;
  105.     int secs ;
  106.     char *ap ;
  107.     struct hostent *gethostbyaddr(), *hp ;
  108.  
  109.     if((fc = open(fcore,0)) < 0)
  110.     {
  111.         perror(fcore) ;
  112.         exit(1) ;
  113.     }
  114. #ifdef CCI
  115.     if (nlist(fnlist, nl) == -1)
  116.         {
  117.         fprintf(stderr, "arptab: nlist on %s returned error\n", fnlist);
  118.         exit(1);
  119.         }
  120. #else
  121.     nlist(fnlist,nl) ;
  122. #endif
  123.     arptab = (struct arptab *) nl[ARPTAB].n_value ;
  124. #ifdef CCI
  125.     if (nl[0].n_value == 0)
  126. #else
  127.     if(nl[0].n_type == 0)
  128. #endif
  129.     {
  130.         fprintf(stderr,"No name list %s\n",fnlist) ;
  131.         exit(1) ;
  132.     }
  133. #ifdef DPB
  134.     secs = 0;
  135.     /* primitive error checking on arguments */
  136.     for (i = 1; i < argc; ++i)
  137.         if (strcmp(argv[i], "-a") == 0)
  138.             ++allflag;
  139.         else if (strcmp(argv[i], "-v") == 0)
  140.             ++verbose;
  141.         else
  142.             secs = atoi(argv[i]);
  143. #else
  144.     if(argc > 1) secs = atoi(argv[1]) ;
  145.     else secs = 0 ;
  146. #endif
  147.     printf("\t\tACTIVE ARP TABLE DUMP\n") ;
  148. loop:
  149. #ifdef DPB
  150.     printf("Index\tIP Addr\t\tEthernet Interface\tHostName\tTimer\tFlags\n") ;
  151. #else
  152.     printf("Index\tAddr\t\tEthernet Interface\tHostName\n") ;
  153. #endif
  154.     lseek(fc,(int) arptab,0) ;
  155. #ifdef DPB
  156.     ap = (char *) &atab;
  157. #else
  158.     /* old way requires that at_iaddr be first */
  159.     ap = (char *) &atab.at_iaddr ;
  160. #endif
  161.     for(i=0 ; i < ARPTAB_SIZE ; i++)
  162.     {
  163.         if(read(fc,&atab,sizeof atab) != (sizeof atab))
  164.         {
  165.             fprintf(stderr,"Error reading arp table\n") ;
  166.             exit(1) ;
  167.         }
  168. #ifdef DPB
  169.         if (verbose)
  170.             goto printslot;    /* print every slot it */
  171.  
  172.         if (allflag)    /* print old timed out "half-filled" slots */
  173.             if ( inet_netof(atab.at_iaddr) != 0 ||
  174.                 atab.at_flags != 0 ||
  175.                 ge_addr(&atab.at_enaddr[0]) != 0)
  176.                 ;    /* print slot below */
  177.             else
  178.                 continue;
  179.         else        /* print only entries with IP addresses */
  180.             if (inet_netof(atab.at_iaddr) != 0)
  181.                 ;    /* print slot below */
  182.             else
  183.                 continue;
  184. printslot:
  185. #else
  186.         if(inet_netof(atab.at_iaddr) == 0) continue ;
  187. #endif
  188.         printf("%2d:\t",i) ;
  189.         printf("%s\t",ap = (char *) inet_ntoa(atab.at_iaddr)) ;
  190. #ifdef DPB
  191.         /* kludgy way to align columns because IP addr is var len */
  192.         if (strlen(ap)<8) printf("\t");
  193. #endif
  194.         for(j=0 ; j < 6 ; j++)
  195.             printf("%02x",atab.at_enaddr[j]) ;
  196.         if((hp = gethostbyaddr(&atab.at_iaddr.s_addr,
  197.                     sizeof atab.at_iaddr.s_addr,
  198.                     AF_INET)) == NULL)
  199. #ifdef DPB
  200.         /* save ptr to host name printed for strlen later */
  201.             printf(ap = "\t\t(unknown)") ;
  202.         else printf("\t\t%s",ap = hp->h_name) ;
  203. #else
  204.             printf("\t\t(unknown)") ;
  205.         else printf("\t\t%s",hp->h_name) ;
  206. #endif
  207. #ifdef DPB
  208.         /* again kludgy way to align columns because hostname var len */
  209.         if (strlen(ap)<8) printf("\t");
  210.         printf("\t%3d", atab.at_timer);
  211.         printf("\t");
  212.         if (atab.at_flags & ATF_INUSE) printf("U");
  213.         if (atab.at_flags & ATF_COM)   printf("C");
  214.         if (atab.at_flags & ATF_PERM)  printf("P");
  215.         if (atab.at_flags & ATF_PUBL)  printf("R"); /* responding */
  216. #endif
  217.         printf("\n") ;
  218.     }
  219.     if(secs <= 0) exit(0) ;
  220.     sleep(secs) ;
  221.     goto loop ;
  222. }
  223. /*
  224.     -Barry Shein, Boston University
  225. */
  226.  
  227. #ifdef DPB
  228. /*
  229.  * get ethernet address
  230.  * returns 0 iff 6 byte ethernet address is all zeros
  231.  */
  232. ge_addr(addr)
  233.     register    char    *addr;
  234. {
  235.     register    int    i;
  236.  
  237.     for (i = 0; i < 6; ++i, ++addr)
  238.         if (*addr)
  239.             return    1;    /* found a non-zero byte */
  240.  
  241.     return 0;            /* all bytes were zero */
  242. }
  243. #endif
  244. -- 
  245. Dennis Bednar    Computer Consoles Inc.    Reston VA    703-648-3300
  246. {decvax,ihnp4,harpo,allegra}!seismo!rlgvax!dennis
  247.