home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / tcplogger / tcpsyn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-17  |  3.9 KB  |  173 lines

  1. /*
  2.      tcplogger - A TCP traffic logger
  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. tcpsyn.c - 03/20/93
  8.  
  9. */
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <netdb.h>
  15. #include <netinet/in_systm.h>
  16. #include <netinet/ip.h>
  17. #include <netinet/tcp.h>
  18. #include <sys/time.h>
  19. #include <netdb.h>
  20. #include <memory.h>
  21.  
  22. extern char *optarg;
  23. extern int optind, opterr;
  24.  
  25. #include "defs.h"
  26.  
  27. struct ipaddr {
  28.      unsigned int addr:32;
  29. };
  30.  
  31. struct tcpsynout {
  32.      struct timeval tp;
  33.      unsigned long ipsrcaddr;
  34.      unsigned long ipdstaddr;
  35.      unsigned long tcpseq;
  36.      unsigned short tcpsrcport;
  37.      unsigned short tcpdstport;
  38. };
  39.  
  40. static void etherhandler(char *, int, struct timeval);
  41. static void tcpsyn(struct ip *, struct timeval);
  42. static void tcplogbin(struct tcpsynout *);
  43. static void tcplogascii(struct tcpsynout *);
  44. static char *gettcpportname(unsigned short);
  45. extern char *gettimestr(struct timeval);
  46.  
  47. extern void pushfilter(int);
  48.  
  49. struct val_str_hndl ether_types[] = {
  50.      0x0800, "DOD Internet Protocol", tcpsyn,
  51.      0, (char *)0, 0
  52.      };
  53.  
  54. #define LOGBINARY 0
  55. #define LOGASCII 1
  56. static int logtype = LOGBINARY;
  57. static FILE *logfile = stdout;
  58.  
  59. int resolve = 1;
  60.  
  61. extern char *getdefaultif(char *);
  62.  
  63. main(int argc, char **argv)
  64. {
  65.      char *eif = getdefaultif((char *)0);
  66.      int fd;
  67.      int c;
  68.      
  69.      while((c=getopt(argc, argv, "ni:abf:")) != -1){
  70.       switch(c){
  71.       case 'a':
  72.            logtype = LOGASCII;
  73.            break;
  74.       case 'b':
  75.            logtype = LOGBINARY;
  76.            break;
  77.       case 'n':
  78.            resolve = 0;
  79.            break;
  80.       case 'i':
  81.            eif = optarg;
  82.            break;
  83.       case 'f':
  84.            if(!(logfile = fopen(optarg, "a"))){
  85.             perror(optarg);
  86.             exit(1);
  87.            }
  88.            break;
  89.       default:
  90.            fprintf(stderr, "Unknown option: %c\n", c);
  91.            exit(1);
  92.       }
  93.      }
  94.  
  95.      fd = opennit(eif, pushfilter);
  96.      procpkts(fd, etherhandler);
  97. }
  98.  
  99. void
  100. etherhandler(char *pkt, int size, struct timeval tp)
  101. {
  102.      tcpsyn((struct ip *)(pkt + 14), tp);
  103. }
  104.  
  105.  
  106. void
  107. tcpsyn(struct ip *ip, struct timeval tp)
  108. {
  109.      struct tcphdr *tcp;
  110.      struct tcpsynout outbuf;
  111.  
  112.      tcp = (struct tcphdr *)(((char *)ip) + sizeof(struct ip));
  113.  
  114.      memcpy((char *)&outbuf.tcpseq, (char *)&tcp->th_seq, sizeof(long));
  115.      memcpy((char *)&outbuf.tp, (char *)&tp, sizeof(struct timeval));
  116.      memcpy((char *)&outbuf.ipsrcaddr, (char *)&ip->ip_src, 4);
  117.      memcpy((char *)&outbuf.ipdstaddr, (char *)&ip->ip_dst, 4);
  118.      memcpy((char *)&outbuf.tcpsrcport, (char *)&tcp->th_sport, 2);
  119.      memcpy((char *)&outbuf.tcpdstport, (char *)&tcp->th_dport, 2);
  120.  
  121.      switch(logtype){
  122.      case LOGBINARY:
  123.       tcplogbin(&outbuf);
  124.       break;
  125.      case LOGASCII:
  126.       tcplogascii(&outbuf);
  127.       break;
  128.      default:
  129.       fprintf(stderr, "Log type unknown: %d\n", logtype);
  130.       break;
  131.      }
  132. }
  133.  
  134. void
  135. tcplogbin(struct tcpsynout *outbuf)
  136. {
  137.      write(fileno(logfile), (char *)outbuf, sizeof(struct tcpsynout));
  138. }
  139.  
  140. void
  141. tcplogascii(struct tcpsynout *outbuf)
  142. {
  143.      struct in_addr haddr;
  144.  
  145.      fputs(gettimestr(outbuf->tp), logfile);
  146.      fprintf(logfile, " %8X ", outbuf->tcpseq);
  147.      memcpy(&haddr.s_addr, &outbuf->ipsrcaddr, 4);
  148.      fprintf(logfile, "%-21s", resolve ? cgethostbyaddr(haddr) : inet_ntoa(haddr));
  149.      fputc(' ', logfile);
  150.      fputs(gettcpportname(outbuf->tcpsrcport), logfile);
  151.      fputs(" -> ", logfile);
  152.      memcpy(&haddr.s_addr, &outbuf->ipdstaddr, 4);
  153.      fprintf(logfile, "%-21s", resolve ? cgethostbyaddr(haddr) : inet_ntoa(haddr));
  154.      fputc(' ', logfile);
  155.      fputs(gettcpportname(outbuf->tcpdstport), logfile);;
  156.      fputc('\n', logfile);
  157.      fflush(logfile);
  158. }
  159.  
  160. char *
  161. gettcpportname(unsigned short p)
  162. {
  163.      struct servent *se;
  164.      static char result[80];
  165.  
  166.      if(resolve && (se = getservbyport((unsigned int)ntohs(p), "tcp")))
  167.       strcpy(result, se->s_name);
  168.      else
  169.       sprintf(result, "%u", (unsigned int)ntohs(p));
  170.      
  171.      return result;
  172. }
  173.