/* 
 *  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. 
 *  
 */
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H 1

#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>

#define TCP_STATE_SYN_SYNACK    1 // initial SYN sent, waiting for SYN ACK
#define TCP_STATE_SYNACK_ACK 2 // SYN&ACK response sent, waiting for ACK
#define TCP_STATE_UP    3 // SYNACK response sent
#define TCP_STATE_FIN_FINACK 4
#define TCP_STATE_CLOSED 5
#define TCP_STATE_RESET 6

class TCPConnection
{
public:
	TCPConnection( const u_char *packet );
	bool match(in_addr sa, in_addr da, int sp, int dp);
	void process_packet();
	void interval();
	void print2( FILE *fp );
	float compute_rate();
	bool acceptPacket( const u_char *packet );
	in_addr getSrcAddr();
	int getSrcPort();
	in_addr getDstAddr();
	int getDstPort();
	bool operator<( TCPConnection & );
	bool operator>( TCPConnection & );

	int getState();
	bool isFinished();

	time_t getLastPktTimestamp();
	time_t getIdleSeconds();
	void recalcAvg();
	bool activityToggle();

	int getPacketCount();
	int getPacketsPerSecond();
	int getPayloadBytesPerSecond();
	int getAllBytesPerSecond();
private:
	int port_src;
	int port_dst;
	in_addr addr_src;
	in_addr addr_dst;

	int state;

	time_t last_pkt_ts;
	int packet_count;

	bool activity_toggle;

	// per-second stats
	time_t this_second;
	int packets_this_second;
	int payload_bytes_this_second; // payload only
	int all_bytes_this_second; // ip hdr + tcp hdr + payload
	int packets_last_second;
	int payload_bytes_last_second; // payload only
	int all_bytes_last_second; // ip hdr + tcp hdr + payload
	
};

#endif

