2 * IRC - Internet Relay Chat, ircd/class.c
3 * Copyright (C) 1990 Darren Reed
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "ircd_alloc.h"
25 #include "ircd_reply.h"
34 #define BAD_CONF_CLASS ((unsigned int)-1)
35 #define BAD_PING ((unsigned int)-2)
36 #define BAD_CLIENT_CLASS ((unsigned int)-3)
38 static struct ConnectionClass* connClassList;
39 static unsigned int connClassAllocCount;
41 const struct ConnectionClass* get_class_list(void)
46 struct ConnectionClass* make_class(void)
48 struct ConnectionClass *tmp;
50 tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
52 ++connClassAllocCount;
56 void free_class(struct ConnectionClass* p)
59 assert(0 == p->valid);
61 --connClassAllocCount;
66 * init_class - initialize the class list
70 connClassList = (struct ConnectionClass*) make_class();
72 ConClass(connClassList) = 0;
73 PingFreq(connClassList) = PINGFREQUENCY;
74 ConFreq(connClassList) = CONNECTFREQUENCY;
75 MaxLinks(connClassList) = MAXIMUM_LINKS;
76 MaxSendq(connClassList) = DEFAULTMAXSENDQLENGTH;
77 connClassList->valid = 1;
78 Links(connClassList) = 0;
79 connClassList->next = 0;
83 * class_mark_delete - mark classes for delete
84 * We don't delete the class table, rather mark all entries
85 * for deletion. The table is cleaned up by class_delete_marked(). - avalon
86 * XXX - This destroys data
88 void class_mark_delete(void)
90 struct ConnectionClass* p;
91 assert(0 != connClassList);
93 for (p = connClassList->next; p; p = p->next)
99 * delete classes marked for deletion
100 * XXX - memory leak, no one deletes classes that become unused
103 void class_delete_marked(void)
105 struct ConnectionClass* cl;
106 struct ConnectionClass* prev;
108 Debug((DEBUG_DEBUG, "Class check:"));
110 for (prev = cl = connClassList; cl; cl = prev->next) {
111 Debug((DEBUG_DEBUG, "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
112 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl), Links(cl), MaxSendq(cl)));
114 * unlink marked classes, delete unreferenced ones
119 prev->next = cl->next;
120 if (0 == cl->ref_count)
126 unsigned int get_conf_class(const struct ConfItem* aconf)
128 if ((aconf) && (aconf->conn_class))
129 return (ConfClass(aconf));
131 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
133 return (BAD_CONF_CLASS);
136 int get_conf_ping(const struct ConfItem* aconf)
139 if (aconf->conn_class)
140 return (ConfPingFreq(aconf));
142 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
147 unsigned int get_client_class(struct Client *acptr)
150 struct ConnectionClass *cl;
151 unsigned int retc = BAD_CLIENT_CLASS;
153 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
154 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
156 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
158 if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
162 Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, cli_name(acptr)));
167 unsigned int get_client_ping(struct Client *acptr)
169 unsigned int ping = 0;
171 struct ConfItem *aconf;
174 link = cli_confs(acptr);
178 aconf = link->value.aconf;
179 if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
180 ping2 = get_conf_ping(aconf);
181 if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
188 ping = PINGFREQUENCY;
189 Debug((DEBUG_DEBUG, "No Attached Confs for: %s", cli_name(acptr)));
192 ping = PINGFREQUENCY;
193 Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
197 unsigned int get_con_freq(struct ConnectionClass * clptr)
200 return (ConFreq(clptr));
202 return (CONNECTFREQUENCY);
206 * When adding a class, check to see if it is already present first.
207 * if so, then update the information for that class, rather than create
208 * a new entry for it and later delete the old entry.
209 * if no present entry is found, then create a new one and add it in
210 * immeadiately after the first one (class 0).
212 void add_class(unsigned int conClass, unsigned int ping, unsigned int confreq,
213 unsigned int maxli, unsigned int sendq)
215 struct ConnectionClass* t;
216 struct ConnectionClass* p;
218 t = find_class(conClass);
219 if ((t == connClassList) && (conClass != 0))
221 p = (struct ConnectionClass *) make_class();
227 Debug((DEBUG_DEBUG, "Add Class %u: cf: %u pf: %u ml: %u sq: %d",
228 conClass, confreq, ping, maxli, sendq));
229 ConClass(p) = conClass;
230 ConFreq(p) = confreq;
233 MaxSendq(p) = (sendq > 0) ? sendq : DEFAULTMAXSENDQLENGTH;
239 struct ConnectionClass* find_class(unsigned int cclass)
241 struct ConnectionClass *cltmp;
243 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
244 if (ConClass(cltmp) == cclass)
247 return connClassList;
250 void report_classes(struct Client *sptr)
252 struct ConnectionClass *cltmp;
254 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
255 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
256 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
260 unsigned int get_sendq(struct Client *cptr)
263 assert(0 != cli_local(cptr));
265 if (cli_max_sendq(cptr))
266 return cli_max_sendq(cptr);
268 else if (cli_confs(cptr)) {
270 struct ConnectionClass* cl;
272 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
273 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
275 if (ConClass(cl) != BAD_CLIENT_CLASS) {
276 cli_max_sendq(cptr) = MaxSendq(cl);
277 return cli_max_sendq(cptr);
281 return DEFAULTMAXSENDQLENGTH;
284 void class_send_meminfo(struct Client* cptr)
286 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
287 connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));