Merge branch 'stroke-timeout'
[strongswan.git] / src / manager / controller / gateway_controller.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "gateway_controller.h"
17 #include "../manager.h"
18 #include "../gateway.h"
19
20 #include <library.h>
21
22
23 typedef struct private_gateway_controller_t private_gateway_controller_t;
24
25 /**
26 * private data of the gateway_controller
27 */
28 struct private_gateway_controller_t {
29
30 /**
31 * public functions
32 */
33 gateway_controller_t public;
34
35 /**
36 * manager instance
37 */
38 manager_t *manager;
39
40 };
41
42 static void list(private_gateway_controller_t *this, request_t *request)
43 {
44 enumerator_t *enumerator;
45 char *name, *address;
46 int id, port;
47
48 enumerator = this->manager->create_gateway_enumerator(this->manager);
49 while (enumerator->enumerate(enumerator, &id, &name, &port, &address))
50 {
51 request->setf(request, "gateways.%d.name=%s", id, name);
52 if (port)
53 {
54 request->setf(request, "gateways.%d.address=tcp://%s:%d",
55 id, address, port);
56 }
57 else
58 {
59 request->setf(request, "gateways.%d.address=unix://%s",
60 id, IPSEC_PIDDIR"/charon.xml");
61 }
62 }
63 enumerator->destroy(enumerator);
64 request->set(request, "action", "select");
65 request->set(request, "title", "Choose gateway");
66 request->render(request, "templates/gateway/list.cs");
67 }
68
69 static void _select(private_gateway_controller_t *this, request_t *request)
70 {
71 char *id;
72
73 id = request->get_query_data(request, "gateway");
74 if (id)
75 {
76 if (this->manager->select_gateway(this->manager, atoi(id)))
77 {
78 request->redirect(request, "ikesa/list");
79 return;
80 }
81 }
82 request->redirect(request, "gateway/list");
83 }
84
85 METHOD(controller_t, get_name, char*,
86 private_gateway_controller_t *this)
87 {
88 return "gateway";
89 }
90
91 METHOD(controller_t, handle, void,
92 private_gateway_controller_t *this, request_t *request, char *action,
93 char *p2, char *p3, char *p4, char *p5)
94 {
95 if (!this->manager->logged_in(this->manager))
96 {
97 return request->redirect(request, "auth/login");
98 }
99 if (action)
100 {
101 if (streq(action, "list"))
102 {
103 return list(this, request);
104 }
105 else if (streq(action, "select"))
106 {
107 return _select(this, request);
108 }
109 }
110 request->redirect(request, "gateway/list");
111 }
112
113 METHOD(controller_t, destroy, void,
114 private_gateway_controller_t *this)
115 {
116 free(this);
117 }
118
119 /*
120 * see header file
121 */
122 controller_t *gateway_controller_create(context_t *context, void *param)
123 {
124 private_gateway_controller_t *this;
125
126 INIT(this,
127 .public = {
128 .controller = {
129 .get_name = _get_name,
130 .handle = _handle,
131 .destroy = _destroy,
132 },
133 },
134 .manager = (manager_t*)context,
135 );
136
137 return &this->public.controller;
138 }
139