8 int plugin_api_version
= PLUGIN_API_VERSION
;
9 static struct pluginfuncs
*f
= 0;
11 char *plugin_control_help
[] = {
12 " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic",
13 " unthrottle USER|SID Stop throttling user",
17 int plugin_control(struct param_control
*data
)
29 if (strcmp(data
->argv
[0], "throttle") &&
30 strcmp(data
->argv
[0], "unthrottle"))
31 return PLUGIN_RET_OK
; // not for us
33 if (!data
->iam_master
)
34 return PLUGIN_RET_NOTMASTER
;
36 flag
= data
->argv
[0][0] == 't';
40 if (data
->argc
< 2 || data
->argc
> 6)
42 data
->response
= NSCTL_RES_ERR
;
43 data
->additional
= "requires username or session id and optional rate(s)";
44 return PLUGIN_RET_STOP
;
51 data
->response
= NSCTL_RES_ERR
;
52 data
->additional
= "requires username or session id";
53 return PLUGIN_RET_STOP
;
57 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
58 session
= f
->get_session_by_username(data
->argv
[1]);
61 s
= f
->get_session_by_id(session
);
65 data
->response
= NSCTL_RES_ERR
;
66 data
->additional
= "session not found";
67 return PLUGIN_RET_STOP
;
72 rate_in
= rate_out
= -1;
75 unsigned long *rate
= f
->getconfig("throttle_speed", UNSIGNED_LONG
);
76 rate_in
= rate_out
= *rate
;
78 else if (data
->argc
== 3)
80 rate_in
= rate_out
= atoi(data
->argv
[2]);
85 for (i
= 2; i
< data
->argc
- 1; i
+= 2)
87 int len
= strlen(data
->argv
[i
]);
88 if (!strncmp(data
->argv
[i
], "in", len
))
90 rate_in
= atoi(data
->argv
[i
+1]);
92 else if (!strncmp(data
->argv
[i
], "out", len
))
94 rate_out
= atoi(data
->argv
[i
+1]);
98 data
->response
= NSCTL_RES_ERR
;
99 data
->additional
= "invalid rate";
100 return PLUGIN_RET_STOP
;
105 if (!rate_in
|| !rate_out
)
107 data
->response
= NSCTL_RES_ERR
;
108 data
->additional
= "invalid rate";
109 return PLUGIN_RET_STOP
;
113 if (rate_in
!= -1 && rate_in
== s
->throttle_in
&&
114 rate_out
!= -1 && rate_out
== s
->throttle_out
)
116 data
->response
= NSCTL_RES_ERR
;
117 data
->additional
= flag
? "already throttled" : "not throttled";
118 return PLUGIN_RET_STOP
;
121 f
->throttle(session
, rate_in
, rate_out
);
122 f
->session_changed(session
);
124 data
->response
= NSCTL_RES_OK
;
125 data
->additional
= 0;
127 return PLUGIN_RET_STOP
;
130 int plugin_init(struct pluginfuncs
*funcs
)
132 return ((f
= funcs
)) ? 1 : 0;