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 } clients, connections, users, servs, links;
54 static unsigned int clientAllocCount;
55 static struct Client* clientFreeList;
57 static unsigned int connectionAllocCount;
58 static struct Connection* connectionFreeList;
60 static unsigned int slinkAllocCount;
61 static struct SLink* slinkFreeList;
66 struct Connection* con;
69 * pre-allocate MAXCONNECTIONS clients and connections
71 for (i = 0; i < MAXCONNECTIONS; ++i) {
72 cptr = (struct Client*) MyMalloc(sizeof(struct Client));
73 cli_next(cptr) = clientFreeList;
74 clientFreeList = cptr;
77 con = (struct Connection*) MyMalloc(sizeof(struct Connection));
78 con_next(con) = connectionFreeList;
79 connectionFreeList = con;
80 ++connectionAllocCount;
84 memset(&clients, 0, sizeof(clients));
85 memset(&connections, 0, sizeof(connections));
86 memset(&users, 0, sizeof(users));
87 memset(&servs, 0, sizeof(servs));
88 memset(&links, 0, sizeof(links));
92 static struct Client* alloc_client(void)
94 struct Client* cptr = clientFreeList;
97 cptr = (struct Client*) MyMalloc(sizeof(struct Client));
100 clientFreeList = cli_next(cptr);
106 memset(cptr, 0, sizeof(struct Client));
111 static void dealloc_client(struct Client* cptr)
117 cli_next(cptr) = clientFreeList;
118 clientFreeList = cptr;
121 static struct Connection* alloc_connection(void)
123 struct Connection* con = connectionFreeList;
126 con = (struct Connection*) MyMalloc(sizeof(struct Connection));
127 ++connectionAllocCount;
129 connectionFreeList = con_next(con);
135 memset(con, 0, sizeof(struct Connection));
140 static void dealloc_connection(struct Connection* con)
142 if (con_dns_reply(con))
143 --(con_dns_reply(con)->ref_count);
144 if (-1 < con_fd(con))
146 MsgQClear(&(con_sendQ(con)));
147 DBufClear(&(con_recvQ(con)));
148 if (con_listener(con))
149 release_listener(con_listener(con));
155 con_next(con) = connectionFreeList;
156 connectionFreeList = con;
160 * Create a new struct Client structure and set it to initial state.
162 * from == NULL, create local client (a client connected to a socket).
164 * from != NULL, create remote client (behind a socket associated with
165 * the client defined by 'from').
166 * ('from' is a local client!!).
168 struct Client* make_client(struct Client *from, int status)
170 struct Client* cptr = 0;
171 struct Connection* con = 0;
173 cptr = alloc_client();
177 if (!from) { /* local client, allocate a struct Connection */
178 con = alloc_connection();
182 con_fd(con) = -1; /* initialize struct Connection */
183 con_nextnick(con) = CurrentTime - NICK_DELAY;
184 con_nexttarget(con) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
185 con_handler(con) = UNREGISTERED_HANDLER;
186 con_client(con) = cptr;
188 cli_local(cptr) = 1; /* Set certain fields of the struct Client */
189 cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
190 cli_lastnick(cptr) = TStime();
192 con = cli_connect(from); /* use 'from's connection */
196 cli_connect(cptr) = con; /* set the connection and other fields */
197 cli_status(cptr) = status;
198 cli_hnext(cptr) = cptr;
199 strcpy(cli_username(cptr), "unknown");
204 void free_client(struct Client* cptr)
209 * forget to remove the client from the hash table?
211 assert(cli_hnext(cptr) == cptr);
213 if (cli_from(cptr) == cptr) /* in other words, we're local */
214 dealloc_connection(cli_connect(cptr)); /* deallocate the connection... */
215 dealloc_client(cptr); /* deallocate the client */
218 struct Server *make_server(struct Client *cptr)
220 struct Server *serv = cli_serv(cptr);
224 serv = (struct Server*) MyMalloc(sizeof(struct Server));
226 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
230 cli_serv(cptr) = serv;
231 cli_serv(cptr)->lag = 60000;
233 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
235 return cli_serv(cptr);
239 * Taken the code from ExitOneClient() for this and placed it here.
242 void remove_client_from_list(struct Client *cptr)
245 cli_next(cli_prev(cptr)) = cli_next(cptr);
247 GlobalClientList = cli_next(cptr);
248 cli_prev(GlobalClientList) = 0;
251 cli_prev(cli_next(cptr)) = cli_prev(cptr);
253 cli_next(cptr) = cli_prev(cptr) = 0;
255 if (IsUser(cptr) && cli_user(cptr)) {
256 add_history(cptr, 0);
259 if (cli_user(cptr)) {
260 free_user(cli_user(cptr));
264 if (cli_serv(cptr)) {
265 if (cli_serv(cptr)->user) {
266 free_user(cli_serv(cptr)->user);
267 cli_serv(cptr)->user = 0;
269 if (cli_serv(cptr)->client_list)
270 MyFree(cli_serv(cptr)->client_list);
271 MyFree(cli_serv(cptr)->last_error_msg);
272 MyFree(cli_serv(cptr));
281 * Although only a small routine, it appears in a number of places
282 * as a collection of a few lines...functions like this *should* be
283 * in this file, shouldnt they ? after all, this is list.c, isn't it ?
286 void add_client_to_list(struct Client *cptr)
289 * Since we always insert new clients to the top of the list,
290 * this should mean the "me" is the bottom most item in the list.
291 * XXX - don't always count on the above, things change
294 cli_next(cptr) = GlobalClientList;
295 GlobalClientList = cptr;
297 cli_prev(cli_next(cptr)) = cptr;
301 * Look for ptr in the linked listed pointed to by link.
303 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
307 if (lp->value.cptr == ptr)
315 struct SLink* make_link(void)
317 struct SLink* lp = slinkFreeList;
319 slinkFreeList = lp->next;
321 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
331 void free_link(struct SLink* lp)
334 lp->next = slinkFreeList;
342 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
344 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
348 if ((lp->next = *lpp))
354 void remove_dlink(struct DLink **lpp, struct DLink *lp)
360 if ((lp->prev->next = lp->next))
361 lp->next->prev = lp->prev;
363 else if ((*lpp = lp->next))
364 lp->next->prev = NULL;
369 void send_listinfo(struct Client *cptr, char *name)
371 int inuse = 0, mem = 0, tmp = 0;
373 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Clients: inuse: %d(%d)",
374 clients.inuse, tmp = clients.inuse * sizeof(struct Client));
376 inuse += clients.inuse;
377 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, "Connections: inuse: %d(%d)",
379 tmp = connections.inuse * sizeof(struct Connection));
381 inuse += connections.inuse;
382 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
383 users.inuse, tmp = users.inuse * sizeof(struct User));
385 inuse += users.inuse;
386 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
387 servs.inuse, tmp = servs.inuse * sizeof(struct Server));
389 inuse += servs.inuse;
390 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
391 links.inuse, tmp = links.inuse * sizeof(struct SLink));
393 inuse += links.inuse;
394 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
395 GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
397 inuse += GlobalConfCount;
398 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",