Fix intercepts: don't double-snoop throttled customers, ensure
[l2tpns.git] / autothrottle.c
1 #include <string.h>
2 #include "l2tpns.h"
3 #include "plugin.h"
4
5 /* set up throttling based on RADIUS reply */
6
7 char const *cvs_id = "$Id: autothrottle.c,v 1.7 2004-11-05 04:55:26 bodea Exp $";
8
9 int __plugin_api_version = 1;
10 struct pluginfuncs *p;
11
12 #define THROTTLE_KEY "lcp:interface-config"
13
14 int plugin_radius_response(struct param_radius_response *data)
15 {
16 char *t;
17 int i = 0;
18 int rate;
19
20 if (strncmp(data->key, THROTTLE_KEY, strlen(THROTTLE_KEY)) == 0)
21 {
22 char *pt = strdup(data->value);
23 while ((t = strsep(&pt, " ")) != NULL)
24 {
25 if (strcmp(t, "serv") == 0)
26 i = 1;
27 else if (strcmp(t, "o") && i == 1)
28 i = 2;
29 else if (strcmp(t, "i") && i == 1)
30 i = 3;
31 else if (i > 1 && (rate = atoi(t)) > 0)
32 {
33 switch (i)
34 {
35 case 2: // output
36 data->s->throttle_out = rate;
37 free(pt);
38 p->log(3, 0, p->get_id_by_session(data->s), data->s->tunnel, " Set output throttle rate %dkb/s\n", rate);
39 return PLUGIN_RET_OK;
40
41 case 3: //input
42 data->s->throttle_in = rate;
43 free(pt);
44 p->log(3, 0, p->get_id_by_session(data->s), data->s->tunnel, " Set input throttle rate %dkb/s\n", rate);
45 return PLUGIN_RET_OK;
46
47 default:
48 p->log(1, 0, p->get_id_by_session(data->s), data->s->tunnel, "Syntax error in rate limit AV pair: %s=%s\n", data->key, data->value);
49 free(pt);
50 return PLUGIN_RET_OK;
51 }
52 }
53 else
54 {
55 free(pt);
56 p->log(1, 0, p->get_id_by_session(data->s), data->s->tunnel, "Syntax error in rate limit AV pair: %s=%s\n",
57 data->key, data->value);
58 return PLUGIN_RET_OK;
59 }
60 }
61 free(pt);
62 }
63 else if (strcmp(data->key, "throttle") == 0)
64 {
65 if (strcmp(data->value, "yes") == 0)
66 {
67 p->log(3, 0, p->get_id_by_session(data->s), data->s->tunnel, " Throttling user\n");
68 data->s->throttle_in = data->s->throttle_out = config->rl_rate;
69 }
70 else if (strcmp(data->value, "no") == 0)
71 {
72 p->log(3, 0, p->get_id_by_session(data->s), data->s->tunnel, " Not throttling user\n");
73 data->s->throttle_in = data->s->throttle_out = 0;
74 }
75 }
76
77 p->log(4, 0, p->get_id_by_session(data->s), data->s->tunnel, "autothrottle module ignoring AV pair %s=%s\n",
78 data->key, data->value);
79
80 return PLUGIN_RET_OK;
81 }
82
83 int plugin_init(struct pluginfuncs *funcs)
84 {
85 return ((p = funcs)) ? 1 : 0;
86 }