first revision of new manager webapp
[strongswan.git] / src / manager / controller / gateway_controller.c
1 /**
2 * @file gateway_controller.c
3 *
4 * @brief Implementation of gateway_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 "gateway_controller.h"
24 #include "../manager.h"
25 #include "../gateway.h"
26
27 #include <template.h>
28
29 #include <library.h>
30
31
32 typedef struct private_gateway_controller_t private_gateway_controller_t;
33
34 /**
35 * private data of the gateway_controller
36 */
37 struct private_gateway_controller_t {
38
39 /**
40 * public functions
41 */
42 gateway_controller_t public;
43
44 /**
45 * manager instance
46 */
47 manager_t *manager;
48
49 };
50
51 static void list(private_gateway_controller_t *this,
52 request_t *request, response_t *response)
53 {
54 template_t *t;
55 enumerator_t *enumerator;
56 char *name, *address;
57 int id, port;
58
59 t = template_create("templates/gateway/list.cs");
60 enumerator = this->manager->create_gateway_enumerator(this->manager);
61 while (enumerator->enumerate(enumerator, &id, &name, &port, &address))
62 {
63 t->setf(t, "gateways.%d.name=%s", id, name);
64 if (port)
65 {
66 t->setf(t, "gateways.%d.address=tcp://%s:%d", id, address, port);
67 }
68 else
69 {
70 t->setf(t, "gateways.%d.address=unix://%s", id, address);
71 }
72 }
73 enumerator->destroy(enumerator);
74 t->set(t, "action", "select");
75 t->render(t, response);
76 t->destroy(t);
77 }
78
79 static void _select(private_gateway_controller_t *this,
80 request_t *request, response_t *response)
81 {
82 char *id;
83
84 id = request->get_post_data(request, "gateway");
85 if (id)
86 {
87 if (this->manager->select_gateway(this->manager, atoi(id)))
88 {
89 response->redirect(response, "status/ikesalist");
90 return;
91 }
92 }
93 response->printf(response, "selecting dings failed: %s", id);
94 }
95
96 /**
97 * redirect to authentication login
98 */
99 static void login(private_gateway_controller_t *this,
100 request_t *request, response_t *response)
101 {
102 response->redirect(response, "auth/login");
103 }
104
105 /**
106 * Implementation of controller_t.get_name
107 */
108 static char* get_name(private_gateway_controller_t *this)
109 {
110 return "gateway";
111 }
112
113 /**
114 * Implementation of controller_t.get_handler
115 */
116 static controller_handler_t get_handler(private_gateway_controller_t *this, char *name)
117 {
118 if (!this->manager->logged_in(this->manager)) return (controller_handler_t)login;
119 if (streq(name, "list")) return (controller_handler_t)list;
120 if (streq(name, "select")) return (controller_handler_t)_select;
121 return NULL;
122 }
123
124 /**
125 * Implementation of controller_t.destroy
126 */
127 static void destroy(private_gateway_controller_t *this)
128 {
129 free(this);
130 }
131
132 /*
133 * see header file
134 */
135 controller_t *gateway_controller_create(context_t *context, void *param)
136 {
137 private_gateway_controller_t *this = malloc_thing(private_gateway_controller_t);
138
139 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
140 this->public.controller.get_handler = (controller_handler_t(*)(controller_t*,char*))get_handler;
141 this->public.controller.destroy = (void(*)(controller_t*))destroy;
142
143 this->manager = (manager_t*)context;
144
145 return &this->public.controller;
146 }
147