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.
26 #include "ircd_alloc.h"
27 #include "ircd_reply.h"
28 #include "ircd_string.h"
44 #include <stddef.h> /* offsetof */
45 #include <unistd.h> /* close */
49 static struct liststats {
51 } cloc, crem, users, servs, links;
54 static unsigned int localClientAllocCount;
55 static struct Client* localClientFreeList;
57 static unsigned int remoteClientAllocCount;
58 static struct Client* remoteClientFreeList;
60 static unsigned int slinkAllocCount;
61 static struct SLink* slinkFreeList;
68 * pre-allocate MAXCONNECTIONS local clients
70 for (i = 0; i < MAXCONNECTIONS; ++i) {
71 cptr = (struct Client*) MyMalloc(CLIENT_LOCAL_SIZE);
72 cli_next(cptr) = localClientFreeList;
73 localClientFreeList = cptr;
74 ++localClientAllocCount;
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));
87 * Create a new struct Client structure and set it to initial state.
89 * from == NULL, create local client (a client connected to a socket).
91 * from != NULL, create remote client (behind a socket associated with
92 * the client defined by 'from').
93 * ('from' is a local client!!).
95 struct Client* make_client(struct Client *from, int status)
97 struct Client* cptr = 0;
99 * Check freelists first to see if we can grab a client without
100 * having to call malloc.
106 if ((cptr = remoteClientFreeList))
107 remoteClientFreeList = cli_next(cptr);
109 cptr = (struct Client*) MyMalloc(CLIENT_REMOTE_SIZE);
110 ++remoteClientAllocCount;
114 * NOTE: Do not remove this, a lot of code depends on the entire
115 * structure being zeroed out
117 memset(cptr, 0, CLIENT_REMOTE_SIZE); /* All variables are 0 by default */
118 cli_from(cptr) = from;
124 if ((cptr = localClientFreeList))
125 localClientFreeList = cli_next(cptr);
127 cptr = (struct Client*) MyMalloc(CLIENT_LOCAL_SIZE);
128 ++localClientAllocCount;
132 * NOTE: Do not remove this, a lot of code depends on the entire
133 * structure being zeroed out
135 memset(cptr, 0, CLIENT_LOCAL_SIZE); /* All variables are 0 by default */
138 cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
139 cli_lastnick(cptr) = TStime();
140 cli_nextnick(cptr) = CurrentTime - NICK_DELAY;
141 cli_nexttarget(cptr) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
142 cli_handler(cptr) = UNREGISTERED_HANDLER;
143 cli_from(cptr) = cptr; /* 'from' of local client is self! */
145 cli_status(cptr) = status;
146 cli_hnext(cptr) = cptr;
147 strcpy(cli_username(cptr), "unknown");
159 void free_client(struct Client* cptr)
164 * forget to remove the client from the hash table?
166 assert(cli_hnext(cptr) == cptr);
175 if (cli_local(cptr)) {
177 * make sure we have cleaned up local resources
179 if (cli_dns_reply(cptr))
180 --(cli_dns_reply(cptr))->ref_count;
181 if (-1 < cli_fd(cptr)) {
184 MsgQClear(&(cli_sendQ(cptr)));
185 DBufClear(&(cli_recvQ(cptr)));
186 if (cli_listener(cptr))
187 release_listener(cli_listener(cptr));
188 cli_next(cptr) = localClientFreeList;
189 localClientFreeList = cptr;
192 cli_next(cptr) = remoteClientFreeList;
193 remoteClientFreeList = cptr;
197 struct Server *make_server(struct Client *cptr)
199 struct Server *serv = cli_serv(cptr);
203 serv = (struct Server*) MyMalloc(sizeof(struct Server));
205 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
209 cli_serv(cptr) = serv;
210 cli_serv(cptr)->lag = 60000;
212 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
214 return cli_serv(cptr);
218 * Taken the code from ExitOneClient() for this and placed it here.
221 void remove_client_from_list(struct Client *cptr)
224 cli_next(cli_prev(cptr)) = cli_next(cptr);
226 GlobalClientList = cli_next(cptr);
227 cli_prev(GlobalClientList) = 0;
230 cli_prev(cli_next(cptr)) = cli_prev(cptr);
232 cli_next(cptr) = cli_prev(cptr) = 0;
234 if (IsUser(cptr) && cli_user(cptr)) {
235 add_history(cptr, 0);
238 if (cli_user(cptr)) {
239 free_user(cli_user(cptr));
243 if (cli_serv(cptr)) {
244 if (cli_serv(cptr)->user) {
245 free_user(cli_serv(cptr)->user);
246 cli_serv(cptr)->user = 0;
248 if (cli_serv(cptr)->client_list)
249 MyFree(cli_serv(cptr)->client_list);
250 MyFree(cli_serv(cptr)->last_error_msg);
251 MyFree(cli_serv(cptr));
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 ?
265 void add_client_to_list(struct Client *cptr)
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
273 cli_next(cptr) = GlobalClientList;
274 GlobalClientList = cptr;
276 cli_prev(cli_next(cptr)) = cptr;
280 * Look for ptr in the linked listed pointed to by link.
282 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
286 if (lp->value.cptr == ptr)
294 struct SLink* make_link(void)
296 struct SLink* lp = slinkFreeList;
298 slinkFreeList = lp->next;
300 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
310 void free_link(struct SLink* lp)
313 lp->next = slinkFreeList;
321 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
323 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
327 if ((lp->next = *lpp))
333 void remove_dlink(struct DLink **lpp, struct DLink *lp)
339 if ((lp->prev->next = lp->next))
340 lp->next->prev = lp->prev;
342 else if ((*lpp = lp->next))
343 lp->next->prev = NULL;
348 void send_listinfo(struct Client *cptr, char *name)
350 int inuse = 0, mem = 0, tmp = 0;
352 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Local: inuse: %d(%d)",
353 inuse += cloc.inuse, tmp = cloc.inuse * CLIENT_LOCAL_SIZE);
355 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Remote: inuse: %d(%d)",
356 crem.inuse, tmp = crem.inuse * CLIENT_REMOTE_SIZE);
359 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
360 users.inuse, tmp = users.inuse * sizeof(struct User));
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));
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));
370 inuse += links.inuse;
371 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
372 GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
374 inuse += GlobalConfCount;
375 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",