8 char const *cvs_id
= "$Id: throttlectl.c,v 1.3 2004-11-18 06:41:03 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_init(struct pluginfuncs
*funcs
)
28 int plugin_control(struct param_control
*data
)
40 if (strcmp(data
->argv
[0], "throttle")
41 && strcmp(data
->argv
[0], "unthrottle"))
42 return PLUGIN_RET_OK
; // not for us
44 if (!data
->iam_master
)
45 return PLUGIN_RET_NOTMASTER
;
47 flag
= data
->argv
[0][0] != 'g';
51 if (data
->argc
< 2 || data
->argc
> 4)
53 data
->response
= NSCTL_RES_ERR
;
54 data
->additional
= "requires username or session id and optional rate(s)";
55 return PLUGIN_RET_STOP
;
62 data
->response
= NSCTL_RES_ERR
;
63 data
->additional
= "requires username or session id";
64 return PLUGIN_RET_STOP
;
68 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
69 session
= p
->get_session_by_username(data
->argv
[1]);
72 s
= p
->get_session_by_id(session
);
76 data
->response
= NSCTL_RES_ERR
;
77 data
->additional
= "session not found";
78 return PLUGIN_RET_STOP
;
83 rate_in
= rate_out
= -1;
86 unsigned long *rate
= p
->getconfig("throttle_speed", UNSIGNED_LONG
);
87 rate_in
= rate_out
= *rate
;
89 else if (data
->argc
== 3)
91 rate_in
= rate_out
= atoi(data
->argv
[2]);
96 for (i
= 2; i
< data
->argc
- 1; i
+= 2)
98 int len
= strlen(data
->argv
[i
]);
99 if (!strncmp(data
->argv
[i
], "in", len
))
101 rate_in
= atoi(data
->argv
[i
+1]);
103 else if (!strncmp(data
->argv
[i
], "out", len
))
105 rate_out
= atoi(data
->argv
[i
+1]);
109 data
->response
= NSCTL_RES_ERR
;
110 data
->additional
= "invalid rate";
111 return PLUGIN_RET_STOP
;
116 if (!rate_in
|| !rate_out
)
118 data
->response
= NSCTL_RES_ERR
;
119 data
->additional
= "invalid rate";
120 return PLUGIN_RET_STOP
;
124 if (rate_in
!= -1 && rate_in
== s
->throttle_in
&&
125 rate_out
!= -1 && rate_out
== s
->throttle_out
)
127 data
->response
= NSCTL_RES_ERR
;
128 data
->additional
= flag
? "already throttled" : "not throttled";
129 return PLUGIN_RET_STOP
;
132 p
->throttle(session
, rate_in
, rate_out
);
133 p
->session_changed(session
);
135 data
->response
= NSCTL_RES_OK
;
136 data
->additional
= 0;
138 return PLUGIN_RET_STOP
;