first revision of new manager webapp
[strongswan.git] / src / manager / manager.c
1 /**
2 * @file manager.c
3 *
4 * @brief Implementation of manager_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 "manager.h"
24
25 #include "gateway.h"
26
27 #include <utils/linked_list.h>
28
29 typedef struct private_manager_t private_manager_t;
30
31 /**
32 * private data of manager
33 */
34 struct private_manager_t {
35
36 /**
37 * public functions
38 */
39 manager_t public;
40
41 /**
42 * underlying database
43 */
44 database_t *db;
45
46 /**
47 * user id, if we are logged in
48 */
49 int user;
50
51 /**
52 * selected gateway
53 */
54 gateway_t *gateway;
55 };
56
57 /**
58 * Implementation of manager_t.create_gateway_enumerator.
59 */
60 static enumerator_t* create_gateway_enumerator(private_manager_t *this)
61 {
62 return this->db->create_gateway_enumerator(this->db, this->user);
63 }
64
65 /**
66 * Implementation of manager_t.select_gateway.
67 */
68 static gateway_t* select_gateway(private_manager_t *this, int select_id)
69 {
70 if (select_id != 0)
71 {
72 enumerator_t *enumerator;
73 int id, port;
74 char *name, *address;
75 host_t *host;
76
77 if (this->gateway) this->gateway->destroy(this->gateway);
78 this->gateway = NULL;
79
80 enumerator = this->db->create_gateway_enumerator(this->db, this->user);
81 while (enumerator->enumerate(enumerator, &id, &name, &port, &address))
82 {
83 if (select_id == id)
84 {
85 if (port != 0)
86 {
87 host = host_create_from_string(address, port);
88 if (host)
89 {
90 this->gateway = gateway_create(name, host);
91 }
92 }
93 break;
94 }
95 }
96 enumerator->destroy(enumerator);
97 }
98 return this->gateway;
99 }
100
101 /**
102 * Implementation of manager_t.logged_in.
103 */
104 static bool logged_in(private_manager_t *this)
105 {
106 return this->user != 0;
107 }
108
109 /**
110 * Implementation of manager_t.login.
111 */
112 static bool login(private_manager_t *this, char *username, char *password)
113 {
114 if (!this->user)
115 {
116 this->user = this->db->login(this->db, username, password);
117 }
118 return this->user != 0;
119 }
120
121 /**
122 * Implementation of manager_t.logout.
123 */
124 static void logout(private_manager_t *this)
125 {
126 this->user = 0;
127 this->gateway->destroy(this->gateway);
128 this->gateway = NULL;
129 }
130
131 /**
132 * Implementation of manager_t.destroy
133 */
134 static void destroy(private_manager_t *this)
135 {
136 if (this->gateway) this->gateway->destroy(this->gateway);
137 free(this);
138 }
139
140 /*
141 * see header file
142 */
143 manager_t *manager_create(database_t *database)
144 {
145 private_manager_t *this = malloc_thing(private_manager_t);
146
147 this->public.login = (bool(*)(manager_t*, char *username, char *password))login;
148 this->public.logged_in = (bool(*)(manager_t*))logged_in;
149 this->public.logout = (void(*)(manager_t*))logout;
150 this->public.create_gateway_enumerator = (enumerator_t*(*)(manager_t*))create_gateway_enumerator;
151 this->public.select_gateway = (gateway_t*(*)(manager_t*, int id))select_gateway;
152 this->public.context.destroy = (void(*)(context_t*))destroy;
153
154 this->user = 0;
155 this->db = database;
156 this->gateway = NULL;
157
158 return &this->public;
159 }
160