#include <netinet/tcp.h> /* struct tcphdr */
#include <net/ethernet.h> /* struct ethhdr */
#include <linux/ip.h> /* struct iphdr */

#include <netinet/in.h> /* ntohs, inet_ntoa */
#include <sys/socket.h> /* inet_ntoa */
#include <arpa/inet.h> /* inet_ntoa */

#include <string.h> /* strdup */
#include <stdlib.h> /* free */

#include "main.h"
#include "pcap_tcp.h"
#include "pcap_ip.h" /* get_ip*addr */

void handle_tcp(const u_char *packet)
{
	struct tcphdr *t;
	unsigned short sport, dport;
	char *saddr, *daddr;

	t = (struct tcphdr*)(packet + sizeof(struct ethhdr)
			+ sizeof(struct iphdr));
	
	sport = ntohs(t->source);
	dport = ntohs(t->dest);

	saddr = strdup(inet_ntoa(get_ipsaddr(packet)));
	daddr = strdup(inet_ntoa(get_ipdaddr(packet)));

	DEBUGP(D_NOW, "TCP packet source: %s:%d, destination: %s:%d\n",
			saddr, sport, daddr, dport);

	free(saddr);
	free(daddr);
}

