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"
29 #include "ircd_string.h"
38 #define BAD_CONF_CLASS ((unsigned int)-1)
39 #define BAD_PING ((unsigned int)-2)
40 #define BAD_CLIENT_CLASS ((unsigned int)-3)
42 static struct ConnectionClass* connClassList = 0;
43 static unsigned int connClassAllocCount;
45 const struct ConnectionClass* get_class_list(void)
50 struct ConnectionClass* make_class(void)
52 struct ConnectionClass *tmp;
54 tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
56 ++connClassAllocCount;
60 void free_class(struct ConnectionClass* p)
63 assert(0 == p->valid);
67 --connClassAllocCount;
72 * init_class - initialize the class list
77 connClassList = (struct ConnectionClass*) make_class();
79 /* We had better not try and free this... */
80 ConClass(connClassList) = "default";
81 PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
82 ConFreq(connClassList) = feature_int(FEAT_CONNECTFREQUENCY);
83 MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
84 MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
85 connClassList->valid = 1;
86 Links(connClassList) = 0;
87 connClassList->next = 0;
91 * class_mark_delete - mark classes for delete
92 * We don't delete the class table, rather mark all entries
93 * for deletion. The table is cleaned up by class_delete_marked(). - avalon
94 * XXX - This destroys data
96 void class_mark_delete(void)
98 struct ConnectionClass* p;
99 assert(0 != connClassList);
101 for (p = connClassList->next; p; p = p->next)
106 * class_delete_marked
107 * delete classes marked for deletion
108 * XXX - memory leak, no one deletes classes that become unused
111 void class_delete_marked(void)
113 struct ConnectionClass* cl;
114 struct ConnectionClass* prev;
116 Debug((DEBUG_DEBUG, "Class check:"));
118 for (prev = cl = connClassList; cl; cl = prev->next) {
119 Debug((DEBUG_DEBUG, "Class %s : CF: %d PF: %d ML: %d LI: %d SQ: %d",
120 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl), Links(cl), MaxSendq(cl)));
122 * unlink marked classes, delete unreferenced ones
127 prev->next = cl->next;
128 if (0 == cl->ref_count)
135 get_conf_class(const struct ConfItem* aconf)
137 if ((aconf) && (aconf->conn_class))
138 return (ConfClass(aconf));
140 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
145 int get_conf_ping(const struct ConfItem* aconf)
148 if (aconf->conn_class)
149 return (ConfPingFreq(aconf));
151 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
157 get_client_class(struct Client *acptr)
160 struct ConnectionClass *cl;
162 /* Return the most recent(first on LL) client class... */
163 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
164 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
166 if (tmp->value.aconf && !(cl = tmp->value.aconf->conn_class))
169 return "(null-class)";
172 unsigned int get_client_ping(struct Client *acptr)
174 unsigned int ping = 0;
176 struct ConfItem *aconf;
179 link = cli_confs(acptr);
183 aconf = link->value.aconf;
184 if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
185 ping2 = get_conf_ping(aconf);
186 if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
193 ping = feature_int(FEAT_PINGFREQUENCY);
194 Debug((DEBUG_DEBUG, "No Attached Confs for: %s", cli_name(acptr)));
197 ping = feature_int(FEAT_PINGFREQUENCY);
198 Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
202 unsigned int get_con_freq(struct ConnectionClass * clptr)
205 return (ConFreq(clptr));
207 return feature_int(FEAT_CONNECTFREQUENCY);
211 * When adding a class, check to see if it is already present first.
212 * if so, then update the information for that class, rather than create
213 * a new entry for it and later delete the old entry.
214 * if no present entry is found, then create a new one and add it in
215 * immeadiately after the first one (class 0).
217 void add_class(char *name, unsigned int ping, unsigned int confreq,
218 unsigned int maxli, unsigned int sendq)
220 struct ConnectionClass* t;
221 struct ConnectionClass* p;
223 t = find_class(name);
224 if ((t == connClassList) && (name != NULL))
226 p = (struct ConnectionClass *) make_class();
232 Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
233 name, confreq, ping, maxli, sendq));
235 ConFreq(p) = confreq;
238 MaxSendq(p) = (sendq > 0) ?
239 sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
245 struct ConnectionClass* find_class(const char *name)
247 struct ConnectionClass *cltmp;
249 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
250 if (!ircd_strcmp(ConClass(cltmp), name))
253 return connClassList;
257 report_classes(struct Client *sptr)
259 struct ConnectionClass *cltmp;
261 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
262 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
263 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
268 get_sendq(struct Client *cptr)
271 assert(0 != cli_local(cptr));
273 if (cli_max_sendq(cptr))
274 return cli_max_sendq(cptr);
276 else if (cli_confs(cptr)) {
278 struct ConnectionClass* cl;
280 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
281 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
283 if (ConClass(cl) != NULL) {
284 cli_max_sendq(cptr) = MaxSendq(cl);
285 return cli_max_sendq(cptr);
289 return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
292 void class_send_meminfo(struct Client* cptr)
294 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
295 connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));