update extended acl syntax
[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.10 2004/11/29 02:17:17 bodea Exp $";
8
9 int plugin_api_version = PLUGIN_API_VERSION;
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, p->get_id_by_session(data->s), data->s->tunnel,
39 " Set output throttle rate %dkb/s\n", rate);
40
41 return PLUGIN_RET_OK;
42
43 case 3: //input
44 data->s->throttle_in = rate;
45 free(pt);
46 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
47 " Set input throttle rate %dkb/s\n", rate);
48
49 return PLUGIN_RET_OK;
50
51 default:
52 p->log(1, p->get_id_by_session(data->s), data->s->tunnel,
53 "Syntax error in rate limit AV pair: %s=%s\n", data->key, data->value);
54
55 free(pt);
56 return PLUGIN_RET_OK;
57 }
58 }
59 else
60 {
61 free(pt);
62 p->log(1, p->get_id_by_session(data->s), data->s->tunnel,
63 "Syntax error in rate limit AV pair: %s=%s\n",
64 data->key, data->value);
65
66 return PLUGIN_RET_OK;
67 }
68 }
69 free(pt);
70 }
71 else if (strcmp(data->key, "throttle") == 0)
72 {
73 if (strcmp(data->value, "yes") == 0)
74 {
75 unsigned long *rate = p->getconfig("throttle_speed", UNSIGNED_LONG);
76 if (rate)
77 {
78 if (*rate)
79 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
80 " Throttling user to %dkb/s\n", *rate);
81 else
82 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
83 " Not throttling user (throttle_speed=0)\n");
84
85 data->s->throttle_in = data->s->throttle_out = *rate;
86 }
87 else
88 p->log(1, p->get_id_by_session(data->s), data->s->tunnel,
89 "Not throttling user (can't get throttle_speed)\n");
90 }
91 else if (strcmp(data->value, "no") == 0)
92 {
93 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
94 " Not throttling user\n");
95
96 data->s->throttle_in = data->s->throttle_out = 0;
97 }
98 }
99
100 p->log(4, p->get_id_by_session(data->s), data->s->tunnel,
101 "autothrottle module ignoring AV pair %s=%s\n",
102 data->key, data->value);
103
104 return PLUGIN_RET_OK;
105 }
106
107 int plugin_init(struct pluginfuncs *funcs)
108 {
109 return ((p = funcs)) ? 1 : 0;
110 }