4 * Copyright (c) 2014-2015 Samsung Electronics. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
25 #include <dvpnlib-vpn.h>
26 #include <dvpnlib-vpn-manager.h>
27 #include <dvpnlib-vpn-connection.h>
29 #include "vpn-internal.h"
32 vpn_created_cb create_cb;
33 void *create_user_data;
34 vpn_removed_cb remove_cb;
35 void *remove_user_data;
36 vpn_connect_cb connect_cb;
37 void *connect_user_data;
38 vpn_disconnect_cb disconnect_cb;
39 void *disconnect_user_data;
40 GHashTable *state_cb_datas;
43 static struct _vpn_cb_s vpn_callbacks = {0,};
44 static GHashTable *settings_hash;
51 static void print_key_value_string(gpointer key,
52 gpointer value, gpointer user_data)
55 VPN_LOG(VPN_INFO, " %s[%s]=%s",
56 (gchar *)user_data, (gchar *)key, (gchar *)value);
59 VPN_LOG(VPN_INFO, " %s=%s", (gchar *)key, (gchar *)value);
62 vpn_error_e _dvpnlib_error2vpn_error(enum dvpnlib_err err_type)
65 case DVPNLIB_ERR_NONE:
66 return VPN_ERROR_NONE;
67 case DVPNLIB_ERR_ALREADY_EXISTS:
68 return VPN_ERROR_INVALID_OPERATION;
69 case DVPNLIB_ERR_NOT_REGISTERED:
70 return VPN_ERROR_INVALID_OPERATION;
71 case DVPNLIB_ERR_NOT_CONNECTED:
72 return VPN_ERROR_NO_CONNECTION;
73 case DVPNLIB_ERR_ALREADY_CONNECTED:
74 return VPN_ERROR_ALREADY_EXISTS;
75 case DVPNLIB_ERR_IN_PROGRESS:
76 return VPN_ERROR_NOW_IN_PROGRESS;
77 case DVPNLIB_ERR_OPERATION_ABORTED:
78 return VPN_ERROR_OPERATION_ABORTED;
79 case DVPNLIB_ERR_OPERATION_TIMEOUT:
80 case DVPNLIB_ERR_TIMEOUT:
81 return VPN_ERROR_NO_REPLY;
83 return VPN_ERROR_OPERATION_FAILED;
89 *Functions Actually use Default VPN Library
96 rv = dvpnlib_vpn_init();
101 vpn_callbacks.state_cb_datas = g_hash_table_new_full(g_direct_hash,
108 bool _vpn_deinit(void)
110 dvpnlib_vpn_deinit();
112 g_hash_table_remove_all(vpn_callbacks.state_cb_datas);
117 int _vpn_settings_init()
119 if (settings_hash != NULL) {
120 VPN_LOG(VPN_INFO, "Settings Hash: %p Already present!", settings_hash); //LCOV_EXCL_LINE
121 return VPN_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
124 settings_hash = g_hash_table_new_full(
125 g_str_hash, g_str_equal,
127 VPN_LOG(VPN_INFO, "Settings Hash: %p", settings_hash);
129 return VPN_ERROR_NONE;
132 int _vpn_settings_deinit()
134 if (settings_hash == NULL)
135 return VPN_ERROR_INVALID_OPERATION;
137 VPN_LOG(VPN_INFO, "Settings Hash: %p Destroyed", settings_hash);
138 g_hash_table_destroy(settings_hash);
139 settings_hash = NULL;
141 return VPN_ERROR_NONE;
144 int _vpn_settings_set_specific(const char *key, const char *value)
147 "Settings Hash: %p {%s=%s}", settings_hash, key, value);
150 return VPN_ERROR_INVALID_PARAMETER;
152 if (settings_hash == NULL)
153 return VPN_ERROR_INVALID_OPERATION;
155 if (g_hash_table_contains(settings_hash, key)) {
158 VPN_LOG(VPN_INFO, "Settings Hash: %p {%s=%s} (Removed)",
160 (gchar *)g_hash_table_lookup(
161 settings_hash, key));
162 g_hash_table_remove(settings_hash, key);
163 return VPN_ERROR_NONE;
168 g_hash_table_replace(settings_hash,
169 (gpointer)g_strdup(key),
170 (gpointer)g_strdup(value));
172 return VPN_ERROR_NONE;
179 static void __vpn_create_cb(vpn_error_e result)
181 if (vpn_callbacks.create_cb)
182 vpn_callbacks.create_cb(result,
183 vpn_callbacks.create_user_data);
185 vpn_callbacks.create_cb = NULL;
186 vpn_callbacks.create_user_data = NULL;
189 static void vpn_manager_create_cb(enum dvpnlib_err result,
192 VPN_LOG(VPN_INFO, "callback: %d Settings: %p\n", result, user_data);
194 __vpn_create_cb(_dvpnlib_error2vpn_error(result));
197 static void __vpn_remove_cb(vpn_error_e result)
199 if (vpn_callbacks.create_cb)
200 vpn_callbacks.create_cb(result,
201 vpn_callbacks.create_user_data);
203 vpn_callbacks.remove_cb = NULL;
204 vpn_callbacks.remove_user_data = NULL;
207 static void vpn_manager_remove_cb(enum dvpnlib_err result,
210 VPN_LOG(VPN_INFO, "callback: %d Settings: %p\n", result, user_data);
212 __vpn_remove_cb(_dvpnlib_error2vpn_error(result));
216 int _vpn_create(vpn_created_cb callback, void *user_data)
218 enum dvpnlib_err err = DVPNLIB_ERR_NONE;
220 return VPN_ERROR_INVALID_OPERATION;
223 VPN_LOG(VPN_INFO, "");
225 vpn_callbacks.create_cb = callback;
226 vpn_callbacks.create_user_data = user_data;
228 g_hash_table_foreach(settings_hash,
229 print_key_value_string, "VPNSettings");
231 err = dvpnlib_vpn_manager_create(settings_hash,
232 vpn_manager_create_cb, NULL);
233 if (err != DVPNLIB_ERR_NONE)
234 return _dvpnlib_error2vpn_error(err);
236 return VPN_ERROR_NONE;
241 int _vpn_remove(vpn_h handle, vpn_removed_cb callback, void *user_data)
243 enum dvpnlib_err err = DVPNLIB_ERR_NONE;
245 VPN_LOG(VPN_INFO, "");
247 vpn_callbacks.remove_cb = callback;
248 vpn_callbacks.remove_user_data = user_data;
250 GList *connections = vpn_get_connections();
251 if (NULL == g_list_find(connections, handle)) {
252 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
253 return VPN_ERROR_INVALID_PARAMETER;
256 const char *path = vpn_connection_get_path(handle); //LCOV_EXCL_LINE
257 err = dvpnlib_vpn_manager_remove(path, vpn_manager_remove_cb, NULL); //LCOV_EXCL_LINE
258 if (err != DVPNLIB_ERR_NONE) //LCOV_EXCL_LINE
259 return _dvpnlib_error2vpn_error(err); //LCOV_EXCL_LINE
261 return VPN_ERROR_NONE;
266 *Connect Disconnect Callbacks
269 static void __vpn_connect_cb(vpn_error_e result)
271 if (vpn_callbacks.connect_cb)
272 vpn_callbacks.connect_cb(result,
273 vpn_callbacks.connect_user_data);
275 vpn_callbacks.connect_cb = NULL;
276 vpn_callbacks.connect_user_data = NULL;
279 static void vpn_manager_connect_cb(enum dvpnlib_err result,
282 VPN_LOG(VPN_INFO, "callback: %d Settings: %p\n", result, user_data);
284 __vpn_connect_cb(_dvpnlib_error2vpn_error(result));
289 *Connect to VPN Profile
292 int _vpn_connect(vpn_h handle, vpn_removed_cb callback, void *user_data)
294 enum dvpnlib_err err = DVPNLIB_ERR_NONE;
296 VPN_LOG(VPN_INFO, "");
298 vpn_callbacks.connect_cb = callback;
299 vpn_callbacks.connect_user_data = user_data;
301 GList *connections = vpn_get_connections();
302 if (NULL == g_list_find(connections, handle)) {
303 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
304 return VPN_ERROR_INVALID_PARAMETER;
307 enum vpn_connection_state state = vpn_connection_get_state(handle); //LCOV_EXCL_LINE
308 if (state == VPN_CONN_STATE_READY) //LCOV_EXCL_LINE
309 return VPN_ERROR_ALREADY_EXISTS; //LCOV_EXCL_LINE
311 err = vpn_connection_connect(handle, vpn_manager_connect_cb, NULL); //LCOV_EXCL_LINE
312 if (err != DVPNLIB_ERR_NONE) //LCOV_EXCL_LINE
313 return _dvpnlib_error2vpn_error(err); //LCOV_EXCL_LINE
315 return VPN_ERROR_NONE;
319 *Disconnect from VPN Profile
322 int _vpn_disconnect(vpn_h handle)
324 enum dvpnlib_err err = DVPNLIB_ERR_NONE;
326 VPN_LOG(VPN_INFO, "");
328 GList *connections = vpn_get_connections();
329 if (NULL == g_list_find(connections, handle)) {
330 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
331 return VPN_ERROR_INVALID_PARAMETER;
334 enum vpn_connection_state state = vpn_connection_get_state(handle); //LCOV_EXCL_LINE
335 if (state != VPN_CONN_STATE_READY) //LCOV_EXCL_LINE
336 return VPN_ERROR_NO_CONNECTION; //LCOV_EXCL_LINE
338 err = vpn_connection_disconnect(handle); //LCOV_EXCL_LINE
339 if (err != DVPNLIB_ERR_NONE) //LCOV_EXCL_LINE
340 return _dvpnlib_error2vpn_error(err); //LCOV_EXCL_LINE
342 return VPN_ERROR_NONE;
346 *Gets the VPN Handles List from VPN Profile
348 GList *_vpn_get_vpn_handle_list(void)
350 return vpn_get_connections();
354 * Get a specific VPN Handle based on name, host & domain parameters
356 int _vpn_get_vpn_handle(const char *name, const char *host, const char *domain, vpn_h *handle)
358 VPN_LOG(VPN_INFO, "");
360 struct vpn_connection *connection = vpn_get_connection(name, host, domain);
362 if (connection == NULL) {
363 VPN_LOG(VPN_ERROR, "name=%s host=%s domain=%s", name, host, domain ? domain : "NULL");
364 return VPN_ERROR_INVALID_PARAMETER;
367 *handle = connection; //LCOV_EXCL_LINE
368 return VPN_ERROR_NONE; //LCOV_EXCL_LINE
372 * Get VPN Info (Name) from VPN Handle
374 int _vpn_get_vpn_info_name(vpn_h handle, const char **name)
376 VPN_LOG(VPN_INFO, "");
378 GList *connections = vpn_get_connections();
379 if (NULL == g_list_find(connections, handle)) {
380 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
381 return VPN_ERROR_INVALID_PARAMETER;
384 *name = vpn_connection_get_name(handle); //LCOV_EXCL_LINE
385 return VPN_ERROR_NONE; //LCOV_EXCL_LINE
389 * Get VPN Info (Type) from VPN Handle
391 int _vpn_get_vpn_info_type(vpn_h handle, const char **type)
393 VPN_LOG(VPN_INFO, "");
395 GList *connections = vpn_get_connections();
396 if (NULL == g_list_find(connections, handle)) {
397 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
398 return VPN_ERROR_INVALID_PARAMETER;
401 *type = vpn_connection_get_type(handle); //LCOV_EXCL_LINE
402 return VPN_ERROR_NONE; //LCOV_EXCL_LINE
406 * Get VPN Info (Host) from VPN Handle
408 int _vpn_get_vpn_info_host(vpn_h handle, const char **host)
410 VPN_LOG(VPN_INFO, "");
412 GList *connections = vpn_get_connections();
413 if (NULL == g_list_find(connections, handle)) {
414 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
415 return VPN_ERROR_INVALID_PARAMETER;
418 *host = vpn_connection_get_host(handle); //LCOV_EXCL_LINE
419 return VPN_ERROR_NONE; //LCOV_EXCL_LINE
423 * Get VPN Info (Domain) from VPN Handle
425 int _vpn_get_vpn_info_domain(vpn_h handle, const char **domain)
427 VPN_LOG(VPN_INFO, "");
429 GList *connections = vpn_get_connections();
430 if (NULL == g_list_find(connections, handle)) {
431 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
432 return VPN_ERROR_INVALID_PARAMETER;
435 *domain = vpn_connection_get_domain(handle); //LCOV_EXCL_LINE
436 return VPN_ERROR_NONE; //LCOV_EXCL_LINE
444 static void __vpn_state_cb(struct vpn_connection *connection, void *user_data)
446 vpn_state_cb cb = NULL;
447 cb = (vpn_state_cb)g_hash_table_lookup(vpn_callbacks.state_cb_datas, connection);
449 cb(connection, vpn_connection_get_state(connection), user_data);
452 static void vpn_manager_state_cb(struct vpn_connection *connection, void *user_data)
454 VPN_LOG(VPN_INFO, "connection: %p user_data: %p\n", connection, user_data);
456 __vpn_state_cb(connection, user_data);
461 *Set state changed callback to VPN handle
464 int _vpn_set_state_callback(const vpn_h handle, vpn_state_cb cb, void *user_data)
466 enum dvpnlib_err err = DVPNLIB_ERR_NONE;
468 VPN_LOG(VPN_INFO, "");
470 GList *connections = vpn_get_connections();
471 if (NULL == g_list_find(connections, handle)) {
472 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
473 return VPN_ERROR_INVALID_PARAMETER;
476 g_hash_table_replace(vpn_callbacks.state_cb_datas, handle, cb); //LCOV_EXCL_LINE
477 err = vpn_connection_set_property_changed_cb(handle, VPN_CONN_PROP_STATE, vpn_manager_state_cb, (void *)user_data); //LCOV_EXCL_LINE
478 if (err != DVPNLIB_ERR_NONE) //LCOV_EXCL_LINE
479 return _dvpnlib_error2vpn_error(err); //LCOV_EXCL_LINE
481 return VPN_ERROR_NONE;
485 *Unset state changed callback to VPN handle
488 int _vpn_unset_state_callback(const vpn_h handle)
490 enum dvpnlib_err err = DVPNLIB_ERR_NONE;
492 VPN_LOG(VPN_INFO, "");
494 GList *connections = vpn_get_connections();
495 if (NULL == g_list_find(connections, handle)) {
496 VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle);
497 return VPN_ERROR_INVALID_PARAMETER;
500 err = vpn_connection_unset_property_changed_cb(handle, VPN_CONN_PROP_STATE); //LCOV_EXCL_LINE
501 if (err != DVPNLIB_ERR_NONE) //LCOV_EXCL_LINE
502 return _dvpnlib_error2vpn_error(err); //LCOV_EXCL_LINE
504 return VPN_ERROR_NONE;