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