X-Git-Url: http://git.sameswireless.fr/l2tpns.git/blobdiff_plain/4f253feef08f01c03bc63f70f9de3937541ccd79..63492a7d1d9e52143db5b3bcbbe9ab32a2bf00b9:/throttlectl.c?ds=inline diff --git a/throttlectl.c b/throttlectl.c index 2e2c5c7..0f0b055 100644 --- a/throttlectl.c +++ b/throttlectl.c @@ -5,137 +5,131 @@ /* throttle control */ -char const *cvs_id = "$Id: throttlectl.c,v 1.7 2005/06/28 14:48:28 bodea Exp $"; +char const *cvs_id = "$Id: throttlectl.c,v 1.9 2005/10/11 09:04:53 bodea Exp $"; int plugin_api_version = PLUGIN_API_VERSION; -static struct pluginfuncs *p = 0; +static struct pluginfuncs *f = 0; char *plugin_control_help[] = { - " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic", - " unthrottle USER|SID Stop throttling user", - 0 + " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic", + " unthrottle USER|SID Stop throttling user", + 0 }; int plugin_control(struct param_control *data) { - sessionidt session; - sessiont *s = 0; - int flag; - char *end; - int rate_in = 0; - int rate_out = 0; - - if (data->argc < 1) - return PLUGIN_RET_OK; + sessionidt session; + sessiont *s = 0; + int flag; + char *end; + int rate_in = 0; + int rate_out = 0; + + if (data->argc < 1) + return PLUGIN_RET_OK; - if (strcmp(data->argv[0], "throttle") - && strcmp(data->argv[0], "unthrottle")) - return PLUGIN_RET_OK; // not for us + if (strcmp(data->argv[0], "throttle") && + strcmp(data->argv[0], "unthrottle")) + return PLUGIN_RET_OK; // not for us - if (!data->iam_master) - return PLUGIN_RET_NOTMASTER; + if (!data->iam_master) + return PLUGIN_RET_NOTMASTER; - flag = data->argv[0][0] == 't'; + flag = data->argv[0][0] == 't'; - if (flag) + if (flag) + { + if (data->argc < 2 || data->argc > 6) { - if (data->argc < 2 || data->argc > 6) - { - data->response = NSCTL_RES_ERR; - data->additional = "requires username or session id and optional rate(s)"; - return PLUGIN_RET_STOP; - } + data->response = NSCTL_RES_ERR; + data->additional = "requires username or session id and optional rate(s)"; + return PLUGIN_RET_STOP; } - else + } + else + { + if (data->argc != 2) { - if (data->argc != 2) - { - data->response = NSCTL_RES_ERR; - data->additional = "requires username or session id"; - return PLUGIN_RET_STOP; - } + data->response = NSCTL_RES_ERR; + data->additional = "requires username or session id"; + return PLUGIN_RET_STOP; } + } + + if (!(session = strtol(data->argv[1], &end, 10)) || *end) + session = f->get_session_by_username(data->argv[1]); - if (!(session = strtol(data->argv[1], &end, 10)) || *end) - session = p->get_session_by_username(data->argv[1]); + if (session) + s = f->get_session_by_id(session); - if (session) - s = p->get_session_by_id(session); + if (!s || !s->ip) + { + data->response = NSCTL_RES_ERR; + data->additional = "session not found"; + return PLUGIN_RET_STOP; + } - if (!s || !s->ip) + if (flag) + { + rate_in = rate_out = -1; + if (data->argc == 2) { - data->response = NSCTL_RES_ERR; - data->additional = "session not found"; - return PLUGIN_RET_STOP; + unsigned long *rate = f->getconfig("throttle_speed", UNSIGNED_LONG); + rate_in = rate_out = *rate; } - - if (flag) + else if (data->argc == 3) { - rate_in = rate_out = -1; - if (data->argc == 2) + rate_in = rate_out = atoi(data->argv[2]); + } + else + { + int i; + for (i = 2; i < data->argc - 1; i += 2) + { + int len = strlen(data->argv[i]); + if (!strncmp(data->argv[i], "in", len)) { - unsigned long *rate = p->getconfig("throttle_speed", UNSIGNED_LONG); - rate_in = rate_out = *rate; + rate_in = atoi(data->argv[i+1]); } - else if (data->argc == 3) + else if (!strncmp(data->argv[i], "out", len)) { - rate_in = rate_out = atoi(data->argv[2]); + rate_out = atoi(data->argv[i+1]); } else { - int i; - for (i = 2; i < data->argc - 1; i += 2) - { - int len = strlen(data->argv[i]); - if (!strncmp(data->argv[i], "in", len)) - { - rate_in = atoi(data->argv[i+1]); - } - else if (!strncmp(data->argv[i], "out", len)) - { - rate_out = atoi(data->argv[i+1]); - } - else - { - data->response = NSCTL_RES_ERR; - data->additional = "invalid rate"; - return PLUGIN_RET_STOP; - } - } - } - - if (!rate_in || !rate_out) - { - data->response = NSCTL_RES_ERR; - data->additional = "invalid rate"; - return PLUGIN_RET_STOP; + data->response = NSCTL_RES_ERR; + data->additional = "invalid rate"; + return PLUGIN_RET_STOP; } + } } - if (rate_in != -1 && rate_in == s->throttle_in && - rate_out != -1 && rate_out == s->throttle_out) + if (!rate_in || !rate_out) { - data->response = NSCTL_RES_ERR; - data->additional = flag ? "already throttled" : "not throttled"; - return PLUGIN_RET_STOP; + data->response = NSCTL_RES_ERR; + data->additional = "invalid rate"; + return PLUGIN_RET_STOP; } + } - p->throttle(session, rate_in, rate_out); - p->session_changed(session); + if (rate_in != -1 && rate_in == s->throttle_in && + rate_out != -1 && rate_out == s->throttle_out) + { + data->response = NSCTL_RES_ERR; + data->additional = flag ? "already throttled" : "not throttled"; + return PLUGIN_RET_STOP; + } - data->response = NSCTL_RES_OK; - data->additional = 0; + f->throttle(session, rate_in, rate_out); + f->session_changed(session); - return PLUGIN_RET_STOP; -} + data->response = NSCTL_RES_OK; + data->additional = 0; -int plugin_radius_reset(struct param_radius_reset *data) -{ - p->throttle(p->get_id_by_session(data->s), 0, 0); - return PLUGIN_RET_OK; + return PLUGIN_RET_STOP; } int plugin_init(struct pluginfuncs *funcs) { - return ((p = funcs)) ? 1 : 0; + return ((f = funcs)) ? 1 : 0; }