8 char const *cvs_id
= "$Id: throttlectl.c,v 1.7 2005-06-28 14:48:28 bodea Exp $";
10 int plugin_api_version
= PLUGIN_API_VERSION
;
11 static struct pluginfuncs
*p
= 0;
13 char *plugin_control_help
[] = {
14 " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic",
15 " unthrottle USER|SID Stop throttling user",
19 int plugin_control(struct param_control
*data
)
31 if (strcmp(data
->argv
[0], "throttle")
32 && strcmp(data
->argv
[0], "unthrottle"))
33 return PLUGIN_RET_OK
; // not for us
35 if (!data
->iam_master
)
36 return PLUGIN_RET_NOTMASTER
;
38 flag
= data
->argv
[0][0] == 't';
42 if (data
->argc
< 2 || data
->argc
> 6)
44 data
->response
= NSCTL_RES_ERR
;
45 data
->additional
= "requires username or session id and optional rate(s)";
46 return PLUGIN_RET_STOP
;
53 data
->response
= NSCTL_RES_ERR
;
54 data
->additional
= "requires username or session id";
55 return PLUGIN_RET_STOP
;
59 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
60 session
= p
->get_session_by_username(data
->argv
[1]);
63 s
= p
->get_session_by_id(session
);
67 data
->response
= NSCTL_RES_ERR
;
68 data
->additional
= "session not found";
69 return PLUGIN_RET_STOP
;
74 rate_in
= rate_out
= -1;
77 unsigned long *rate
= p
->getconfig("throttle_speed", UNSIGNED_LONG
);
78 rate_in
= rate_out
= *rate
;
80 else if (data
->argc
== 3)
82 rate_in
= rate_out
= atoi(data
->argv
[2]);
87 for (i
= 2; i
< data
->argc
- 1; i
+= 2)
89 int len
= strlen(data
->argv
[i
]);
90 if (!strncmp(data
->argv
[i
], "in", len
))
92 rate_in
= atoi(data
->argv
[i
+1]);
94 else if (!strncmp(data
->argv
[i
], "out", len
))
96 rate_out
= atoi(data
->argv
[i
+1]);
100 data
->response
= NSCTL_RES_ERR
;
101 data
->additional
= "invalid rate";
102 return PLUGIN_RET_STOP
;
107 if (!rate_in
|| !rate_out
)
109 data
->response
= NSCTL_RES_ERR
;
110 data
->additional
= "invalid rate";
111 return PLUGIN_RET_STOP
;
115 if (rate_in
!= -1 && rate_in
== s
->throttle_in
&&
116 rate_out
!= -1 && rate_out
== s
->throttle_out
)
118 data
->response
= NSCTL_RES_ERR
;
119 data
->additional
= flag
? "already throttled" : "not throttled";
120 return PLUGIN_RET_STOP
;
123 p
->throttle(session
, rate_in
, rate_out
);
124 p
->session_changed(session
);
126 data
->response
= NSCTL_RES_OK
;
127 data
->additional
= 0;
129 return PLUGIN_RET_STOP
;
132 int plugin_radius_reset(struct param_radius_reset
*data
)
134 p
->throttle(p
->get_id_by_session(data
->s
), 0, 0);
135 return PLUGIN_RET_OK
;
138 int plugin_init(struct pluginfuncs
*funcs
)
140 return ((p
= funcs
)) ? 1 : 0;