comparison mcabber/libjabber/rate.c @ 417:c3ae9251c197

Sync libjabber with upstream Sync with jabberd-1.4.4.
author Mikael Berthe <mikael@lilotux.net>
date Thu, 01 Sep 2005 23:29:21 +0200
parents ec86d759ed54
children
comparison
equal deleted inserted replaced
416:48e7808c4191 417:c3ae9251c197
11 * 11 *
12 * You should have received a copy of the GNU General Public License 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 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. 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 * 15 *
16 * Jabber 16 * Copyrights
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ 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/.
21 *
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 */
30
31 /**
32 * create a new instance of jlimit that is used to limit events
33 *
34 * limit the events to maxp points per maxt seconds
35 *
36 * @param maxt time interval (in seconds) after which the points are cleared
37 * @param maxp maximum number of points available for the time interval given in maxt
38 * @return new instance of jlimit (has to be freed with jlimit_free if not used anymore)
18 */ 39 */
19 40
20 #include "jabber.h" 41 #include "jabber.h"
21 42
22 jlimit jlimit_new(int maxt, int maxp) 43 jlimit jlimit_new(int maxt, int maxp)
33 r->p = p; 54 r->p = p;
34 55
35 return r; 56 return r;
36 } 57 }
37 58
59 /**
60 * free a jlimit instance
61 *
62 * @param r the jlimit instance that should be freed
63 */
38 void jlimit_free(jlimit r) 64 void jlimit_free(jlimit r)
39 { 65 {
40 if(r != NULL) 66 if(r != NULL)
41 { 67 {
42 if(r->key != NULL) free(r->key); 68 if(r->key != NULL) free(r->key);
43 pool_free(r->p); 69 pool_free(r->p);
44 } 70 }
45 } 71 }
46 72
73 /**
74 * update/check a key in a jlimit instance
75 *
76 * Each jlimit instance can track many limits (that have the same setup).
77 * The limit is selected by the key, which can be an IP address.
78 *
79 * @param r the jlimit instance
80 * @param key for which key the limit should be checked
81 * @param points how many points of the limit should be consumed
82 * @return 1 if limit reached, 0 if we are still within the rate limit
83 */
47 int jlimit_check(jlimit r, char *key, int points) 84 int jlimit_check(jlimit r, char *key, int points)
48 { 85 {
49 int now = time(NULL); 86 int now = time(NULL);
50 87
51 if(r == NULL) return 0; 88 if(r == NULL) return 0;
52 89
53 /* make sure we didn't go over the time frame or get a null/new key */ 90 /* make sure we didn't go over the time frame or get a null/new key */
54 if((now - r->start) > r->maxt || key == NULL || j_strcmp(key,r->key) != 0) 91 if((now - r->start) > r->maxt || key == NULL || j_strcmp(key,r->key) != 0)
55 { /* start a new key */ 92 { /* start a new key */
56 free(r->key); 93 free(r->key);
57 if(key != NULL) 94 if(key != NULL)
58 /* We use strdup instead of pstrdup since r->key needs to be free'd before 95 /* We use strdup instead of pstrdup since r->key needs to be free'd before
59 and more often than the rest of the rlimit structure */ 96 and more often than the rest of the rlimit structure */
60 r->key = strdup(key); 97 r->key = strdup(key);
61 else 98 else
62 r->key = NULL; 99 r->key = NULL;
63 r->start = now; 100 r->start = now;
64 r->points = 0; 101 r->points = 0;
65 } 102 }
66 103
67 r->points += points; 104 r->points += points;
68 105
69 /* if we're within the time frame and over the point limit */ 106 /* if we're within the time frame and over the point limit */
70 if(r->points > r->maxp && (now - r->start) < r->maxt) 107 if(r->points > r->maxp && (now - r->start) < r->maxt)
71 { 108 {
72 return 1; /* we don't reset the rate here, so that it remains rated until the time runs out */ 109 return 1; /* we don't reset the rate here, so that it remains rated until the time runs out */
73 } 110 }
74 111
75 return 0; 112 return 0;
76 } 113 }