2086
|
1 /*
|
|
2 * This program is free software; you can redistribute it and/or modify
|
|
3 * it under the terms of the GNU General Public License as published by
|
|
4 * the Free Software Foundation; either version 2 of the License, or
|
|
5 * (at your option) any later version.
|
|
6 *
|
|
7 * This program is distributed in the hope that it will be useful,
|
|
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 * GNU General Public License for more details.
|
|
11 *
|
|
12 * You should have received a copy of the GNU General Public License
|
|
13 * along with this program; if not, write to the Free Software
|
|
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
15 *
|
|
16 * Jabber
|
|
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
|
|
18 */
|
|
19
|
|
20
|
|
21 #include "jabber.h"
|
|
22
|
|
23 jlimit jlimit_new(int maxt, int maxp)
|
|
24 {
|
|
25 pool p;
|
|
26 jlimit r;
|
|
27
|
|
28 p = pool_new();
|
|
29 r = pmalloc(p,sizeof(_jlimit));
|
|
30 r->key = NULL;
|
|
31 r->start = r->points = 0;
|
|
32 r->maxt = maxt;
|
|
33 r->maxp = maxp;
|
|
34 r->p = p;
|
|
35
|
|
36 return r;
|
|
37 }
|
|
38
|
|
39 void jlimit_free(jlimit r)
|
|
40 {
|
|
41 if(r != NULL)
|
|
42 {
|
|
43 if(r->key != NULL) free(r->key);
|
|
44 pool_free(r->p);
|
|
45 }
|
|
46 }
|
|
47
|
|
48 int jlimit_check(jlimit r, char *key, int points)
|
|
49 {
|
|
50 int now = time(NULL);
|
|
51
|
|
52 if(r == NULL) return 0;
|
|
53
|
|
54 /* make sure we didn't go over the time frame or get a null/new key */
|
|
55 if((now - r->start) > r->maxt || key == NULL || j_strcmp(key,r->key) != 0)
|
|
56 { /* start a new key */
|
|
57 free(r->key);
|
|
58 if(key != NULL)
|
|
59 /* We use strdup instead of pstrdup since r->key needs to be free'd before
|
|
60 and more often than the rest of the rlimit structure */
|
|
61 r->key = strdup(key);
|
|
62 else
|
|
63 r->key = NULL;
|
|
64 r->start = now;
|
|
65 r->points = 0;
|
|
66 }
|
|
67
|
|
68 r->points += points;
|
|
69
|
|
70 /* if we're within the time frame and over the point limit */
|
|
71 if(r->points > r->maxp && (now - r->start) < r->maxt)
|
|
72 {
|
|
73 return 1; /* we don't reset the rate here, so that it remains rated until the time runs out */
|
|
74 }
|
|
75
|
|
76 return 0;
|
|
77 }
|