Improved flood control counting to strictly not allow more than

the given amount of messages in the configured interval.

The previous behavior allowed n+2 messages in the interval, due to
two off by one comparison rules.

In addition, if flooding is detected then each new message after the flooding
is detected will reset the interval timer, which means the client
cannot send another message until the timeout interval expires.
Цей коміт міститься в:
Jan Vidar Krey
2014-05-08 13:30:09 +02:00
джерело c26e8aaefe
коміт e2b0757f4a
+12 -13
Переглянути файл
@@ -30,26 +30,25 @@ int flood_control_check(struct flood_control* data, size_t max_count, size_t ti
if (!time_delay || !max_count) if (!time_delay || !max_count)
return 0; return 0;
if (!data->time) // No previous message, or a long time since
// the last message. We allow the message.
if (!data->time || ((now - data->time) > time_delay))
{ {
data->time = now; data->time = now;
data->count = 0; data->count = 1;
return 0; return 0;
} }
if ((now - data->time) > time_delay) // increase hit count
{
data->time = now;
data->count = 0;
return 0;
}
if (data->count <= max_count)
{
data->count++; data->count++;
return 0;
}
// did we overflow the limits yet?
if (data->count < max_count)
return 0;
// if we continue sending spam messages we extend the flood interval
// based on the last message.
data->time = now;
return 1; return 1;
} }