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"
46 #include <stddef.h> /* offsetof */
47 #include <unistd.h> /* close */
51 static struct liststats {
53 } clients, connections, users, servs, links;
56 static unsigned int clientAllocCount;
57 static struct Client* clientFreeList;
59 static unsigned int connectionAllocCount;
60 static struct Connection* connectionFreeList;
62 static unsigned int slinkAllocCount;
63 static struct SLink* slinkFreeList;
68 struct Connection* con;
71 * pre-allocate MAXCONNECTIONS clients and connections
73 for (i = 0; i < MAXCONNECTIONS; ++i) {
74 cptr = (struct Client*) MyMalloc(sizeof(struct Client));
75 cli_next(cptr) = clientFreeList;
76 clientFreeList = cptr;
79 con = (struct Connection*) MyMalloc(sizeof(struct Connection));
80 con_next(con) = connectionFreeList;
81 connectionFreeList = con;
82 ++connectionAllocCount;
86 memset(&clients, 0, sizeof(clients));
87 memset(&connections, 0, sizeof(connections));
88 memset(&users, 0, sizeof(users));
89 memset(&servs, 0, sizeof(servs));
90 memset(&links, 0, sizeof(links));
94 static struct Client* alloc_client(void)
96 struct Client* cptr = clientFreeList;
99 cptr = (struct Client*) MyMalloc(sizeof(struct Client));
102 clientFreeList = cli_next(cptr);
108 memset(cptr, 0, sizeof(struct Client));
113 static void dealloc_client(struct Client* cptr)
115 assert(cli_verify(cptr));
116 assert(0 == cli_connect(cptr));
122 cli_next(cptr) = clientFreeList;
123 clientFreeList = cptr;
128 static struct Connection* alloc_connection(void)
130 struct Connection* con = connectionFreeList;
133 con = (struct Connection*) MyMalloc(sizeof(struct Connection));
134 ++connectionAllocCount;
136 connectionFreeList = con_next(con);
142 memset(con, 0, sizeof(struct Connection));
147 static void dealloc_connection(struct Connection* con)
149 assert(con_verify(con));
151 Debug((DEBUG_LIST, "Deallocating connection %p", con));
153 if (con_dns_reply(con))
154 --(con_dns_reply(con)->ref_count);
155 if (-1 < con_fd(con))
157 MsgQClear(&(con_sendQ(con)));
158 client_drop_sendq(con);
159 DBufClear(&(con_recvQ(con)));
160 if (con_listener(con))
161 release_listener(con_listener(con));
167 con_next(con) = connectionFreeList;
168 connectionFreeList = con;
174 * Create a new struct Client structure and set it to initial state.
176 * from == NULL, create local client (a client connected to a socket).
178 * from != NULL, create remote client (behind a socket associated with
179 * the client defined by 'from').
180 * ('from' is a local client!!).
182 struct Client* make_client(struct Client *from, int status)
184 struct Client* cptr = 0;
185 struct Connection* con = 0;
187 assert(!from || cli_verify(from));
189 cptr = alloc_client();
192 assert(!cli_magic(cptr));
193 assert(0 == from || 0 != cli_connect(from));
195 if (!from) { /* local client, allocate a struct Connection */
196 con = alloc_connection();
199 assert(!con_magic(con));
201 con_magic(con) = CONNECTION_MAGIC;
202 con_fd(con) = -1; /* initialize struct Connection */
203 con_freeflag(con) = 0;
204 con_nextnick(con) = CurrentTime - NICK_DELAY;
205 con_nexttarget(con) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
206 con_handler(con) = UNREGISTERED_HANDLER;
207 con_client(con) = cptr;
209 cli_local(cptr) = 1; /* Set certain fields of the struct Client */
210 cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
211 cli_lastnick(cptr) = TStime();
213 con = cli_connect(from); /* use 'from's connection */
216 assert(con_verify(con));
218 cli_magic(cptr) = CLIENT_MAGIC;
219 cli_connect(cptr) = con; /* set the connection and other fields */
220 cli_status(cptr) = status;
221 cli_hnext(cptr) = cptr;
222 strcpy(cli_username(cptr), "unknown");
227 void free_connection(struct Connection* con)
232 assert(con_verify(con));
233 assert(0 == con_client(con));
235 dealloc_connection(con); /* deallocate the connection */
238 void free_client(struct Client* cptr)
243 * forget to remove the client from the hash table?
245 assert(cli_verify(cptr));
246 assert(cli_hnext(cptr) == cptr);
248 Debug((DEBUG_LIST, "Freeing client %s [%p], connection %p", cli_name(cptr),
249 cptr, cli_connect(cptr)));
252 destroy_auth_request(cli_auth(cptr), 0);
254 if (cli_from(cptr) == cptr) { /* in other words, we're local */
256 /* timer must be marked as not active */
257 if (!cli_freeflag(cptr) && !t_active(&(cli_proc(cptr))))
258 dealloc_connection(cli_connect(cptr)); /* connection not open anymore */
260 if (-1 < cli_fd(cptr) && cli_freeflag(cptr) & FREEFLAG_SOCKET)
261 socket_del(&(cli_socket(cptr))); /* queue a socket delete */
262 if (cli_freeflag(cptr) & FREEFLAG_TIMER)
263 timer_del(&(cli_proc(cptr))); /* queue a timer delete */
267 cli_connect(cptr) = 0;
269 dealloc_client(cptr); /* actually destroy the client */
272 struct Server *make_server(struct Client *cptr)
274 struct Server *serv = cli_serv(cptr);
276 assert(cli_verify(cptr));
280 serv = (struct Server*) MyMalloc(sizeof(struct Server));
282 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
286 cli_serv(cptr) = serv;
287 cli_serv(cptr)->lag = 60000;
289 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
291 return cli_serv(cptr);
295 * Taken the code from ExitOneClient() for this and placed it here.
298 void remove_client_from_list(struct Client *cptr)
300 assert(cli_verify(cptr));
301 assert(con_verify(cli_connect(cptr)));
304 cli_next(cli_prev(cptr)) = cli_next(cptr);
306 GlobalClientList = cli_next(cptr);
307 if (GlobalClientList)
308 cli_prev(GlobalClientList) = 0;
311 cli_prev(cli_next(cptr)) = cli_prev(cptr);
313 cli_next(cptr) = cli_prev(cptr) = 0;
315 if (IsUser(cptr) && cli_user(cptr)) {
316 add_history(cptr, 0);
319 if (cli_user(cptr)) {
320 free_user(cli_user(cptr));
324 if (cli_serv(cptr)) {
325 if (cli_serv(cptr)->user) {
326 free_user(cli_serv(cptr)->user);
327 cli_serv(cptr)->user = 0;
329 if (cli_serv(cptr)->client_list)
330 MyFree(cli_serv(cptr)->client_list);
331 MyFree(cli_serv(cptr)->last_error_msg);
332 MyFree(cli_serv(cptr));
341 * Although only a small routine, it appears in a number of places
342 * as a collection of a few lines...functions like this *should* be
343 * in this file, shouldnt they ? after all, this is list.c, isn't it ?
346 void add_client_to_list(struct Client *cptr)
348 assert(cli_verify(cptr));
350 * Since we always insert new clients to the top of the list,
351 * this should mean the "me" is the bottom most item in the list.
352 * XXX - don't always count on the above, things change
355 cli_next(cptr) = GlobalClientList;
356 GlobalClientList = cptr;
358 cli_prev(cli_next(cptr)) = cptr;
362 * Look for ptr in the linked listed pointed to by link.
364 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
368 if (lp->value.cptr == ptr)
376 struct SLink* make_link(void)
378 struct SLink* lp = slinkFreeList;
380 slinkFreeList = lp->next;
382 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
392 void free_link(struct SLink* lp)
395 lp->next = slinkFreeList;
403 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
405 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
409 if ((lp->next = *lpp))
415 void remove_dlink(struct DLink **lpp, struct DLink *lp)
421 if ((lp->prev->next = lp->next))
422 lp->next->prev = lp->prev;
424 else if ((*lpp = lp->next))
425 lp->next->prev = NULL;
430 void send_listinfo(struct Client *cptr, char *name)
432 int inuse = 0, mem = 0, tmp = 0;
434 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Clients: inuse: %d(%d)",
435 clients.inuse, tmp = clients.inuse * sizeof(struct Client));
437 inuse += clients.inuse;
438 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, "Connections: inuse: %d(%d)",
440 tmp = connections.inuse * sizeof(struct Connection));
442 inuse += connections.inuse;
443 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
444 users.inuse, tmp = users.inuse * sizeof(struct User));
446 inuse += users.inuse;
447 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
448 servs.inuse, tmp = servs.inuse * sizeof(struct Server));
450 inuse += servs.inuse;
451 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
452 links.inuse, tmp = links.inuse * sizeof(struct SLink));
454 inuse += links.inuse;
455 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
456 GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
458 inuse += GlobalConfCount;
459 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",