~ubuntu-branches/ubuntu/trusty/charybdis/trusty-proposed

« back to all changes in this revision

Viewing changes to src/class.c

  • Committer: Package Import Robot
  • Author(s): Antoine Beaupré
  • Date: 2011-11-10 23:07:37 UTC
  • Revision ID: package-import@ubuntu.com-20111110230737-kqo6qsglp5oh02hr
Tags: upstream-3.3.0
Import upstream version 3.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  ircd-ratbox: A slightly useful ircd.
 
3
 *  class.c: Controls connection classes.
 
4
 *
 
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
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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
 
23
 *
 
24
 *  $Id: class.c 254 2005-09-21 23:35:12Z nenolod $
 
25
 */
 
26
 
 
27
#include "stdinc.h"
 
28
#include "config.h"
 
29
 
 
30
#include "class.h"
 
31
#include "client.h"
 
32
#include "common.h"
 
33
#include "ircd.h"
 
34
#include "numeric.h"
 
35
#include "s_conf.h"
 
36
#include "s_newconf.h"
 
37
#include "send.h"
 
38
#include "match.h"
 
39
 
 
40
#define BAD_CONF_CLASS          -1
 
41
#define BAD_PING                -2
 
42
#define BAD_CLIENT_CLASS        -3
 
43
 
 
44
rb_dlink_list class_list;
 
45
struct Class *default_class;
 
46
 
 
47
struct Class *
 
48
make_class(void)
 
49
{
 
50
        struct Class *tmp;
 
51
 
 
52
        tmp = rb_malloc(sizeof(struct Class));
 
53
 
 
54
        ConFreq(tmp) = DEFAULT_CONNECTFREQUENCY;
 
55
        PingFreq(tmp) = DEFAULT_PINGFREQUENCY;
 
56
        MaxUsers(tmp) = 1;
 
57
        MaxSendq(tmp) = DEFAULT_SENDQ;
 
58
 
 
59
        tmp->ip_limits = rb_new_patricia(PATRICIA_BITS);
 
60
        return tmp;
 
61
}
 
62
 
 
63
void
 
64
free_class(struct Class *tmp)
 
65
{
 
66
        if(tmp->ip_limits)
 
67
                rb_destroy_patricia(tmp->ip_limits, NULL);
 
68
 
 
69
        rb_free(tmp->class_name);
 
70
        rb_free(tmp);
 
71
 
 
72
}
 
73
 
 
74
/*
 
75
 * get_conf_ping
 
76
 *
 
77
 * inputs       - pointer to struct ConfItem
 
78
 * output       - ping frequency
 
79
 * side effects - NONE
 
80
 */
 
81
static int
 
82
get_conf_ping(struct ConfItem *aconf)
 
83
{
 
84
        if((aconf) && ClassPtr(aconf))
 
85
                return (ConfPingFreq(aconf));
 
86
 
 
87
        return (BAD_PING);
 
88
}
 
89
 
 
90
/*
 
91
 * get_client_class
 
92
 *
 
93
 * inputs       - pointer to client struct
 
94
 * output       - pointer to name of class
 
95
 * side effects - NONE
 
96
 */
 
97
const char *
 
98
get_client_class(struct Client *target_p)
 
99
{
 
100
        const char *retc = "unknown";
 
101
 
 
102
        if(target_p == NULL || IsMe(target_p))
 
103
                return retc;
 
104
 
 
105
        if(IsServer(target_p))
 
106
        {
 
107
                struct server_conf *server_p = target_p->localClient->att_sconf;
 
108
                return server_p->class_name;
 
109
        }
 
110
        else
 
111
        {
 
112
                struct ConfItem *aconf;
 
113
                aconf = target_p->localClient->att_conf;
 
114
 
 
115
                if((aconf == NULL) || (aconf->className == NULL))
 
116
                        retc = "default";
 
117
                else
 
118
                        retc = aconf->className;
 
119
        }
 
120
 
 
121
        return (retc);
 
122
}
 
123
 
 
124
/*
 
125
 * get_client_ping
 
126
 *
 
127
 * inputs       - pointer to client struct
 
128
 * output       - ping frequency
 
129
 * side effects - NONE
 
130
 */
 
131
int
 
132
get_client_ping(struct Client *target_p)
 
133
{
 
134
        int ping = 0;
 
135
 
 
136
        if(IsServer(target_p))
 
137
        {
 
138
                struct server_conf *server_p = target_p->localClient->att_sconf;
 
139
                ping = PingFreq(server_p->class);
 
140
        }
 
141
        else
 
142
        {
 
143
                struct ConfItem *aconf;
 
144
 
 
145
                aconf = target_p->localClient->att_conf;
 
146
 
 
147
                if(aconf != NULL)
 
148
                        ping = get_conf_ping(aconf);
 
149
                else
 
150
                        ping = DEFAULT_PINGFREQUENCY;
 
151
        }
 
152
 
 
153
        if(ping <= 0)
 
154
                ping = DEFAULT_PINGFREQUENCY;
 
155
 
 
156
        return ping;
 
157
}
 
158
 
 
159
/*
 
160
 * get_con_freq
 
161
 *
 
162
 * inputs       - pointer to class struct
 
163
 * output       - connection frequency
 
164
 * side effects - NONE
 
165
 */
 
166
int
 
167
get_con_freq(struct Class *clptr)
 
168
{
 
169
        if(clptr)
 
170
                return (ConFreq(clptr));
 
171
        return (DEFAULT_CONNECTFREQUENCY);
 
172
}
 
173
 
 
174
/* add_class()
 
175
 *
 
176
 * input        - class to add
 
177
 * output       -
 
178
 * side effects - class is added to class_list if new, else old class
 
179
 *                is updated with new values.
 
180
 */
 
181
void
 
182
add_class(struct Class *classptr)
 
183
{
 
184
        struct Class *tmpptr;
 
185
 
 
186
        tmpptr = find_class(classptr->class_name);
 
187
 
 
188
        if(tmpptr == default_class)
 
189
        {
 
190
                rb_dlinkAddAlloc(classptr, &class_list);
 
191
                CurrUsers(classptr) = 0;
 
192
        }
 
193
        else
 
194
        {
 
195
                MaxUsers(tmpptr) = MaxUsers(classptr);
 
196
                MaxLocal(tmpptr) = MaxLocal(classptr);
 
197
                MaxGlobal(tmpptr) = MaxGlobal(classptr);
 
198
                MaxIdent(tmpptr) = MaxIdent(classptr);
 
199
                PingFreq(tmpptr) = PingFreq(classptr);
 
200
                MaxSendq(tmpptr) = MaxSendq(classptr);
 
201
                ConFreq(tmpptr) = ConFreq(classptr);
 
202
                CidrIpv4Bitlen(tmpptr) = CidrIpv4Bitlen(classptr);
 
203
                CidrIpv6Bitlen(tmpptr) = CidrIpv6Bitlen(classptr);
 
204
                CidrAmount(tmpptr) = CidrAmount(classptr);
 
205
 
 
206
                free_class(classptr);
 
207
        }
 
208
}
 
209
 
 
210
 
 
211
/*
 
212
 * find_class
 
213
 *
 
214
 * inputs       - string name of class
 
215
 * output       - corresponding class pointer
 
216
 * side effects - NONE
 
217
 */
 
218
struct Class *
 
219
find_class(const char *classname)
 
220
{
 
221
        struct Class *cltmp;
 
222
        rb_dlink_node *ptr;
 
223
 
 
224
        if(classname == NULL)
 
225
                return default_class;
 
226
 
 
227
        RB_DLINK_FOREACH(ptr, class_list.head)
 
228
        {
 
229
                cltmp = ptr->data;
 
230
 
 
231
                if(!strcmp(ClassName(cltmp), classname))
 
232
                        return cltmp;
 
233
        }
 
234
 
 
235
        return default_class;
 
236
}
 
237
 
 
238
/*
 
239
 * check_class
 
240
 *
 
241
 * inputs       - NONE
 
242
 * output       - NONE
 
243
 * side effects - 
 
244
 */
 
245
void
 
246
check_class()
 
247
{
 
248
        struct Class *cltmp;
 
249
        rb_dlink_node *ptr;
 
250
        rb_dlink_node *next_ptr;
 
251
 
 
252
        RB_DLINK_FOREACH_SAFE(ptr, next_ptr, class_list.head)
 
253
        {
 
254
                cltmp = ptr->data;
 
255
 
 
256
                if(MaxUsers(cltmp) < 0)
 
257
                {
 
258
                        rb_dlinkDestroy(ptr, &class_list);
 
259
                        if(CurrUsers(cltmp) <= 0)
 
260
                                free_class(cltmp);
 
261
                }
 
262
        }
 
263
}
 
264
 
 
265
/*
 
266
 * initclass
 
267
 *
 
268
 * inputs       - NONE
 
269
 * output       - NONE
 
270
 * side effects - 
 
271
 */
 
272
void
 
273
initclass()
 
274
{
 
275
        default_class = make_class();
 
276
        ClassName(default_class) = rb_strdup("default");
 
277
}
 
278
 
 
279
/*
 
280
 * report_classes
 
281
 *
 
282
 * inputs       - pointer to client to report to
 
283
 * output       - NONE
 
284
 * side effects - class report is done to this client
 
285
 */
 
286
void
 
287
report_classes(struct Client *source_p)
 
288
{
 
289
        struct Class *cltmp;
 
290
        rb_dlink_node *ptr;
 
291
 
 
292
        RB_DLINK_FOREACH(ptr, class_list.head)
 
293
        {
 
294
                cltmp = ptr->data;
 
295
 
 
296
                sendto_one_numeric(source_p, RPL_STATSYLINE, 
 
297
                                form_str(RPL_STATSYLINE),
 
298
                                ClassName(cltmp), PingFreq(cltmp), 
 
299
                                ConFreq(cltmp), MaxUsers(cltmp), 
 
300
                                MaxSendq(cltmp), 
 
301
                                MaxLocal(cltmp), MaxIdent(cltmp),
 
302
                                MaxGlobal(cltmp), MaxIdent(cltmp),
 
303
                                CurrUsers(cltmp));
 
304
        }
 
305
 
 
306
        /* also output the default class */
 
307
        sendto_one_numeric(source_p, RPL_STATSYLINE, form_str(RPL_STATSYLINE),
 
308
                        ClassName(default_class), PingFreq(default_class), 
 
309
                        ConFreq(default_class), MaxUsers(default_class), 
 
310
                        MaxSendq(default_class), 
 
311
                        MaxLocal(default_class), MaxIdent(default_class),
 
312
                        MaxGlobal(default_class), MaxIdent(default_class),
 
313
                        CurrUsers(default_class));
 
314
}
 
315
 
 
316
/*
 
317
 * get_sendq
 
318
 *
 
319
 * inputs       - pointer to client
 
320
 * output       - sendq for this client as found from its class
 
321
 * side effects - NONE
 
322
 */
 
323
long
 
324
get_sendq(struct Client *client_p)
 
325
{
 
326
        if(client_p == NULL || IsMe(client_p))
 
327
                return DEFAULT_SENDQ;
 
328
 
 
329
        if(IsServer(client_p))
 
330
        {
 
331
                struct server_conf *server_p;
 
332
                server_p = client_p->localClient->att_sconf;
 
333
                return MaxSendq(server_p->class);
 
334
        }
 
335
        else
 
336
        {
 
337
                struct ConfItem *aconf = client_p->localClient->att_conf;
 
338
 
 
339
                if(aconf != NULL && aconf->status & CONF_CLIENT)
 
340
                        return ConfMaxSendq(aconf);
 
341
        }
 
342
 
 
343
        return DEFAULT_SENDQ;
 
344
}