2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "control_controller.h"
17 #include "../manager.h"
18 #include "../gateway.h"
25 typedef struct private_control_controller_t private_control_controller_t
;
28 * private data of the task manager
30 struct private_control_controller_t
{
35 control_controller_t
public;
44 * handle the result of a control operation
46 static void handle_result(private_control_controller_t
*this, fast_request_t
*r
,
56 while (e
->enumerate(e
, &xml
, &name
, &value
))
58 if (streq(name
, "status"))
60 if (value
&& atoi(value
) == 0)
62 r
->set(r
, "result", "Operation executed successfully:");
66 r
->set(r
, "result", "Operation failed:");
69 else if (streq(name
, "log"))
71 e1
= xml
->children(xml
);
72 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
74 if (streq(name
, "item"))
76 r
->setf(r
, "log.%d=%s", ++num
, value
);
83 r
->render(r
, "templates/control/result.cs");
87 r
->set(r
, "title", "Error");
88 r
->set(r
, "error", "controlling the gateway failed");
89 r
->render(r
, "templates/error.cs");
94 * initiate an IKE or CHILD SA
96 static void initiate(private_control_controller_t
*this, fast_request_t
*r
,
97 bool ike
, char *config
)
102 r
->setf(r
, "title=Establishing %s SA %s", ike ?
"IKE" : "CHILD", config
);
103 gateway
= this->manager
->select_gateway(this->manager
, 0);
104 e
= gateway
->initiate(gateway
, ike
, config
);
105 handle_result(this, r
, e
);
109 * terminate an IKE or CHILD SA
111 static void terminate(private_control_controller_t
*this, fast_request_t
*r
,
112 bool ike
, u_int32_t id
)
117 r
->setf(r
, "title=Terminate %s SA %d", ike ?
"IKE" : "CHILD", id
);
118 gateway
= this->manager
->select_gateway(this->manager
, 0);
119 e
= gateway
->terminate(gateway
, ike
, id
);
120 handle_result(this, r
, e
);
123 METHOD(fast_controller_t
, get_name
, char*,
124 private_control_controller_t
*this)
129 METHOD(fast_controller_t
, handle
, void,
130 private_control_controller_t
*this, fast_request_t
*request
, char *action
,
131 char *str
, char *p3
, char *p4
, char *p5
)
133 if (!this->manager
->logged_in(this->manager
))
135 return request
->redirect(request
, "auth/login");
137 if (this->manager
->select_gateway(this->manager
, 0) == NULL
)
139 return request
->redirect(request
, "gateway/list");
145 if (streq(action
, "terminateike"))
147 if (str
&& (id
= atoi(str
)))
149 return terminate(this, request
, TRUE
, id
);
152 if (streq(action
, "terminatechild"))
154 if (str
&& (id
= atoi(str
)))
156 return terminate(this, request
, FALSE
, id
);
159 if (streq(action
, "initiateike"))
163 return initiate(this, request
, TRUE
, str
);
166 if (streq(action
, "initiatechild"))
170 return initiate(this, request
, FALSE
, str
);
174 return request
->redirect(request
, "ikesa/list");
177 METHOD(fast_controller_t
, destroy
, void,
178 private_control_controller_t
*this)
186 fast_controller_t
*control_controller_create(fast_context_t
*context
,
189 private_control_controller_t
*this;
194 .get_name
= _get_name
,
199 .manager
= (manager_t
*)context
,
202 return &this->public.controller
;