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 } clients, connections, users, servs, links;
55 static unsigned int clientAllocCount;
56 static struct Client* clientFreeList;
58 static unsigned int connectionAllocCount;
59 static struct Connection* connectionFreeList;
61 static unsigned int slinkAllocCount;
62 static struct SLink* slinkFreeList;
67 struct Connection* con;
70 * pre-allocate MAXCONNECTIONS clients and connections
72 for (i = 0; i < MAXCONNECTIONS; ++i) {
73 cptr = (struct Client*) MyMalloc(sizeof(struct Client));
74 cli_next(cptr) = clientFreeList;
75 clientFreeList = cptr;
78 con = (struct Connection*) MyMalloc(sizeof(struct Connection));
79 con_next(con) = connectionFreeList;
80 connectionFreeList = con;
81 ++connectionAllocCount;
85 memset(&clients, 0, sizeof(clients));
86 memset(&connections, 0, sizeof(connections));
87 memset(&users, 0, sizeof(users));
88 memset(&servs, 0, sizeof(servs));
89 memset(&links, 0, sizeof(links));
93 static struct Client* alloc_client(void)
95 struct Client* cptr = clientFreeList;
98 cptr = (struct Client*) MyMalloc(sizeof(struct Client));
101 clientFreeList = cli_next(cptr);
107 memset(cptr, 0, sizeof(struct Client));
112 static void dealloc_client(struct Client* cptr)
118 cli_next(cptr) = clientFreeList;
119 clientFreeList = cptr;
122 static struct Connection* alloc_connection(void)
124 struct Connection* con = connectionFreeList;
127 con = (struct Connection*) MyMalloc(sizeof(struct Connection));
128 ++connectionAllocCount;
130 connectionFreeList = con_next(con);
136 memset(con, 0, sizeof(struct Connection));
141 static void dealloc_connection(struct Connection* con)
143 if (con_dns_reply(con))
144 --(con_dns_reply(con)->ref_count);
145 if (-1 < con_fd(con))
147 MsgQClear(&(con_sendQ(con)));
148 client_drop_sendq(con);
149 DBufClear(&(con_recvQ(con)));
150 if (con_listener(con))
151 release_listener(con_listener(con));
157 con_next(con) = connectionFreeList;
158 connectionFreeList = con;
162 * Create a new struct Client structure and set it to initial state.
164 * from == NULL, create local client (a client connected to a socket).
166 * from != NULL, create remote client (behind a socket associated with
167 * the client defined by 'from').
168 * ('from' is a local client!!).
170 struct Client* make_client(struct Client *from, int status)
172 struct Client* cptr = 0;
173 struct Connection* con = 0;
175 cptr = alloc_client();
179 if (!from) { /* local client, allocate a struct Connection */
180 con = alloc_connection();
184 con_fd(con) = -1; /* initialize struct Connection */
185 con_nextnick(con) = CurrentTime - NICK_DELAY;
186 con_nexttarget(con) = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
187 con_handler(con) = UNREGISTERED_HANDLER;
188 con_client(con) = cptr;
190 cli_local(cptr) = 1; /* Set certain fields of the struct Client */
191 cli_since(cptr) = cli_lasttime(cptr) = cli_firsttime(cptr) = CurrentTime;
192 cli_lastnick(cptr) = TStime();
194 con = cli_connect(from); /* use 'from's connection */
198 cli_connect(cptr) = con; /* set the connection and other fields */
199 cli_status(cptr) = status;
200 cli_hnext(cptr) = cptr;
201 strcpy(cli_username(cptr), "unknown");
206 void free_client(struct Client* cptr)
211 * forget to remove the client from the hash table?
213 assert(cli_hnext(cptr) == cptr);
215 if (cli_from(cptr) == cptr) /* in other words, we're local */
216 dealloc_connection(cli_connect(cptr)); /* deallocate the connection... */
217 dealloc_client(cptr); /* deallocate the client */
220 struct Server *make_server(struct Client *cptr)
222 struct Server *serv = cli_serv(cptr);
226 serv = (struct Server*) MyMalloc(sizeof(struct Server));
228 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
232 cli_serv(cptr) = serv;
233 cli_serv(cptr)->lag = 60000;
235 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
237 return cli_serv(cptr);
241 * Taken the code from ExitOneClient() for this and placed it here.
244 void remove_client_from_list(struct Client *cptr)
247 cli_next(cli_prev(cptr)) = cli_next(cptr);
249 GlobalClientList = cli_next(cptr);
250 cli_prev(GlobalClientList) = 0;
253 cli_prev(cli_next(cptr)) = cli_prev(cptr);
255 cli_next(cptr) = cli_prev(cptr) = 0;
257 if (IsUser(cptr) && cli_user(cptr)) {
258 add_history(cptr, 0);
261 if (cli_user(cptr)) {
262 free_user(cli_user(cptr));
266 if (cli_serv(cptr)) {
267 if (cli_serv(cptr)->user) {
268 free_user(cli_serv(cptr)->user);
269 cli_serv(cptr)->user = 0;
271 if (cli_serv(cptr)->client_list)
272 MyFree(cli_serv(cptr)->client_list);
273 MyFree(cli_serv(cptr)->last_error_msg);
274 MyFree(cli_serv(cptr));
283 * Although only a small routine, it appears in a number of places
284 * as a collection of a few lines...functions like this *should* be
285 * in this file, shouldnt they ? after all, this is list.c, isn't it ?
288 void add_client_to_list(struct Client *cptr)
291 * Since we always insert new clients to the top of the list,
292 * this should mean the "me" is the bottom most item in the list.
293 * XXX - don't always count on the above, things change
296 cli_next(cptr) = GlobalClientList;
297 GlobalClientList = cptr;
299 cli_prev(cli_next(cptr)) = cptr;
303 * Look for ptr in the linked listed pointed to by link.
305 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
309 if (lp->value.cptr == ptr)
317 struct SLink* make_link(void)
319 struct SLink* lp = slinkFreeList;
321 slinkFreeList = lp->next;
323 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
333 void free_link(struct SLink* lp)
336 lp->next = slinkFreeList;
344 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
346 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
350 if ((lp->next = *lpp))
356 void remove_dlink(struct DLink **lpp, struct DLink *lp)
362 if ((lp->prev->next = lp->next))
363 lp->next->prev = lp->prev;
365 else if ((*lpp = lp->next))
366 lp->next->prev = NULL;
371 void send_listinfo(struct Client *cptr, char *name)
373 int inuse = 0, mem = 0, tmp = 0;
375 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Clients: inuse: %d(%d)",
376 clients.inuse, tmp = clients.inuse * sizeof(struct Client));
378 inuse += clients.inuse;
379 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, "Connections: inuse: %d(%d)",
381 tmp = connections.inuse * sizeof(struct Connection));
383 inuse += connections.inuse;
384 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
385 users.inuse, tmp = users.inuse * sizeof(struct User));
387 inuse += users.inuse;
388 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
389 servs.inuse, tmp = servs.inuse * sizeof(struct Server));
391 inuse += servs.inuse;
392 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
393 links.inuse, tmp = links.inuse * sizeof(struct SLink));
395 inuse += links.inuse;
396 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
397 GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
399 inuse += GlobalConfCount;
400 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",