***************************************************************************
Programs:  timeslice.c network.c network.h list.c list.h commonhdrs.h bittorrent.c netflix.c

Last update to this file:  5/15/2016

Revisions:  See below and most recent is here:
 $A100:  Added support for variable size intervals.  
  The original design added packets into fixed bins where the first 
  bin started at the time observed of the first packet in the trace.
  Setting option '-d 0' fill retain this FIXED interval behavior.
  Setting '-d 1' will put the tool in this mode.

Compiling: make (you might have to enter super user password for the copy command to work
		as not every user has access to /usr/bin) 
       
Brief Summary:

timeslice is a program that takes a tcpdump file and processes it. It
can provide summary information about the trace file and it can
provide detailed per flow information.  

WARNING WARNING WARNING:  If you do NOT specify 
a filter, it treats DS as a separate flow from US....
So when you expect a single TCP cx, you will see 
to flows listed.  Also, aggregate counts will include
both DS and US.  
Usually we want to specify a dst or src IP or network to
limit the anallysis to all DS or all US.

The program has 3 basic modes of operation.

Mode 1.
Can provide a timeseries of aggregate data (and # active flows) per unit time.  
The getBW.awk runs after timeslice on the output file to get the avg BW and
number flows.

Example usage:
  timeslice -t 2.0 -a 4.0 -b 10000 -f "src 10.0.0.2" trace.dmp > traceUS.dat
	OR
  timeslice -m 1 -f "src 10.0.0.2" trace.dmp > traceUS.dat (this will set the default values of
			the options a = 1.0 and b = 1000 and t= 1.0)

Mode 2.
It provides TCP cx stats on a number of TCP connections.

Example usage:
  timeslice  -S -r -i -t 1.0 -a 30.0 -b 37500 -f "src 10.0.0.2 " ../8-11-2005/trace4.dmp >> traceUS.dat
	OR
  timeslice -m 2 -f "src 10.0.0.2" ../8-11-2005/trace4.dmp >> traceUS.dat

Mode 3.
It provides a global summary of the trace file.
  timeslice -s -i -t 2.0 -a 4.0 -b 10000 -f "src 10.0.0.2" trace.dmp > traceUS.dat
	OR
  timeslice -m 3 "src 10.0.0.2" trace.dmp > traceUS.dat

Example usage:


This README contains the following sections


 REVISIONS:  Changes made to the original program

 USAGE: summary of input program parameters

 MODE1: details of mode 1

 MODE2: details of mode 2

 MODE3: details of mode 3

 DESIGN: details of design

 BUGS: known bugs

 TODO:  list of things to be done

***************************************************************************

***************************************************************************
REVISIONS

DECOMPOSED the single C file into multiple files for different modules:
timeslice.c 			(includes network.h and pcap.h)
network.c	network.h 	(includes list.h and all the network header files)
list.c		list.h 		(includes commonhdrs.h)
commonhdrs.h			(includes the basic C header files)

$A4
ADDED the -m option to specify the mode of operation which makes the rest of the parameters
optional. The number after -m signifies the mode of operation.

$A5
CHANGED the program to allow deletion of buffered packet data without
affecting the calculation of statistics. This is done by doing
the statistics calculation each time a new slice is created AND 
more than 10000 packets have been buffered
Newly added methods:
	void delete_list(HEAD *linked_list)
	void delete_buffered_packets(struct trace_data* td)
	void memory_cleanup(struct trace_data *td)

$A6
ADDED the bittorrent.c file which contains the handler for detecting BitTorrent flows
This file includes the network.h header file and is invoked via a function pointer.
Each of the modes will now display the BitTorrent specific flows as a fraction of the total 
data captured in the dump. Moreover, in MODE 1, the program will show BitTorrent traffic in each slice
as a fraction of the total bytes in the slice.
As and when new application types are registered, the output will display similar statistics for
each of the application types.
newly added methods:
	void init_application_types()
	void set_flow_application_type(struct trace_data* td, struct packet *pkt, const u_char *app_data)
	struct connection *get_conn_by_id(struct trace_data* td, int conn_id)
	void process_app_specific_stats(struct trace_data *td)

Any new application type must be registered in init_application_types() function in
the following manner:
	applicationTypes[0].applicationType = APP_BITTORRENT;//declare a const with value > 1
	//function name which will test the packet's application type
	//flow_app_type is a function pointer
	//make sure that network.h is included in the file containing the new handler
	applicationTypes[0].flow_app_type= flow_bittorrent_type;

$A7
ADDED the netflix.c file which contains the handler for detecting NetFlix traffic.This file includes the
network.h header file and invoked via a function pointer.

netflix.c contains the following functions:
---------------------------------
a)is_netflix_handshake()

This function checks for the initial handshake/connection packet. If the first byte of the application data
contains a "netflix" or "nflx" the program understands it as a netflix flow and stores the packets src_ip,dst_ip,
src_port and dst_port along with the tag applicationType as APP_NETFLIX.

b)add_to_tree()

This function is called by is_netflix_handshake() inorder to store the packet info to the AVL tree. The function
checks for the packets src_ip and src_port in the tree. If found adds the packets dst_ip and dst_port to the
AVL tree. If not both the packets source and destination info are added to the AVL tree.


c)check_present()

This function checks the packet info for the TCP packets which are part of the netflix flow(data) and may not
contain the keyword "netflix" or "nflx" in them but they are part of the netflix flow establised previously
which contains the same ip and port info. When the info is changed due to video changeover then, the new 
dst_ip/dst_port or src_ip/src_port is added to the AVL tree.

d)flow_netflix_type()

This function calls the is_netflix_handshake() function and check_present() function inorder to confirm whether the
packet is a part of Netflix flow.
***************************************************************************
USAGE

     printf("USAGE: \n"
            "%s [OPTION]... file\n"
            "   where file is a packet capture (only tcp packets are processed).\n"
            "   Results are printed to stdout in the form:\n" 
            "   [slice #] [slice bytes] [unique sessions] [active sessions]\n\n"
            "Options:\n"
            " -f \"filter\"    same as tcpdump, example: -f \"src 10.0.0.2\"\n"
            " -t n           slice interval seconds (default 1.0)\n"
            " -a n           active session seconds (default 1.0)\n"
            " -b n           active session byte value (default 1000)\n"
            " -c n           only process n packets\n\n"
	    " -C             get count of all packets in trace file"
	    " -B n           begin at packet n: if -E n is used. -B n must be less than -E n."
	    " -E n           end at packet n: cannot use with -c n"
	    " -m n	     sets the program to one of the three possible modes of operation"
	    "		     and sets the default values accordingly"
            " Alternative outputs: (-f and -c are the only applicable options from above)\n"
            " Summary:\n"
            " -s             only print summary of file\n"
            " Session Data:\n"
            " -S             only print data for each session in the format:\n"
            " [#] [rel. start] [rel. end] [bytes] [packets] [#SYN] [#FIN] [activity ratio] 
                                 [retransmits] [lossrate] [avg rtt] [max rtt]\n"
            " -A             interval length in seconds for activity ratio (default 0.1)\n"
            " -i             print ip's with session data\n"
            " -p             print without formating\n"
            " -r             use alternative method to determine retransmit\n"
            " -U \"ip\"        print only connections with this src ip \n"
            " -D \"ip\"        print only connections with this dst ip \n"
            "\n", name, name);




***************************************************************************
MODE1

            This program reads a tcpdump file and computes the average
            number of TCP flows that are active over a given time interval.
	    An active flow is defined by the -a and -b params.  For example, if
	    -b is 37500 bytes and -a is 30 seconds,  an active session must
	    send at an avg rate of 10Kbps over the interval.

	    -a   -b     rate
	    30  37500   10Kbps
	    30  375000  100Kbps
	    30  150000  40Kbps


  timeslice -t 2.0 -a 4.0 -b 10000 -f "src 10.0.0.2" trace.dmp > traceUS.dat
  timeslice -t 2.0 -a 4.0 -b 10000 -f "dst 10.0.0.2" trace.dmp > traceDS.dat

  This causes the program to issue the following to std for each timeslice:
  (this is in timeslice.c print_results )
        printf("%d %d %d %d\n", slc->id+1, slc->bytes, slc->unique_cnt,slc->active_cnt);
	Each line:   slice id,  number bytes in slice,  
                     total number of cxs seen in the slice, number of active cxs in the slice
        And then run goBW.script which processes the data and 
        prints the following summary to standard out:
             trace1-2DS.dmp (cal)
             upstream BW:
             average BW:128736
             mean total flows:1.13939
             mean active flows:12.5
             downstream BW:
             average BW:128630
             mean total flows:1.11515
             mean active flows:11.8



***************************************************************************
MODE2

  timeslice -S -r -t 2.0 -a 4.0 -b 10000 -f "src 10.0.0.2" trace.dmp > traceUS.dat
      This puts summary data in traceUS.dat
  timeslice -S -r -i -t 2.0 -a 4.0 -b 10000 -f "src 10.0.0.2" trace.dmp > traceUS.dat
      This puts summary data in traceUS.dat and ids each ip host

    When using the -S the print information displayed will not make sense.
    Use the '-r' as this engages the correct packet loss detection code.

   This is in network.c  process_connection_stats

   The traceUS.dat will have lines for each flow:   
1        10.0.0.2:  3038 -->   201.27.73.229: 20219        
0.000     4402.008 21338665      19790  0  0     0.275034   
116    11   0.59 0.0000   0.00

Field #
1         flow id
2         src IP
3         src port
4         dst IP
5         dst port
6         start time (time when the first pkt in the trace is seen)
7         end time  (time when the last pkt in the trace is seen)
8         bytes
9         pkt count
10         syn count
11        fin count
         %ofslices it was active (0-1.0)
12         retransmit count
13         outoforder count
14         loss rate (as a percent)
15        avg rtt
16         max rtt




***************************************************************************
MODE3

  timeslice -s -i -t 2.0 -a 4.0 -b 10000 -f "src 10.0.0.2" trace.dmp > traceUS.dat
       Now just a summary of the US data is produced:

./8-11-2005/trace4.dmp summary:

202423 packets
171069950 bytes
4402.241112 seconds
88 SYN packets
66  unique cxs
66  unique cxs pairs
0.019990 SYNs per second
0.000000 unique cxs per second




***************************************************************************
DESIGN
Modules:
All the list functions are present in the list.c file which are invoked from
the network.c file for storage and memory deallocation.
The network.c file contains the functions which tag a given packet to its
correct connection, connection_par and slice. It also contain functions for calculating the RTT,
calculating slice and connection specific statistics, freeing up memory and calling
application specific handlers
The timeslice.c file has the main method which parses the commandline options and fills them
into the timeslice data structure. This file also has the method which is invoked by the pcap
library whenever it has processed another packet.
The bittorrent.c file has the handler for detecting BitTorrent flows and is called from
the network.c file indirectly using a function pointer.
The netflix.c file has the handler for detecting Netlix flows and is called from 
the network.c file indirectly using a function pointer.
***************************************************************************
TODO
Store all the data in hashtables so that we do not have to loop through older data

Make the calculation of RTT independent from the previous packets of the connection;
add some additional fields to the connection structure to do the same

***************************************************************************


Problems and todo  (March-May 2016)

1  Summary that include bytes and pkt counts is not right. 



