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 client_drop_sendq(con);
148 DBufClear(&(con_recvQ(con)));
149 if (con_listener(con))
150 release_listener(con_listener(con));
156 con_next(con) = connectionFreeList;
157 connectionFreeList = con;
161 * Create a new struct Client structure and set it to initial state.
163 * from == NULL, create local client (a client connected to a socket).
165 * from != NULL, create remote client (behind a socket associated with
166 * the client defined by 'from').
167 * ('from' is a local client!!).
169 struct Client* make_client(struct Client *from, int status)
171 struct Client* cptr = 0;
172 struct Connection* con = 0;
174 cptr = alloc_client();
178 if (!from) { /* local client, allocate a struct Connection */
179 con = alloc_connection();
183 con_fd(con) = -1; /* initialize struct Connection */
184 con_nextnick(con) = CurrentTime - NICK_DELAY;
185 con_nexttarget(con) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
186 con_handler(con) = UNREGISTERED_HANDLER;
187 con_client(con) = cptr;
189 cli_local(cptr) = 1; /* Set certain fields of the struct Client */
190 cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
191 cli_lastnick(cptr) = TStime();
193 con = cli_connect(from); /* use 'from's connection */
197 cli_connect(cptr) = con; /* set the connection and other fields */
198 cli_status(cptr) = status;
199 cli_hnext(cptr) = cptr;
200 strcpy(cli_username(cptr), "unknown");
205 void free_client(struct Client* cptr)
210 * forget to remove the client from the hash table?
212 assert(cli_hnext(cptr) == cptr);
214 if (cli_from(cptr) == cptr) /* in other words, we're local */
215 dealloc_connection(cli_connect(cptr)); /* deallocate the connection... */
216 dealloc_client(cptr); /* deallocate the client */
219 struct Server *make_server(struct Client *cptr)
221 struct Server *serv = cli_serv(cptr);
225 serv = (struct Server*) MyMalloc(sizeof(struct Server));
227 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
231 cli_serv(cptr) = serv;
232 cli_serv(cptr)->lag = 60000;
234 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
236 return cli_serv(cptr);
240 * Taken the code from ExitOneClient() for this and placed it here.
243 void remove_client_from_list(struct Client *cptr)
246 cli_next(cli_prev(cptr)) = cli_next(cptr);
248 GlobalClientList = cli_next(cptr);
249 cli_prev(GlobalClientList) = 0;
252 cli_prev(cli_next(cptr)) = cli_prev(cptr);
254 cli_next(cptr) = cli_prev(cptr) = 0;
256 if (IsUser(cptr) && cli_user(cptr)) {
257 add_history(cptr, 0);
260 if (cli_user(cptr)) {
261 free_user(cli_user(cptr));
265 if (cli_serv(cptr)) {
266 if (cli_serv(cptr)->user) {
267 free_user(cli_serv(cptr)->user);
268 cli_serv(cptr)->user = 0;
270 if (cli_serv(cptr)->client_list)
271 MyFree(cli_serv(cptr)->client_list);
272 MyFree(cli_serv(cptr)->last_error_msg);
273 MyFree(cli_serv(cptr));
282 * Although only a small routine, it appears in a number of places
283 * as a collection of a few lines...functions like this *should* be
284 * in this file, shouldnt they ? after all, this is list.c, isn't it ?
287 void add_client_to_list(struct Client *cptr)
290 * Since we always insert new clients to the top of the list,
291 * this should mean the "me" is the bottom most item in the list.
292 * XXX - don't always count on the above, things change
295 cli_next(cptr) = GlobalClientList;
296 GlobalClientList = cptr;
298 cli_prev(cli_next(cptr)) = cptr;
302 * Look for ptr in the linked listed pointed to by link.
304 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
308 if (lp->value.cptr == ptr)
316 struct SLink* make_link(void)
318 struct SLink* lp = slinkFreeList;
320 slinkFreeList = lp->next;
322 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
332 void free_link(struct SLink* lp)
335 lp->next = slinkFreeList;
343 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
345 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
349 if ((lp->next = *lpp))
355 void remove_dlink(struct DLink **lpp, struct DLink *lp)
361 if ((lp->prev->next = lp->next))
362 lp->next->prev = lp->prev;
364 else if ((*lpp = lp->next))
365 lp->next->prev = NULL;
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, ":Clients: inuse: %d(%d)",
375 clients.inuse, tmp = clients.inuse * sizeof(struct Client));
377 inuse += clients.inuse;
378 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, "Connections: inuse: %d(%d)",
380 tmp = connections.inuse * sizeof(struct Connection));
382 inuse += connections.inuse;
383 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
384 users.inuse, tmp = users.inuse * sizeof(struct User));
386 inuse += users.inuse;
387 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
388 servs.inuse, tmp = servs.inuse * sizeof(struct Server));
390 inuse += servs.inuse;
391 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
392 links.inuse, tmp = links.inuse * sizeof(struct SLink));
394 inuse += links.inuse;
395 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
396 GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
398 inuse += GlobalConfCount;
399 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",