Move check_if_ipmask() from support.* to match.*.
[ircu2.10.12-pk.git] / ircd / whowas.c
1 /*
2  * IRC - Internet Relay Chat, ircd/whowas.c
3  * Copyright (C) 1990 Markku Savela
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * --- avalon --- 6th April 1992
20  * rewritten to scrap linked lists and use a table of structures which
21  * is referenced like a circular loop. Should be faster and more efficient.
22  *
23  * --- comstud --- 25th March 1997
24  * Everything rewritten from scratch.  Avalon's code was bad.  My version
25  * is faster and more efficient.  No more hangs on /squits and you can
26  * safely raise NICKNAMEHISTORYLENGTH to a higher value without hurting
27  * performance.
28  *
29  * --- comstud --- 5th August 1997
30  * Fixed for Undernet..
31  *
32  * --- Run --- 27th August 1997
33  * Speeded up the code, added comments.
34  *
35  * $Id$
36  */
37 #include "config.h"
38
39 #include "whowas.h"
40 #include "client.h"
41 #include "ircd.h"
42 #include "ircd_alloc.h"
43 #include "ircd_chattr.h"
44 #include "ircd_features.h"
45 #include "ircd_string.h"
46 #include "list.h"
47 #include "numeric.h"
48 #include "s_debug.h"
49 #include "s_misc.h"
50 #include "s_user.h"
51 #include "send.h"
52 #include "struct.h"
53 #include "sys.h"
54 #include "msg.h"
55
56 #include <assert.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60
61 static struct {
62   struct Whowas *ww_list;       /* list of whowas structures */
63   struct Whowas *ww_tail;       /* tail of list for getting structures */
64   unsigned int   ww_alloc;      /* alloc count */
65 } wwList = { 0, 0, 0 };
66
67 struct Whowas* whowashash[WW_MAX];
68
69 /*
70  * Since the introduction of numeric nicks (at least for upstream messages,
71  * like MODE +o <nick>, KICK #chan <nick>, KILL <nick> etc), there is no
72  * real important reason for a nick history anymore.
73  * Nevertheless, there are two reason why we might want to keep it:
74  * 1) The /WHOWAS command, which is often usefull to catch harrashing
75  *    users or abusers in general.
76  * 2) Clients still use the normal nicks in the client-server protocol,
77  *    and it might be considered a nice feature that here we still have
78  *    nick chasing.
79  * Note however that BOTH reasons make it redundant to keep a whowas history
80  * for users that split off.
81  *
82  * The rewrite of comstud was many to safe cpu during net.breaks and therefore
83  * a bit redundant imho (Run).
84  *
85  * But - it was written anyway.  So lets look at the structure of the
86  * whowas history now:
87  *
88  * We still have a static table of 'struct Whowas' structures in which we add
89  * new nicks (plus info) as in a rotating buffer.  We keep a global pointer
90  * `whowas_next' that points to the next entry to be overwritten - or to
91  * the oldest entry in the table (which is the same).
92  *
93  * Each entry keeps pointers for two doubly linked lists (thus four pointers):
94  * A list of the entries that have the same hash value ('hashv list'), and
95  * a list of the entries that have the same online pointer (`online list').
96  * Note that the last list (pointers) is only updated as long as online points
97  * to the corresponding client: As soon as the client signs off, this list
98  * is not anymore maintained (and hopefully not used anymore either ;).
99  *
100  * So now we have two ways of accessing this database:
101  * 1) Given a <nick> we can calculate a hashv and then whowashash[hashv] will
102  *    point to the start of the 'hash list': all entries with the same hashv.
103  *    We'll have to search this list to find the entry with the correct <nick>.
104  *    Once we found the correct whowas entry, we have a pointer to the
105  *    corresponding client - if still online - for nich chasing purposes.
106  *    Note that the same nick can occur multiple times in the whowas history,
107  *    each of these having the same hash value of course.  While a /WHOWAS on
108  *    just a nick will return all entries, nick chasing will only find the
109  *    first in the list.  Because new entries are added at the start of the
110  *    'hash list' we will always find the youngest entry, which is what we want.
111  * 2) Given an online client we have a pointer to the first whowas entry
112  *    of the linked list of whowas entries that all belong to this client.
113  *    We ONLY need this to reset all `online' pointers when this client
114  *    signs off.
115  *
116  * 27/8/79:
117  *
118  * Note that following:
119  *
120  * a) We *only* (need to) change the 'hash list' and the 'online' list
121  *    in add_history().
122  * b) There we always ADD an entry to the BEGINNING of the 'hash list'
123  *    and the 'online list': *new* entries are at the start of the lists.
124  *    The oldest entries are at the end of the lists.
125  * c) We always REMOVE the oldest entry we have (whowas_next), this means
126  *    that this is always an entry that is at the *end* of the 'hash list'
127  *    and 'online list' that it is a part of: the next pointer will
128  *    always be NULL.
129  * d) The previous pointer is *only* used to update the next pointer of the
130  *    previous entry, therefore we could better use a pointer to this
131  *    next pointer: That is faster - saves us a 'if' test (it will never be
132  *    NULL because the last added entry will point to the pointer that
133  *    points to the start of the list) and we won't need special code to
134  *    update the list start pointers.
135  *
136  * I incorporated these considerations into the code below.
137  *
138  * --Run
139  */
140
141 /* whowas_clean()
142  *
143  * Clean up a whowas structure
144  */
145 static struct Whowas *
146 whowas_clean(struct Whowas *ww)
147 {
148   if (!ww)
149     return 0;
150
151   Debug((DEBUG_LIST, "Cleaning whowas structure for %s", ww->name));
152
153   if (ww->online) { /* unlink from client */
154     if (ww->cnext) /* shouldn't happen, but I'm not confident of that */
155       ww->cnext->cprevnextp = ww->cprevnextp;
156     *ww->cprevnextp = ww->cnext;
157   }
158
159   if (ww->hnext) /* now unlink from hash table */
160     ww->hnext->hprevnextp = ww->hprevnextp;
161   *ww->hprevnextp = ww->hnext;
162
163   if (ww->wnext) /* unlink from whowas linked list... */
164     ww->wnext->wprev = ww->wprev;
165   if (ww->wprev)
166     ww->wprev->wnext = ww->wnext;
167
168   if (wwList.ww_tail == ww) /* update tail pointer appropriately */
169     wwList.ww_tail = ww->wprev;
170
171   /* Free old info */
172   if (ww->name)
173     MyFree(ww->name);
174   if (ww->username)
175     MyFree(ww->username);
176   if (ww->hostname)
177     MyFree(ww->hostname);
178   if (ww->realhost)
179     MyFree(ww->realhost);
180   if (ww->servername)
181     MyFree(ww->servername);
182   if (ww->realname)
183     MyFree(ww->realname);
184   if (ww->away)
185     MyFree(ww->away);
186
187   return ww;
188 }
189
190 /* whowas_free()
191  *
192  * Free a struct Whowas...
193  */
194 static void
195 whowas_free(struct Whowas *ww)
196 {
197   if (!ww)
198     return;
199
200   Debug((DEBUG_LIST, "Destroying whowas structure for %s", ww->name));
201
202   whowas_clean(ww);
203   MyFree(ww);
204
205   wwList.ww_alloc--;
206 }
207
208 /* whowas_init()
209  *
210  * Initializes a given whowas structure
211  */
212 static struct Whowas *
213 whowas_init(struct Whowas *ww)
214 {
215   if (!ww)
216     return 0;
217
218   ww->hashv = 0;
219   ww->name = 0;
220   ww->username = 0;
221   ww->hostname = 0;
222   ww->realhost = 0;
223   ww->servername = 0;
224   ww->realname = 0;
225   ww->away = 0;
226   ww->logoff = 0;
227   ww->online = 0;
228   ww->hnext = 0;
229   ww->hprevnextp = 0;
230   ww->cnext = 0;
231   ww->cprevnextp = 0;
232   ww->wnext = 0;
233   ww->wprev = 0;
234
235   return ww;
236 }
237
238 /* whowas_alloc()
239  *
240  * Returns a whowas structure to use
241  */
242 static struct Whowas *
243 whowas_alloc(void)
244 {
245   if (wwList.ww_alloc >= feature_int(FEAT_NICKNAMEHISTORYLENGTH))
246     return whowas_init(whowas_clean(wwList.ww_tail));
247
248   wwList.ww_alloc++; /* going to allocate a new one... */
249   return whowas_init((struct Whowas *) MyMalloc(sizeof(struct Whowas)));
250 }
251
252 /* whowas_realloc()
253  *
254  * Prune whowas list
255  */
256 void
257 whowas_realloc(void)
258 {
259   Debug((DEBUG_LIST, "whowas_realloc() called with alloc count %d, "
260          "history length %d, tail pointer %p", wwList.ww_alloc,
261          feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwList.ww_tail));
262
263   while (wwList.ww_alloc > feature_int(FEAT_NICKNAMEHISTORYLENGTH)) {
264     if (!wwList.ww_tail) { /* list is empty... */
265       Debug((DEBUG_LIST, "whowas list emptied with alloc count %d",
266              wwList.ww_alloc));
267       return;
268     }
269
270     whowas_free(wwList.ww_tail); /* free oldest element of whowas list */
271   }
272 }
273
274 /*
275  * add_history
276  *
277  * Add a client (cptr) that just changed nick (still_on == true), or
278  * just signed off (still_on == false) to the `whowas' table.
279  *
280  * If the entry used was already in use, then this entry is
281  * freed (lost).
282  */
283 void add_history(struct Client *cptr, int still_on)
284 {
285   struct Whowas *ww;
286
287   if (!(ww = whowas_alloc()))
288     return; /* couldn't get a structure */
289
290   ww->hashv = hash_whowas_name(cli_name(cptr)); /* initialize struct */
291   ww->logoff = CurrentTime;
292   DupString(ww->name, cli_name(cptr));
293   DupString(ww->username, cli_user(cptr)->username);
294   DupString(ww->hostname, cli_user(cptr)->host);
295   if (HasHiddenHost(cptr))
296     DupString(ww->realhost, cli_user(cptr)->realhost);
297   DupString(ww->servername, cli_name(cli_user(cptr)->server));
298   DupString(ww->realname, cli_info(cptr));
299   if (cli_user(cptr)->away)
300     DupString(ww->away, cli_user(cptr)->away);
301
302   if (still_on) { /* user changed nicknames... */
303     ww->online = cptr;
304     if ((ww->cnext = cli_whowas(cptr)))
305       ww->cnext->cprevnextp = &ww->cnext;
306     ww->cprevnextp = &(cli_whowas(cptr));
307     cli_whowas(cptr) = ww;
308   } else /* user quit */
309     ww->online = 0;
310
311   /* link new whowas structure to list */
312   ww->wnext = wwList.ww_list;
313   if (wwList.ww_list)
314     wwList.ww_list->wprev = ww;
315   wwList.ww_list = ww;
316
317   if (!wwList.ww_tail) /* update the tail pointer... */
318     wwList.ww_tail = ww;
319
320   /* Now link it into the hash table */
321   if ((ww->hnext = whowashash[ww->hashv]))
322     ww->hnext->hprevnextp = &ww->hnext;
323   ww->hprevnextp = &whowashash[ww->hashv];
324   whowashash[ww->hashv] = ww;
325 }
326
327 /*
328  * off_history
329  *
330  * Client `cptr' signed off: Set all `online' pointers
331  * corresponding to this client to NULL.
332  */
333 void off_history(const struct Client *cptr)
334 {
335   struct Whowas *temp;
336
337   for (temp = cli_whowas(cptr); temp; temp = temp->cnext)
338     temp->online = NULL;
339 }
340
341 /*
342  * get_history
343  *
344  * Return a pointer to a client that had nick `nick' not more then
345  * `timelimit' seconds ago, if still on line.  Otherwise return NULL.
346  *
347  * This function is used for "nick chasing"; since the use of numeric
348  * nicks for "upstream" messages in ircu2.10, this is only used for
349  * looking up non-existing nicks in client->server messages.
350  */
351 struct Client *get_history(const char *nick, time_t timelimit)
352 {
353   struct Whowas *temp = whowashash[hash_whowas_name(nick)];
354   timelimit = CurrentTime - timelimit;
355
356   for (; temp; temp = temp->hnext)
357     if (0 == ircd_strcmp(nick, temp->name) && temp->logoff > timelimit)
358       return temp->online;
359
360   return NULL;
361 }
362
363 void count_whowas_memory(int *wwu, size_t *wwum, int *wwa, size_t *wwam)
364 {
365   struct Whowas *tmp;
366   int u = 0;
367   int a = 0;
368   size_t um = 0;
369   size_t am = 0;
370   assert(0 != wwu);
371   assert(0 != wwum);
372   assert(0 != wwa);
373   assert(0 != wwam);
374
375   for (tmp = wwList.ww_list; tmp; tmp = tmp->wnext) {
376     u++;
377     um += (strlen(tmp->name) + 1);
378     um += (strlen(tmp->username) + 1);
379     um += (strlen(tmp->hostname) + 1);
380     um += (strlen(tmp->servername) + 1);
381     if (tmp->away) {
382       a++;
383       am += (strlen(tmp->away) + 1);
384     }
385   }
386   *wwu = u;
387   *wwum = um;
388   *wwa = a;
389   *wwam = am;
390 }
391
392
393 void initwhowas(void)
394 {
395   int i;
396
397   for (i = 0; i < WW_MAX; i++)
398     whowashash[i] = 0;
399 }
400
401 unsigned int hash_whowas_name(const char *name)
402 {
403   unsigned int hash = 0;
404   unsigned int hash2 = 0;
405   unsigned char lower;
406
407   do
408   {
409     lower = ToLower(*name);
410     hash = (hash << 1) + lower;
411     hash2 = (hash2 >> 1) + lower;
412   }
413   while (*++name);
414
415   return ((hash & WW_MAX_INITIAL_MASK) << BITS_PER_COL) +
416       (hash2 & BITS_PER_COL_MASK);
417 }
418