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   DBufClear(&(con_recvQ(con)));
148   if (con_listener(con))
149     release_listener(con_listener(con));
150
151 #ifdef DEBUGMODE
152   --connections.inuse;
153 #endif
154
155   con_next(con) = connectionFreeList;
156   connectionFreeList = con;
157 }
158
159 /*
160  * Create a new struct Client structure and set it to initial state.
161  *
162  *   from == NULL,   create local client (a client connected to a socket).
163  *
164  *   from != NULL,   create remote client (behind a socket associated with
165  *                   the client defined by 'from').
166  *                   ('from' is a local client!!).
167  */
168 struct Client* make_client(struct Client *from, int status)
169 {
170   struct Client* cptr = 0;
171   struct Connection* con = 0;
172
173   cptr = alloc_client();
174
175   assert(0 != cptr);
176
177   if (!from) { /* local client, allocate a struct Connection */
178     con = alloc_connection();
179
180     assert(0 != con);
181
182     con_fd(con) = -1; /* initialize struct Connection */
183     con_nextnick(con) = CurrentTime - NICK_DELAY;
184     con_nexttarget(con) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
185     con_handler(con) = UNREGISTERED_HANDLER;
186     con_client(con) = cptr;
187
188     cli_local(cptr) = 1; /* Set certain fields of the struct Client */
189     cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
190     cli_lastnick(cptr) = TStime();
191   } else
192     con = cli_connect(from); /* use 'from's connection */
193
194   assert(0 != con);
195
196   cli_connect(cptr) = con; /* set the connection and other fields */
197   cli_status(cptr) = status;
198   cli_hnext(cptr) = cptr;
199   strcpy(cli_username(cptr), "unknown");
200
201   return cptr;
202 }
203
204 void free_client(struct Client* cptr)
205 {
206   if (!cptr)
207     return;
208   /*
209    * forget to remove the client from the hash table?
210    */
211   assert(cli_hnext(cptr) == cptr);
212
213   if (cli_from(cptr) == cptr) /* in other words, we're local */
214     dealloc_connection(cli_connect(cptr)); /* deallocate the connection... */
215   dealloc_client(cptr); /* deallocate the client */
216 }
217
218 struct Server *make_server(struct Client *cptr)
219 {
220   struct Server *serv = cli_serv(cptr);
221
222   if (!serv)
223   {
224     serv = (struct Server*) MyMalloc(sizeof(struct Server));
225     assert(0 != serv);
226     memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
227 #ifdef  DEBUGMODE
228     servs.inuse++;
229 #endif
230     cli_serv(cptr) = serv;
231     cli_serv(cptr)->lag = 60000;
232     *serv->by = '\0';
233     DupString(serv->last_error_msg, "<>");      /* String must be non-empty */
234   }
235   return cli_serv(cptr);
236 }
237
238 /*
239  * Taken the code from ExitOneClient() for this and placed it here.
240  * - avalon
241  */
242 void remove_client_from_list(struct Client *cptr)
243 {
244   if (cli_prev(cptr))
245     cli_next(cli_prev(cptr)) = cli_next(cptr);
246   else {
247     GlobalClientList = cli_next(cptr);
248     cli_prev(GlobalClientList) = 0;
249   }
250   if (cli_next(cptr))
251     cli_prev(cli_next(cptr)) = cli_prev(cptr);
252
253   cli_next(cptr) = cli_prev(cptr) = 0;
254
255   if (IsUser(cptr) && cli_user(cptr)) {
256     add_history(cptr, 0);
257     off_history(cptr);
258   }
259   if (cli_user(cptr)) {
260     free_user(cli_user(cptr));
261     cli_user(cptr) = 0;
262   }
263
264   if (cli_serv(cptr)) {
265     if (cli_serv(cptr)->user) {
266       free_user(cli_serv(cptr)->user);
267       cli_serv(cptr)->user = 0;
268     }
269     if (cli_serv(cptr)->client_list)
270       MyFree(cli_serv(cptr)->client_list);
271     MyFree(cli_serv(cptr)->last_error_msg);
272     MyFree(cli_serv(cptr));
273 #ifdef  DEBUGMODE
274     --servs.inuse;
275 #endif
276   }
277   free_client(cptr);
278 }
279
280 /*
281  * Although only a small routine, it appears in a number of places
282  * as a collection of a few lines...functions like this *should* be
283  * in this file, shouldnt they ?  after all, this is list.c, isn't it ?
284  * -avalon
285  */
286 void add_client_to_list(struct Client *cptr)
287 {
288   /*
289    * Since we always insert new clients to the top of the list,
290    * this should mean the "me" is the bottom most item in the list.
291    * XXX - don't always count on the above, things change
292    */
293   cli_prev(cptr) = 0;
294   cli_next(cptr) = GlobalClientList;
295   GlobalClientList = cptr;
296   if (cli_next(cptr))
297     cli_prev(cli_next(cptr)) = cptr;
298 }
299
300 /*
301  * Look for ptr in the linked listed pointed to by link.
302  */
303 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
304 {
305   if (ptr) {
306     while (lp) {
307       if (lp->value.cptr == ptr)
308         return (lp);
309       lp = lp->next;
310     }
311   }
312   return NULL;
313 }
314
315 struct SLink* make_link(void)
316 {
317   struct SLink* lp = slinkFreeList;
318   if (lp)
319     slinkFreeList = lp->next;
320   else {
321     lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
322     ++slinkAllocCount;
323   }
324   assert(0 != lp);
325 #ifdef  DEBUGMODE
326   links.inuse++;
327 #endif
328   return lp;
329 }
330
331 void free_link(struct SLink* lp)
332 {
333   if (lp) {
334     lp->next = slinkFreeList;
335     slinkFreeList = lp;
336   }
337 #ifdef  DEBUGMODE
338   links.inuse--;
339 #endif
340 }
341
342 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
343 {
344   struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
345   assert(0 != lp);
346   lp->value.cptr = cp;
347   lp->prev = 0;
348   if ((lp->next = *lpp))
349     lp->next->prev = lp;
350   *lpp = lp;
351   return lp;
352 }
353
354 void remove_dlink(struct DLink **lpp, struct DLink *lp)
355 {
356   assert(0 != lpp);
357   assert(0 != lp);
358
359   if (lp->prev) {
360     if ((lp->prev->next = lp->next))
361       lp->next->prev = lp->prev;
362   }
363   else if ((*lpp = lp->next))
364     lp->next->prev = NULL;
365   MyFree(lp);
366 }
367
368 #ifdef  DEBUGMODE
369 void send_listinfo(struct Client *cptr, char *name)
370 {
371   int inuse = 0, mem = 0, tmp = 0;
372
373   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Clients: inuse: %d(%d)",
374              clients.inuse, tmp = clients.inuse * sizeof(struct Client));
375   mem += tmp;
376   inuse += clients.inuse;
377   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, "Connections: inuse: %d(%d)",
378              connections.inuse,
379              tmp = connections.inuse * sizeof(struct Connection));
380   mem += tmp;
381   inuse += connections.inuse;
382   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
383              users.inuse, tmp = users.inuse * sizeof(struct User));
384   mem += tmp;
385   inuse += users.inuse;
386   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
387              servs.inuse, tmp = servs.inuse * sizeof(struct Server));
388   mem += tmp;
389   inuse += servs.inuse;
390   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
391              links.inuse, tmp = links.inuse * sizeof(struct SLink));
392   mem += tmp;
393   inuse += links.inuse;
394   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
395              GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
396   mem += tmp;
397   inuse += GlobalConfCount;
398   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",
399              inuse, mem);
400 }
401
402 #endif