make log a conditional macro for _log
[l2tpns.git] / autothrottle.c
1 #include <string.h>
2 #include <malloc.h>
3 #include <stdlib.h>
4 #include <sys/wait.h>
5 #include <sys/types.h>
6 #include "l2tpns.h"
7 #include "plugin.h"
8 #include "control.h"
9
10 int __plugin_api_version = 1;
11 struct pluginfuncs *p;
12
13 #define THROTTLE_KEY "lcp:interface-config"
14
15 int plugin_radius_response(struct param_radius_response *data)
16 {
17 char *t;
18 int i = 0;
19 int rate;
20
21 if (strncmp(data->key, THROTTLE_KEY, strlen(THROTTLE_KEY)) == 0)
22 {
23 char *pt = strdup(data->value);
24 while ((t = strsep(&pt, " ")) != NULL)
25 {
26 if (strcmp(t, "serv") == 0)
27 i = 1;
28 else if (strcmp(t, "o") && i == 1)
29 i = 2;
30 else if (strcmp(t, "i") && i == 1)
31 i = 3;
32 else if (i > 1 && (rate = atoi(t)) > 0)
33 {
34 switch (i)
35 {
36 case 2: // output
37 data->s->throttle_out = rate;
38 free(pt);
39 p->_log(3, 0, p->get_id_by_session(data->s), data->s->tunnel, " Set output throttle rate %dkb/s\n", rate);
40 return PLUGIN_RET_OK;
41
42 case 3: //input
43 data->s->throttle_in = rate;
44 free(pt);
45 p->_log(3, 0, p->get_id_by_session(data->s), data->s->tunnel, " Set input throttle rate %dkb/s\n", rate);
46 return PLUGIN_RET_OK;
47
48 default:
49 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);
50 free(pt);
51 return PLUGIN_RET_OK;
52 }
53 }
54 else
55 {
56 free(pt);
57 p->_log(1, 0, p->get_id_by_session(data->s), data->s->tunnel, "Syntax error in rate limit AV pair: %s=%s\n",
58 data->key, data->value);
59 return PLUGIN_RET_OK;
60 }
61 }
62 free(pt);
63 }
64 else if (strcmp(data->key, "throttle") == 0)
65 {
66 if (strcmp(data->value, "yes") == 0)
67 {
68 p->_log(3, 0, p->get_id_by_session(data->s), data->s->tunnel, " Throttling user\n");
69 data->s->throttle_in = data->s->throttle_out = config->rl_rate;
70 }
71 else if (strcmp(data->value, "no") == 0)
72 {
73 p->_log(3, 0, p->get_id_by_session(data->s), data->s->tunnel, " Not throttling user\n");
74 data->s->throttle_in = data->s->throttle_out = 0;
75 }
76 }
77
78 p->_log(4, 0, p->get_id_by_session(data->s), data->s->tunnel, "autothrottle module ignoring AV pair %s=%s\n",
79 data->key, data->value);
80
81 return PLUGIN_RET_OK;
82 }
83
84 int plugin_init(struct pluginfuncs *funcs)
85 {
86 return ((p = funcs)) ? 1 : 0;
87 }
88
89 void plugin_done()
90 {
91 }
92