Author: Ghostwolf <foxxe@wtfs.net>
[ircu2.10.12-pk.git] / ircd / list.c
1 /*
2  * IRC - Internet Relay Chat, ircd/list.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Finland
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 1, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * $Id$
21  */
22 #include "config.h"
23
24 #include "list.h"
25 #include "client.h"
26 #include "ircd.h"
27 #include "ircd_alloc.h"
28 #include "ircd_events.h"
29 #include "ircd_reply.h"
30 #include "ircd_string.h"
31 #include "listener.h"
32 #include "match.h"
33 #include "numeric.h"
34 #include "res.h"
35 #include "s_auth.h"
36 #include "s_bsd.h"
37 #include "s_conf.h"
38 #include "s_debug.h"
39 #include "s_misc.h"
40 #include "s_user.h"
41 #include "send.h"
42 #include "struct.h"
43 #include "support.h"
44 #include "whowas.h"
45
46 #include <assert.h>
47 #include <stddef.h>  /* offsetof */
48 #include <unistd.h>  /* close */
49 #include <string.h>
50
51 #ifdef DEBUGMODE
52 static struct liststats {
53   int inuse;
54 } clients, connections, users, servs, links;
55 #endif
56
57 static unsigned int clientAllocCount;
58 static struct Client* clientFreeList;
59
60 static unsigned int connectionAllocCount;
61 static struct Connection* connectionFreeList;
62
63 static unsigned int slinkAllocCount;
64 static struct SLink* slinkFreeList;
65
66 void init_list(void)
67 {
68   struct Client* cptr;
69   struct Connection* con;
70   int i;
71   /*
72    * pre-allocate MAXCONNECTIONS clients and connections
73    */
74   for (i = 0; i < MAXCONNECTIONS; ++i) {
75     cptr = (struct Client*) MyMalloc(sizeof(struct Client));
76     cli_next(cptr) = clientFreeList;
77     clientFreeList = cptr;
78     ++clientAllocCount;
79
80     con = (struct Connection*) MyMalloc(sizeof(struct Connection));
81     con_next(con) = connectionFreeList;
82     connectionFreeList = con;
83     ++connectionAllocCount;
84   }
85
86 #ifdef DEBUGMODE
87   memset(&clients, 0, sizeof(clients));
88   memset(&connections, 0, sizeof(connections));
89   memset(&users, 0, sizeof(users));
90   memset(&servs, 0, sizeof(servs));
91   memset(&links, 0, sizeof(links));
92 #endif
93 }
94
95 static struct Client* alloc_client(void)
96 {
97   struct Client* cptr = clientFreeList;
98
99   if (!cptr) {
100     cptr = (struct Client*) MyMalloc(sizeof(struct Client));
101     ++clientAllocCount;
102   } else
103     clientFreeList = cli_next(cptr);
104
105 #ifdef DEBUGMODE
106   clients.inuse++;
107 #endif
108
109   memset(cptr, 0, sizeof(struct Client));
110
111   return cptr;
112 }
113
114 static void dealloc_client(struct Client* cptr)
115 {
116   assert(cli_verify(cptr));
117   assert(0 == cli_connect(cptr));
118
119 #ifdef DEBUGMODE
120   --clients.inuse;
121 #endif
122
123   cli_next(cptr) = clientFreeList;
124   clientFreeList = cptr;
125
126   cli_magic(cptr) = 0;
127 }
128
129 static struct Connection* alloc_connection(void)
130 {
131   struct Connection* con = connectionFreeList;
132
133   if (!con) {
134     con = (struct Connection*) MyMalloc(sizeof(struct Connection));
135     ++connectionAllocCount;
136   } else
137     connectionFreeList = con_next(con);
138
139 #ifdef DEBUGMODE
140   connections.inuse++;
141 #endif
142
143   memset(con, 0, sizeof(struct Connection));
144   timer_init(&(con_proc(con)));
145
146   return con;
147 }
148
149 static void dealloc_connection(struct Connection* con)
150 {
151   assert(con_verify(con));
152   assert(!t_active(&(con_proc(con))));
153   assert(!t_onqueue(&(con_proc(con))));
154
155   Debug((DEBUG_LIST, "Deallocating connection %p", con));
156
157   if (con_dns_reply(con))
158     --(con_dns_reply(con)->ref_count);
159   if (-1 < con_fd(con))
160     close(con_fd(con));
161   MsgQClear(&(con_sendQ(con)));
162   client_drop_sendq(con);
163   DBufClear(&(con_recvQ(con)));
164   if (con_listener(con))
165     release_listener(con_listener(con));
166
167 #ifdef DEBUGMODE
168   --connections.inuse;
169 #endif
170
171   con_next(con) = connectionFreeList;
172   connectionFreeList = con;
173
174   con_magic(con) = 0;
175 }
176
177 /*
178  * Create a new struct Client structure and set it to initial state.
179  *
180  *   from == NULL,   create local client (a client connected to a socket).
181  *
182  *   from != NULL,   create remote client (behind a socket associated with
183  *                   the client defined by 'from').
184  *                   ('from' is a local client!!).
185  */
186 struct Client* make_client(struct Client *from, int status)
187 {
188   struct Client* cptr = 0;
189   struct Connection* con = 0;
190
191   assert(!from || cli_verify(from));
192
193   cptr = alloc_client();
194
195   assert(0 != cptr);
196   assert(!cli_magic(cptr));
197   assert(0 == from || 0 != cli_connect(from));
198
199   if (!from) { /* local client, allocate a struct Connection */
200     con = alloc_connection();
201
202     assert(0 != con);
203     assert(!con_magic(con));
204
205     con_magic(con) = CONNECTION_MAGIC;
206     con_fd(con) = -1; /* initialize struct Connection */
207     con_freeflag(con) = 0;
208     con_nextnick(con) = CurrentTime - NICK_DELAY;
209     con_nexttarget(con) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
210     con_handler(con) = UNREGISTERED_HANDLER;
211     con_client(con) = cptr;
212
213     cli_local(cptr) = 1; /* Set certain fields of the struct Client */
214     cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
215     cli_lastnick(cptr) = TStime();
216   } else
217     con = cli_connect(from); /* use 'from's connection */
218
219   assert(0 != con);
220   assert(con_verify(con));
221
222   cli_magic(cptr) = CLIENT_MAGIC;
223   cli_connect(cptr) = con; /* set the connection and other fields */
224   cli_status(cptr) = status;
225   cli_hnext(cptr) = cptr;
226   strcpy(cli_username(cptr), "unknown");
227
228   return cptr;
229 }
230
231 void free_connection(struct Connection* con)
232 {
233   if (!con)
234     return;
235
236   assert(con_verify(con));
237   assert(0 == con_client(con));
238
239   dealloc_connection(con); /* deallocate the connection */
240 }
241
242 void free_client(struct Client* cptr)
243 {
244   if (!cptr)
245     return;
246   /*
247    * forget to remove the client from the hash table?
248    */
249   assert(cli_verify(cptr));
250   assert(cli_hnext(cptr) == cptr);
251   /* or from linked list? */
252   assert(cli_next(cptr) == 0);
253   assert(cli_prev(cptr) == 0);
254
255   Debug((DEBUG_LIST, "Freeing client %s [%p], connection %p", cli_name(cptr),
256          cptr, cli_connect(cptr)));
257
258   if (cli_auth(cptr))
259     destroy_auth_request(cli_auth(cptr), 0);
260
261   /* Make sure we didn't magically get re-added to the list */
262   assert(cli_next(cptr) == 0);
263   assert(cli_prev(cptr) == 0);
264
265   if (cli_from(cptr) == cptr) { /* in other words, we're local */
266     cli_from(cptr) = 0;
267     /* timer must be marked as not active */
268     if (!cli_freeflag(cptr) && !t_active(&(cli_proc(cptr))))
269       dealloc_connection(cli_connect(cptr)); /* connection not open anymore */
270     else {
271       if (-1 < cli_fd(cptr) && cli_freeflag(cptr) & FREEFLAG_SOCKET)
272         socket_del(&(cli_socket(cptr))); /* queue a socket delete */
273       if (cli_freeflag(cptr) & FREEFLAG_TIMER)
274         timer_del(&(cli_proc(cptr))); /* queue a timer delete */
275     }
276   }
277
278   cli_connect(cptr) = 0;
279
280   dealloc_client(cptr); /* actually destroy the client */
281 }
282
283 struct Server *make_server(struct Client *cptr)
284 {
285   struct Server *serv = cli_serv(cptr);
286
287   assert(cli_verify(cptr));
288
289   if (!serv)
290   {
291     serv = (struct Server*) MyMalloc(sizeof(struct Server));
292     assert(0 != serv);
293     memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
294 #ifdef  DEBUGMODE
295     servs.inuse++;
296 #endif
297     cli_serv(cptr) = serv;
298     cli_serv(cptr)->lag = 60000;
299     *serv->by = '\0';
300     DupString(serv->last_error_msg, "<>");      /* String must be non-empty */
301   }
302   return cli_serv(cptr);
303 }
304
305 /*
306  * Taken the code from ExitOneClient() for this and placed it here.
307  * - avalon
308  */
309 void remove_client_from_list(struct Client *cptr)
310 {
311   assert(cli_verify(cptr));
312   assert(con_verify(cli_connect(cptr)));
313   assert(!cli_prev(cptr) || cli_verify(cli_prev(cptr)));
314   assert(!cli_next(cptr) || cli_verify(cli_next(cptr)));
315   assert(!IsMe(cptr));
316
317   /* Only try to remove cptr from the list if it IS in the list.
318    * cli_next(cptr) cannot be NULL here, as &me is always the end
319    * the list, and we never remove &me.    -GW 
320    */
321   if(cli_next(cptr))
322   {
323     if (cli_prev(cptr))
324       cli_next(cli_prev(cptr)) = cli_next(cptr);
325     else {
326       GlobalClientList = cli_next(cptr);
327       cli_prev(GlobalClientList) = 0;
328     }
329     cli_prev(cli_next(cptr)) = cli_prev(cptr);
330   }
331   cli_next(cptr) = cli_prev(cptr) = 0;
332
333   if (IsUser(cptr) && cli_user(cptr)) {
334     add_history(cptr, 0);
335     off_history(cptr);
336   }
337   if (cli_user(cptr)) {
338     free_user(cli_user(cptr));
339     cli_user(cptr) = 0;
340   }
341
342   if (cli_serv(cptr)) {
343     if (cli_serv(cptr)->user) {
344       free_user(cli_serv(cptr)->user);
345       cli_serv(cptr)->user = 0;
346     }
347     if (cli_serv(cptr)->client_list)
348       MyFree(cli_serv(cptr)->client_list);
349     MyFree(cli_serv(cptr)->last_error_msg);
350     MyFree(cli_serv(cptr));
351 #ifdef  DEBUGMODE
352     --servs.inuse;
353 #endif
354   }
355   free_client(cptr);
356 }
357
358 /*
359  * Although only a small routine, it appears in a number of places
360  * as a collection of a few lines...functions like this *should* be
361  * in this file, shouldnt they ?  after all, this is list.c, isn't it ?
362  * -avalon
363  */
364 void add_client_to_list(struct Client *cptr)
365 {
366   assert(cli_verify(cptr));
367   assert(cli_next(cptr) == 0);
368   assert(cli_prev(cptr) == 0);
369
370   /*
371    * Since we always insert new clients to the top of the list,
372    * this should mean the "me" is the bottom most item in the list.
373    * XXX - don't always count on the above, things change
374    */
375   cli_prev(cptr) = 0;
376   cli_next(cptr) = GlobalClientList;
377   GlobalClientList = cptr;
378   if (cli_next(cptr))
379     cli_prev(cli_next(cptr)) = cptr;
380 }
381
382 #if 0
383 /* WARNING: Major CPU sink!
384  *
385  * This is a debugging routine meant to verify the integrity of the client
386  * linked list.  It is meant to be comprehensive, to detect *any* corruption
387  * of that list.  This means that it will be majorly CPU-intensive, and
388  * should *only* be enabled on servers that have DEBUGMODE enabled.  Ignore
389  * this warning at your peril!
390  */
391 void verify_client_list(void)
392 {
393   struct Client *client, *prev = 0, *sentinel = 0;
394   extern unsigned int ircrandom(void);
395
396   for (client = GlobalClientList; client; client = cli_next(client)) {
397     /* Verify that this is a valid client, not a free'd one */
398     assert(cli_verify(client));
399     /* Verify that the list hasn't suddenly jumped around */
400     assert(cli_prev(client) == prev);
401     /* Verify that the list hasn't become circular */
402     assert(cli_next(client) != GlobalClientList);
403     assert(!sentinel || client != sentinel);
404
405     prev = client; /* Remember what should preceed us */
406     if (!(ircrandom() % 50)) /* probabilistic loop detector */
407       sentinel = client;
408   }
409 }
410 #endif /* DEBUGMODE */
411
412 /*
413  * Look for ptr in the linked listed pointed to by link.
414  */
415 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
416 {
417   if (ptr) {
418     while (lp) {
419       if (lp->value.cptr == ptr)
420         return (lp);
421       lp = lp->next;
422     }
423   }
424   return NULL;
425 }
426
427 struct SLink* make_link(void)
428 {
429   struct SLink* lp = slinkFreeList;
430   if (lp)
431     slinkFreeList = lp->next;
432   else {
433     lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
434     ++slinkAllocCount;
435   }
436   assert(0 != lp);
437 #ifdef  DEBUGMODE
438   links.inuse++;
439 #endif
440   return lp;
441 }
442
443 void free_link(struct SLink* lp)
444 {
445   if (lp) {
446     lp->next = slinkFreeList;
447     slinkFreeList = lp;
448   }
449 #ifdef  DEBUGMODE
450   links.inuse--;
451 #endif
452 }
453
454 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
455 {
456   struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
457   assert(0 != lp);
458   lp->value.cptr = cp;
459   lp->prev = 0;
460   if ((lp->next = *lpp))
461     lp->next->prev = lp;
462   *lpp = lp;
463   return lp;
464 }
465
466 void remove_dlink(struct DLink **lpp, struct DLink *lp)
467 {
468   assert(0 != lpp);
469   assert(0 != lp);
470
471   if (lp->prev) {
472     if ((lp->prev->next = lp->next))
473       lp->next->prev = lp->prev;
474   }
475   else if ((*lpp = lp->next))
476     lp->next->prev = NULL;
477   MyFree(lp);
478 }
479
480 #ifdef  DEBUGMODE
481 void send_listinfo(struct Client *cptr, char *name)
482 {
483   int inuse = 0, mem = 0, tmp = 0;
484
485   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Clients: inuse: %d(%d)",
486              clients.inuse, tmp = clients.inuse * sizeof(struct Client));
487   mem += tmp;
488   inuse += clients.inuse;
489   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, "Connections: inuse: %d(%d)",
490              connections.inuse,
491              tmp = connections.inuse * sizeof(struct Connection));
492   mem += tmp;
493   inuse += connections.inuse;
494   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
495              users.inuse, tmp = users.inuse * sizeof(struct User));
496   mem += tmp;
497   inuse += users.inuse;
498   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
499              servs.inuse, tmp = servs.inuse * sizeof(struct Server));
500   mem += tmp;
501   inuse += servs.inuse;
502   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
503              links.inuse, tmp = links.inuse * sizeof(struct SLink));
504   mem += tmp;
505   inuse += links.inuse;
506   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
507              GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
508   mem += tmp;
509   inuse += GlobalConfCount;
510   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",
511              inuse, mem);
512 }
513
514 #endif