home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / nfs / nfswatch4.0 / pfilt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-01  |  3.7 KB  |  193 lines

  1. #ifndef lint
  2. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/pfilt.c,v 4.0 1993/03/01 19:59:00 davy Exp $";
  3. #endif
  4.  
  5. #include "os.h"
  6.  
  7. #ifdef USE_PFILT
  8. /*
  9.  * pfilt.c - routines for messing with the packet filter
  10.  *
  11.  * Jeffrey Mogul
  12.  * DECWRL
  13.  *
  14.  * $Log: pfilt.c,v $
  15.  * Revision 4.0  1993/03/01  19:59:00  davy
  16.  * NFSWATCH Version 4.0.
  17.  *
  18.  * Revision 3.5  1993/01/15  19:33:39  davy
  19.  * Miscellaneous cleanups.
  20.  *
  21.  * Revision 3.4  1993/01/13  21:25:05  davy
  22.  * #endif cleanup.
  23.  *
  24.  * Revision 3.3  1993/01/13  20:18:17  davy
  25.  * Put in OS-specific define scheme, and merged in Tim Hudson's code for
  26.  * SGI systems (as yet untested).
  27.  *
  28.  * Revision 3.2  1992/07/24  18:47:57  mogul
  29.  * Added FDDI support
  30.  *
  31.  * Revision 3.1  1992/07/23  00:02:18  mogul
  32.  * Fixed improper use of if_fd[] variable.
  33.  *
  34.  * Revision 3.0  1991/01/23  08:23:15  davy
  35.  * NFSWATCH Version 3.0.
  36.  *
  37.  * Revision 1.2  90/12/04  08:02:43  davy
  38.  * Changes from Jeff Mogul for Ultrix 4.1 and higher.
  39.  * 
  40.  * Revision 1.1  90/08/17  15:47:34  davy
  41.  * Initial revision
  42.  * 
  43.  * Revision 1.1  90/04/20  13:59:36  mogul
  44.  * Initial revision
  45.  * 
  46.  */
  47. #include <sys/param.h>
  48. #include <sys/socket.h>
  49. #include <sys/ioctl.h>
  50. #include <sys/time.h>
  51. #include <sys/file.h>
  52. #include <net/if.h>
  53. #include <signal.h>
  54. #include <stdio.h>
  55.  
  56. #include <net/pfilt.h>
  57.  
  58. #include "nfswatch.h"
  59. #include "externs.h"
  60.  
  61. static struct ifreq ifr;            /* holds interface name    */
  62.  
  63. /*
  64.  * setup_pfilt_dev - set up the packet filter
  65.  */
  66. int
  67. setup_pfilt_dev(device)
  68. char **device;
  69. {
  70.     int fd;
  71.     struct timeval timeout;
  72.     short enmode;
  73.     short backlog = -1;    /* request the most */
  74.     struct enfilter Filter;
  75.  
  76.     /*
  77.      * Open the packetfilter.  If it fails, we're out of
  78.      * devices.
  79.      */
  80.     if ((fd = pfopen(*device, 0)) < 0) {
  81.         return(-1);
  82.     }
  83.  
  84.     /*
  85.      * We want the ethernet in promiscuous mode
  86.      */
  87.     enmode = ENBATCH|ENTSTAMP|ENNONEXCL|ENPROMISC;
  88.     if (ioctl(fd, EIOCMBIS, &enmode) < 0) {
  89.         error("ioctl: EIOCMBIS");
  90.         finish(-1);
  91.     }
  92.  
  93. #ifdef ENCOPYALL
  94.     /*
  95.      * Attempt to set "copyall" mode (see our own packets).
  96.      * Okay if this fails.
  97.      */
  98.     enmode = ENCOPYALL;
  99.     (void) ioctl(fd, EIOCMBIS, &enmode);
  100. #endif
  101.  
  102.     /*
  103.      * Set the read timeout.
  104.      */
  105.     timeout.tv_sec = 1;
  106.     timeout.tv_usec = 0;
  107.     if (ioctl(fd, EIOCSRTIMEOUT, &timeout) < 0) {
  108.         error("ioctl: EIOCSRTIMEOUT");
  109.         finish(-1);
  110.     }
  111.  
  112.     /* set the backlog */
  113.     if (ioctl(fd, EIOCSETW, &backlog) < 0) {
  114.         error("ioctl: EIOCSETW");
  115.         finish(-1);
  116.     }
  117.  
  118.     /* set the truncation */
  119.     if (ioctl(fd, EIOCTRUNCATE, &truncation) < 0) {
  120.         error("ioctl: EIOCTRUNCATE");
  121.         finish(-1);
  122.     }
  123.  
  124.     /* find out the actual device name */
  125.     if (*device == NULL) {
  126.         if (ioctl(fd, EIOCIFNAME, &ifr) >= 0) {
  127.             *device = ifr.ifr_name;
  128.         }
  129.         else {
  130.             *device = "pf0";
  131.         }
  132.     }
  133.  
  134.     /* accept all packets */
  135.     Filter.enf_Priority = 37;    /* anything > 2 */
  136.     Filter.enf_FilterLen = 0;    /* means "always true" */
  137.     if (ioctl(fd, EIOCSETF, &Filter) < 0) {
  138.         error("ioctl: EIOCSETF");
  139.         finish(-1);
  140.     }
  141.  
  142.     return(fd);
  143. }
  144.  
  145. /*
  146.  * pfilt_devtype - return device type code for packet filter device
  147.  */
  148. int
  149. pfilt_devtype(fd)
  150. int fd;
  151. {
  152.     struct endevp devparams;
  153.  
  154.     if (ioctl(fd, EIOCDEVP, &devparams) < 0) {
  155.         error("ioctl: EIOCDEVP");
  156.         finish(-1);
  157.     }
  158.  
  159.     switch (devparams.end_dev_type) {
  160.     case ENDT_10MB:
  161.         return(DLT_EN10MB);
  162.         
  163. #ifdef    ENDT_FDDI        /* HACK: to compile prior to Ultrix 4.2 */
  164.     case ENDT_FDDI:
  165.         return(DLT_FDDI);
  166. #endif
  167.         
  168.     default:
  169.         /*
  170.          * Currently, the Ultrix packet filter supports only
  171.          * Ethernet and FDDI.
  172.          */
  173.         fprintf(stderr, "Packet filter data-link type %d unknown\n",
  174.                 devparams.end_dev_type);
  175.         fprintf(stderr, "Assuming Ethernet\n");
  176.         return(DLT_EN10MB);
  177.     }
  178. }
  179.  
  180. /*
  181.  * flush_pfilt - flush data from the packet filter
  182.  */
  183. void
  184. flush_pfilt(fd)
  185. int fd;
  186. {
  187.     if (ioctl(fd, EIOCFLUSH) < 0) {
  188.         error("ioctl: EIOCFLUSH");
  189.         finish(-1);
  190.     }
  191. }
  192. #endif /* USE_PFILT */
  193.