added subnets of CHILD_SAs to xml interface
[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->set(t, "title", "Choose gateway");
76 t->render(t, response);
77 t->destroy(t);
78 }
79
80 static void _select(private_gateway_controller_t *this,
81 request_t *request, response_t *response)
82 {
83 char *id;
84
85 id = request->get_post_data(request, "gateway");
86 if (id)
87 {
88 if (this->manager->select_gateway(this->manager, atoi(id)))
89 {
90 response->redirect(response, "status/ikesalist");
91 return;
92 }
93 }
94 response->printf(response, "selecting gateway failed: %s", id);
95 }
96
97 /**
98 * Implementation of controller_t.get_name
99 */
100 static char* get_name(private_gateway_controller_t *this)
101 {
102 return "gateway";
103 }
104
105 /**
106 * Implementation of controller_t.handle
107 */
108 static void handle(private_gateway_controller_t *this,
109 request_t *request, response_t *response, char *action)
110 {
111 if (!this->manager->logged_in(this->manager))
112 {
113 return response->redirect(response, "auth/login");
114 }
115 if (action)
116 {
117 if (streq(action, "list"))
118 {
119 return list(this, request, response);
120 }
121 else if (streq(action, "select"))
122 {
123 return _select(this, request, response);
124 }
125 }
126 response->redirect(response, "gateway/list");
127 }
128
129
130 /**
131 * Implementation of controller_t.destroy
132 */
133 static void destroy(private_gateway_controller_t *this)
134 {
135 free(this);
136 }
137
138 /*
139 * see header file
140 */
141 controller_t *gateway_controller_create(context_t *context, void *param)
142 {
143 private_gateway_controller_t *this = malloc_thing(private_gateway_controller_t);
144
145 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
146 this->public.controller.handle = (void(*)(controller_t*,request_t*,response_t*,char*,char*,char*,char*,char*))handle;
147 this->public.controller.destroy = (void(*)(controller_t*))destroy;
148
149 this->manager = (manager_t*)context;
150
151 return &this->public.controller;
152 }
153