eb438b823c6789c9d26dd3ab1ba8066791f295a2
[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.11 2004-11-30 00:46:36 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, sizeof(THROTTLE_KEY) - 1) == 0)
21 {
22 char *pt;
23
24 if (strncmp(data->value, "serv", 4))
25 return PLUGIN_RET_OK;
26
27 pt = strdup(data->value);
28 while ((t = strsep(&pt, " ")) != NULL)
29 {
30 if (strcmp(t, "serv") == 0)
31 i = 1;
32 else if (strcmp(t, "o") == 0 && i == 1)
33 i = 3;
34 else if (strcmp(t, "i") == 0 && i == 1)
35 i = 2;
36 else if (i > 1 )
37 {
38 if ((rate = strtol(t, (char **)NULL, 10)) < 0 )
39 {
40 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
41 "Syntax Error: rate is not a number %s\n", t);
42 free(pt);
43 return PLUGIN_RET_OK;
44 }
45
46 switch (i)
47 {
48 case 2: // output
49 data->s->throttle_out = rate;
50 free(pt);
51 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
52 " Set output throttle rate %dkb/s\n", rate);
53
54 return PLUGIN_RET_OK;
55
56 case 3: //input
57 data->s->throttle_in = rate;
58 free(pt);
59 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
60 " Set input throttle rate %dkb/s\n", rate);
61
62 return PLUGIN_RET_OK;
63
64 default:
65 p->log(1, p->get_id_by_session(data->s), data->s->tunnel,
66 "Syntax error in rate limit AV pair: %s=%s\n", data->key, data->value);
67
68 free(pt);
69 return PLUGIN_RET_OK;
70 }
71 }
72 }
73
74 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
75 "Unknown lcp:interface-config AV pair %s=%s\n",data->key, data->value);
76 free(pt);
77 return PLUGIN_RET_OK;
78 }
79 else if (strcmp(data->key, "throttle") == 0)
80 {
81 if (strcmp(data->value, "yes") == 0)
82 {
83 unsigned long *rate = p->getconfig("throttle_speed", UNSIGNED_LONG);
84 if (rate)
85 {
86 if (*rate)
87 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
88 " Throttling user to %dkb/s\n", *rate);
89 else
90 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
91 " Not throttling user (throttle_speed=0)\n");
92
93 data->s->throttle_in = data->s->throttle_out = *rate;
94 }
95 else
96 p->log(1, p->get_id_by_session(data->s), data->s->tunnel,
97 "Not throttling user (can't get throttle_speed)\n");
98 }
99 else if (strcmp(data->value, "no") == 0)
100 {
101 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
102 " Not throttling user\n");
103
104 data->s->throttle_in = data->s->throttle_out = 0;
105 }
106 }
107
108 p->log(4, p->get_id_by_session(data->s), data->s->tunnel,
109 "autothrottle module ignoring AV pair %s=%s\n",
110 data->key, data->value);
111
112 return PLUGIN_RET_OK;
113 }
114
115 int plugin_init(struct pluginfuncs *funcs)
116 {
117 return ((p = funcs)) ? 1 : 0;
118 }