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