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 } cloc, crem, users, servs, links, classs;
58 memset(&cloc, 0, sizeof(cloc));
59 memset(&crem, 0, sizeof(crem));
60 memset(&users, 0, sizeof(users));
61 memset(&servs, 0, sizeof(servs));
62 memset(&links, 0, sizeof(links));
63 memset(&classs, 0, sizeof(classs));
68 * Create a new struct Client structure and set it to initial state.
70 * from == NULL, create local client (a client connected to a socket).
72 * from != NULL, create remote client (behind a socket associated with
73 * the client defined by 'from').
74 * ('from' is a local client!!).
76 struct Client* make_client(struct Client *from, int status)
78 struct Client *cptr = NULL;
79 size_t size = CLIENT_REMOTE_SIZE;
82 * Check freelists first to see if we can grab a client without
83 * having to call malloc.
86 size = CLIENT_LOCAL_SIZE;
88 cptr = (struct Client*) MyMalloc(size);
91 * NOTE: Do not remove this, a lot of code depends on the entire
92 * structure being zeroed out
94 memset(cptr, 0, size); /* All variables are 0 by default */
97 if (size == CLIENT_LOCAL_SIZE)
103 /* Note: structure is zero (memset) */
104 cptr->from = from ? from : cptr; /* 'from' of local client is self! */
105 cptr->status = status;
107 strcpy(cptr->username, "unknown");
109 if (CLIENT_LOCAL_SIZE == size) {
112 cptr->since = cptr->lasttime = cptr->firsttime = CurrentTime;
113 cptr->lastnick = TStime();
114 cptr->nextnick = CurrentTime - NICK_DELAY;
115 cptr->nexttarget = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
116 cptr->handler = UNREGISTERED_HANDLER;
121 void free_client(struct Client *cptr)
123 if (cptr && cptr->local) {
125 * make sure we have cleaned up local resources
128 --cptr->dns_reply->ref_count;
130 ip_registry_local_disconnect(cptr);
133 DBufClear(&cptr->sendQ);
134 DBufClear(&cptr->recvQ);
136 release_listener(cptr->listener);
139 * forget to remove the client from the hash table?
141 assert(cptr->hnext == cptr);
146 struct Server *make_server(struct Client *cptr)
148 struct Server *serv = cptr->serv;
152 serv = (struct Server*) MyMalloc(sizeof(struct Server));
154 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
159 cptr->serv->lag = 60000;
161 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
167 * Taken the code from ExitOneClient() for this and placed it here.
170 void remove_client_from_list(struct Client *cptr)
173 cptr->prev->next = cptr->next;
175 GlobalClientList = cptr->next;
176 GlobalClientList->prev = 0;
179 cptr->next->prev = cptr->prev;
181 cptr->next = cptr->prev = 0;
183 if (IsUser(cptr) && cptr->user) {
184 add_history(cptr, 0);
188 free_user(cptr->user);
193 if (cptr->serv->user) {
194 free_user(cptr->serv->user);
195 cptr->serv->user = 0;
197 if (cptr->serv->client_list)
198 MyFree(cptr->serv->client_list);
199 MyFree(cptr->serv->last_error_msg);
215 * Although only a small routine, it appears in a number of places
216 * as a collection of a few lines...functions like this *should* be
217 * in this file, shouldnt they ? after all, this is list.c, isn't it ?
220 void add_client_to_list(struct Client *cptr)
223 * Since we always insert new clients to the top of the list,
224 * this should mean the "me" is the bottom most item in the list.
225 * XXX - don't always count on the above, things change
228 cptr->next = GlobalClientList;
229 GlobalClientList = cptr;
231 cptr->next->prev = cptr;
235 * Look for ptr in the linked listed pointed to by link.
237 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
242 if (lp->value.cptr == ptr)
249 struct SLink *make_link(void)
253 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
261 void free_link(struct SLink *lp)
269 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
271 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
275 if ((lp->next = *lpp))
281 void remove_dlink(struct DLink **lpp, struct DLink *lp)
287 if ((lp->prev->next = lp->next))
288 lp->next->prev = lp->prev;
290 else if ((*lpp = lp->next))
291 lp->next->prev = NULL;
295 struct ConfClass *make_class(void)
297 struct ConfClass *tmp;
299 tmp = (struct ConfClass*) MyMalloc(sizeof(struct ConfClass));
307 void free_class(struct ConfClass * tmp)
316 void send_listinfo(struct Client *cptr, char *name)
318 int inuse = 0, mem = 0, tmp = 0;
320 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Local: inuse: %d(%d)",
321 inuse += cloc.inuse, tmp = cloc.inuse * CLIENT_LOCAL_SIZE);
323 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Remote: inuse: %d(%d)",
324 crem.inuse, tmp = crem.inuse * CLIENT_REMOTE_SIZE);
327 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
328 users.inuse, tmp = users.inuse * sizeof(struct User));
330 inuse += users.inuse;
331 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
332 servs.inuse, tmp = servs.inuse * sizeof(struct Server));
334 inuse += servs.inuse;
335 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
336 links.inuse, tmp = links.inuse * sizeof(struct SLink));
338 inuse += links.inuse;
339 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
340 classs.inuse, tmp = classs.inuse * sizeof(struct ConfClass));
342 inuse += classs.inuse;
343 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
344 GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
346 inuse += GlobalConfCount;
347 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",