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_features.h"
26 #include "ircd_reply.h"
35 #define BAD_CONF_CLASS ((unsigned int)-1)
36 #define BAD_PING ((unsigned int)-2)
37 #define BAD_CLIENT_CLASS ((unsigned int)-3)
39 static struct ConnectionClass* connClassList;
40 static unsigned int connClassAllocCount;
42 const struct ConnectionClass* get_class_list(void)
47 struct ConnectionClass* make_class(void)
49 struct ConnectionClass *tmp;
51 tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
53 ++connClassAllocCount;
57 void free_class(struct ConnectionClass* p)
60 assert(0 == p->valid);
62 --connClassAllocCount;
67 * init_class - initialize the class list
71 connClassList = (struct ConnectionClass*) make_class();
73 ConClass(connClassList) = 0;
74 PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
75 ConFreq(connClassList) = feature_int(FEAT_CONNECTFREQUENCY);
76 MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
77 MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
78 connClassList->valid = 1;
79 Links(connClassList) = 0;
80 connClassList->next = 0;
84 * class_mark_delete - mark classes for delete
85 * We don't delete the class table, rather mark all entries
86 * for deletion. The table is cleaned up by class_delete_marked(). - avalon
87 * XXX - This destroys data
89 void class_mark_delete(void)
91 struct ConnectionClass* p;
92 assert(0 != connClassList);
94 for (p = connClassList->next; p; p = p->next)
100 * delete classes marked for deletion
101 * XXX - memory leak, no one deletes classes that become unused
104 void class_delete_marked(void)
106 struct ConnectionClass* cl;
107 struct ConnectionClass* prev;
109 Debug((DEBUG_DEBUG, "Class check:"));
111 for (prev = cl = connClassList; cl; cl = prev->next) {
112 Debug((DEBUG_DEBUG, "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
113 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl), Links(cl), MaxSendq(cl)));
115 * unlink marked classes, delete unreferenced ones
120 prev->next = cl->next;
121 if (0 == cl->ref_count)
127 unsigned int get_conf_class(const struct ConfItem* aconf)
129 if ((aconf) && (aconf->conn_class))
130 return (ConfClass(aconf));
132 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
134 return (BAD_CONF_CLASS);
137 int get_conf_ping(const struct ConfItem* aconf)
140 if (aconf->conn_class)
141 return (ConfPingFreq(aconf));
143 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
148 unsigned int get_client_class(struct Client *acptr)
151 struct ConnectionClass *cl;
152 unsigned int retc = BAD_CLIENT_CLASS;
154 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
155 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
157 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
159 if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
163 Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, cli_name(acptr)));
168 unsigned int get_client_ping(struct Client *acptr)
170 unsigned int ping = 0;
172 struct ConfItem *aconf;
175 link = cli_confs(acptr);
179 aconf = link->value.aconf;
180 if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
181 ping2 = get_conf_ping(aconf);
182 if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
189 ping = feature_int(FEAT_PINGFREQUENCY);
190 Debug((DEBUG_DEBUG, "No Attached Confs for: %s", cli_name(acptr)));
193 ping = feature_int(FEAT_PINGFREQUENCY);
194 Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
198 unsigned int get_con_freq(struct ConnectionClass * clptr)
201 return (ConFreq(clptr));
203 return feature_int(FEAT_CONNECTFREQUENCY);
207 * When adding a class, check to see if it is already present first.
208 * if so, then update the information for that class, rather than create
209 * a new entry for it and later delete the old entry.
210 * if no present entry is found, then create a new one and add it in
211 * immeadiately after the first one (class 0).
213 void add_class(unsigned int conClass, unsigned int ping, unsigned int confreq,
214 unsigned int maxli, unsigned int sendq)
216 struct ConnectionClass* t;
217 struct ConnectionClass* p;
219 t = find_class(conClass);
220 if ((t == connClassList) && (conClass != 0))
222 p = (struct ConnectionClass *) make_class();
228 Debug((DEBUG_DEBUG, "Add Class %u: cf: %u pf: %u ml: %u sq: %d",
229 conClass, confreq, ping, maxli, sendq));
230 ConClass(p) = conClass;
231 ConFreq(p) = confreq;
234 MaxSendq(p) = (sendq > 0) ? sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
240 struct ConnectionClass* find_class(unsigned int cclass)
242 struct ConnectionClass *cltmp;
244 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
245 if (ConClass(cltmp) == cclass)
248 return connClassList;
251 void report_classes(struct Client *sptr)
253 struct ConnectionClass *cltmp;
255 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
256 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
257 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
261 unsigned int get_sendq(struct Client *cptr)
264 assert(0 != cli_local(cptr));
266 if (cli_max_sendq(cptr))
267 return cli_max_sendq(cptr);
269 else if (cli_confs(cptr)) {
271 struct ConnectionClass* cl;
273 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
274 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
276 if (ConClass(cl) != BAD_CLIENT_CLASS) {
277 cli_max_sendq(cptr) = MaxSendq(cl);
278 return cli_max_sendq(cptr);
282 return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
285 void class_send_meminfo(struct Client* cptr)
287 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
288 connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));