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