2 * IRC - Internet Relay Chat, ircd/list.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Finland
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)
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.
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.
27 #include "ircd_alloc.h"
28 #include "ircd_reply.h"
29 #include "ircd_string.h"
45 #include <stddef.h> /* offsetof */
46 #include <unistd.h> /* close */
50 static struct liststats {
52 } cloc, crem, users, servs, links, classs;
55 static unsigned int localClientAllocCount;
56 static struct Client* localClientFreeList;
58 static unsigned int remoteClientAllocCount;
59 static struct Client* remoteClientFreeList;
61 static unsigned int slinkAllocCount;
62 static struct SLink* slinkFreeList;
69 * pre-allocate MAXCONNECTIONS local clients
71 for (i = 0; i < MAXCONNECTIONS; ++i) {
72 cptr = (struct Client*) MyMalloc(CLIENT_LOCAL_SIZE);
73 cptr->next = localClientFreeList;
74 localClientFreeList = cptr;
75 ++localClientAllocCount;
79 memset(&cloc, 0, sizeof(cloc));
80 memset(&crem, 0, sizeof(crem));
81 memset(&users, 0, sizeof(users));
82 memset(&servs, 0, sizeof(servs));
83 memset(&links, 0, sizeof(links));
84 memset(&classs, 0, sizeof(classs));
89 * Create a new struct Client structure and set it to initial state.
91 * from == NULL, create local client (a client connected to a socket).
93 * from != NULL, create remote client (behind a socket associated with
94 * the client defined by 'from').
95 * ('from' is a local client!!).
97 struct Client* make_client(struct Client *from, int status)
99 struct Client* cptr = 0;
101 * Check freelists first to see if we can grab a client without
102 * having to call malloc.
108 if ((cptr = remoteClientFreeList))
109 remoteClientFreeList = cptr->next;
111 cptr = (struct Client*) MyMalloc(CLIENT_REMOTE_SIZE);
112 ++remoteClientAllocCount;
116 * NOTE: Do not remove this, a lot of code depends on the entire
117 * structure being zeroed out
119 memset(cptr, 0, CLIENT_REMOTE_SIZE); /* All variables are 0 by default */
126 if ((cptr = localClientFreeList))
127 localClientFreeList = cptr->next;
129 cptr = (struct Client*) MyMalloc(CLIENT_LOCAL_SIZE);
130 ++localClientAllocCount;
134 * NOTE: Do not remove this, a lot of code depends on the entire
135 * structure being zeroed out
137 memset(cptr, 0, CLIENT_LOCAL_SIZE); /* All variables are 0 by default */
140 cptr->since = cptr->lasttime = cptr->firsttime = CurrentTime;
141 cptr->lastnick = TStime();
142 cptr->nextnick = CurrentTime - NICK_DELAY;
143 cptr->nexttarget = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
144 cptr->handler = UNREGISTERED_HANDLER;
145 cptr->from = cptr; /* 'from' of local client is self! */
147 cptr->status = status;
149 strcpy(cptr->username, "unknown");
161 void free_client(struct Client* cptr)
166 * forget to remove the client from the hash table?
168 assert(cptr->hnext == cptr);
179 * make sure we have cleaned up local resources
182 --cptr->dns_reply->ref_count;
186 DBufClear(&cptr->sendQ);
187 DBufClear(&cptr->recvQ);
189 release_listener(cptr->listener);
190 cptr->next = localClientFreeList;
191 localClientFreeList = cptr;
194 cptr->next = remoteClientFreeList;
195 remoteClientFreeList = cptr;
199 struct Server *make_server(struct Client *cptr)
201 struct Server *serv = cptr->serv;
205 serv = (struct Server*) MyMalloc(sizeof(struct Server));
207 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
212 cptr->serv->lag = 60000;
214 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
220 * Taken the code from ExitOneClient() for this and placed it here.
223 void remove_client_from_list(struct Client *cptr)
226 cptr->prev->next = cptr->next;
228 GlobalClientList = cptr->next;
229 GlobalClientList->prev = 0;
232 cptr->next->prev = cptr->prev;
234 cptr->next = cptr->prev = 0;
236 if (IsUser(cptr) && cptr->user) {
237 add_history(cptr, 0);
241 free_user(cptr->user);
246 if (cptr->serv->user) {
247 free_user(cptr->serv->user);
248 cptr->serv->user = 0;
250 if (cptr->serv->client_list)
251 MyFree(cptr->serv->client_list);
252 MyFree(cptr->serv->last_error_msg);
262 * Although only a small routine, it appears in a number of places
263 * as a collection of a few lines...functions like this *should* be
264 * in this file, shouldnt they ? after all, this is list.c, isn't it ?
267 void add_client_to_list(struct Client *cptr)
270 * Since we always insert new clients to the top of the list,
271 * this should mean the "me" is the bottom most item in the list.
272 * XXX - don't always count on the above, things change
275 cptr->next = GlobalClientList;
276 GlobalClientList = cptr;
278 cptr->next->prev = cptr;
282 * Look for ptr in the linked listed pointed to by link.
284 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
288 if (lp->value.cptr == ptr)
296 struct SLink* make_link(void)
298 struct SLink* lp = slinkFreeList;
300 slinkFreeList = lp->next;
302 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
312 void free_link(struct SLink* lp)
315 lp->next = slinkFreeList;
323 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
325 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
329 if ((lp->next = *lpp))
335 void remove_dlink(struct DLink **lpp, struct DLink *lp)
341 if ((lp->prev->next = lp->next))
342 lp->next->prev = lp->prev;
344 else if ((*lpp = lp->next))
345 lp->next->prev = NULL;
349 struct ConfClass *make_class(void)
351 struct ConfClass *tmp;
353 tmp = (struct ConfClass*) MyMalloc(sizeof(struct ConfClass));
361 void free_class(struct ConfClass * tmp)
370 void send_listinfo(struct Client *cptr, char *name)
372 int inuse = 0, mem = 0, tmp = 0;
374 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Local: inuse: %d(%d)",
375 inuse += cloc.inuse, tmp = cloc.inuse * CLIENT_LOCAL_SIZE);
377 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Remote: inuse: %d(%d)",
378 crem.inuse, tmp = crem.inuse * CLIENT_REMOTE_SIZE);
381 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
382 users.inuse, tmp = users.inuse * sizeof(struct User));
384 inuse += users.inuse;
385 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
386 servs.inuse, tmp = servs.inuse * sizeof(struct Server));
388 inuse += servs.inuse;
389 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
390 links.inuse, tmp = links.inuse * sizeof(struct SLink));
392 inuse += links.inuse;
393 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
394 classs.inuse, tmp = classs.inuse * sizeof(struct ConfClass));
396 inuse += classs.inuse;
397 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
398 GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
400 inuse += GlobalConfCount;
401 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",