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