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 = 0;
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
72 connClassList = (struct ConnectionClass*) make_class();
74 ConClass(connClassList) = 0;
75 PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
76 ConFreq(connClassList) = feature_int(FEAT_CONNECTFREQUENCY);
77 MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
78 MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
79 connClassList->valid = 1;
80 Links(connClassList) = 0;
81 connClassList->next = 0;
85 * class_mark_delete - mark classes for delete
86 * We don't delete the class table, rather mark all entries
87 * for deletion. The table is cleaned up by class_delete_marked(). - avalon
88 * XXX - This destroys data
90 void class_mark_delete(void)
92 struct ConnectionClass* p;
93 assert(0 != connClassList);
95 for (p = connClassList->next; p; p = p->next)
100 * class_delete_marked
101 * delete classes marked for deletion
102 * XXX - memory leak, no one deletes classes that become unused
105 void class_delete_marked(void)
107 struct ConnectionClass* cl;
108 struct ConnectionClass* prev;
110 Debug((DEBUG_DEBUG, "Class check:"));
112 for (prev = cl = connClassList; cl; cl = prev->next) {
113 Debug((DEBUG_DEBUG, "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
114 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl), Links(cl), MaxSendq(cl)));
116 * unlink marked classes, delete unreferenced ones
121 prev->next = cl->next;
122 if (0 == cl->ref_count)
128 unsigned int get_conf_class(const struct ConfItem* aconf)
130 if ((aconf) && (aconf->conn_class))
131 return (ConfClass(aconf));
133 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
135 return (BAD_CONF_CLASS);
138 int get_conf_ping(const struct ConfItem* aconf)
141 if (aconf->conn_class)
142 return (ConfPingFreq(aconf));
144 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
149 unsigned int get_client_class(struct Client *acptr)
152 struct ConnectionClass *cl;
153 unsigned int retc = BAD_CLIENT_CLASS;
155 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
156 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
158 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
160 if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
164 Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, cli_name(acptr)));
169 unsigned int get_client_ping(struct Client *acptr)
171 unsigned int ping = 0;
173 struct ConfItem *aconf;
176 link = cli_confs(acptr);
180 aconf = link->value.aconf;
181 if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
182 ping2 = get_conf_ping(aconf);
183 if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
190 ping = feature_int(FEAT_PINGFREQUENCY);
191 Debug((DEBUG_DEBUG, "No Attached Confs for: %s", cli_name(acptr)));
194 ping = feature_int(FEAT_PINGFREQUENCY);
195 Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
199 unsigned int get_con_freq(struct ConnectionClass * clptr)
202 return (ConFreq(clptr));
204 return feature_int(FEAT_CONNECTFREQUENCY);
208 * When adding a class, check to see if it is already present first.
209 * if so, then update the information for that class, rather than create
210 * a new entry for it and later delete the old entry.
211 * if no present entry is found, then create a new one and add it in
212 * immeadiately after the first one (class 0).
214 void add_class(unsigned int conClass, unsigned int ping, unsigned int confreq,
215 unsigned int maxli, unsigned int sendq)
217 struct ConnectionClass* t;
218 struct ConnectionClass* p;
220 t = find_class(conClass);
221 if ((t == connClassList) && (conClass != 0))
223 p = (struct ConnectionClass *) make_class();
229 Debug((DEBUG_DEBUG, "Add Class %u: cf: %u pf: %u ml: %u sq: %d",
230 conClass, confreq, ping, maxli, sendq));
231 ConClass(p) = conClass;
232 ConFreq(p) = confreq;
235 MaxSendq(p) = (sendq > 0) ? sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
241 struct ConnectionClass* find_class(unsigned int cclass)
243 struct ConnectionClass *cltmp;
245 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
246 if (ConClass(cltmp) == cclass)
249 return connClassList;
252 void report_classes(struct Client *sptr)
254 struct ConnectionClass *cltmp;
256 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
257 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
258 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
262 unsigned int get_sendq(struct Client *cptr)
265 assert(0 != cli_local(cptr));
267 if (cli_max_sendq(cptr))
268 return cli_max_sendq(cptr);
270 else if (cli_confs(cptr)) {
272 struct ConnectionClass* cl;
274 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
275 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
277 if (ConClass(cl) != BAD_CLIENT_CLASS) {
278 cli_max_sendq(cptr) = MaxSendq(cl);
279 return cli_max_sendq(cptr);
283 return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
286 void class_send_meminfo(struct Client* cptr)
288 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
289 connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));