Drop duplication definition of MODS_INCREMENT macro.
[seven-1.x.git] / src / class.c
blob1d3f60127a112cda2e4e067a4fcb418592f881c7
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * class.c: Controls connection classes.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
24 * $Id: class.c 26 2006-09-20 18:02:06Z spb $
27 #include "stdinc.h"
28 #include "config.h"
30 #include "tools.h"
31 #include "class.h"
32 #include "client.h"
33 #include "common.h"
34 #include "ircd.h"
35 #include "numeric.h"
36 #include "s_conf.h"
37 #include "s_newconf.h"
38 #include "send.h"
39 #include "irc_string.h"
40 #include "memory.h"
41 #include "patricia.h"
43 #define BAD_CONF_CLASS -1
44 #define BAD_PING -2
45 #define BAD_CLIENT_CLASS -3
47 dlink_list class_list;
48 struct Class *default_class;
50 struct Class *
51 make_class(void)
53 struct Class *tmp;
55 tmp = (struct Class *) MyMalloc(sizeof(struct Class));
57 ConFreq(tmp) = DEFAULT_CONNECTFREQUENCY;
58 PingFreq(tmp) = DEFAULT_PINGFREQUENCY;
59 MaxUsers(tmp) = 1;
60 MaxSendq(tmp) = DEFAULT_SENDQ;
62 tmp->ip_limits = New_Patricia(PATRICIA_BITS);
63 return tmp;
66 void
67 free_class(struct Class *tmp)
69 if(tmp->ip_limits)
70 Destroy_Patricia(tmp->ip_limits, NULL);
72 MyFree(tmp->class_name);
73 MyFree(tmp);
78 * get_conf_ping
80 * inputs - pointer to struct ConfItem
81 * output - ping frequency
82 * side effects - NONE
84 static int
85 get_conf_ping(struct ConfItem *aconf)
87 if((aconf) && ClassPtr(aconf))
88 return (ConfPingFreq(aconf));
90 return (BAD_PING);
94 * get_client_class
96 * inputs - pointer to client struct
97 * output - pointer to name of class
98 * side effects - NONE
100 const char *
101 get_client_class(struct Client *target_p)
103 const char *retc = "unknown";
105 if(target_p == NULL || IsMe(target_p))
106 return retc;
108 if(IsServer(target_p))
110 struct server_conf *server_p = target_p->localClient->att_sconf;
111 return server_p->class_name;
113 else
115 struct ConfItem *aconf;
116 aconf = target_p->localClient->att_conf;
118 if((aconf == NULL) || (aconf->className == NULL))
119 retc = "default";
120 else
121 retc = aconf->className;
124 return (retc);
128 * get_client_ping
130 * inputs - pointer to client struct
131 * output - ping frequency
132 * side effects - NONE
135 get_client_ping(struct Client *target_p)
137 int ping = 0;
139 if(IsServer(target_p))
141 struct server_conf *server_p = target_p->localClient->att_sconf;
142 ping = PingFreq(server_p->class);
144 else
146 struct ConfItem *aconf;
148 aconf = target_p->localClient->att_conf;
150 if(aconf != NULL)
151 ping = get_conf_ping(aconf);
152 else
153 ping = DEFAULT_PINGFREQUENCY;
156 if(ping <= 0)
157 ping = DEFAULT_PINGFREQUENCY;
159 return ping;
163 * get_con_freq
165 * inputs - pointer to class struct
166 * output - connection frequency
167 * side effects - NONE
170 get_con_freq(struct Class *clptr)
172 if(clptr)
173 return (ConFreq(clptr));
174 return (DEFAULT_CONNECTFREQUENCY);
177 /* add_class()
179 * input - class to add
180 * output -
181 * side effects - class is added to class_list if new, else old class
182 * is updated with new values.
184 void
185 add_class(struct Class *classptr)
187 struct Class *tmpptr;
189 tmpptr = find_class(classptr->class_name);
191 if(tmpptr == default_class)
193 dlinkAddAlloc(classptr, &class_list);
194 CurrUsers(classptr) = 0;
196 else
198 MaxUsers(tmpptr) = MaxUsers(classptr);
199 MaxLocal(tmpptr) = MaxLocal(classptr);
200 MaxGlobal(tmpptr) = MaxGlobal(classptr);
201 MaxIdent(tmpptr) = MaxIdent(classptr);
202 PingFreq(tmpptr) = PingFreq(classptr);
203 MaxSendq(tmpptr) = MaxSendq(classptr);
204 ConFreq(tmpptr) = ConFreq(classptr);
205 CidrBitlen(tmpptr) = CidrBitlen(classptr);
206 CidrAmount(tmpptr) = CidrAmount(classptr);
208 free_class(classptr);
214 * find_class
216 * inputs - string name of class
217 * output - corresponding class pointer
218 * side effects - NONE
220 struct Class *
221 find_class(const char *classname)
223 struct Class *cltmp;
224 dlink_node *ptr;
226 if(classname == NULL)
227 return default_class;
229 DLINK_FOREACH(ptr, class_list.head)
231 cltmp = ptr->data;
233 if(!strcmp(ClassName(cltmp), classname))
234 return cltmp;
237 return default_class;
241 * check_class
243 * inputs - NONE
244 * output - NONE
245 * side effects -
247 void
248 check_class()
250 struct Class *cltmp;
251 dlink_node *ptr;
252 dlink_node *next_ptr;
254 DLINK_FOREACH_SAFE(ptr, next_ptr, class_list.head)
256 cltmp = ptr->data;
258 if(MaxUsers(cltmp) < 0)
260 dlinkDestroy(ptr, &class_list);
261 if(CurrUsers(cltmp) <= 0)
262 free_class(cltmp);
268 * initclass
270 * inputs - NONE
271 * output - NONE
272 * side effects -
274 void
275 initclass()
277 default_class = make_class();
278 DupString(ClassName(default_class), "default");
282 * report_classes
284 * inputs - pointer to client to report to
285 * output - NONE
286 * side effects - class report is done to this client
288 void
289 report_classes(struct Client *source_p)
291 struct Class *cltmp;
292 dlink_node *ptr;
294 DLINK_FOREACH(ptr, class_list.head)
296 cltmp = ptr->data;
298 sendto_one_numeric(source_p, RPL_STATSYLINE,
299 form_str(RPL_STATSYLINE),
300 ClassName(cltmp), PingFreq(cltmp),
301 ConFreq(cltmp), MaxUsers(cltmp),
302 MaxSendq(cltmp),
303 MaxLocal(cltmp), MaxIdent(cltmp),
304 MaxGlobal(cltmp), MaxIdent(cltmp),
305 CurrUsers(cltmp));
308 /* also output the default class */
309 sendto_one_numeric(source_p, RPL_STATSYLINE, form_str(RPL_STATSYLINE),
310 ClassName(default_class), PingFreq(default_class),
311 ConFreq(default_class), MaxUsers(default_class),
312 MaxSendq(default_class),
313 MaxLocal(default_class), MaxIdent(default_class),
314 MaxGlobal(default_class), MaxIdent(default_class),
315 CurrUsers(default_class));
319 * get_sendq
321 * inputs - pointer to client
322 * output - sendq for this client as found from its class
323 * side effects - NONE
325 long
326 get_sendq(struct Client *client_p)
328 if(client_p == NULL || IsMe(client_p))
329 return DEFAULT_SENDQ;
331 if(IsServer(client_p))
333 struct server_conf *server_p;
334 server_p = client_p->localClient->att_sconf;
335 return MaxSendq(server_p->class);
337 else
339 struct ConfItem *aconf = client_p->localClient->att_conf;
341 if(aconf != NULL && aconf->status & CONF_CLIENT)
342 return ConfMaxSendq(aconf);
345 return DEFAULT_SENDQ;