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.
26 #include "ircd_alloc.h"
27 #include "ircd_features.h"
28 #include "ircd_reply.h"
37 #define BAD_CONF_CLASS ((unsigned int)-1)
38 #define BAD_PING ((unsigned int)-2)
39 #define BAD_CLIENT_CLASS ((unsigned int)-3)
41 static struct ConnectionClass* connClassList = 0;
42 static unsigned int connClassAllocCount;
44 const struct ConnectionClass* get_class_list(void)
49 struct ConnectionClass* make_class(void)
51 struct ConnectionClass *tmp;
53 tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
55 ++connClassAllocCount;
59 void free_class(struct ConnectionClass* p)
62 assert(0 == p->valid);
64 --connClassAllocCount;
69 * init_class - initialize the class list
74 connClassList = (struct ConnectionClass*) make_class();
76 ConClass(connClassList) = 0;
77 PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
78 ConFreq(connClassList) = feature_int(FEAT_CONNECTFREQUENCY);
79 MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
80 MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
81 connClassList->valid = 1;
82 Links(connClassList) = 0;
83 connClassList->next = 0;
87 * class_mark_delete - mark classes for delete
88 * We don't delete the class table, rather mark all entries
89 * for deletion. The table is cleaned up by class_delete_marked(). - avalon
90 * XXX - This destroys data
92 void class_mark_delete(void)
94 struct ConnectionClass* p;
95 assert(0 != connClassList);
97 for (p = connClassList->next; p; p = p->next)
102 * class_delete_marked
103 * delete classes marked for deletion
104 * XXX - memory leak, no one deletes classes that become unused
107 void class_delete_marked(void)
109 struct ConnectionClass* cl;
110 struct ConnectionClass* prev;
112 Debug((DEBUG_DEBUG, "Class check:"));
114 for (prev = cl = connClassList; cl; cl = prev->next) {
115 Debug((DEBUG_DEBUG, "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
116 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl), Links(cl), MaxSendq(cl)));
118 * unlink marked classes, delete unreferenced ones
123 prev->next = cl->next;
124 if (0 == cl->ref_count)
130 unsigned int get_conf_class(const struct ConfItem* aconf)
132 if ((aconf) && (aconf->conn_class))
133 return (ConfClass(aconf));
135 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
137 return (BAD_CONF_CLASS);
140 int get_conf_ping(const struct ConfItem* aconf)
143 if (aconf->conn_class)
144 return (ConfPingFreq(aconf));
146 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
151 unsigned int get_client_class(struct Client *acptr)
154 struct ConnectionClass *cl;
155 unsigned int retc = BAD_CLIENT_CLASS;
157 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
158 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
160 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
162 if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
166 Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, cli_name(acptr)));
171 unsigned int get_client_ping(struct Client *acptr)
173 unsigned int ping = 0;
175 struct ConfItem *aconf;
178 link = cli_confs(acptr);
182 aconf = link->value.aconf;
183 if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
184 ping2 = get_conf_ping(aconf);
185 if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
192 ping = feature_int(FEAT_PINGFREQUENCY);
193 Debug((DEBUG_DEBUG, "No Attached Confs for: %s", cli_name(acptr)));
196 ping = feature_int(FEAT_PINGFREQUENCY);
197 Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
201 unsigned int get_con_freq(struct ConnectionClass * clptr)
204 return (ConFreq(clptr));
206 return feature_int(FEAT_CONNECTFREQUENCY);
210 * When adding a class, check to see if it is already present first.
211 * if so, then update the information for that class, rather than create
212 * a new entry for it and later delete the old entry.
213 * if no present entry is found, then create a new one and add it in
214 * immeadiately after the first one (class 0).
216 void add_class(unsigned int conClass, unsigned int ping, unsigned int confreq,
217 unsigned int maxli, unsigned int sendq)
219 struct ConnectionClass* t;
220 struct ConnectionClass* p;
222 t = find_class(conClass);
223 if ((t == connClassList) && (conClass != 0))
225 p = (struct ConnectionClass *) make_class();
231 Debug((DEBUG_DEBUG, "Add Class %u: cf: %u pf: %u ml: %u sq: %d",
232 conClass, confreq, ping, maxli, sendq));
233 ConClass(p) = conClass;
234 ConFreq(p) = confreq;
237 MaxSendq(p) = (sendq > 0) ? sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
243 struct ConnectionClass* find_class(unsigned int cclass)
245 struct ConnectionClass *cltmp;
247 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
248 if (ConClass(cltmp) == cclass)
251 return connClassList;
254 void report_classes(struct Client *sptr)
256 struct ConnectionClass *cltmp;
258 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
259 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
260 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
264 unsigned int get_sendq(struct Client *cptr)
267 assert(0 != cli_local(cptr));
269 if (cli_max_sendq(cptr))
270 return cli_max_sendq(cptr);
272 else if (cli_confs(cptr)) {
274 struct ConnectionClass* cl;
276 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
277 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
279 if (ConClass(cl) != BAD_CLIENT_CLASS) {
280 cli_max_sendq(cptr) = MaxSendq(cl);
281 return cli_max_sendq(cptr);
285 return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
288 void class_send_meminfo(struct Client* cptr)
290 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
291 connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));