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