2 * IRC - Internet Relay Chat, ircd/whowas.c
3 * Copyright (C) 1990 Markku Savela
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)
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.
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.
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.
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
29 * --- comstud --- 5th August 1997
30 * Fixed for Undernet..
32 * --- Run --- 27th August 1997
33 * Speeded up the code, added comments.
40 #include "ircd_alloc.h"
41 #include "ircd_chattr.h"
42 #include "ircd_string.h"
57 static struct Whowas whowas[NICKNAMEHISTORYLENGTH];
58 static struct Whowas* whowas_next = whowas;
59 struct Whowas* whowashash[WW_MAX];
62 * Since the introduction of numeric nicks (at least for upstream messages,
63 * like MODE +o <nick>, KICK #chan <nick>, KILL <nick> etc), there is no
64 * real important reason for a nick history anymore.
65 * Nevertheless, there are two reason why we might want to keep it:
66 * 1) The /WHOWAS command, which is often usefull to catch harrashing
67 * users or abusers in general.
68 * 2) Clients still use the normal nicks in the client-server protocol,
69 * and it might be considered a nice feature that here we still have
71 * Note however that BOTH reasons make it redundant to keep a whowas history
72 * for users that split off.
74 * The rewrite of comstud was many to safe cpu during net.breaks and therefore
75 * a bit redundant imho (Run).
77 * But - it was written anyway. So lets look at the structure of the
80 * We still have a static table of 'struct Whowas' structures in which we add
81 * new nicks (plus info) as in a rotating buffer. We keep a global pointer
82 * `whowas_next' that points to the next entry to be overwritten - or to
83 * the oldest entry in the table (which is the same).
85 * Each entry keeps pointers for two doubly linked lists (thus four pointers):
86 * A list of the entries that have the same hash value ('hashv list'), and
87 * a list of the entries that have the same online pointer (`online list').
88 * Note that the last list (pointers) is only updated as long as online points
89 * to the corresponding client: As soon as the client signs off, this list
90 * is not anymore maintained (and hopefully not used anymore either ;).
92 * So now we have two ways of accessing this database:
93 * 1) Given a <nick> we can calculate a hashv and then whowashash[hashv] will
94 * point to the start of the 'hash list': all entries with the same hashv.
95 * We'll have to search this list to find the entry with the correct <nick>.
96 * Once we found the correct whowas entry, we have a pointer to the
97 * corresponding client - if still online - for nich chasing purposes.
98 * Note that the same nick can occur multiple times in the whowas history,
99 * each of these having the same hash value of course. While a /WHOWAS on
100 * just a nick will return all entries, nick chasing will only find the
101 * first in the list. Because new entries are added at the start of the
102 * 'hash list' we will always find the youngest entry, which is what we want.
103 * 2) Given an online client we have a pointer to the first whowas entry
104 * of the linked list of whowas entries that all belong to this client.
105 * We ONLY need this to reset all `online' pointers when this client
110 * Note that following:
112 * a) We *only* (need to) change the 'hash list' and the 'online' list
114 * b) There we always ADD an entry to the BEGINNING of the 'hash list'
115 * and the 'online list': *new* entries are at the start of the lists.
116 * The oldest entries are at the end of the lists.
117 * c) We always REMOVE the oldest entry we have (whowas_next), this means
118 * that this is always an entry that is at the *end* of the 'hash list'
119 * and 'online list' that it is a part of: the next pointer will
121 * d) The previous pointer is *only* used to update the next pointer of the
122 * previous entry, therefore we could better use a pointer to this
123 * next pointer: That is faster - saves us a 'if' test (it will never be
124 * NULL because the last added entry will point to the pointer that
125 * points to the start of the list) and we won't need special code to
126 * update the list start pointers.
128 * I incorporated these considerations into the code below.
134 struct Whowas *newww;
135 struct Whowas *oldww;
138 #define WHOWAS_UNUSED ((unsigned int)-1)
143 * Add a client (cptr) that just changed nick (still_on == true), or
144 * just signed off (still_on == false) to the `whowas' table.
146 * If the entry used was already in use, then this entry is
149 void add_history(struct Client *cptr, int still_on)
152 ww.newww = whowas_next;
154 /* If this entry has already been used, remove it from the lists */
155 if (ww.newww->hashv != WHOWAS_UNUSED)
157 if (ww.oldww->online) /* No need to update cnext/cprev when offline! */
159 /* Remove ww.oldww from the linked list with the same `online' pointers */
160 *ww.oldww->cprevnextp = ww.oldww->cnext;
162 assert(0 == ww.oldww->cnext);
165 /* Remove ww.oldww from the linked list with the same `hashv' */
166 *ww.oldww->hprevnextp = ww.oldww->hnext;
168 assert(0 == ww.oldww->hnext);
171 MyFree(ww.oldww->name);
172 if (ww.oldww->username)
173 MyFree(ww.oldww->username);
174 if (ww.oldww->hostname)
175 MyFree(ww.oldww->hostname);
176 if (ww.oldww->servername)
177 MyFree(ww.oldww->servername);
178 if (ww.oldww->realname)
179 MyFree(ww.oldww->realname);
181 MyFree(ww.oldww->away);
184 /* Initialize aWhoWas struct `newww' */
185 ww.newww->hashv = hash_whowas_name(cptr->name);
186 ww.newww->logoff = CurrentTime;
187 DupString(ww.newww->name, cptr->name);
188 DupString(ww.newww->username, cptr->user->username);
189 DupString(ww.newww->hostname, cptr->user->host);
190 /* Should be changed to server numeric */
191 DupString(ww.newww->servername, cptr->user->server->name);
192 DupString(ww.newww->realname, cptr->info);
193 if (cptr->user->away)
194 DupString(ww.newww->away, cptr->user->away);
196 ww.newww->away = NULL;
198 /* Update/initialize online/cnext/cprev: */
199 if (still_on) /* User just changed nicknames */
201 ww.newww->online = cptr;
202 /* Add struct Whowas struct `newww' to start of 'online list': */
203 if ((ww.newww->cnext = cptr->whowas))
204 ww.newww->cnext->cprevnextp = &ww.newww->cnext;
205 ww.newww->cprevnextp = &cptr->whowas;
206 cptr->whowas = ww.newww;
208 else /* User quitting */
209 ww.newww->online = NULL;
211 /* Add struct Whowas struct `newww' to start of 'hashv list': */
212 if ((ww.newww->hnext = whowashash[ww.newww->hashv]))
213 ww.newww->hnext->hprevnextp = &ww.newww->hnext;
214 ww.newww->hprevnextp = &whowashash[ww.newww->hashv];
215 whowashash[ww.newww->hashv] = ww.newww;
217 /* Advance `whowas_next' to next entry in the `whowas' table: */
218 if (++whowas_next == &whowas[NICKNAMEHISTORYLENGTH])
219 whowas_next = whowas;
225 * Client `cptr' signed off: Set all `online' pointers
226 * corresponding to this client to NULL.
228 void off_history(const struct Client *cptr)
232 for (temp = cptr->whowas; temp; temp = temp->cnext)
239 * Return a pointer to a client that had nick `nick' not more then
240 * `timelimit' seconds ago, if still on line. Otherwise return NULL.
242 * This function is used for "nick chasing"; since the use of numeric
243 * nicks for "upstream" messages in ircu2.10, this is only used for
244 * looking up non-existing nicks in client->server messages.
246 struct Client *get_history(const char *nick, time_t timelimit)
248 struct Whowas *temp = whowashash[hash_whowas_name(nick)];
249 timelimit = CurrentTime - timelimit;
251 for (; temp; temp = temp->hnext)
252 if (0 == ircd_strcmp(nick, temp->name) && temp->logoff > timelimit)
258 void count_whowas_memory(int *wwu, size_t *wwum, int *wwa, size_t *wwam)
271 for (i = 0, tmp = whowas; i < NICKNAMEHISTORYLENGTH; i++, tmp++) {
272 if (tmp->hashv != WHOWAS_UNUSED) {
274 um += (strlen(tmp->name) + 1);
275 um += (strlen(tmp->username) + 1);
276 um += (strlen(tmp->hostname) + 1);
277 um += (strlen(tmp->servername) + 1);
280 am += (strlen(tmp->away) + 1);
291 void initwhowas(void)
295 for (i = 0; i < NICKNAMEHISTORYLENGTH; i++)
296 whowas[i].hashv = WHOWAS_UNUSED;
299 unsigned int hash_whowas_name(const char *name)
301 unsigned int hash = 0;
302 unsigned int hash2 = 0;
307 lower = ToLower(*name);
308 hash = (hash << 1) + lower;
309 hash2 = (hash2 >> 1) + lower;
313 return ((hash & WW_MAX_INITIAL_MASK) << BITS_PER_COL) +
314 (hash2 & BITS_PER_COL_MASK);