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