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.
28 #include "ircd_alloc.h"
29 #include "ircd_reply.h"
30 #include "ircd_string.h"
46 #include <stddef.h> /* offsetof */
47 #include <unistd.h> /* close */
51 static struct liststats {
53 } cloc, crem, users, servs, links, classs;
59 memset(&cloc, 0, sizeof(cloc));
60 memset(&crem, 0, sizeof(crem));
61 memset(&users, 0, sizeof(users));
62 memset(&servs, 0, sizeof(servs));
63 memset(&links, 0, sizeof(links));
64 memset(&classs, 0, sizeof(classs));
69 * Create a new struct Client structure and set it to initial state.
71 * from == NULL, create local client (a client connected to a socket).
73 * from != NULL, create remote client (behind a socket associated with
74 * the client defined by 'from').
75 * ('from' is a local client!!).
77 struct Client* make_client(struct Client *from, int status)
79 struct Client *cptr = NULL;
80 size_t size = CLIENT_REMOTE_SIZE;
83 * Check freelists first to see if we can grab a client without
84 * having to call malloc.
87 size = CLIENT_LOCAL_SIZE;
89 cptr = (struct Client*) MyMalloc(size);
92 * NOTE: Do not remove this, a lot of code depends on the entire
93 * structure being zeroed out
95 memset(cptr, 0, size); /* All variables are 0 by default */
98 if (size == CLIENT_LOCAL_SIZE)
104 /* Note: structure is zero (memset) */
105 cptr->from = from ? from : cptr; /* 'from' of local client is self! */
106 cptr->status = status;
108 strcpy(cptr->username, "unknown");
110 if (CLIENT_LOCAL_SIZE == size) {
113 cptr->since = cptr->lasttime = cptr->firsttime = CurrentTime;
114 cptr->lastnick = TStime();
115 cptr->nextnick = CurrentTime - NICK_DELAY;
116 cptr->nexttarget = CurrentTime - (TARGET_DELAY * (STARTTARGETS - 1));
117 cptr->handler = UNREGISTERED_HANDLER;
122 void free_client(struct Client *cptr)
124 if (cptr && cptr->local) {
126 * make sure we have cleaned up local resources
129 --cptr->dns_reply->ref_count;
131 ip_registry_local_disconnect(cptr);
134 DBufClear(&cptr->sendQ);
135 DBufClear(&cptr->recvQ);
137 release_listener(cptr->listener);
140 * forget to remove the client from the hash table?
142 assert(cptr->hnext == cptr);
147 struct Server *make_server(struct Client *cptr)
149 struct Server *serv = cptr->serv;
153 serv = (struct Server*) MyMalloc(sizeof(struct Server));
155 memset(serv, 0, sizeof(struct Server)); /* All variables are 0 by default */
160 cptr->serv->lag = 60000;
162 DupString(serv->last_error_msg, "<>"); /* String must be non-empty */
168 * Taken the code from ExitOneClient() for this and placed it here.
171 void remove_client_from_list(struct Client *cptr)
174 cptr->prev->next = cptr->next;
176 GlobalClientList = cptr->next;
177 GlobalClientList->prev = 0;
180 cptr->next->prev = cptr->prev;
182 cptr->next = cptr->prev = 0;
184 if (IsUser(cptr) && cptr->user) {
185 add_history(cptr, 0);
189 free_user(cptr->user);
194 if (cptr->serv->user) {
195 free_user(cptr->serv->user);
196 cptr->serv->user = 0;
198 if (cptr->serv->client_list)
199 MyFree(cptr->serv->client_list);
200 MyFree(cptr->serv->last_error_msg);
216 * Although only a small routine, it appears in a number of places
217 * as a collection of a few lines...functions like this *should* be
218 * in this file, shouldnt they ? after all, this is list.c, isn't it ?
221 void add_client_to_list(struct Client *cptr)
224 * Since we always insert new clients to the top of the list,
225 * this should mean the "me" is the bottom most item in the list.
226 * XXX - don't always count on the above, things change
229 cptr->next = GlobalClientList;
230 GlobalClientList = cptr;
232 cptr->next->prev = cptr;
236 * Look for ptr in the linked listed pointed to by link.
238 struct SLink *find_user_link(struct SLink *lp, struct Client *ptr)
243 if (lp->value.cptr == ptr)
250 struct SLink *make_link(void)
254 lp = (struct SLink*) MyMalloc(sizeof(struct SLink));
262 void free_link(struct SLink *lp)
270 struct DLink *add_dlink(struct DLink **lpp, struct Client *cp)
272 struct DLink* lp = (struct DLink*) MyMalloc(sizeof(struct DLink));
276 if ((lp->next = *lpp))
282 void remove_dlink(struct DLink **lpp, struct DLink *lp)
288 if ((lp->prev->next = lp->next))
289 lp->next->prev = lp->prev;
291 else if ((*lpp = lp->next))
292 lp->next->prev = NULL;
296 struct ConfClass *make_class(void)
298 struct ConfClass *tmp;
300 tmp = (struct ConfClass*) MyMalloc(sizeof(struct ConfClass));
308 void free_class(struct ConfClass * tmp)
317 void send_listinfo(struct Client *cptr, char *name)
319 int inuse = 0, mem = 0, tmp = 0;
321 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Local: inuse: %d(%d)",
322 inuse += cloc.inuse, tmp = cloc.inuse * CLIENT_LOCAL_SIZE);
324 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Remote: inuse: %d(%d)",
325 crem.inuse, tmp = crem.inuse * CLIENT_REMOTE_SIZE);
328 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Users: inuse: %d(%d)",
329 users.inuse, tmp = users.inuse * sizeof(struct User));
331 inuse += users.inuse;
332 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Servs: inuse: %d(%d)",
333 servs.inuse, tmp = servs.inuse * sizeof(struct Server));
335 inuse += servs.inuse;
336 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Links: inuse: %d(%d)",
337 links.inuse, tmp = links.inuse * sizeof(struct SLink));
339 inuse += links.inuse;
340 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
341 classs.inuse, tmp = classs.inuse * sizeof(struct ConfClass));
343 inuse += classs.inuse;
344 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Confs: inuse: %d(%d)",
345 GlobalConfCount, tmp = GlobalConfCount * sizeof(struct ConfItem));
347 inuse += GlobalConfCount;
348 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Totals: inuse %d %d",