2 * @file control_controller.c
4 * @brief Implementation of control_controller_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
23 #include "control_controller.h"
24 #include "../manager.h"
25 #include "../gateway.h"
32 typedef struct private_control_controller_t private_control_controller_t
;
35 * private data of the task manager
37 struct private_control_controller_t
{
42 control_controller_t
public;
51 * handle the result of a control operation
53 static void handle_result(private_control_controller_t
*this, request_t
*r
,
63 while (e
->enumerate(e
, &xml
, &name
, &value
))
65 if (streq(name
, "status"))
67 if (value
&& atoi(value
) == 0)
69 r
->set(r
, "result", "Operation executed successfully:");
73 r
->set(r
, "result", "Operation failed:");
76 else if (streq(name
, "log"))
78 e1
= xml
->children(xml
);
79 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
81 if (streq(name
, "item"))
83 r
->setf(r
, "log.%d=%s", ++num
, value
);
90 r
->render(r
, "templates/control/result.cs");
94 r
->set(r
, "title", "Error");
95 r
->set(r
, "error", "controlling the gateway failed");
96 r
->render(r
, "templates/error.cs");
101 * initiate an IKE or CHILD SA
103 static void initiate(private_control_controller_t
*this, request_t
*r
,
104 bool ike
, char *config
)
109 r
->setf(r
, "title=Establishing %s SA %s", ike ?
"IKE" : "CHILD", config
);
110 gateway
= this->manager
->select_gateway(this->manager
, 0);
111 e
= gateway
->initiate(gateway
, ike
, config
);
112 handle_result(this, r
, e
);
116 * terminate an IKE or CHILD SA
118 static void terminate(private_control_controller_t
*this, request_t
*r
,
119 bool ike
, u_int32_t id
)
124 r
->setf(r
, "title=Terminate %s SA %d", ike ?
"IKE" : "CHILD", id
);
125 gateway
= this->manager
->select_gateway(this->manager
, 0);
126 e
= gateway
->terminate(gateway
, ike
, id
);
127 handle_result(this, r
, e
);
131 * Implementation of controller_t.get_name
133 static char* get_name(private_control_controller_t
*this)
139 * Implementation of controller_t.handle
141 static void handle(private_control_controller_t
*this,
142 request_t
*request
, char *action
, char *str
)
144 if (!this->manager
->logged_in(this->manager
))
146 return request
->redirect(request
, "auth/login");
148 if (this->manager
->select_gateway(this->manager
, 0) == NULL
)
150 return request
->redirect(request
, "gateway/list");
156 if (streq(action
, "terminateike"))
158 if (str
&& (id
= atoi(str
)))
160 return terminate(this, request
, TRUE
, id
);
163 if (streq(action
, "terminatechild"))
165 if (str
&& (id
= atoi(str
)))
167 return terminate(this, request
, FALSE
, id
);
170 if (streq(action
, "initiateike"))
174 return initiate(this, request
, TRUE
, str
);
177 if (streq(action
, "initiatechild"))
181 return initiate(this, request
, FALSE
, str
);
185 return request
->redirect(request
, "ikesa/list");
189 * Implementation of controller_t.destroy
191 static void destroy(private_control_controller_t
*this)
199 controller_t
*control_controller_create(context_t
*context
, void *param
)
201 private_control_controller_t
*this = malloc_thing(private_control_controller_t
);
203 this->public.controller
.get_name
= (char*(*)(controller_t
*))get_name
;
204 this->public.controller
.handle
= (void(*)(controller_t
*,request_t
*,char*,char*,char*,char*,char*))handle
;
205 this->public.controller
.destroy
= (void(*)(controller_t
*))destroy
;
207 this->manager
= (manager_t
*)context
;
209 return &this->public.controller
;