3127
|
1 /* --------------------------------------------------------------------------
|
|
2 *
|
|
3 * License
|
|
4 *
|
|
5 * The contents of this file are subject to the Jabber Open Source License
|
|
6 * Version 1.0 (the "JOSL"). You may not copy or use this file, in either
|
|
7 * source code or executable form, except in compliance with the JOSL. You
|
|
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at
|
|
9 * http://www.opensource.org/.
|
2086
|
10 *
|
3127
|
11 * Software distributed under the JOSL is distributed on an "AS IS" basis,
|
|
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL
|
|
13 * for the specific language governing rights and limitations under the
|
|
14 * JOSL.
|
|
15 *
|
|
16 * Copyrights
|
|
17 *
|
|
18 * Portions created by or assigned to Jabber.com, Inc. are
|
|
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact
|
|
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
|
2086
|
21 *
|
3127
|
22 * Portions Copyright (c) 1998-1999 Jeremie Miller.
|
|
23 *
|
|
24 * Acknowledgements
|
|
25 *
|
|
26 * Special thanks to the Jabber Open Source Contributors for their
|
|
27 * suggestions and support of Jabber.
|
|
28 *
|
|
29 * Alternatively, the contents of this file may be used under the terms of the
|
|
30 * GNU General Public License Version 2 or later (the "GPL"), in which case
|
|
31 * the provisions of the GPL are applicable instead of those above. If you
|
|
32 * wish to allow use of your version of this file only under the terms of the
|
|
33 * GPL and not to allow others to use your version of this file under the JOSL,
|
|
34 * indicate your decision by deleting the provisions above and replace them
|
|
35 * with the notice and other provisions required by the GPL. If you do not
|
|
36 * delete the provisions above, a recipient may use your version of this file
|
|
37 * under either the JOSL or the GPL.
|
|
38 *
|
|
39 *
|
|
40 * --------------------------------------------------------------------------*/
|
2086
|
41
|
3127
|
42 #include "lib.h"
|
2086
|
43
|
|
44 jlimit jlimit_new(int maxt, int maxp)
|
|
45 {
|
|
46 pool p;
|
|
47 jlimit r;
|
|
48
|
|
49 p = pool_new();
|
|
50 r = pmalloc(p,sizeof(_jlimit));
|
|
51 r->key = NULL;
|
|
52 r->start = r->points = 0;
|
|
53 r->maxt = maxt;
|
|
54 r->maxp = maxp;
|
|
55 r->p = p;
|
|
56
|
|
57 return r;
|
|
58 }
|
|
59
|
|
60 void jlimit_free(jlimit r)
|
|
61 {
|
|
62 if(r != NULL)
|
|
63 {
|
|
64 if(r->key != NULL) free(r->key);
|
|
65 pool_free(r->p);
|
|
66 }
|
|
67 }
|
|
68
|
|
69 int jlimit_check(jlimit r, char *key, int points)
|
|
70 {
|
|
71 int now = time(NULL);
|
|
72
|
|
73 if(r == NULL) return 0;
|
|
74
|
|
75 /* make sure we didn't go over the time frame or get a null/new key */
|
|
76 if((now - r->start) > r->maxt || key == NULL || j_strcmp(key,r->key) != 0)
|
|
77 { /* start a new key */
|
|
78 free(r->key);
|
|
79 if(key != NULL)
|
|
80 /* We use strdup instead of pstrdup since r->key needs to be free'd before
|
|
81 and more often than the rest of the rlimit structure */
|
|
82 r->key = strdup(key);
|
|
83 else
|
|
84 r->key = NULL;
|
|
85 r->start = now;
|
|
86 r->points = 0;
|
|
87 }
|
|
88
|
|
89 r->points += points;
|
|
90
|
|
91 /* if we're within the time frame and over the point limit */
|
|
92 if(r->points > r->maxp && (now - r->start) < r->maxt)
|
|
93 {
|
|
94 return 1; /* we don't reset the rate here, so that it remains rated until the time runs out */
|
|
95 }
|
|
96
|
|
97 return 0;
|
|
98 }
|