813ccbca95cc53b1fab13527338d544f5d2d3081
[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   
158   if (con_dns_reply(con)) {
159     MyFree(con_dns_reply(con));
160     con_dns_reply(con) = 0;
161   }
162   if (-1 < con_fd(con))
163     close(con_fd(con));
164   MsgQClear(&(con_sendQ(con)));
165   client_drop_sendq(con);
166   DBufClear(&(con_recvQ(con)));
167   if (con_listener(con))
168     release_listener(con_listener(con));
169
170 #ifdef DEBUGMODE
171   --connections.inuse;
172 #endif
173
174   con_next(con) = connectionFreeList;
175   connectionFreeList = con;
176
177   con_magic(con) = 0;
178 }
179
180 /*
181  * Create a new struct Client structure and set it to initial state.
182  *
183  *   from == NULL,   create local client (a client connected to a socket).
184  *
185  *   from != NULL,   create remote client (behind a socket associated with
186  *                   the client defined by 'from').
187  *                   ('from' is a local client!!).
188  */
189 struct Client* make_client(struct Client *from, int status)
190 {
191   struct Client* cptr = 0;
192   struct Connection* con = 0;
193
194   assert(!from || cli_verify(from));
195
196   cptr = alloc_client();
197
198   assert(0 != cptr);
199   assert(!cli_magic(cptr));
200   assert(0 == from || 0 != cli_connect(from));
201
202   if (!from) { /* local client, allocate a struct Connection */
203     con = alloc_connection();
204
205     assert(0 != con);
206     assert(!con_magic(con));
207
208     con_magic(con) = CONNECTION_MAGIC;
209     con_fd(con) = -1; /* initialize struct Connection */
210     con_freeflag(con) = 0;
211     con_nextnick(con) = CurrentTime - NICK_DELAY;
212     con_nexttarget(con) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
213     con_handler(con) = UNREGISTERED_HANDLER;
214     con_client(con) = cptr;
215
216     cli_local(cptr) = 1; /* Set certain fields of the struct Client */
217     cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
218     cli_lastnick(cptr) = TStime();
219   } else
220     con = cli_connect(from); /* use 'from's connection */
221
222   assert(0 != con);
223   assert(con_verify(con));
224
225   cli_magic(cptr) = CLIENT_MAGIC;
226   cli_connect(cptr) = con; /* set the connection and other fields */
227   cli_status(cptr) = status;
228   cli_hnext(cptr) = cptr;
229   strcpy(cli_username(cptr), "unknown");
230
231   return cptr;
232 }
233
234 void free_connection(struct Connection* con)
235 {
236   if (!con)
237     return;
238
239   assert(con_verify(con));
240   assert(0 == con_client(con));
241
242   dealloc_connection(con); /* deallocate the connection */
243 }
244
245 void free_client(struct Client* cptr)
246 {
247   if (!cptr)
248     return;
249   /*
250    * forget to remove the client from the hash table?
251    */
252   assert(cli_verify(cptr));
253   assert(cli_hnext(cptr) == cptr);
254   /* or from linked list? */
255   assert(cli_next(cptr) == 0);
256   assert(cli_prev(cptr) == 0);
257
258   Debug((DEBUG_LIST, "Freeing client %s [%p], connection %p", cli_name(cptr),
259          cptr, cli_connect(cptr)));
260
261   if (cli_auth(cptr))
262     destroy_auth_request(cli_auth(cptr), 0);
263
264   /* Make sure we didn't magically get re-added to the list */
265   assert(cli_next(cptr) == 0);
266   assert(cli_prev(cptr) == 0);
267
268   if (cli_from(cptr) == cptr) { /* in other words, we're local */
269     cli_from(cptr) = 0;
270     /* timer must be marked as not active */
271     if (!cli_freeflag(cptr) && !t_active(&(cli_proc(cptr))))
272       dealloc_connection(cli_connect(cptr)); /* connection not open anymore */
273     else {
274       if (-1 < cli_fd(cptr) && cli_freeflag(cptr) & FREEFLAG_SOCKET)
275         socket_del(&(cli_socket(cptr))); /* queue a socket delete */
276       if (cli_freeflag(cptr) & FREEFLAG_TIMER)
277         timer_del(&(cli_proc(cptr))); /* queue a timer delete */
278     }
279   }
280
281   cli_connect(cptr) = 0;
282
283   dealloc_client(cptr); /* actually destroy the client */
284 }
285
286 struct Server *make_server(struct Client *cptr)
287 {
288   struct Server *serv = cli_serv(cptr);
289
290   assert(cli_verify(cptr));
291
292   if (!serv)
293   {
294     serv = (struct Server*) MyMalloc(sizeof(struct Server));
295     assert(0 != serv);
296     memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
297 #ifdef  DEBUGMODE
298     servs.inuse++;
299 #endif
300     cli_serv(cptr) = serv;
301     cli_serv(cptr)->lag = 60000;
302     *serv->by = '\0';
303     DupString(serv->last_error_msg, "<>");      /* String must be non-empty */
304   }
305   return cli_serv(cptr);
306 }
307
308 /*
309  * Taken the code from ExitOneClient() for this and placed it here.
310  * - avalon
311  */
312 void remove_client_from_list(struct Client *cptr)
313 {
314   assert(cli_verify(cptr));
315   assert(con_verify(cli_connect(cptr)));
316   assert(!cli_prev(cptr) || cli_verify(cli_prev(cptr)));
317   assert(!cli_next(cptr) || cli_verify(cli_next(cptr)));
318   assert(!IsMe(cptr));
319
320   /* Only try remove cptr from the list if it IS in the list.
321    * cli_next(cptr) cannot be NULL here, as &me is always the end
322    * the list, and we never remove &me.    -GW 
323    */
324   if(cli_next(cptr))
325   {
326     if (cli_prev(cptr))
327       cli_next(cli_prev(cptr)) = cli_next(cptr);
328     else {
329       GlobalClientList = cli_next(cptr);
330       cli_prev(GlobalClientList) = 0;
331     }
332     cli_prev(cli_next(cptr)) = cli_prev(cptr);
333   }
334   cli_next(cptr) = cli_prev(cptr) = 0;
335
336   if (IsUser(cptr) && cli_user(cptr)) {
337     add_history(cptr, 0);
338     off_history(cptr);
339   }
340   if (cli_user(cptr)) {
341     free_user(cli_user(cptr));
342     cli_user(cptr) = 0;
343   }
344
345   if (cli_serv(cptr)) {
346     if (cli_serv(cptr)->user) {
347       free_user(cli_serv(cptr)->user);
348       cli_serv(cptr)->user = 0;
349     }
350     if (cli_serv(cptr)->client_list)
351       MyFree(cli_serv(cptr)->client_list);
352     MyFree(cli_serv(cptr)->last_error_msg);
353     MyFree(cli_serv(cptr));
354 #ifdef  DEBUGMODE
355     --servs.inuse;
356 #endif
357   }
358   free_client(cptr);
359 }
360
361 /*
362  * Although only a small routine, it appears in a number of places
363  * as a collection of a few lines...functions like this *should* be
364  * in this file, shouldnt they ?  after all, this is list.c, isn't it ?
365  * -avalon
366  */
367 void add_client_to_list(struct Client *cptr)
368 {
369   assert(cli_verify(cptr));
370   assert(cli_next(cptr) == 0);
371   assert(cli_prev(cptr) == 0);
372
373   /*
374    * Since we always insert new clients to the top of the list,
375    * this should mean the "me" is the bottom most item in the list.
376    * XXX - don't always count on the above, things change
377    */
378   cli_prev(cptr) = 0;
379   cli_next(cptr) = GlobalClientList;
380   GlobalClientList = cptr;
381   if (cli_next(cptr))
382     cli_prev(cli_next(cptr)) = cptr;
383 }
384
385 #if 0
386 /* WARNING: Major CPU sink!
387  *
388  * This is a debugging routine meant to verify the integrity of the client
389  * linked list.  It is meant to be comprehensive, to detect *any* corruption
390  * of that list.  This means that it will be majorly CPU-intensive, and
391  * should *only* be enabled on servers that have DEBUGMODE enabled.  Ignore
392  * this warning at your peril!
393  */
394 void verify_client_list(void)
395 {
396   struct Client *client, *prev = 0, *sentinel = 0;
397   extern unsigned int ircrandom(void);
398
399   for (client = GlobalClientList; client; client = cli_next(client)) {
400     /* Verify that this is a valid client, not a free'd one */
401     assert(cli_verify(client));
402     /* Verify that the list hasn't suddenly jumped around */
403     assert(cli_prev(client) == prev);
404     /* Verify that the list hasn't become circular */
405     assert(cli_next(client) != GlobalClientList);
406     assert(!sentinel || client != sentinel);
407
408     prev = client; /* Remember what should preceed us */
409     if (!(ircrandom() % 50)) /* probabilistic loop detector */
410       sentinel = client;
411   }
412 }
413 #endif /* DEBUGMODE */
414
415 /*
416  * Look for ptr in the linked listed pointed to by link.
417  */
418 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
419 {
420   if (ptr) {
421     while (lp) {
422       if (lp->value.cptr == ptr)
423         return (lp);
424       lp = lp->next;
425     }
426   }
427   return NULL;
428 }
429
430 struct SLink* make_link(void)
431 {
432   struct SLink* lp = slinkFreeList;
433   if (lp)
434     slinkFreeList = lp->next;
435   else {
436     lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
437     ++slinkAllocCount;
438   }
439   assert(0 != lp);
440 #ifdef  DEBUGMODE
441   links.inuse++;
442 #endif
443   return lp;
444 }
445
446 void free_link(struct SLink* lp)
447 {
448   if (lp) {
449     lp->next = slinkFreeList;
450     slinkFreeList = lp;
451   }
452 #ifdef  DEBUGMODE
453   links.inuse--;
454 #endif
455 }
456
457 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
458 {
459   struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
460   assert(0 != lp);
461   lp->value.cptr = cp;
462   lp->prev = 0;
463   if ((lp->next = *lpp))
464     lp->next->prev = lp;
465   *lpp = lp;
466   return lp;
467 }
468
469 void remove_dlink(struct DLink **lpp, struct DLink *lp)
470 {
471   assert(0 != lpp);
472   assert(0 != lp);
473
474   if (lp->prev) {
475     if ((lp->prev->next = lp->next))
476       lp->next->prev = lp->prev;
477   }
478   else if ((*lpp = lp->next))
479     lp->next->prev = NULL;
480   MyFree(lp);
481 }
482
483 #ifdef  DEBUGMODE
484 void send_listinfo(struct Client *cptr, char *name)
485 {
486   int inuse = 0, mem = 0, tmp = 0;
487
488   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Clients: inuse: %d(%d)",
489              clients.inuse, tmp = clients.inuse * sizeof(struct Client));
490   mem += tmp;
491   inuse += clients.inuse;
492   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, "Connections: inuse: %d(%d)",
493              connections.inuse,
494              tmp = connections.inuse * sizeof(struct Connection));
495   mem += tmp;
496   inuse += connections.inuse;
497   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
498              users.inuse, tmp = users.inuse * sizeof(struct User));
499   mem += tmp;
500   inuse += users.inuse;
501   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
502              servs.inuse, tmp = servs.inuse * sizeof(struct Server));
503   mem += tmp;
504   inuse += servs.inuse;
505   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
506              links.inuse, tmp = links.inuse * sizeof(struct SLink));
507   mem += tmp;
508   inuse += links.inuse;
509   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
510              GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
511   mem += tmp;
512   inuse += GlobalConfCount;
513   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",
514              inuse, mem);
515 }
516
517 #endif