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));
57 ++connClassAllocCount;
61 void free_class(struct ConnectionClass* p)
65 assert(0 == p->valid);
69 --connClassAllocCount;
74 * init_class - initialize the class list
79 connClassList = (struct ConnectionClass*) make_class();
81 /* We had better not try and free this... */
82 ConClass(connClassList) = "default";
83 PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
84 ConFreq(connClassList) = feature_int(FEAT_CONNECTFREQUENCY);
85 MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
86 MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
87 connClassList->valid = 1;
88 Links(connClassList) = 0;
89 connClassList->next = 0;
93 * class_mark_delete - mark classes for delete
94 * We don't delete the class table, rather mark all entries
95 * for deletion. The table is cleaned up by class_delete_marked(). - avalon
96 * XXX - This destroys data
98 void class_mark_delete(void)
100 struct ConnectionClass* p;
101 assert(0 != connClassList);
103 for (p = connClassList->next; p; p = p->next)
108 * class_delete_marked
109 * delete classes marked for deletion
110 * XXX - memory leak, no one deletes classes that become unused
113 void class_delete_marked(void)
115 struct ConnectionClass* cl;
116 struct ConnectionClass* prev;
118 Debug((DEBUG_DEBUG, "Class check:"));
120 for (prev = cl = connClassList; cl; cl = prev->next) {
121 Debug((DEBUG_DEBUG, "Class %s : CF: %d PF: %d ML: %d LI: %d SQ: %d",
122 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl), Links(cl), MaxSendq(cl)));
124 * unlink marked classes, delete unreferenced ones
130 prev->next = cl->next;
131 if (0 == --cl->ref_count)
138 get_conf_class(const struct ConfItem* aconf)
140 if ((aconf) && (aconf->conn_class))
141 return (ConfClass(aconf));
143 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
148 int get_conf_ping(const struct ConfItem* aconf)
151 if (aconf->conn_class)
152 return (ConfPingFreq(aconf));
154 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
160 get_client_class(struct Client *acptr)
163 struct ConnectionClass *cl;
165 /* Return the most recent(first on LL) client class... */
166 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
167 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
169 if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
172 return "(null-class)";
175 unsigned int get_con_freq(struct ConnectionClass * clptr)
178 return (ConFreq(clptr));
180 return feature_int(FEAT_CONNECTFREQUENCY);
184 * When adding a class, check to see if it is already present first.
185 * if so, then update the information for that class, rather than create
186 * a new entry for it and later delete the old entry.
187 * if no present entry is found, then create a new one and add it in
188 * immeadiately after the first one (class 0).
190 void add_class(char *name, unsigned int ping, unsigned int confreq,
191 unsigned int maxli, unsigned int sendq)
193 struct ConnectionClass* t;
194 struct ConnectionClass* p;
196 t = find_class(name);
197 if ((t == connClassList) && (name != NULL))
199 p = (struct ConnectionClass *) make_class();
205 if (ConClass(t) != NULL)
209 Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
210 name, confreq, ping, maxli, sendq));
212 ConFreq(p) = confreq;
215 MaxSendq(p) = (sendq > 0) ?
216 sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
222 struct ConnectionClass* find_class(const char *name)
224 struct ConnectionClass *cltmp;
226 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
227 if (!ircd_strcmp(ConClass(cltmp), name))
230 return connClassList;
234 report_classes(struct Client *sptr)
236 struct ConnectionClass *cltmp;
238 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
239 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
240 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
245 get_sendq(struct Client *cptr)
248 assert(0 != cli_local(cptr));
250 if (cli_max_sendq(cptr))
251 return cli_max_sendq(cptr);
253 else if (cli_confs(cptr)) {
255 struct ConnectionClass* cl;
257 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
258 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
260 if (ConClass(cl) != NULL) {
261 cli_max_sendq(cptr) = MaxSendq(cl);
262 return cli_max_sendq(cptr);
266 return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
269 void class_send_meminfo(struct Client* cptr)
271 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
272 connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));