[SVACE] 67131,67132 Fixed svace issues
[platform/core/api/vpn-setting.git] / src / vpn.c
1 /*
2 * Network VPN library
3 *
4 * Copyright (c) 2014-2015 Samsung Electronics. All rights reserved.
5 *
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
9 *
10 *              http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <vconf/vconf.h>
24
25 #include "vpn-internal.h"
26
27 static bool is_init = false;
28
29 EXPORT_API int vpn_initialize(void)
30 {
31         if (is_init) {
32                 VPN_LOG(VPN_ERROR, "Already initialized\n");
33                 return VPN_ERROR_INVALID_OPERATION;
34         }
35
36         if (_vpn_init() == false) {
37                 VPN_LOG(VPN_ERROR, "Init failed!\n");
38                 return VPN_ERROR_OPERATION_FAILED;
39         }
40
41         is_init = true;
42
43         VPN_LOG(VPN_INFO, "VPN successfully initialized!\n");
44
45         return VPN_ERROR_NONE;
46 }
47
48 EXPORT_API int vpn_deinitialize(void)
49 {
50         if (is_init == false) {
51                 VPN_LOG(VPN_ERROR, "Not initialized\n");
52                 return VPN_ERROR_INVALID_OPERATION;
53         }
54
55         if (_vpn_deinit() == false) {
56                 VPN_LOG(VPN_ERROR, "Deinit failed!\n");
57                 return VPN_ERROR_OPERATION_FAILED;
58         }
59
60         is_init = false;
61         VPN_LOG(VPN_INFO, "VPN successfully de-initialized!\n");
62
63         return VPN_ERROR_NONE;
64 }
65
66 /* Settings API's */
67 EXPORT_API int vpn_settings_init()
68 {
69         int rv;
70
71         if (is_init == false) {
72                 VPN_LOG(VPN_ERROR, "Not initialized\n");
73                 return VPN_ERROR_INVALID_OPERATION;
74         }
75
76         rv = _vpn_settings_init();
77
78         if (rv != VPN_ERROR_NONE)
79                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
80
81         return rv;
82 }
83
84 EXPORT_API int vpn_settings_deinit()
85 {
86         int rv;
87
88         if (is_init == false) {
89                 VPN_LOG(VPN_ERROR, "Not initialized\n");
90                 return VPN_ERROR_INVALID_OPERATION;
91         }
92
93         rv = _vpn_settings_deinit();
94
95         if (rv != VPN_ERROR_NONE)
96                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
97
98         return rv;
99 }
100
101 EXPORT_API int vpn_settings_set_specific(const char *key, const char *value)
102 {
103         int rv;
104
105         if (is_init == false) {
106                 VPN_LOG(VPN_ERROR, "Not initialized\n");
107                 return VPN_ERROR_INVALID_OPERATION;
108         }
109
110         rv = _vpn_settings_set_specific(key, value);
111
112         if (rv != VPN_ERROR_NONE)
113                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
114
115         return rv;
116 }
117
118
119 EXPORT_API int vpn_settings_set_type(const char *type)
120 {
121         int rv;
122
123         if (is_init == false) {
124                 VPN_LOG(VPN_ERROR, "Not initialized\n");
125                 return VPN_ERROR_INVALID_OPERATION;
126         }
127
128         rv = _vpn_settings_set_specific("Type", type);
129
130         if (rv != VPN_ERROR_NONE)
131                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
132
133         return rv;
134 }
135
136 EXPORT_API int vpn_settings_set_name(const char *name)
137 {
138         int rv;
139
140         if (is_init == false) {
141                 VPN_LOG(VPN_ERROR, "Not initialized\n");
142                 return VPN_ERROR_INVALID_OPERATION;
143         }
144
145         rv = _vpn_settings_set_specific("Name", name);
146
147         if (rv != VPN_ERROR_NONE)
148                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
149
150         return rv;
151 }
152
153 EXPORT_API int vpn_settings_set_host(const char *host)
154 {
155         int rv;
156
157         if (is_init == false) {
158                 VPN_LOG(VPN_ERROR, "Not initialized\n");
159                 return VPN_ERROR_INVALID_OPERATION;
160         }
161
162         rv = _vpn_settings_set_specific("Host", host);
163
164         if (rv != VPN_ERROR_NONE)
165                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
166
167         return rv;
168 }
169
170 EXPORT_API int vpn_settings_set_domain(const char *domain)
171 {
172         int rv;
173
174         if (is_init == false) {
175                 VPN_LOG(VPN_ERROR, "Not initialized\n");
176                 return VPN_ERROR_INVALID_OPERATION;
177         }
178
179         rv = _vpn_settings_set_specific("Domain", domain);
180
181         if (rv != VPN_ERROR_NONE)
182                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
183
184         return rv;
185 }
186
187 EXPORT_API int vpn_create(vpn_created_cb callback, void *user_data)
188 {
189         int rv;
190
191         if (is_init == false) {
192                 VPN_LOG(VPN_ERROR, "Not initialized\n");
193                 return VPN_ERROR_INVALID_OPERATION;
194         }
195
196         rv = _vpn_create(callback, user_data);
197
198         if (rv != VPN_ERROR_NONE)
199                 VPN_LOG(VPN_ERROR, "Error!! VPN Create failed.\n");
200
201         return rv;
202 }
203
204 EXPORT_API
205 int vpn_remove(vpn_h handle, vpn_removed_cb callback, void *user_data)
206 {
207         int rv;
208
209         if (is_init == false) {
210                 VPN_LOG(VPN_ERROR, "Not initialized\n");
211                 return VPN_ERROR_INVALID_OPERATION;
212         }
213
214         if (handle == NULL) {
215                 VPN_LOG(VPN_ERROR, "VPN Handle is NULL\n");
216                 return VPN_ERROR_INVALID_PARAMETER;
217         }
218
219         rv = _vpn_remove(handle, callback, user_data);
220
221         if (rv != VPN_ERROR_NONE)
222                 VPN_LOG(VPN_ERROR, "Error!! VPN Remove failed.\n");
223
224         return rv;
225 }
226
227 EXPORT_API
228 int vpn_connect(vpn_h handle, vpn_connect_cb callback, void *user_data)
229 {
230         int rv;
231
232         if (is_init == false) {
233                 VPN_LOG(VPN_ERROR, "Not initialized\n");
234                 return VPN_ERROR_INVALID_OPERATION;
235         }
236
237         if (handle == NULL) {
238                 VPN_LOG(VPN_ERROR, "VPN Handle is NULL\n");
239                 return VPN_ERROR_INVALID_PARAMETER;
240         }
241
242         rv = _vpn_connect(handle, callback, user_data);
243
244         if (rv != VPN_ERROR_NONE)
245                 VPN_LOG(VPN_ERROR, "Error!! VPN Remove failed.\n");
246
247         return rv;
248 }
249
250 EXPORT_API
251 int vpn_disconnect(vpn_h handle, vpn_disconnect_cb callback, void *user_data)
252 {
253         int rv;
254
255         if (is_init == false) {
256                 VPN_LOG(VPN_ERROR, "Not initialized\n");
257                 return VPN_ERROR_INVALID_OPERATION;
258         }
259
260         if (handle == NULL) {
261                 VPN_LOG(VPN_ERROR, "VPN Handle is NULL\n");
262                 return VPN_ERROR_INVALID_PARAMETER;
263         }
264
265         rv = _vpn_disconnect(handle);
266
267         if (rv != VPN_ERROR_NONE)
268                 VPN_LOG(VPN_ERROR, "Error!! VPN Remove failed.\n");
269
270         return rv;
271 }
272
273 EXPORT_API
274 GList *vpn_get_vpn_handle_list(void)
275 {
276         if (is_init == false) {
277                 VPN_LOG(VPN_ERROR, "Not initialized\n");
278                 return NULL;
279         }
280
281         return _vpn_get_vpn_handle_list();
282 }
283
284 EXPORT_API
285 int vpn_get_vpn_handle(const char *host, const char *domain, vpn_h *handle)
286 {
287         int rv;
288
289         if (is_init == false) {
290                 VPN_LOG(VPN_ERROR, "Not initialized\n");
291                 return VPN_ERROR_INVALID_OPERATION;
292         }
293
294         if (host == NULL || domain == NULL || handle == NULL)
295                 return VPN_ERROR_INVALID_PARAMETER;
296
297         rv = _vpn_get_vpn_handle(host, domain, handle);
298
299         if (rv != VPN_ERROR_NONE)
300                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Handle failed.\n");
301
302         return rv;
303 }
304
305 EXPORT_API
306 int vpn_get_vpn_info_name(const vpn_h handle, const char **name)
307 {
308         int rv;
309
310         if (is_init == false) {
311                 VPN_LOG(VPN_ERROR, "Not initialized\n");
312                 return VPN_ERROR_INVALID_OPERATION;
313         }
314
315         if (handle == NULL || name == NULL)
316                 return VPN_ERROR_INVALID_PARAMETER;
317
318         rv = _vpn_get_vpn_info_name(handle, name);
319
320         if (rv != VPN_ERROR_NONE)
321                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Info (Name) failed.\n");
322
323         return rv;
324 }
325
326 EXPORT_API
327 int vpn_get_vpn_info_type(const vpn_h handle, const char **type)
328 {
329         int rv;
330
331         if (is_init == false) {
332                 VPN_LOG(VPN_ERROR, "Not initialized\n");
333                 return VPN_ERROR_INVALID_OPERATION;
334         }
335
336         if (handle == NULL || type == NULL)
337                 return VPN_ERROR_INVALID_PARAMETER;
338
339         rv = _vpn_get_vpn_info_type(handle, type);
340
341         if (rv != VPN_ERROR_NONE)
342                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Info (Type) failed.\n");
343
344         return rv;
345 }
346
347 EXPORT_API
348 int vpn_get_vpn_info_host(const vpn_h handle, const char **host)
349 {
350         int rv;
351
352         if (is_init == false) {
353                 VPN_LOG(VPN_ERROR, "Not initialized\n");
354                 return VPN_ERROR_INVALID_OPERATION;
355         }
356
357         if (handle == NULL || host == NULL)
358                 return VPN_ERROR_INVALID_PARAMETER;
359
360         rv = _vpn_get_vpn_info_host(handle, host);
361
362         if (rv != VPN_ERROR_NONE)
363                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Info (Host) failed.\n");
364
365         return rv;
366 }
367
368 EXPORT_API
369 int vpn_get_vpn_info_domain(const vpn_h handle, const char **domain)
370 {
371         int rv;
372
373         if (is_init == false) {
374                 VPN_LOG(VPN_ERROR, "Not initialized\n");
375                 return VPN_ERROR_INVALID_OPERATION;
376         }
377
378         if (handle == NULL || domain == NULL)
379                 return VPN_ERROR_INVALID_PARAMETER;
380
381         rv = _vpn_get_vpn_info_domain(handle, domain);
382
383         if (rv != VPN_ERROR_NONE)
384                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Info (Domain) failed.\n");
385
386         return rv;
387 }
388