home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / lib / nit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-20  |  2.6 KB  |  118 lines

  1. /*
  2.      net logger - network traffic logging library
  3.      Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
  4.  
  5.      Please see the file `COPYING' for the complete copyright notice.
  6.  
  7. nit.c - 03/20/93
  8.  
  9. */
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <sys/time.h>
  13. #include <sys/stream.h>
  14. #include <sys/stropts.h>
  15. #include <sys/file.h>
  16. #include <sys/ioctl.h>
  17. #include <net/nit.h>
  18. #include <net/nit_pf.h>
  19. #include <net/nit_buf.h>
  20. #include <sys/socket.h>
  21. #include <net/if.h>
  22. #include <net/nit_if.h>
  23. #include <fcntl.h>
  24.  
  25. #define BUFSIZE 32768
  26.  
  27. int
  28. opennit(char *eif, void (*filter)(int))
  29. {
  30.      int fd;
  31.      int bufsize = BUFSIZE;
  32.  
  33.      struct strioctl si;
  34.      struct ifreq ifr;
  35.      struct timeval timeout;
  36.      unsigned long flags;
  37.      
  38.      fd = open("/dev/nit", O_RDONLY);
  39.      ioctl(fd, I_SRDOPT, (char *)RMSGD);
  40.  
  41.      if(ioctl(fd, I_PUSH, "pf") == -1)
  42.       perror("pf push");
  43.  
  44.      /* Push filter */
  45.      (*filter)(fd);
  46.  
  47.      ioctl(fd, I_PUSH, "nbuf");
  48.  
  49.      /* One second timeout */
  50.      timeout.tv_sec = 1;
  51.      timeout.tv_usec = 0;
  52.      si.ic_timout = INFTIM;
  53.      si.ic_cmd = NIOCSTIME;
  54.      si.ic_len = sizeof (timeout);
  55.      si.ic_dp = (char *)&timeout;
  56.      ioctl(fd, I_STR, (char *)&si);
  57.  
  58.  
  59.      /* 32k buffer */
  60.      si.ic_cmd = NIOCSCHUNK;
  61.      si.ic_len = sizeof(bufsize);
  62.      si.ic_dp = (char *)&bufsize;
  63.      ioctl(fd, I_STR, (char *)&si);
  64.  
  65.      /* Select which interface */
  66.      strncpy(ifr.ifr_name, eif, sizeof(ifr.ifr_name));
  67.      ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0;
  68.      si.ic_cmd = NIOCBIND;
  69.      si.ic_len = sizeof(ifr);
  70.      si.ic_dp = (char *)𝔦
  71.      if(ioctl(fd, I_STR, (char *)&si) == -1){
  72.       perror(eif);
  73.       exit(1);
  74.      }
  75.  
  76.      /* Set promiscuous mode & timestamp flags */
  77.      flags = NI_TIMESTAMP;
  78.      flags |= NI_PROMISC;
  79.      ioctl(fd, NIOCSFLAGS, &flags);
  80.  
  81.      /* Flush any packets that came in before we finished configuring */
  82.      ioctl(fd, I_FLUSH, (char *)FLUSHR);
  83.  
  84.      return fd;
  85. }
  86.  
  87. void
  88. procpkts(int fd, void (*handler)())
  89. {
  90.      int n;
  91.      char buf[BUFSIZE];
  92.  
  93.      while((n = read(fd, buf, BUFSIZE)) >= 0){
  94.       char *bp = buf;
  95.       char *bufend = buf+n;
  96.  
  97.       while(bp < bufend){
  98.            register char *cp = bp;
  99.            struct nit_bufhdr *bufhdrp;
  100.            struct nit_iftime *hdrtime;
  101.            struct timeval *tvp = NULL;
  102.            unsigned long drops = 0;
  103.            unsigned long pktlen;
  104.            struct nit_iftime *ntp;
  105.            
  106.            bufhdrp = (struct nit_bufhdr *)cp;
  107.            cp += sizeof(struct nit_bufhdr);
  108.  
  109.            hdrtime = (struct nit_iftime *)cp;
  110.            cp += sizeof(struct nit_iftime);
  111.  
  112.            (*handler)(cp, bufhdrp->nhb_totlen, *hdrtime);
  113.  
  114.            bp += bufhdrp->nhb_totlen;
  115.       }
  116.      }
  117. }
  118.