implemented IKE/CHILD_SA close through manager
[strongswan.git] / src / manager / controller / control_controller.c
1 /**
2 * @file control_controller.c
3 *
4 * @brief Implementation of control_controller_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "control_controller.h"
24 #include "../manager.h"
25 #include "../gateway.h"
26
27 #include <xml.h>
28
29 #include <library.h>
30
31
32 typedef struct private_control_controller_t private_control_controller_t;
33
34 /**
35 * private data of the task manager
36 */
37 struct private_control_controller_t {
38
39 /**
40 * public functions
41 */
42 control_controller_t public;
43
44 /**
45 * manager instance
46 */
47 manager_t *manager;
48 };
49
50 /**
51 * terminate a IKE or CHILD SA
52 */
53 static void terminate(private_control_controller_t *this, request_t *r,
54 bool ike, u_int32_t id)
55 {
56 gateway_t *gateway;
57
58 gateway = this->manager->select_gateway(this->manager, 0);
59 if (gateway->terminate(gateway, ike, id))
60 {
61 r->redirect(r, "status/ikesalist");
62 }
63 else
64 {
65 r->set(r, "title", "Error");
66 r->set(r, "error", "controlling the gateway failed");
67 r->render(r, "templates/error.cs");
68 }
69 }
70
71 /**
72 * Implementation of controller_t.get_name
73 */
74 static char* get_name(private_control_controller_t *this)
75 {
76 return "control";
77 }
78
79 /**
80 * Implementation of controller_t.handle
81 */
82 static void handle(private_control_controller_t *this,
83 request_t *request, char *action, char *strid)
84 {
85 if (!this->manager->logged_in(this->manager))
86 {
87 return request->redirect(request, "auth/login");
88 }
89 if (this->manager->select_gateway(this->manager, 0) == NULL)
90 {
91 return request->redirect(request, "gateway/list");
92 }
93 if (action)
94 {
95 u_int32_t id;
96
97 if (streq(action, "terminateike"))
98 {
99 if (strid && (id = atoi(strid)))
100 {
101 return terminate(this, request, TRUE, id);
102 }
103 }
104 if (streq(action, "terminatechild"))
105 {
106 if (strid && (id = atoi(strid)))
107 {
108 return terminate(this, request, FALSE, id);
109 }
110 }
111 }
112 return request->redirect(request, "status/ikesalist");
113 }
114
115 /**
116 * Implementation of controller_t.destroy
117 */
118 static void destroy(private_control_controller_t *this)
119 {
120 free(this);
121 }
122
123 /*
124 * see header file
125 */
126 controller_t *control_controller_create(context_t *context, void *param)
127 {
128 private_control_controller_t *this = malloc_thing(private_control_controller_t);
129
130 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
131 this->public.controller.handle = (void(*)(controller_t*,request_t*,char*,char*,char*,char*,char*))handle;
132 this->public.controller.destroy = (void(*)(controller_t*))destroy;
133
134 this->manager = (manager_t*)context;
135
136 return &this->public.controller;
137 }
138