Add wireguard related changes and test cases
[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"); //LCOV_EXCL_LINE
38                 return VPN_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
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         _vpn_deinit();
56
57         is_init = false;
58         VPN_LOG(VPN_INFO, "VPN successfully de-initialized!\n");
59
60         return VPN_ERROR_NONE;
61 }
62
63 /* Settings API's */
64 EXPORT_API int vpn_settings_init()
65 {
66         int rv;
67
68         if (is_init == false) {
69                 VPN_LOG(VPN_ERROR, "Not initialized\n");
70                 return VPN_ERROR_INVALID_OPERATION;
71         }
72
73         rv = _vpn_settings_init();
74
75         if (rv != VPN_ERROR_NONE)
76                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n"); //LCOV_EXCL_LINE
77
78         return rv;
79 }
80
81 EXPORT_API int vpn_settings_deinit()
82 {
83         int rv;
84
85         if (is_init == false) {
86                 VPN_LOG(VPN_ERROR, "Not initialized\n");
87                 return VPN_ERROR_INVALID_OPERATION;
88         }
89
90         rv = _vpn_settings_deinit();
91
92         if (rv != VPN_ERROR_NONE)
93                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
94
95         return rv;
96 }
97
98 EXPORT_API int vpn_settings_set_specific(const char *key, const char *value)
99 {
100         int rv;
101
102         if (is_init == false) {
103                 VPN_LOG(VPN_ERROR, "Not initialized\n");
104                 return VPN_ERROR_INVALID_OPERATION;
105         }
106
107         rv = _vpn_settings_set_specific(key, value);
108
109         if (rv != VPN_ERROR_NONE)
110                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
111
112         return rv;
113 }
114
115
116 EXPORT_API int vpn_settings_set_type(const char *type)
117 {
118         int rv;
119
120         if (is_init == false) {
121                 VPN_LOG(VPN_ERROR, "Not initialized\n");
122                 return VPN_ERROR_INVALID_OPERATION;
123         }
124
125         rv = _vpn_settings_set_specific("Type", type);
126
127         if (rv != VPN_ERROR_NONE)
128                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
129
130         return rv;
131 }
132
133 EXPORT_API int vpn_settings_set_name(const char *name)
134 {
135         int rv;
136
137         if (is_init == false) {
138                 VPN_LOG(VPN_ERROR, "Not initialized\n");
139                 return VPN_ERROR_INVALID_OPERATION;
140         }
141
142         rv = _vpn_settings_set_specific("Name", name);
143
144         if (rv != VPN_ERROR_NONE)
145                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
146
147         return rv;
148 }
149
150 EXPORT_API int vpn_settings_set_host(const char *host)
151 {
152         int rv;
153
154         if (is_init == false) {
155                 VPN_LOG(VPN_ERROR, "Not initialized\n");
156                 return VPN_ERROR_INVALID_OPERATION;
157         }
158
159         rv = _vpn_settings_set_specific("Host", host);
160
161         if (rv != VPN_ERROR_NONE)
162                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
163
164         return rv;
165 }
166
167 EXPORT_API int vpn_settings_set_domain(const char *domain)
168 {
169         int rv;
170
171         if (is_init == false) {
172                 VPN_LOG(VPN_ERROR, "Not initialized\n");
173                 return VPN_ERROR_INVALID_OPERATION;
174         }
175
176         rv = _vpn_settings_set_specific("Domain", domain);
177
178         if (rv != VPN_ERROR_NONE)
179                 VPN_LOG(VPN_ERROR, "Error!! VPN Settings Deinit failed.\n");
180
181         return rv;
182 }
183
184 EXPORT_API int vpn_create(vpn_created_cb callback, void *user_data)
185 {
186         int rv;
187
188         if (is_init == false) {
189                 VPN_LOG(VPN_ERROR, "Not initialized\n");
190                 return VPN_ERROR_INVALID_OPERATION;
191         }
192
193         rv = _vpn_create(callback, user_data);
194
195         if (rv != VPN_ERROR_NONE)
196                 VPN_LOG(VPN_ERROR, "Error!! VPN Create failed.\n");
197
198         return rv;
199 }
200
201 EXPORT_API
202 int vpn_remove(vpn_h handle, vpn_removed_cb callback, void *user_data)
203 {
204         int rv;
205
206         if (is_init == false) {
207                 VPN_LOG(VPN_ERROR, "Not initialized\n");
208                 return VPN_ERROR_INVALID_OPERATION;
209         }
210
211         if (handle == NULL) {
212                 VPN_LOG(VPN_ERROR, "VPN Handle is NULL\n");
213                 return VPN_ERROR_INVALID_PARAMETER;
214         }
215
216         rv = _vpn_remove(handle, callback, user_data);
217
218         if (rv != VPN_ERROR_NONE)
219                 VPN_LOG(VPN_ERROR, "Error!! VPN Remove failed.\n");
220
221         return rv;
222 }
223
224 EXPORT_API
225 int vpn_connect(vpn_h handle, vpn_connect_cb callback, void *user_data)
226 {
227         int rv;
228
229         if (is_init == false) {
230                 VPN_LOG(VPN_ERROR, "Not initialized\n");
231                 return VPN_ERROR_INVALID_OPERATION;
232         }
233
234         if (handle == NULL) {
235                 VPN_LOG(VPN_ERROR, "VPN Handle is NULL\n");
236                 return VPN_ERROR_INVALID_PARAMETER;
237         }
238
239         rv = _vpn_connect(handle, callback, user_data);
240
241         if (rv != VPN_ERROR_NONE)
242                 VPN_LOG(VPN_ERROR, "Error!! VPN Remove failed.\n");
243
244         return rv;
245 }
246
247 EXPORT_API
248 int vpn_disconnect(vpn_h handle, vpn_disconnect_cb callback, void *user_data)
249 {
250         int rv;
251
252         if (is_init == false) {
253                 VPN_LOG(VPN_ERROR, "Not initialized\n");
254                 return VPN_ERROR_INVALID_OPERATION;
255         }
256
257         if (handle == NULL) {
258                 VPN_LOG(VPN_ERROR, "VPN Handle is NULL\n");
259                 return VPN_ERROR_INVALID_PARAMETER;
260         }
261
262         rv = _vpn_disconnect(handle);
263
264         if (rv != VPN_ERROR_NONE)
265                 VPN_LOG(VPN_ERROR, "Error!! VPN Remove failed.\n");
266
267         return rv;
268 }
269
270 EXPORT_API
271 GList *vpn_get_vpn_handle_list(void)
272 {
273         if (is_init == false) {
274                 VPN_LOG(VPN_ERROR, "Not initialized\n");
275                 return NULL;
276         }
277
278         return _vpn_get_vpn_handle_list();
279 }
280
281 EXPORT_API
282 int vpn_get_vpn_handle(const char *name, const char *host, const char *domain, vpn_h *handle)
283 {
284         int rv;
285
286         if (is_init == false) {
287                 VPN_LOG(VPN_ERROR, "Not initialized\n");
288                 return VPN_ERROR_INVALID_OPERATION;
289         }
290
291         // Note: Domain is optional parameter at connman
292         if (name == NULL || host == NULL || handle == NULL)
293                 return VPN_ERROR_INVALID_PARAMETER;
294
295         rv = _vpn_get_vpn_handle(name, host, domain, handle);
296
297         if (rv != VPN_ERROR_NONE)
298                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Handle failed.\n");
299
300         return rv;
301 }
302
303 EXPORT_API
304 int vpn_get_vpn_info_name(const vpn_h handle, const char **name)
305 {
306         int rv;
307
308         if (is_init == false) {
309                 VPN_LOG(VPN_ERROR, "Not initialized\n");
310                 return VPN_ERROR_INVALID_OPERATION;
311         }
312
313         if (handle == NULL || name == NULL)
314                 return VPN_ERROR_INVALID_PARAMETER;
315
316         rv = _vpn_get_vpn_info_name(handle, name);
317
318         if (rv != VPN_ERROR_NONE)
319                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Info (Name) failed.\n");
320
321         return rv;
322 }
323
324 EXPORT_API
325 int vpn_get_vpn_info_type(const vpn_h handle, const char **type)
326 {
327         int rv;
328
329         if (is_init == false) {
330                 VPN_LOG(VPN_ERROR, "Not initialized\n");
331                 return VPN_ERROR_INVALID_OPERATION;
332         }
333
334         if (handle == NULL || type == NULL)
335                 return VPN_ERROR_INVALID_PARAMETER;
336
337         rv = _vpn_get_vpn_info_type(handle, type);
338
339         if (rv != VPN_ERROR_NONE)
340                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Info (Type) failed.\n");
341
342         return rv;
343 }
344
345 EXPORT_API
346 int vpn_get_vpn_info_host(const vpn_h handle, const char **host)
347 {
348         int rv;
349
350         if (is_init == false) {
351                 VPN_LOG(VPN_ERROR, "Not initialized\n");
352                 return VPN_ERROR_INVALID_OPERATION;
353         }
354
355         if (handle == NULL || host == NULL)
356                 return VPN_ERROR_INVALID_PARAMETER;
357
358         rv = _vpn_get_vpn_info_host(handle, host);
359
360         if (rv != VPN_ERROR_NONE)
361                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Info (Host) failed.\n");
362
363         return rv;
364 }
365
366 EXPORT_API
367 int vpn_get_vpn_info_domain(const vpn_h handle, const char **domain)
368 {
369         int rv;
370
371         if (is_init == false) {
372                 VPN_LOG(VPN_ERROR, "Not initialized\n");
373                 return VPN_ERROR_INVALID_OPERATION;
374         }
375
376         if (handle == NULL || domain == NULL)
377                 return VPN_ERROR_INVALID_PARAMETER;
378
379         rv = _vpn_get_vpn_info_domain(handle, domain);
380
381         if (rv != VPN_ERROR_NONE)
382                 VPN_LOG(VPN_ERROR, "Error!! VPN Get Info (Domain) failed.\n");
383
384         return rv;
385 }
386
387 EXPORT_API
388 int vpn_set_state_callback(const vpn_h handle, vpn_state_cb cb, void *user_data)
389 {
390         int rv;
391
392         if (is_init == false) {
393                 VPN_LOG(VPN_ERROR, "Not initialized\n");
394                 return VPN_ERROR_INVALID_OPERATION;
395         }
396
397         if (handle == NULL || cb == NULL)
398                 return VPN_ERROR_INVALID_PARAMETER;
399
400         rv = _vpn_set_state_callback(handle, cb, user_data);
401
402         if (rv != VPN_ERROR_NONE)
403                 VPN_LOG(VPN_ERROR, "Error!! VPN Set state callback failed.\n");
404
405         return rv;
406 }
407
408 EXPORT_API
409 int vpn_unset_state_callback(const vpn_h handle)
410 {
411         int rv;
412
413         if (is_init == false) {
414                 VPN_LOG(VPN_ERROR, "Not initialized\n");
415                 return VPN_ERROR_INVALID_OPERATION;
416         }
417
418         if (handle == NULL)
419                 return VPN_ERROR_INVALID_PARAMETER;
420
421         rv = _vpn_unset_state_callback(handle);
422
423         if (rv != VPN_ERROR_NONE)
424                 VPN_LOG(VPN_ERROR, "Error!! VPN Unset state callback failed.\n");
425
426         return rv;
427 }