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, 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, 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, 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
);
124 * Implementation of controller_t.get_name
126 static char* get_name(private_control_controller_t
*this)
132 * Implementation of controller_t.handle
134 static void handle(private_control_controller_t
*this,
135 request_t
*request
, char *action
, char *str
)
137 if (!this->manager
->logged_in(this->manager
))
139 return request
->redirect(request
, "auth/login");
141 if (this->manager
->select_gateway(this->manager
, 0) == NULL
)
143 return request
->redirect(request
, "gateway/list");
149 if (streq(action
, "terminateike"))
151 if (str
&& (id
= atoi(str
)))
153 return terminate(this, request
, TRUE
, id
);
156 if (streq(action
, "terminatechild"))
158 if (str
&& (id
= atoi(str
)))
160 return terminate(this, request
, FALSE
, id
);
163 if (streq(action
, "initiateike"))
167 return initiate(this, request
, TRUE
, str
);
170 if (streq(action
, "initiatechild"))
174 return initiate(this, request
, FALSE
, str
);
178 return request
->redirect(request
, "ikesa/list");
182 * Implementation of controller_t.destroy
184 static void destroy(private_control_controller_t
*this)
192 controller_t
*control_controller_create(context_t
*context
, void *param
)
194 private_control_controller_t
*this = malloc_thing(private_control_controller_t
);
196 this->public.controller
.get_name
= (char*(*)(controller_t
*))get_name
;
197 this->public.controller
.handle
= (void(*)(controller_t
*,request_t
*,char*,char*,char*,char*,char*))handle
;
198 this->public.controller
.destroy
= (void(*)(controller_t
*))destroy
;
200 this->manager
= (manager_t
*)context
;
202 return &this->public.controller
;