home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / pcap-enet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-02  |  4.7 KB  |  235 lines

  1. /*
  2.  * Stanford Enetfilter subroutines for tcpdump
  3.  *
  4.  * Based on the MERIT NNstat etherifrt.c and the Ultrix pcap-pf.c
  5.  * subroutines.
  6.  *
  7.  * Rayan Zachariassen, CA*Net
  8.  */
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #include <sys/time.h>
  15. #include <sys/file.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/socket.h>
  18.  
  19. #include <net/if.h>
  20. #include <netinet/in.h>
  21. #include <netinet/if_ether.h>
  22. #include <net/bpf.h>
  23. #include <net/enet.h>
  24.  
  25. #include "interface.h"
  26.  
  27. struct packet_header {
  28. #ifdef    IBMRTPC
  29.     struct LengthWords    length;
  30.     struct tap_header    tap;
  31. #endif    /* IBMRTPC */
  32.     u_char            packet[8]
  33. };
  34.  
  35. extern int errno;
  36.  
  37. #define BUFSPACE (4*1024)
  38.  
  39. void
  40. readloop(cnt, if_fd, fp, printit)
  41.     int cnt;
  42.     int if_fd;
  43.     struct bpf_program *fp;
  44.     void (*printit)();
  45. {
  46. #ifdef    IBMRTPC
  47.     register struct packet_header *ph;
  48.     register u_char *bp;
  49.     register int inc;
  50. #else    /* !IBMRTPC */
  51.     static struct timeval tv = { 0 };
  52. #endif    /* IBMRTPC */
  53.     register int cc, caplen;
  54.     register struct bpf_insn *fcode = fp->bf_insns;
  55.     union {
  56.         struct packet_header hdr;
  57.         u_char    p[BUFSPACE];
  58.         u_short    s;
  59.     } buf;
  60.     extern void efReadError();
  61.  
  62.  
  63.     while (1) {
  64.         if ((cc = read(if_fd, (char *)buf.p, sizeof(buf))) < 0)
  65.             efReadError(if_fd, "reader");
  66.  
  67. #ifdef    IBMRTPC
  68.         /*
  69.          * Loop through each packet.
  70.          */
  71.         bp = buf.p;
  72.         while (cc > 0) {
  73.             ph = (struct packet_header *)bp;
  74.             caplen = ph->tap.th_wirelen > snaplen ? snaplen : ph->tap
  75. .th_wirelen ;
  76.             if (bpf_filter(fcode, (char *)ph->packet,
  77.                         ph->tap.th_wirelen, caplen)) {
  78.                 if (cnt >= 0 && --cnt < 0)
  79.                     goto out;
  80.                 (*printit)((char *)ph->packet,
  81.                     (struct timeval *)ph->tap.th_timestamp,
  82.                     ph->tap.th_wirelen, caplen);
  83.             }
  84.             inc = ph->length.PacketOffset;
  85.             cc -= inc;
  86.             bp += inc;
  87.         }
  88. #else    /* !IBMRTPC */
  89.         caplen = cc > snaplen ? snaplen : cc ;
  90.         if (bpf_filter(fcode, buf.hdr.packet, cc, caplen)) {
  91.             if (cnt >= 0 && --cnt < 0)
  92.                 goto out;
  93.             (*printit)(buf.hdr.packet, &tv, cc, caplen);
  94.         }
  95. #endif    /* IBMRTPC */
  96.     }
  97.  out:
  98.     wrapup(if_fd);
  99. }
  100.  
  101. /* Call ONLY if read() has returned an error on packet filter */
  102. void
  103. efReadError(fid, msg)
  104.     int fid;
  105.     char *msg;
  106. {
  107.     if (errno == EINVAL) {    /* read MAXINT bytes already! */
  108.         if (lseek(fid, 0, 0) < 0) {
  109.             perror("tcpdump: efReadError/lseek");
  110.             exit(-1);
  111.         }
  112.         else
  113.             return;
  114.     }
  115.     else {
  116.         (void) fprintf(stderr, "tcpdump: ");
  117.         perror(msg);
  118.         exit(-1);
  119.     }
  120. }
  121.  
  122. wrapup(fd)
  123.     int fd;
  124. {
  125. #ifdef    IBMRTPC
  126.     struct enstats es;
  127.  
  128.     if (ioctl(fd, EIOSTATS, &es) == -1) {
  129.         perror("tcpdump: enet ioctl EIOSTATS error");
  130.         exit(-1);
  131.     }
  132.     
  133.     fprintf(stderr, "%d packets queued", es.enStat_Rcnt);
  134.     if (es.enStat_Rdrops > 0)
  135.         fprintf(stderr, ", %d dropped", es.enStat_Rdrops);
  136.     if (es.enStat_Reads > 0)
  137.         fprintf(stderr, ", %d tcpdump %s", es.enStat_Reads,
  138.                 es.enStat_Reads > 1 ? "reads" : "read");
  139.     if (es.enStat_MaxRead > 1)
  140.         fprintf(stderr, ", %d packets in largest read", 
  141.             es.enStat_MaxRead);
  142.     putc('\n', stderr);
  143. #endif    /* IBMRTPC */
  144.     close(fd);
  145. }
  146.  
  147. int
  148. initdevice(device, pflag, linktype)
  149.     char *device;
  150.     int pflag;
  151.     int *linktype;
  152. {
  153.     struct eniocb ctl;
  154.     struct enfilter filter;
  155.     unsigned int maxwaiting;
  156.     int if_fd;
  157.  
  158. #ifdef    IBMRTPC
  159.     GETENETDEVICE(0, O_RDONLY, &if_fd);
  160. #else    /* !IBMRTPC */
  161.     if_fd = open("/dev/enet", O_RDONLY, 0);
  162. #endif    /* IBMRTPC */
  163.  
  164.     if (if_fd == -1) {
  165.         perror("tcpdump: enet open error");
  166.         error(
  167. "your system may not be properly configured; see \"man enet(4)\"");
  168.         exit(-1);
  169.     }
  170.  
  171.     /*  Get operating parameters. */
  172.  
  173.     if (ioctl(if_fd, EIOCGETP, (char *)&ctl) == -1) {
  174.         perror("tcpdump: enet ioctl EIOCGETP error");
  175.         exit(-1);
  176.     }
  177.  
  178.     /*  Set operating parameters. */
  179.  
  180. #ifdef    IBMRTPC
  181.     ctl.en_rtout = 1 * ctl.en_hz;
  182.     ctl.en_tr_etherhead = 1;
  183.     ctl.en_tap_network = 1;
  184.     ctl.en_multi_packet = 1;
  185.     ctl.en_maxlen = BUFSPACE;
  186. #else    /* !IBMRTPC */
  187.     ctl.en_rtout = 64;    /* randomly picked value for HZ */
  188. #endif    /* IBMRTPC */
  189.     if (ioctl(if_fd, EIOCSETP, &ctl) == -1) {
  190.         perror("tcpdump: enet ioctl EIOCSETP error");
  191.         exit(-1);
  192.     }
  193.  
  194.     /*  Flush the receive queue, since we've changed
  195.         the operating parameters and we otherwise might
  196.         receive data without headers. */
  197.  
  198.     if (ioctl(if_fd, EIOCFLUSH) == -1) {
  199.         perror("tcpdump: enet ioctl EIOCFLUSH error");
  200.         exit(-1);
  201.     }
  202.  
  203.     /*  Set the receive queue depth to its maximum. */
  204.  
  205.     maxwaiting = ctl.en_maxwaiting;
  206.     if (ioctl(if_fd, EIOCSETW, &maxwaiting) == -1) {
  207.         perror("tcpdump: enet ioctl EIOCSETW error");
  208.         exit(-1);
  209.     }
  210.  
  211. #ifdef    IBMRTPC
  212.     /*  Clear statistics. */
  213.  
  214.     if (ioctl(if_fd, EIOCLRSTAT, 0) == -1) {
  215.         perror("tcpdump: enet ioctl EIOCLRSTAT error");
  216.         exit(-1);
  217.     }
  218. #endif    /* IBMRTPC */
  219.  
  220.     /*  Set the filter (accept all packets). */
  221.  
  222.     filter.enf_Priority = 3;
  223.     filter.enf_FilterLen = 0;
  224.     if (ioctl(if_fd, EIOCSETF, &filter) == -1) {
  225.         perror("tcpdump: enet ioctl EIOCSETF error");
  226.         exit(-1);
  227.     }
  228.     /*
  229.      * "enetfilter" supports only ethernets.
  230.      */
  231.     *linktype = DLT_EN10MB;
  232.  
  233.     return(if_fd);
  234. }
  235.