home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1893 / etherinit.c next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  3.4 KB  |  148 lines

  1. /*
  2.  * etherinit.c
  3.  *
  4.  * Network-related subroutines for ether statistics programs
  5.  *
  6.  */
  7.  
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #include <sys/socket.h>
  11. #include <net/if.h>
  12. #include <net/pfilt.h>
  13.  
  14. struct endevp DevParams;
  15. int backlog = -1;
  16.  
  17. /* Returns fid for ethernet device */
  18. StartEther(ifname, MulticastOnly, BroadcastOnly)
  19. char *ifname;
  20. int MulticastOnly;
  21. int BroadcastOnly;
  22. {
  23.     struct eniocb IOCB;
  24.     short enmode;
  25.     int EtherFid;
  26.     
  27.     if ((EtherFid = pfopen(ifname, 0)) < 0) {
  28.         perror(ifname);
  29.         exit(1);
  30.     }
  31.     
  32.     if (ioctl(EtherFid, EIOCDEVP, &DevParams) < 0) {
  33.         perror(ifname);
  34.         exit(1);
  35.     }
  36.  
  37.     IOCB.en_rtout = 50;    /* in clock ticks */
  38.  
  39.     if (ioctl(EtherFid, EIOCSETP, &IOCB) < 0) {
  40.         perror(ifname);
  41.         exit(1);
  42.     }
  43.  
  44.     enmode = ENBATCH|ENTSTAMP|ENPROMISC|ENNONEXCL;
  45.     if (ioctl(EtherFid, EIOCMBIS, &enmode) < 0) {
  46.         perror(ifname);
  47.         exit(1);
  48.     }
  49.  
  50.     if (ioctl(EtherFid, EIOCSETW, &backlog) < 0) {
  51.         perror(ifname);
  52.         exit(1);
  53.     }
  54.  
  55.     BuildFilter(EtherFid, MulticastOnly, BroadcastOnly);
  56.     
  57.     return(EtherFid);
  58. }
  59.  
  60. /*
  61.  * filter accepts all packets (only multicasts if mcOnly is true)
  62.  */
  63.  
  64. BuildFilter(EtherFid, mcOnly, bcOnly)
  65. int EtherFid;
  66. int mcOnly;
  67. int bcOnly;
  68. {
  69.     struct enfilter Filter;
  70.     
  71.     Filter.enf_Priority = 37;    /* anything > 2 should work */
  72.     Filter.enf_FilterLen = 0;
  73.     
  74.     /* null filter should come out to be "always true" */
  75.  
  76.     if (mcOnly) {    /* check the destination address multicast bit */
  77.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  78.         ENF_PUSHWORD + 0;
  79. #ifndef    ENF_PUSHONE
  80.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  81.             ENF_PUSHLIT | ENF_AND;
  82.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  83.         htons(0x0100);
  84.             /* low bit of first byte is first bit on ether */
  85. #else
  86.         /* low bit of first byte is first bit on ether */
  87.         if (htons(0x0100) == 1) {
  88.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  89.             ENF_PUSHONE | ENF_AND;
  90.         }
  91.         else {
  92.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  93.             ENF_PUSHLIT | ENF_AND;
  94.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  95.             htons(0x0100);
  96.         }
  97. #endif    ENF_PUSHONE
  98.     } else if (bcOnly) {    /* check all destination words */
  99. #ifndef    ENF_PUSHONE
  100.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  101.         ENF_PUSHWORD + 0;
  102.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  103.             ENF_PUSHLIT | ENF_CAND;
  104.         Filter.enf_Filter[Filter.enf_FilterLen++] =    0xFFFF;
  105.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  106.         ENF_PUSHWORD + 1;
  107.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  108.             ENF_PUSHLIT | ENF_CAND;
  109.         Filter.enf_Filter[Filter.enf_FilterLen++] =    0xFFFF;
  110.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  111.         ENF_PUSHWORD + 2;
  112.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  113.             ENF_PUSHLIT | ENF_EQ;
  114.         Filter.enf_Filter[Filter.enf_FilterLen++] =    0xFFFF;
  115. #else
  116.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  117.         ENF_PUSHWORD + 0;
  118.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  119.             ENF_PUSHFFFF | ENF_CAND;
  120.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  121.         ENF_PUSHWORD + 1;
  122.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  123.             ENF_PUSHFFFF | ENF_CAND;
  124.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  125.         ENF_PUSHWORD + 2;
  126.         Filter.enf_Filter[Filter.enf_FilterLen++] =
  127.             ENF_PUSHFFFF | ENF_EQ;
  128. #endif    ENF_PUSHONE
  129.     }
  130.  
  131.     if (ioctl(EtherFid, EIOCSETF, &Filter) < 0) {
  132.         perror("setting filter");
  133.         exit(1);
  134.     }    
  135. }
  136.  
  137. char *EtherInterfaceName(EtherFid)
  138. int EtherFid;
  139. {
  140.     static struct ifreq ifr;
  141.     
  142.     if (ioctl(EtherFid, EIOCIFNAME, &ifr) < 0) {
  143.         perror("getting interface name");
  144.         return("unknown");
  145.     }
  146.     return(ifr.ifr_name);
  147. }
  148.