2 #include <sys/socket.h>
3 #include <linux/rtnetlink.h>
11 int plugin_api_version
= PLUGIN_API_VERSION
;
12 static struct pluginfuncs
*f
= 0;
14 char *plugin_control_help
[] = {
15 " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic",
16 " unthrottle USER|SID Stop throttling user",
20 int plugin_control(struct param_control
*data
)
32 if (strcmp(data
->argv
[0], "throttle") &&
33 strcmp(data
->argv
[0], "unthrottle"))
34 return PLUGIN_RET_OK
; // not for us
36 if (!data
->iam_master
)
37 return PLUGIN_RET_NOTMASTER
;
39 flag
= data
->argv
[0][0] == 't';
43 if (data
->argc
< 2 || data
->argc
> 6)
45 data
->response
= NSCTL_RES_ERR
;
46 data
->additional
= "requires username or session id and optional rate(s)";
47 return PLUGIN_RET_STOP
;
54 data
->response
= NSCTL_RES_ERR
;
55 data
->additional
= "requires username or session id";
56 return PLUGIN_RET_STOP
;
60 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
61 session
= f
->get_session_by_username(data
->argv
[1]);
64 s
= f
->get_session_by_id(session
);
68 data
->response
= NSCTL_RES_ERR
;
69 data
->additional
= "session not found";
70 return PLUGIN_RET_STOP
;
75 rate_in
= rate_out
= -1;
78 unsigned long *rate
= f
->getconfig("throttle_speed", UNSIGNED_LONG
);
79 rate_in
= rate_out
= *rate
;
81 else if (data
->argc
== 3)
83 rate_in
= rate_out
= atoi(data
->argv
[2]);
88 for (i
= 2; i
< data
->argc
- 1; i
+= 2)
90 int len
= strlen(data
->argv
[i
]);
91 if (!strncmp(data
->argv
[i
], "in", len
))
93 rate_in
= atoi(data
->argv
[i
+1]);
95 else if (!strncmp(data
->argv
[i
], "out", len
))
97 rate_out
= atoi(data
->argv
[i
+1]);
101 data
->response
= NSCTL_RES_ERR
;
102 data
->additional
= "invalid rate";
103 return PLUGIN_RET_STOP
;
108 if (!rate_in
|| !rate_out
)
110 data
->response
= NSCTL_RES_ERR
;
111 data
->additional
= "invalid rate";
112 return PLUGIN_RET_STOP
;
116 if (rate_in
!= -1 && rate_in
== s
->throttle_in
&&
117 rate_out
!= -1 && rate_out
== s
->throttle_out
)
119 data
->response
= NSCTL_RES_ERR
;
120 data
->additional
= flag
? "already throttled" : "not throttled";
121 return PLUGIN_RET_STOP
;
124 f
->throttle(session
, rate_in
, rate_out
);
125 f
->session_changed(session
);
127 data
->response
= NSCTL_RES_OK
;
128 data
->additional
= 0;
130 return PLUGIN_RET_STOP
;
133 int plugin_init(struct pluginfuncs
*funcs
)
135 return ((f
= funcs
)) ? 1 : 0;