#include "includes.h"

int main(int argc, char **argv){

  int fd;
  struct ether_header *eth, *eth2;
  struct ip *iph;
  struct tcphdr *tcph;
  struct bpf_hdr *bpf_header;
  char *pkt1, *pkt2, *ptr;
  char *databuf;
  char *device = DEVICENAME;
  struct strbuf data;
  int ret, flags, promisc=1, c;
  struct target_host f_Host, t_Host;
  u_short fhost=0, thost=0, kill_all=0, verbose=0, passive=0;
  u_int buffer_size;
  extern char *optarg;

  while ((c = getopt(argc, argv, "af:t:vPd:")) != EOF){
    switch (c) {
      case 'a':
        printf("killing all tcp connections\n");
        kill_all++;
        break;
      case 'f':
        printf("killing connections from %s\n", optarg);
        makehost(&f_Host, optarg);
        fhost++;
        break;
      case 't':
        printf("killing connections to %s\n", optarg);
        makehost(&t_Host, optarg);
        thost++;
        break;
      case 'v':
        verbose++;
        break;
      case 'P':
        passive++;
        break;
      case 'd':
        device = optarg;
        break;
      case '?':
        usage(argv[0]);
        break;
    }
  }

  fd = initdevice(device, promisc);

  pkt1 = (char *)memalign(SIZE_U_LONG, MAXDLBUF);
  pkt2 = (char *)memalign(SIZE_U_LONG, MAXDLBUF);
  databuf = (char *)memalign(SIZE_U_LONG, MAXDLBUF);

  bzero(pkt1, MAXDLBUF);
  bzero(pkt2, MAXDLBUF);
  bzero(databuf, MAXDLBUF);

  eth = (struct ether_header *)pkt1;
  iph = (struct ip *)(pkt1 + SIZE_ETHER_H);
  tcph = (struct tcphdr *)(pkt1 + SIZE_ETHER_H + SIZE_IP_H);

  data.buf = (char *) databuf;
  data.maxlen = MAXDLBUF;
  data.len = 0;

  while (getmsg(fd, (struct strbuf *)NULL, &data, &flags) == 0) {
    make_eth_and_ip((char *)pkt1, (char *)data.buf);

    if (iph->ip_p == 47){
#ifdef DEBUG
      printf("========================\n");
      print_gre_pkt(data.buf, data.len);
      hexdump(data.buf, data.len);
      printf("\n");
#endif

    }    
    bzero(pkt1, MAXDLBUF);
    bzero(pkt2, MAXDLBUF);
  } /* end while (getmsg) */
  return 0;
}

