* replaced __thread with pthread_key_t/pthread_setspecific
[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 this->gateway = gateway_create_unix(name);
88 }
89 else
90 {
91 host = host_create_from_string(address, port);
92 if (host)
93 {
94 this->gateway = gateway_create_tcp(name, host);
95 }
96 }
97 break;
98 }
99 }
100 enumerator->destroy(enumerator);
101 }
102 return this->gateway;
103 }
104
105 /**
106 * Implementation of manager_t.logged_in.
107 */
108 static bool logged_in(private_manager_t *this)
109 {
110 return this->user != 0;
111 }
112
113 /**
114 * Implementation of manager_t.login.
115 */
116 static bool login(private_manager_t *this, char *username, char *password)
117 {
118 if (!this->user)
119 {
120 this->user = this->db->login(this->db, username, password);
121 }
122 return this->user != 0;
123 }
124
125 /**
126 * Implementation of manager_t.logout.
127 */
128 static void logout(private_manager_t *this)
129 {
130 if (this->gateway)
131 {
132 this->gateway->destroy(this->gateway);
133 this->gateway = NULL;
134 }
135 this->user = 0;
136 }
137
138 /**
139 * Implementation of manager_t.destroy
140 */
141 static void destroy(private_manager_t *this)
142 {
143 if (this->gateway) this->gateway->destroy(this->gateway);
144 free(this);
145 }
146
147 /*
148 * see header file
149 */
150 manager_t *manager_create(database_t *database)
151 {
152 private_manager_t *this = malloc_thing(private_manager_t);
153
154 this->public.login = (bool(*)(manager_t*, char *username, char *password))login;
155 this->public.logged_in = (bool(*)(manager_t*))logged_in;
156 this->public.logout = (void(*)(manager_t*))logout;
157 this->public.create_gateway_enumerator = (enumerator_t*(*)(manager_t*))create_gateway_enumerator;
158 this->public.select_gateway = (gateway_t*(*)(manager_t*, int id))select_gateway;
159 this->public.context.destroy = (void(*)(context_t*))destroy;
160
161 this->user = 0;
162 this->db = database;
163 this->gateway = NULL;
164
165 return &this->public;
166 }
167