/* Common include files. 
   The source code may be redistributed, as long it remains intact, and
   with this License included. You may modify or use this source code in
   anyway you see fit, but credit must be given where applicable.

   This software comes with NO warranty, and the author is NOT liable for
   any damages which may come from the use of this software.

  You may not use the name of the author to promote this software.

  shadows@whitefang.com (Thamer Al-Herbish)
*/

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <string.h>
#include <pcap.h>

#include <unistd.h>
#include <signal.h>
#include <errno.h>

#ifndef SYS_LINUX
/* System dependant stuff.
 * This configuration works on BSD/Solaris probably more. */

#include <netinet/in_systm.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>

#include <unistd.h>
#include <signal.h>
#include <errno.h>


/* This usualy fixes things on BSD variants.
 * If they're a bit queer. */

#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6
#endif
#ifndef ETHER_HDR_LEN
#define ETHER_HDR_LEN (sizeof(struct ether_header))
#endif

#else
/* Now for Linux */

#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>

#define ETHER_HDR_LEN  ETH_HLEN
#define ETHER_ADDR_LEN ETH_ALEN

/* Taken from BSD's tcp.h */
#define	TH_FIN	0x01
#define	TH_SYN	0x02
#define	TH_RST	0x04
#define	TH_PUSH	0x08
#define	TH_ACK	0x10
#define	TH_URG	0x20
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG)

#endif

struct con {
  int have;
  unsigned long ack; /* ACK Sequence number. */
  unsigned long seq; /* SYN Sequence number. */
  
  unsigned char state; /* The state of the connection */

  struct in_addr src;  /* Host initiating connection. */
  struct in_addr dst;  /* Listening host. */

  unsigned short sport; /* Source port on initiating host. */
  unsigned short dport; /* Destination port on listening host */

  unsigned char mac_src[ETHER_ADDR_LEN];  /* MAC of initiating host. */
  unsigned char mac_dest[ETHER_ADDR_LEN]; /* MAC of destitination host. */
  struct timeval timestamp; /* Timestamp of last packet sent. */
  unsigned char flags;
  struct con *next;
};

#ifdef ETHERTYPE_LINK
#define ETHERBUFF 88
#endif

#ifdef PPPTYPE_LINK
#define PPPBUFF (sizeof(struct ppp_header)+
                 sizeof(struct ip)+sizeof(struct tcphdr))
#endif
#define IPSIZE 16

#define MAC_SIZE (ETHER_ADDR_LEN * 3)

#define PORTSIZE 6

/* States. */

#define SYN_SENT 1
#define SYN_RECV 2
#define DONE     3     /* Never actually used. */
#define SYN_SCAN 4      /* Possible syn scan attempt */
