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_string.h"
44 #include <stddef.h> /* offsetof */
45 #include <unistd.h> /* close */
49 static struct liststats {
51 } cloc, crem, users, servs, links, classs;
57 memset(&cloc, 0, sizeof(cloc));
58 memset(&crem, 0, sizeof(crem));
59 memset(&users, 0, sizeof(users));
60 memset(&servs, 0, sizeof(servs));
61 memset(&links, 0, sizeof(links));
62 memset(&classs, 0, sizeof(classs));
67 * Create a new struct Client structure and set it to initial state.
69 * from == NULL, create local client (a client connected to a socket).
71 * from != NULL, create remote client (behind a socket associated with
72 * the client defined by 'from').
73 * ('from' is a local client!!).
75 struct Client* make_client(struct Client *from, int status)
77 struct Client *cptr = NULL;
78 size_t size = CLIENT_REMOTE_SIZE;
81 * Check freelists first to see if we can grab a client without
82 * having to call malloc.
85 size = CLIENT_LOCAL_SIZE;
87 cptr = (struct Client*) MyMalloc(size);
90 * NOTE: Do not remove this, a lot of code depends on the entire
91 * structure being zeroed out
93 memset(cptr, 0, size); /* All variables are 0 by default */
96 if (size == CLIENT_LOCAL_SIZE)
102 /* Note: structure is zero (memset) */
103 cptr->from = from ? from : cptr; /* 'from' of local client is self! */
104 cptr->status = status;
106 strcpy(cptr->username, "unknown");
108 if (CLIENT_LOCAL_SIZE == size) {
111 cptr->since = cptr->lasttime = cptr->firsttime = CurrentTime;
112 cptr->lastnick = TStime();
113 cptr->nextnick = CurrentTime - NICK_DELAY;
114 cptr->nexttarget = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
115 cptr->handler = UNREGISTERED_HANDLER;
120 void free_client(struct Client *cptr)
122 if (cptr && cptr->local) {
124 * make sure we have cleaned up local resources
127 --cptr->dns_reply->ref_count;
130 DBufClear(&cptr->sendQ);
131 DBufClear(&cptr->recvQ);
133 release_listener(cptr->listener);
136 * forget to remove the client from the hash table?
138 assert(cptr->hnext == cptr);
143 struct Server *make_server(struct Client *cptr)
145 struct Server *serv = cptr->serv;
149 serv = (struct Server*) MyMalloc(sizeof(struct Server));
151 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
156 cptr->serv->lag = 10000;
158 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
164 * Taken the code from ExitOneClient() for this and placed it here.
167 void remove_client_from_list(struct Client *cptr)
170 cptr->prev->next = cptr->next;
172 GlobalClientList = cptr->next;
173 GlobalClientList->prev = 0;
176 cptr->next->prev = cptr->prev;
178 cptr->next = cptr->prev = 0;
180 if (IsUser(cptr) && cptr->user) {
181 add_history(cptr, 0);
185 free_user(cptr->user);
190 if (cptr->serv->user) {
191 free_user(cptr->serv->user);
192 cptr->serv->user = 0;
194 if (cptr->serv->client_list)
195 MyFree(cptr->serv->client_list);
196 MyFree(cptr->serv->last_error_msg);
212 * Although only a small routine, it appears in a number of places
213 * as a collection of a few lines...functions like this *should* be
214 * in this file, shouldnt they ? after all, this is list.c, isn't it ?
217 void add_client_to_list(struct Client *cptr)
220 * Since we always insert new clients to the top of the list,
221 * this should mean the "me" is the bottom most item in the list.
222 * XXX - don't always count on the above, things change
225 cptr->next = GlobalClientList;
226 GlobalClientList = cptr;
228 cptr->next->prev = cptr;
232 * Look for ptr in the linked listed pointed to by link.
234 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
239 if (lp->value.cptr == ptr)
246 struct SLink *make_link(void)
250 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
258 void free_link(struct SLink *lp)
266 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
268 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
272 if ((lp->next = *lpp))
278 void remove_dlink(struct DLink **lpp, struct DLink *lp)
284 if ((lp->prev->next = lp->next))
285 lp->next->prev = lp->prev;
287 else if ((*lpp = lp->next))
288 lp->next->prev = NULL;
292 struct ConfClass *make_class(void)
294 struct ConfClass *tmp;
296 tmp = (struct ConfClass*) MyMalloc(sizeof(struct ConfClass));
304 void free_class(struct ConfClass * tmp)
313 void send_listinfo(struct Client *cptr, char *name)
315 int inuse = 0, mem = 0, tmp = 0;
317 sendto_one(cptr, ":%s %d %s :Local: inuse: %d(%d)",
318 me.name, RPL_STATSDEBUG, name, inuse += cloc.inuse,
319 tmp = cloc.inuse * CLIENT_LOCAL_SIZE);
321 sendto_one(cptr, ":%s %d %s :Remote: inuse: %d(%d)",
322 me.name, RPL_STATSDEBUG, name,
323 crem.inuse, tmp = crem.inuse * CLIENT_REMOTE_SIZE);
326 sendto_one(cptr, ":%s %d %s :Users: inuse: %d(%d)",
327 me.name, RPL_STATSDEBUG, name, users.inuse,
328 tmp = users.inuse * sizeof(struct User));
330 inuse += users.inuse,
331 sendto_one(cptr, ":%s %d %s :Servs: inuse: %d(%d)",
332 me.name, RPL_STATSDEBUG, name, servs.inuse,
333 tmp = servs.inuse * sizeof(struct Server));
335 inuse += servs.inuse,
336 sendto_one(cptr, ":%s %d %s :Links: inuse: %d(%d)",
337 me.name, RPL_STATSDEBUG, name, links.inuse,
338 tmp = links.inuse * sizeof(struct SLink));
340 inuse += links.inuse,
341 sendto_one(cptr, ":%s %d %s :Classes: inuse: %d(%d)",
342 me.name, RPL_STATSDEBUG, name, classs.inuse,
343 tmp = classs.inuse * sizeof(struct ConfClass));
345 inuse += classs.inuse,
346 sendto_one(cptr, ":%s %d %s :Confs: inuse: %d(%d)",
347 me.name, RPL_STATSDEBUG, name, GlobalConfCount,
348 tmp = GlobalConfCount * sizeof(struct ConfItem));
350 inuse += GlobalConfCount,
351 sendto_one(cptr, ":%s %d %s :Totals: inuse %d %d",
352 me.name, RPL_STATSDEBUG, name, inuse, mem);