File: flood.c

package info (click to toggle)
tirc 1.2-10
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,388 kB
  • ctags: 1,909
  • sloc: ansic: 19,264; sh: 2,671; makefile: 222; awk: 14
file content (208 lines) | stat: -rw-r--r-- 4,452 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*-
 * tirc -- client for the Internet Relay Chat
 *
 *	Copyright (c) 1996, 1997, 1998
 *		Matthias Buelow.  All rights reserved.
 *
 *	See the file ``COPYRIGHT'' for the usage and distribution
 *	license and warranty disclaimer.
 */

#ifndef lint
static char rcsid[] = "$Id: flood.c,v 1.11 1998/02/02 03:41:29 mkb Exp $";
#endif

#include "tirc.h"

#ifdef	HAVE_STRING_H
#include <string.h>
#elif defined(HAVE_MEMORY_H)
#include <memory.h>
#endif

#include "compat.h"
#include "colour.h"

static struct f_cache *flnextentry __P((void));

extern char	ppre[], nick[];

static struct f_cache {
	int	fc_used;
	long	fc_loginhash;
	long	fc_machinehash;
	long	fc_contentshash;
	long	fc_time;
	char	*fc_source;
} msgring[FLRINGSIZE];

static int	flptr;

/*
 * Initialize the flood protection.
 */
void
flinit()
{
	int i;

	for (i = 0; i < FLRINGSIZE; i++) {
		if (msgring[i].fc_used && msgring[i].fc_source != NULL)
			free(msgring[i].fc_source);
		msgring[i].fc_used = 0;
		msgring[i].fc_source = NULL;
	}
	flptr = 0;
}

/*
 * Register a message to the buffer ring of last recently received
 * messages.  Don't register messages that come from the server
 * directly, only those from users.  You must specify which server
 * message parameter counts as the contents of the message.
 * After that, calculate statistics about the messages gathered so
 * far and act if flooding has been detected.
 * flregister returns 1 if the message should be ignored.
 */
int
flregister(sm, contents, type)
	struct servmsg *sm;
	int contents, type;
{
	struct f_cache *fc;
	char *excl, *at;
	int i, strict = 0;
	int acnt = 0, tcnt = 0, ccnt = 0;
	register long x, y;
	struct timeval tv;
	char nname[NICKSZ+1], imask[MSGSZ];
	char ftype[32];
	static long saved_time;
	long now;

	if (!check_conf(CONF_FLOODP))
		return 0;

	gettimeofday(&tv, NULL);
	now = tv.tv_sec;
	/*
	 * If for longer than 15 seconds we have received nothing, clear the
	 * message ring.
	 */
	if (now-saved_time > 15)
		flinit();
	saved_time = now;

	from_nick(sm, nname);
	if (!irc_strcmp(nname, nick))
		return 0;

	if (sm->sm_prefix == NULL ||
	    (excl = strchr(sm->sm_prefix, '!')) == NULL ||
	    (at = strchr(sm->sm_prefix, '@')) == NULL)
		return 0;
	
	fc = flnextentry();
	fc->fc_source = chkmem(Strdup(sm->sm_prefix));
	fc->fc_loginhash = elf_hash(excl+1);
	fc->fc_machinehash = elf_hash(at+1);
	fc->fc_contentshash = elf_hash(sm->sm_par[contents]); 
	fc->fc_time = now;
	fc->fc_used = 1;

	/*
	 * Now iterate through the ring and accumulate statistics.
	 */
	if (check_conf(CONF_FLSTRICT)) {
		strict = 1;
		y = fc->fc_machinehash;
	}
	else
		y = fc->fc_loginhash;

	for (i = 0; i < FLRINGSIZE; i++) {
		if (msgring[i].fc_used) {
			x = (strict) ? msgring[i].fc_machinehash
				: msgring[i].fc_loginhash;
			if (x == y) {
				acnt++;
				/* More than one line / 2 sec? */
				if (labs(msgring[i].fc_time - fc->fc_time) <=
					FLRINGSIZE)
					tcnt++;
				if (msgring[i].fc_contentshash ==
					fc->fc_contentshash)
					ccnt++;
			}
		}
	}

	if (acnt >= FLRINGSIZE/2) {
		/*
		 * Flooding detcted.
		 */
		if (tcnt >= FLRINGSIZE/2 || ccnt >= FLRINGSIZE/2) {
			switch (type) {
			case FL_PUBLIC:
				strcpy(ftype, "Public");
				break;
			case FL_MSG:
				strcpy(ftype, "Msg");
				break;
			case FL_NOTICE:
				strcpy(ftype, "Notice");
				break;
			case FL_INVITE:
				strcpy(ftype, "Invite");
				break;
			default:
				ftype[0] = 0;
				break;
			}

			iw_printf(COLI_WARN, "%s%s flooding detected from %s\n",
				ppre, ftype, fc->fc_source);
			/*
			 * Remove the abuser's occurence from the ring
			 * to avoid constant warnings.
			 */
			x = (strict) ? fc->fc_machinehash : fc->fc_loginhash;
				
			for (i = 0; i < FLRINGSIZE; i++) {
				x = (strict) ? msgring[i].fc_machinehash
					: msgring[i].fc_loginhash;

				if (msgring[i].fc_used && x == y) {
					msgring[i].fc_used = 0;
					if (msgring[i].fc_source != NULL)
						free(msgring[i].fc_source);
				}
			}

			/* Ignore offending source when configured. */
			if (check_conf(CONF_FLIGNORE)) {
				if (strict)
					sprintf(imask, "*!*%s", at);
				else
					sprintf(imask, "*%s", excl);
				add_ignore(imask, 3);
				return 1;
			}
		}
	}
	return 0;
}

static struct f_cache *
flnextentry()
{
	flptr = (flptr+1) % FLRINGSIZE;

	if (msgring[flptr].fc_used && msgring[flptr].fc_source != NULL) {
		free(msgring[flptr].fc_source);
		msgring[flptr].fc_source = NULL;
	}
	msgring[flptr].fc_used = 0;
	return &msgring[flptr];
}