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