/*************** Insert records in the hash tables **************************/
---------------------------------------------------------------------------
/* Insert flow record in flow hash table */
------------------------
function insert_flow()
------------------------
{ 
  /* If table is full and cannot grow anymore, give up. This never happens
     due to emergency cleanup */
  if (flow_hash_table.filled == flow_hash_table.size)
    return NOT_FOUND
  find empty slot i and insert data fs
  /* Initialize statistics */
  fs.sent_to = 0
  fs.good_bytes = 0
  fs.dropped_bytes = 0
  fs.good_bytes_old = 0
  fs.received_from = 0
  fs.sent_bytes = 0
  fs.received_bytes = 0
  fs.TCP_received_from = 0
  fs.TCP_sent_to = 0
  fs.TCP_sent_bytes = 0
  fs.TCP_received_bytes = 0
  fs.UDP_received_from = 0
  fs.UDP_sent_to = 0
  fs.UDP_sent_bytes = 0
  fs.UDP_received_bytes = 0
  fs.ICMP_received_from = 0
  fs.ICMP_sent_to = 0
  fs.ICMP_sent_bytes = 0
  fs.ICMP_received_bytes = 0
  fs.OTHER_received_from = 0
  fs.OTHER_sent_to = 0
  fs.OTHER_sent_bytes = 0
  fs.OTHER_received_bytes = 0
  get current time with maximum precision, as seconds and fractions of
      	seconds and store it in fs.timestamp
  fs.tcprto = 0
  fs.icmprto = 0    
  fs.classification = TRANSIENT
  fs.compliant = 0
  fs.TCP_conns = 0
  fs.UDP_conns = 0
  fs.ICMP_conns = 0
  fs.OTHER_conns = 0
  set fs.min to a very large value
  fs.avg_time = 0
  increment flow_hash_table.filled
  /* Check if we need to do emergency cleanup */
  if (flow_hash_table.filled/flow_hash_table.size > FULL_ALERT_LF)
  {
      /* We will preferrentialy delete entries that had low
	 number of sent bytes/packets */
      int SB = 20
      int SP = 1
      /* Do this until occupancy is reduced below high
	 rehash load factor */
      while(flow_hash_table.filled/flow_hash_table.size 
	    > FLOW_REHASH_LOAD_FACTOR_HIGH)
	{
	  for (each entry fhe in flow_hash_table)
	    {	
		  if (fhe.sent_to <= SP 
		     and fhe.sent_bytes <=SB)
		    {
			delete fhe
		        decrement flow_hash_table.filled 
		    }
	    }
	  SB += BINCR
	  SP++
	}
  } /* end of emergency cleanup */
  return i
}
---------------------------------------------------------------------------
/* Insert connection record in connection hash table */
----------------------------
function insert_connection()
----------------------------
{ 
  /* If table is full and cannot grow anymore, give up. This never happens
     due to emergency cleanup */
  if (connection_hash_table.filled == connection_hash_table.size)
    return NOT_FOUND
  find empty slot i and insert data cs
  /* Initialize statistics */
  cs.sent_to = 0
  cs.received_from = 0
  cs.sent_bytes = 0
  cs.received_bytes = 0
  cs.rto = 0	
  cs.new_connection = 1
  get current time with maximum precision, as seconds and fractions of
      	seconds and store it in cs.timestamp
  increment connection_hash_table.filled
  /* Check if we need to do emergency cleanup */
  if (connection_hash_table.filled/connection_hash_table.size > FULL_ALERT_LF)
  {
      /* We will preferrentialy delete entries that had low
	 number of sent bytes/packets */
      int SB = 20
      int SP = 1
      /* Do this until occupancy is reduced below high
	 rehash load factor */
      while(connection_hash_table.filled/connection_hash_table.size 
	    > CONN_REHASH_LOAD_FACTOR_HIGH)
	{
	  for (each entry che in connection_hash_table)
	    {	
		/* Never delete good entries */
		if (che.classification == GOOD)
			continue
		if (che.sent_to <= SP 
		     and che.sent_bytes <=SB)
		    {
			find flow i which contains che
			switch (che.protocol)
			{
				TCP:  	decrement flow[i].TCP_conn
				UDP:  	decrement flow[i].UDP_conn
				ICMP:  	decrement flow[i].ICMP_conn
				OTHER:  decrement flow[i].OTHER_conn
			}
			delete che
		        decrement connection_hash_table.filled 
		    }
	    }
	  SB += BINCR
	  SP++
	}
  } /* end of emergency cleanup */
  return i
}
---------------------------------------------------------------------------
