/* 
 *  Ths code in this file is part of tcptrack. For more information see
 *    http://www.rhythm.cx/~steve/devel/tcptrack
 *
 *     Copyright (C) Steve Benson - 2003
 *
 *  tcptrack is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your
 *  option) any later version.
 *   
 *  tcptrack is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  
 */
#define _BSD_SOURCE 1
#define _REENTRANT
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "TCPConnection.h"
#include "Collector.h"
#include "TCContainer.h"
#include "SortedIterator.h"
#include "defs.h"

TCContainer::TCContainer()
{
	//
	// Start up maintenence thread
	//
	run_maint_thread = true;
	int rc = pthread_create(&maint_thread_tid,NULL,maint_thread_func,this);
	if( rc )
	{
		perror("pthread_create");
		exit(1);
	}

}

TCContainer::~TCContainer()
{
	// turn off maintenece thread
	run_maint_thread = false;
	// TODO: wait for maintenece thread to exit?
}

SortedIterator TCContainer::getSortedIterator()
{
	return SortedIterator(this);
}

bool TCContainer::processPacket( const u_char *packet )
{
	const struct sniff_ip *ip; /* The IP header */
	const struct sniff_tcp *tcp; /* The TCP header */

	// parse the headers off the packet 
	ip = (struct sniff_ip*)(packet);
	tcp = (struct sniff_tcp*)(packet + IP_HEADER_LEN);

	lock();
	bool found = false;
	for( TCPConnectionListIterator i = conlist.getIterator();
	     TCPConnection *ic = i.getNext(); )
	{
		if( ic->acceptPacket(packet) )
			found=true;
	}
	if( found==false && (tcp->th_flags&TH_SYN) && !(tcp->th_flags&TH_ACK) )
	{
		TCPConnection *newcon = new TCPConnection(packet);
		conlist.append(newcon );
		found = true;
	}
	
	unlock();
	free((void *)packet); 
	
	return found;
}

unsigned int TCContainer::numConnections()
{
	unsigned int count=0;
	for( TCPConnectionListIterator i = this->getIterator();
	     TCPConnection *ic=i.getNext(); )
	{
		++count;
	}
	return count;
}

TCPConnectionListIterator TCContainer::getIterator()
{
	return conlist.getIterator();
}

void TCContainer::maint_thread_run()
{
	while( run_maint_thread )
	{
		sleep(1);
		lock();
		
		TCPConnectionListIterator i = conlist.getIterator();
		TCPConnection *ic = i.getNext();
		int numitems = 0;
		while(ic)
		{
			numitems++;
			ic->recalcAvg();
			if( ic->isFinished() && ic->getIdleSeconds() > 5 )
			{
				TCPConnection *rm = ic;
				ic = i.getNext();
				conlist.remove(rm);
				collector.collect(rm);
			} 
			else if( (ic->getState()==TCP_STATE_SYN_SYNACK) && (ic->getIdleSeconds()>CLOSED_PURGE) )
			{
				TCPConnection *rm = ic;
				ic = i.getNext();
				conlist.remove(rm);
				collector.collect(rm);				
			}
			else
				ic = i.getNext();
		}
		unlock();		
	}
}


void TCContainer::lock()
{
	pthread_mutex_lock(&conlist_lock);
}

void TCContainer::unlock()
{
	pthread_mutex_unlock(&conlist_lock);
}

///////////////////

void *maint_thread_func( void * arg )
{
	TCContainer *c = (TCContainer *) arg;
	c->maint_thread_run();
	return NULL;
}


