581a3ce8091760edba4313c687d7721427b76c47
[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_reply.h"
29 #include "ircd_string.h"
30 #include "listener.h"
31 #include "match.h"
32 #include "numeric.h"
33 #include "res.h"
34 #include "s_bsd.h"
35 #include "s_conf.h"
36 #include "s_debug.h"
37 #include "s_misc.h"
38 #include "s_user.h"
39 #include "send.h"
40 #include "struct.h"
41 #include "support.h"
42 #include "whowas.h"
43
44 #include <assert.h>
45 #include <stddef.h>  /* offsetof */
46 #include <unistd.h>  /* close */
47 #include <string.h>
48
49 #ifdef DEBUGMODE
50 static struct liststats {
51   int inuse;
52 } clients, connections, users, servs, links;
53 #endif
54
55 static unsigned int clientAllocCount;
56 static struct Client* clientFreeList;
57
58 static unsigned int connectionAllocCount;
59 static struct Connection* connectionFreeList;
60
61 static unsigned int slinkAllocCount;
62 static struct SLink* slinkFreeList;
63
64 void init_list(void)
65 {
66   struct Client* cptr;
67   struct Connection* con;
68   int i;
69   /*
70    * pre-allocate MAXCONNECTIONS clients and connections
71    */
72   for (i = 0; i < MAXCONNECTIONS; ++i) {
73     cptr = (struct Client*) MyMalloc(sizeof(struct Client));
74     cli_next(cptr) = clientFreeList;
75     clientFreeList = cptr;
76     ++clientAllocCount;
77
78     con = (struct Connection*) MyMalloc(sizeof(struct Connection));
79     con_next(con) = connectionFreeList;
80     connectionFreeList = con;
81     ++connectionAllocCount;
82   }
83
84 #ifdef DEBUGMODE
85   memset(&clients, 0, sizeof(clients));
86   memset(&connections, 0, sizeof(connections));
87   memset(&users, 0, sizeof(users));
88   memset(&servs, 0, sizeof(servs));
89   memset(&links, 0, sizeof(links));
90 #endif
91 }
92
93 static struct Client* alloc_client(void)
94 {
95   struct Client* cptr = clientFreeList;
96
97   if (!cptr) {
98     cptr = (struct Client*) MyMalloc(sizeof(struct Client));
99     ++clientAllocCount;
100   } else
101     clientFreeList = cli_next(cptr);
102
103 #ifdef DEBUGMODE
104   clients.inuse++;
105 #endif
106
107   memset(cptr, 0, sizeof(struct Client));
108
109   return cptr;
110 }
111
112 static void dealloc_client(struct Client* cptr)
113 {
114 #ifdef DEBUGMODE
115   --clients.inuse;
116 #endif
117
118   cli_next(cptr) = clientFreeList;
119   clientFreeList = cptr;
120 }
121
122 static struct Connection* alloc_connection(void)
123 {
124   struct Connection* con = connectionFreeList;
125
126   if (!con) {
127     con = (struct Connection*) MyMalloc(sizeof(struct Connection));
128     ++connectionAllocCount;
129   } else
130     connectionFreeList = con_next(con);
131
132 #ifdef DEBUGMODE
133   connections.inuse++;
134 #endif
135
136   memset(con, 0, sizeof(struct Connection));
137
138   return con;
139 }
140
141 static void dealloc_connection(struct Connection* con)
142 {
143   if (con_dns_reply(con))
144     --(con_dns_reply(con)->ref_count);
145   if (-1 < con_fd(con))
146     close(con_fd(con));
147   MsgQClear(&(con_sendQ(con)));
148   client_drop_sendq(con);
149   DBufClear(&(con_recvQ(con)));
150   if (con_listener(con))
151     release_listener(con_listener(con));
152
153 #ifdef DEBUGMODE
154   --connections.inuse;
155 #endif
156
157   con_next(con) = connectionFreeList;
158   connectionFreeList = con;
159 }
160
161 /*
162  * Create a new struct Client structure and set it to initial state.
163  *
164  *   from == NULL,   create local client (a client connected to a socket).
165  *
166  *   from != NULL,   create remote client (behind a socket associated with
167  *                   the client defined by 'from').
168  *                   ('from' is a local client!!).
169  */
170 struct Client* make_client(struct Client *from, int status)
171 {
172   struct Client* cptr = 0;
173   struct Connection* con = 0;
174
175   cptr = alloc_client();
176
177   assert(0 != cptr);
178
179   if (!from) { /* local client, allocate a struct Connection */
180     con = alloc_connection();
181
182     assert(0 != con);
183
184     con_fd(con) = -1; /* initialize struct Connection */
185     con_nextnick(con) = CurrentTime - NICK_DELAY;
186     con_nexttarget(con) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
187     con_handler(con) = UNREGISTERED_HANDLER;
188     con_client(con) = cptr;
189
190     cli_local(cptr) = 1; /* Set certain fields of the struct Client */
191     cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
192     cli_lastnick(cptr) = TStime();
193   } else
194     con = cli_connect(from); /* use 'from's connection */
195
196   assert(0 != con);
197
198   cli_connect(cptr) = con; /* set the connection and other fields */
199   cli_status(cptr) = status;
200   cli_hnext(cptr) = cptr;
201   strcpy(cli_username(cptr), "unknown");
202
203   return cptr;
204 }
205
206 void free_client(struct Client* cptr)
207 {
208   if (!cptr)
209     return;
210   /*
211    * forget to remove the client from the hash table?
212    */
213   assert(cli_hnext(cptr) == cptr);
214
215   if (cli_from(cptr) == cptr) /* in other words, we're local */
216     dealloc_connection(cli_connect(cptr)); /* deallocate the connection... */
217   dealloc_client(cptr); /* deallocate the client */
218 }
219
220 struct Server *make_server(struct Client *cptr)
221 {
222   struct Server *serv = cli_serv(cptr);
223
224   if (!serv)
225   {
226     serv = (struct Server*) MyMalloc(sizeof(struct Server));
227     assert(0 != serv);
228     memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
229 #ifdef  DEBUGMODE
230     servs.inuse++;
231 #endif
232     cli_serv(cptr) = serv;
233     cli_serv(cptr)->lag = 60000;
234     *serv->by = '\0';
235     DupString(serv->last_error_msg, "<>");      /* String must be non-empty */
236   }
237   return cli_serv(cptr);
238 }
239
240 /*
241  * Taken the code from ExitOneClient() for this and placed it here.
242  * - avalon
243  */
244 void remove_client_from_list(struct Client *cptr)
245 {
246   if (cli_prev(cptr))
247     cli_next(cli_prev(cptr)) = cli_next(cptr);
248   else {
249     GlobalClientList = cli_next(cptr);
250     cli_prev(GlobalClientList) = 0;
251   }
252   if (cli_next(cptr))
253     cli_prev(cli_next(cptr)) = cli_prev(cptr);
254
255   cli_next(cptr) = cli_prev(cptr) = 0;
256
257   if (IsUser(cptr) && cli_user(cptr)) {
258     add_history(cptr, 0);
259     off_history(cptr);
260   }
261   if (cli_user(cptr)) {
262     free_user(cli_user(cptr));
263     cli_user(cptr) = 0;
264   }
265
266   if (cli_serv(cptr)) {
267     if (cli_serv(cptr)->user) {
268       free_user(cli_serv(cptr)->user);
269       cli_serv(cptr)->user = 0;
270     }
271     if (cli_serv(cptr)->client_list)
272       MyFree(cli_serv(cptr)->client_list);
273     MyFree(cli_serv(cptr)->last_error_msg);
274     MyFree(cli_serv(cptr));
275 #ifdef  DEBUGMODE
276     --servs.inuse;
277 #endif
278   }
279   free_client(cptr);
280 }
281
282 /*
283  * Although only a small routine, it appears in a number of places
284  * as a collection of a few lines...functions like this *should* be
285  * in this file, shouldnt they ?  after all, this is list.c, isn't it ?
286  * -avalon
287  */
288 void add_client_to_list(struct Client *cptr)
289 {
290   /*
291    * Since we always insert new clients to the top of the list,
292    * this should mean the "me" is the bottom most item in the list.
293    * XXX - don't always count on the above, things change
294    */
295   cli_prev(cptr) = 0;
296   cli_next(cptr) = GlobalClientList;
297   GlobalClientList = cptr;
298   if (cli_next(cptr))
299     cli_prev(cli_next(cptr)) = cptr;
300 }
301
302 /*
303  * Look for ptr in the linked listed pointed to by link.
304  */
305 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
306 {
307   if (ptr) {
308     while (lp) {
309       if (lp->value.cptr == ptr)
310         return (lp);
311       lp = lp->next;
312     }
313   }
314   return NULL;
315 }
316
317 struct SLink* make_link(void)
318 {
319   struct SLink* lp = slinkFreeList;
320   if (lp)
321     slinkFreeList = lp->next;
322   else {
323     lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
324     ++slinkAllocCount;
325   }
326   assert(0 != lp);
327 #ifdef  DEBUGMODE
328   links.inuse++;
329 #endif
330   return lp;
331 }
332
333 void free_link(struct SLink* lp)
334 {
335   if (lp) {
336     lp->next = slinkFreeList;
337     slinkFreeList = lp;
338   }
339 #ifdef  DEBUGMODE
340   links.inuse--;
341 #endif
342 }
343
344 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
345 {
346   struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
347   assert(0 != lp);
348   lp->value.cptr = cp;
349   lp->prev = 0;
350   if ((lp->next = *lpp))
351     lp->next->prev = lp;
352   *lpp = lp;
353   return lp;
354 }
355
356 void remove_dlink(struct DLink **lpp, struct DLink *lp)
357 {
358   assert(0 != lpp);
359   assert(0 != lp);
360
361   if (lp->prev) {
362     if ((lp->prev->next = lp->next))
363       lp->next->prev = lp->prev;
364   }
365   else if ((*lpp = lp->next))
366     lp->next->prev = NULL;
367   MyFree(lp);
368 }
369
370 #ifdef  DEBUGMODE
371 void send_listinfo(struct Client *cptr, char *name)
372 {
373   int inuse = 0, mem = 0, tmp = 0;
374
375   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Clients: inuse: %d(%d)",
376              clients.inuse, tmp = clients.inuse * sizeof(struct Client));
377   mem += tmp;
378   inuse += clients.inuse;
379   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, "Connections: inuse: %d(%d)",
380              connections.inuse,
381              tmp = connections.inuse * sizeof(struct Connection));
382   mem += tmp;
383   inuse += connections.inuse;
384   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
385              users.inuse, tmp = users.inuse * sizeof(struct User));
386   mem += tmp;
387   inuse += users.inuse;
388   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
389              servs.inuse, tmp = servs.inuse * sizeof(struct Server));
390   mem += tmp;
391   inuse += servs.inuse;
392   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
393              links.inuse, tmp = links.inuse * sizeof(struct SLink));
394   mem += tmp;
395   inuse += links.inuse;
396   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
397              GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
398   mem += tmp;
399   inuse += GlobalConfCount;
400   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",
401              inuse, mem);
402 }
403
404 #endif