improved load balancing algorithm.
[l2tpns.git] / sessionctl.c
1 #include <string.h>
2 #include <sys/socket.h>
3 #include <linux/rtnetlink.h>
4
5 #include "l2tpns.h"
6 #include "plugin.h"
7 #include "control.h"
8
9 /* session control */
10
11 int plugin_api_version = PLUGIN_API_VERSION;
12 static struct pluginfuncs *f = 0;
13
14 char *plugin_control_help[] = {
15 " drop USER|SID [REASON] Shutdown user session",
16 " kill USER|SID [REASON] Kill user session",
17 0
18 };
19
20 int plugin_control(struct param_control *data)
21 {
22 sessionidt session;
23 sessiont *s = 0;
24 char *end;
25 char *reason;
26
27 if (data->argc < 1)
28 return PLUGIN_RET_OK;
29
30 if (strcmp(data->argv[0], "drop") && strcmp(data->argv[0], "kill"))
31 return PLUGIN_RET_OK; // not for us
32
33 if (!data->iam_master)
34 return PLUGIN_RET_NOTMASTER;
35
36 if (data->argc < 2 || data->argc > 3)
37 {
38 data->response = NSCTL_RES_ERR;
39 data->additional = "requires username or session id and optional reason";
40 return PLUGIN_RET_STOP;
41 }
42
43 if (!(session = strtol(data->argv[1], &end, 10)) || *end)
44 session = f->get_session_by_username(data->argv[1]);
45
46 if (session)
47 s = f->get_session_by_id(session);
48
49 if (!s || !s->ip)
50 {
51 data->response = NSCTL_RES_ERR;
52 data->additional = "session not found";
53 return PLUGIN_RET_STOP;
54 }
55
56 if (data->argc > 2)
57 reason = data->argv[2];
58 else
59 reason = "Requested by administrator.";
60
61 if (data->argv[0][0] == 'd')
62 f->sessionshutdown(session, reason, CDN_ADMIN_DISC, TERM_ADMIN_RESET);
63 else
64 f->sessionkill(session, reason);
65
66 data->response = NSCTL_RES_OK;
67 data->additional = 0;
68
69 return PLUGIN_RET_STOP;
70 }
71
72 int plugin_init(struct pluginfuncs *funcs)
73 {
74 return ((f = funcs)) ? 1 : 0;
75 }