don't trample n
[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.13 2004-11-30 07:14:45 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 if (!strncmp(data->key, THROTTLE_KEY, sizeof(THROTTLE_KEY) - 1))
17 {
18 char *sp = strchr(data->value, ' ');
19 char type;
20 int rate;
21
22 if (!sp || sp - data->value < 4 ||
23 strncmp("service-policy", data->value, sp - data->value))
24 return PLUGIN_RET_OK;
25
26 while (*sp == ' ') sp++;
27 data->value = sp;
28
29 if (!(sp = strchr(data->value, ' ')) ||
30 (strncmp("input", data->value, sp - data->value) &&
31 strncmp("output", data->value, sp - data->value)))
32 {
33 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
34 " Not throttling user (invalid type %.*s)\n",
35 sp - data->value, data->value);
36
37 return PLUGIN_RET_OK;
38 }
39
40 type = *data->value;
41
42 while (*sp == ' ') sp++;
43 data->value = sp;
44
45 if ((rate = strtol(data->value, &sp, 10)) < 0 || *sp)
46 {
47 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
48 " Not throttling user (invalid rate %s)\n",
49 data->value);
50
51 return PLUGIN_RET_OK;
52 }
53
54 if (type == 'i')
55 {
56 data->s->throttle_in = rate;
57 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
58 " Throttling user input to %dkb/s\n",
59 rate);
60 }
61 else
62 {
63 data->s->throttle_out = rate;
64 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
65 " Throttling user output to %dkb/s\n",
66 rate);
67 }
68 }
69
70 if (!strcmp(data->key, "throttle"))
71 {
72 if (!strcmp(data->value, "yes"))
73 {
74 unsigned long *rate = p->getconfig("throttle_speed", UNSIGNED_LONG);
75 if (rate)
76 {
77 if (*rate)
78 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
79 " Throttling user to %dkb/s\n", *rate);
80 else
81 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
82 " Not throttling user (throttle_speed=0)\n");
83
84 data->s->throttle_in = data->s->throttle_out = *rate;
85 }
86 else
87 p->log(1, p->get_id_by_session(data->s), data->s->tunnel,
88 " Not throttling user (can't get throttle_speed)\n");
89 }
90 else if (!strcmp(data->value, "no"))
91 {
92 p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
93 " Not throttling user\n");
94
95 data->s->throttle_in = data->s->throttle_out = 0;
96 }
97 }
98
99 return PLUGIN_RET_OK;
100 }
101
102 int plugin_init(struct pluginfuncs *funcs)
103 {
104 return ((p = funcs)) ? 1 : 0;
105 }