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 } cloc, crem, users, servs, links;
52 #endif
53
54 static unsigned int localClientAllocCount;
55 static struct Client* localClientFreeList;
56
57 static unsigned int remoteClientAllocCount;
58 static struct Client* remoteClientFreeList;
59
60 static unsigned int slinkAllocCount;
61 static struct SLink* slinkFreeList;
62
63 void init_list(void)
64 {
65   struct Client* cptr;
66   int i;
67   /*
68    * pre-allocate MAXCONNECTIONS local clients
69    */
70   for (i = 0; i < MAXCONNECTIONS; ++i) {
71     cptr = (struct Client*) MyMalloc(CLIENT_LOCAL_SIZE);
72     cptr->next = localClientFreeList;
73     localClientFreeList = cptr;
74     ++localClientAllocCount;
75   }
76
77 #ifdef DEBUGMODE
78   memset(&cloc, 0, sizeof(cloc));
79   memset(&crem, 0, sizeof(crem));
80   memset(&users, 0, sizeof(users));
81   memset(&servs, 0, sizeof(servs));
82   memset(&links, 0, sizeof(links));
83 #endif
84 }
85
86 /*
87  * Create a new struct Client structure and set it to initial state.
88  *
89  *   from == NULL,   create local client (a client connected to a socket).
90  *
91  *   from != NULL,   create remote client (behind a socket associated with
92  *                   the client defined by 'from').
93  *                   ('from' is a local client!!).
94  */
95 struct Client* make_client(struct Client *from, int status)
96 {
97   struct Client* cptr = 0;
98   /*
99    * Check freelists first to see if we can grab a client without
100    * having to call malloc.
101    */
102   if (from) {
103     /*
104      * remote client
105      */
106     if ((cptr = remoteClientFreeList))
107       remoteClientFreeList = cptr->next;
108     else {
109       cptr = (struct Client*) MyMalloc(CLIENT_REMOTE_SIZE);
110       ++remoteClientAllocCount;
111     }
112     assert(0 != cptr);
113     /*
114      * NOTE: Do not remove this, a lot of code depends on the entire
115      * structure being zeroed out
116      */
117     memset(cptr, 0, CLIENT_REMOTE_SIZE);        /* All variables are 0 by default */
118     cptr->from = from;
119   }
120   else {
121     /*
122      * local client
123      */
124     if ((cptr = localClientFreeList))
125       localClientFreeList = cptr->next;
126     else {
127       cptr = (struct Client*) MyMalloc(CLIENT_LOCAL_SIZE);
128       ++localClientAllocCount;
129     }
130     assert(0 != cptr);
131     /*
132      * NOTE: Do not remove this, a lot of code depends on the entire
133      * structure being zeroed out
134      */
135     memset(cptr, 0, CLIENT_LOCAL_SIZE);        /* All variables are 0 by default */
136     cptr->fd = -1;
137     cptr->local = 1;
138     cptr->since = cptr->lasttime = cptr->firsttime = CurrentTime;
139     cptr->lastnick = TStime();
140     cptr->nextnick = CurrentTime - NICK_DELAY;
141     cptr->nexttarget = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
142     cptr->handler = UNREGISTERED_HANDLER;
143     cptr->from = cptr;      /* 'from' of local client is self! */
144   }
145   cptr->status = status;
146   cptr->hnext = cptr;
147   strcpy(cptr->username, "unknown");
148
149 #ifdef  DEBUGMODE
150   if (from)
151     crem.inuse++;
152   else
153     cloc.inuse++;
154 #endif
155
156   return cptr;
157 }
158
159 void free_client(struct Client* cptr)
160 {
161   if (!cptr)
162     return;
163   /*
164    * forget to remove the client from the hash table?
165    */
166   assert(cptr->hnext == cptr);
167
168 #ifdef  DEBUGMODE
169   if (cptr->local)
170     --cloc.inuse;
171   else
172     --crem.inuse;
173 #endif
174
175   if (cptr->local) {
176     /*
177      * make sure we have cleaned up local resources
178      */
179     if (cptr->dns_reply)
180       --cptr->dns_reply->ref_count;
181     if (-1 < cptr->fd) {
182       close(cptr->fd);
183     }
184     MsgQClear(&cptr->sendQ);
185     DBufClear(&cptr->recvQ);
186     if (cptr->listener)
187       release_listener(cptr->listener);
188     cptr->next = localClientFreeList;
189     localClientFreeList = cptr;
190   }    
191   else {
192     cptr->next = remoteClientFreeList;
193     remoteClientFreeList = cptr;
194   }
195 }
196
197 struct Server *make_server(struct Client *cptr)
198 {
199   struct Server *serv = cptr->serv;
200
201   if (!serv)
202   {
203     serv = (struct Server*) MyMalloc(sizeof(struct Server));
204     assert(0 != serv);
205     memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
206 #ifdef  DEBUGMODE
207     servs.inuse++;
208 #endif
209     cptr->serv = serv;
210     cptr->serv->lag = 60000;
211     *serv->by = '\0';
212     DupString(serv->last_error_msg, "<>");      /* String must be non-empty */
213   }
214   return cptr->serv;
215 }
216
217 /*
218  * Taken the code from ExitOneClient() for this and placed it here.
219  * - avalon
220  */
221 void remove_client_from_list(struct Client *cptr)
222 {
223   if (cptr->prev)
224     cptr->prev->next = cptr->next;
225   else {
226     GlobalClientList = cptr->next;
227     GlobalClientList->prev = 0;
228   }
229   if (cptr->next)
230     cptr->next->prev = cptr->prev;
231
232   cptr->next = cptr->prev = 0;
233
234   if (IsUser(cptr) && cptr->user) {
235     add_history(cptr, 0);
236     off_history(cptr);
237   }
238   if (cptr->user) {
239     free_user(cptr->user);
240     cptr->user = 0;
241   }
242
243   if (cptr->serv) {
244     if (cptr->serv->user) {
245       free_user(cptr->serv->user);
246       cptr->serv->user = 0;
247     }
248     if (cptr->serv->client_list)
249       MyFree(cptr->serv->client_list);
250     MyFree(cptr->serv->last_error_msg);
251     MyFree(cptr->serv);
252 #ifdef  DEBUGMODE
253     --servs.inuse;
254 #endif
255   }
256   free_client(cptr);
257 }
258
259 /*
260  * Although only a small routine, it appears in a number of places
261  * as a collection of a few lines...functions like this *should* be
262  * in this file, shouldnt they ?  after all, this is list.c, isn't it ?
263  * -avalon
264  */
265 void add_client_to_list(struct Client *cptr)
266 {
267   /*
268    * Since we always insert new clients to the top of the list,
269    * this should mean the "me" is the bottom most item in the list.
270    * XXX - don't always count on the above, things change
271    */
272   cptr->prev = 0;
273   cptr->next = GlobalClientList;
274   GlobalClientList = cptr;
275   if (cptr->next)
276     cptr->next->prev = cptr;
277 }
278
279 /*
280  * Look for ptr in the linked listed pointed to by link.
281  */
282 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
283 {
284   if (ptr) {
285     while (lp) {
286       if (lp->value.cptr == ptr)
287         return (lp);
288       lp = lp->next;
289     }
290   }
291   return NULL;
292 }
293
294 struct SLink* make_link(void)
295 {
296   struct SLink* lp = slinkFreeList;
297   if (lp)
298     slinkFreeList = lp->next;
299   else {
300     lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
301     ++slinkAllocCount;
302   }
303   assert(0 != lp);
304 #ifdef  DEBUGMODE
305   links.inuse++;
306 #endif
307   return lp;
308 }
309
310 void free_link(struct SLink* lp)
311 {
312   if (lp) {
313     lp->next = slinkFreeList;
314     slinkFreeList = lp;
315   }
316 #ifdef  DEBUGMODE
317   links.inuse--;
318 #endif
319 }
320
321 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
322 {
323   struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
324   assert(0 != lp);
325   lp->value.cptr = cp;
326   lp->prev = 0;
327   if ((lp->next = *lpp))
328     lp->next->prev = lp;
329   *lpp = lp;
330   return lp;
331 }
332
333 void remove_dlink(struct DLink **lpp, struct DLink *lp)
334 {
335   assert(0 != lpp);
336   assert(0 != lp);
337
338   if (lp->prev) {
339     if ((lp->prev->next = lp->next))
340       lp->next->prev = lp->prev;
341   }
342   else if ((*lpp = lp->next))
343     lp->next->prev = NULL;
344   MyFree(lp);
345 }
346
347 #ifdef  DEBUGMODE
348 void send_listinfo(struct Client *cptr, char *name)
349 {
350   int inuse = 0, mem = 0, tmp = 0;
351
352   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Local: inuse: %d(%d)",
353              inuse += cloc.inuse, tmp = cloc.inuse * CLIENT_LOCAL_SIZE);
354   mem += tmp;
355   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Remote: inuse: %d(%d)",
356              crem.inuse, tmp = crem.inuse * CLIENT_REMOTE_SIZE);
357   mem += tmp;
358   inuse += crem.inuse;
359   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
360              users.inuse, tmp = users.inuse * sizeof(struct User));
361   mem += tmp;
362   inuse += users.inuse;
363   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
364              servs.inuse, tmp = servs.inuse * sizeof(struct Server));
365   mem += tmp;
366   inuse += servs.inuse;
367   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
368              links.inuse, tmp = links.inuse * sizeof(struct SLink));
369   mem += tmp;
370   inuse += links.inuse;
371   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
372              GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
373   mem += tmp;
374   inuse += GlobalConfCount;
375   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",
376              inuse, mem);
377 }
378
379 #endif