2.0.16 release
[l2tpns.git] / throttlectl.c
1 #include <string.h>
2 #include "l2tpns.h"
3 #include "plugin.h"
4 #include "control.h"
5
6 /* throttle control */
7
8 char const *cvs_id = "$Id: throttlectl.c,v 1.6 2004/12/01 04:44:29 bodea Exp $";
9
10 int plugin_api_version = PLUGIN_API_VERSION;
11 static struct pluginfuncs *p = 0;
12
13 char *plugin_control_help[] = {
14 " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic",
15 " unthrottle USER|SID Stop throttling user",
16 0
17 };
18
19 int plugin_init(struct pluginfuncs *funcs)
20 {
21 if (!funcs)
22 return 0;
23
24 p = funcs;
25 return 1;
26 }
27
28 int plugin_control(struct param_control *data)
29 {
30 sessionidt session;
31 sessiont *s = 0;
32 int flag;
33 char *end;
34 int rate_in = 0;
35 int rate_out = 0;
36
37 if (data->argc < 1)
38 return PLUGIN_RET_OK;
39
40 if (strcmp(data->argv[0], "throttle")
41 && strcmp(data->argv[0], "unthrottle"))
42 return PLUGIN_RET_OK; // not for us
43
44 if (!data->iam_master)
45 return PLUGIN_RET_NOTMASTER;
46
47 flag = data->argv[0][0] == 't';
48
49 if (flag)
50 {
51 if (data->argc < 2 || data->argc > 6)
52 {
53 data->response = NSCTL_RES_ERR;
54 data->additional = "requires username or session id and optional rate(s)";
55 return PLUGIN_RET_STOP;
56 }
57 }
58 else
59 {
60 if (data->argc != 2)
61 {
62 data->response = NSCTL_RES_ERR;
63 data->additional = "requires username or session id";
64 return PLUGIN_RET_STOP;
65 }
66 }
67
68 if (!(session = strtol(data->argv[1], &end, 10)) || *end)
69 session = p->get_session_by_username(data->argv[1]);
70
71 if (session)
72 s = p->get_session_by_id(session);
73
74 if (!s || !s->ip)
75 {
76 data->response = NSCTL_RES_ERR;
77 data->additional = "session not found";
78 return PLUGIN_RET_STOP;
79 }
80
81 if (flag)
82 {
83 rate_in = rate_out = -1;
84 if (data->argc == 2)
85 {
86 unsigned long *rate = p->getconfig("throttle_speed", UNSIGNED_LONG);
87 rate_in = rate_out = *rate;
88 }
89 else if (data->argc == 3)
90 {
91 rate_in = rate_out = atoi(data->argv[2]);
92 }
93 else
94 {
95 int i;
96 for (i = 2; i < data->argc - 1; i += 2)
97 {
98 int len = strlen(data->argv[i]);
99 if (!strncmp(data->argv[i], "in", len))
100 {
101 rate_in = atoi(data->argv[i+1]);
102 }
103 else if (!strncmp(data->argv[i], "out", len))
104 {
105 rate_out = atoi(data->argv[i+1]);
106 }
107 else
108 {
109 data->response = NSCTL_RES_ERR;
110 data->additional = "invalid rate";
111 return PLUGIN_RET_STOP;
112 }
113 }
114 }
115
116 if (!rate_in || !rate_out)
117 {
118 data->response = NSCTL_RES_ERR;
119 data->additional = "invalid rate";
120 return PLUGIN_RET_STOP;
121 }
122 }
123
124 if (rate_in != -1 && rate_in == s->throttle_in &&
125 rate_out != -1 && rate_out == s->throttle_out)
126 {
127 data->response = NSCTL_RES_ERR;
128 data->additional = flag ? "already throttled" : "not throttled";
129 return PLUGIN_RET_STOP;
130 }
131
132 p->throttle(session, rate_in, rate_out);
133 p->session_changed(session);
134
135 data->response = NSCTL_RES_OK;
136 data->additional = 0;
137
138 return PLUGIN_RET_STOP;
139 }