/*** Defines rate limit for the flow based on classification and behavior ***/
----------------------------------
function rate_limit(flow_stats fs)
----------------------------------
{
  if (exists fs.rate_limit)
    {
  	get current time with maximum precision, as seconds and fractions of
      	seconds and store it in time_now
  	/* Calculate sending rate */
  	rate = fs.sent_bytes/
    		(time_now - fs.timestamp)  
        /* Calculate compliance factor */
      	if (fs.sent_bytes + fs.dropped_bytes > 0)
		factor = fs.sent_bytes/(fs.sent_bytes + fs.dropped_bytes)  
        /* If flow is attack */
        if(fs.classification == ATTACK)
	{
	  /* Exponentially decrease rate limit,
	     you can find these formulas in the ICNP paper */
	  if (rate < fs.rate_limit)
	    { 
	      if (rate > MIN_RATE * DEC_SPEED/factor) 
		fs.rate_limit = rate/DEC_SPEED * factor
	      else
		fs.rate_limit = MIN_RATE
	    }
	  else
	    {
	      if (fs.rate_limit > MIN_RATE * DEC_SPEED/factor)
		fs.rate_limit = fs.rate_limit/DEC_SPEED * factor;
	      else
		fs.rate_limit = MIN_RATE;
	    }
	} /* endif classification is ATTACK */
      	else if (fs.classification == SUSPICIOUS)
	{
	      	/* If it is suspicious linearly increase rate limit */
		/* If flow complies with the limit increase freely */
		if (fs.dropped_bytes == 0)
		    fs.rate_limit += (RATE_INC * factor)
		/* Else increase only up to certain rate */
	  	else if (fs.rate_limit+SLOW_RATE_INC * factor <= RATE_ALLOWED)
	    	    fs.rate_limit += (SLOW_RATE_INC * factor)
	} /* endif classification is SUSPICIOUS */
      else
	{
	  /* If it is normal exponentialy increase rate limit */
	  if (fs.rate_limit < MAX_RATE/INC_SPEED)
	      fs.rate_limit = fs.rate_limit + fs.rate_limit * factor 
	    else
	      fs.rate_limit = MAX_RATE
	}
     }/* endif exists(fs.rate_limit) */
     else
     { 
	if (fs.classification == ATTACK)
             fs.rate_limit = DEC_SPEED * fs.sent_bytes/(time_now - fs.timestamp)
     }
 }








