Fixed some svace
[platform/core/connectivity/net-config.git] / src / utils / util.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. 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 #if defined TIZEN_WEARABLE
21 #include <aul.h>
22 #endif
23 #include <app.h>
24 #include <errno.h>
25 #include <vconf.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <net/if.h>
31 #include <net/route.h>
32 #include <arpa/inet.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <vconf-keys.h>
37 #include <syspopup_caller.h>
38 #include <bundle.h>
39 #include <bundle_internal.h>
40 #include <eventsystem.h>
41
42 #include "log.h"
43 #include "util.h"
44 #include "neterror.h"
45 #include "wifi-state.h"
46
47 #define WC_POPUP_EXTRA_DATA_KEY "http://tizen.org/appcontrol/data/connection_type"
48 #define MAC_INFO_FILEPATH               "/opt/etc/.mac.info"
49 #define MAC_ADDRESS_MAX_LEN             18
50
51 static gboolean netconfig_device_picker_test = FALSE;
52
53 GKeyFile *netconfig_keyfile_load(const char *pathname)
54 {
55         GKeyFile *keyfile = NULL;
56         GError *error = NULL;
57
58         keyfile = g_key_file_new();
59         if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
60                 DBG("Unable to open %s, error %s", pathname, error->message);
61                 g_error_free(error);
62
63                 g_key_file_free(keyfile);
64                 keyfile = NULL;
65         }
66
67         return keyfile;
68 }
69
70 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
71 {
72         gsize size = 0;
73         GError *error = NULL;
74         gchar *keydata = NULL;
75         gchar *needle = NULL, *directory = NULL;
76
77         directory = g_strdup(pathname);
78         needle = g_strrstr(directory, "/");
79
80         if (needle != NULL)
81                 *needle = '\0';
82
83         if (directory == NULL || (*directory) == '\0') {
84                 g_free(directory);
85                 return;
86         }
87
88         if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
89                 if (g_mkdir_with_parents(directory,
90                                 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
91                         g_free(directory);
92                         return;
93                 }
94         }
95         g_free(directory);
96
97         keydata = g_key_file_to_data(keyfile, &size, &error);
98         if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
99                 DBG("Unable to save %s, error %s", pathname, error->message);
100                 g_error_free(error);
101         }
102
103         chmod(pathname, S_IRUSR | S_IWUSR);
104
105         g_free(keydata);
106 }
107
108 void netconfig_start_timer_seconds(guint secs,
109                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
110 {
111         guint t_id = 0;
112
113         if (callback == NULL) {
114                 ERR("callback function is NULL");
115                 return;
116         }
117
118         if ((timer_id != NULL && *timer_id != 0)) {
119                 ERR("timer already is registered");
120                 return;
121         }
122
123         t_id = g_timeout_add_seconds(secs, callback, user_data);
124
125         if (t_id == 0) {
126                 ERR("Can't add timer");
127                 return;
128         }
129
130         if (timer_id != NULL)
131                 *timer_id = t_id;
132 }
133
134 void netconfig_start_timer(guint msecs,
135                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
136 {
137         guint t_id = 0;
138
139         INFO("Register timer with callback pointer (%p)", callback);
140
141         if (callback == NULL) {
142                 ERR("callback function is NULL");
143                 return;
144         }
145
146         if ((timer_id != NULL && *timer_id != 0)) {
147                 ERR("timer already is registered");
148                 return;
149         }
150
151         t_id = g_timeout_add(msecs, callback, user_data);
152
153         if (t_id == 0) {
154                 ERR("Can't add timer");
155                 return;
156         }
157
158         if (timer_id != NULL)
159                 *timer_id = t_id;
160 }
161
162 void netconfig_stop_timer(guint *timer_id)
163 {
164         if (timer_id == NULL) {
165                 ERR("timer is NULL");
166                 return;
167         }
168
169         if (*timer_id != 0) {
170                 g_source_remove(*timer_id);
171                 *timer_id = 0;
172         }
173 }
174
175 static gboolean __netconfig_test_device_picker()
176 {
177         char *favorite_wifi_service = NULL;
178
179         favorite_wifi_service = wifi_get_favorite_service();
180         if (favorite_wifi_service != NULL) {
181                 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
182                 g_free(favorite_wifi_service);
183                 return FALSE;
184         }
185
186         return TRUE;
187 }
188
189 static void __netconfig_pop_device_picker(void)
190 {
191 #if defined TIZEN_WEARABLE
192         int ret = 0;
193         app_control_h   control = NULL;
194
195         ret = app_control_create(&control);
196         if (APP_CONTROL_ERROR_NONE != ret) {
197                 DBG("failed to create app control");
198                 return ;
199         }
200
201         app_control_add_extra_data(control, "viewtype", "scanlist");
202
203         app_control_set_app_id(control, "org.tizen.wifi");
204         ret = app_control_send_launch_request(control, NULL, NULL);
205         if (APP_CONTROL_ERROR_NONE == ret)
206                 DBG("Launch request sent successfully");
207
208         app_control_destroy(control);
209 #else
210         bundle *b = NULL;
211         int wifi_ug_state = 0;
212
213         vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
214         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
215                 return;
216
217         b = bundle_create();
218
219         DBG("Launch Wi-Fi device picker");
220         syspopup_launch("wifi-qs", b);
221
222         bundle_free(b);
223 #endif
224 }
225
226 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
227 {
228         if (__netconfig_test_device_picker() == TRUE)
229                 __netconfig_pop_device_picker();
230
231         return FALSE;
232 }
233
234 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
235 {
236         static guint netconfig_wifi_device_picker_service_timer = 0;
237
238         if (is_set_method != TRUE)
239                 return netconfig_wifi_device_picker_service_timer;
240
241         if (netconfig_wifi_device_picker_service_timer != timer_id)
242                 netconfig_wifi_device_picker_service_timer = timer_id;
243
244         return netconfig_wifi_device_picker_service_timer;
245 }
246
247 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
248 {
249         __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
250 }
251
252 static guint __netconfig_wifi_device_picker_get_timer_id(void)
253 {
254         return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
255 }
256
257 void netconfig_wifi_enable_device_picker_test(void)
258 {
259         netconfig_device_picker_test = TRUE;
260 }
261
262 void netconfig_wifi_device_picker_service_start(void)
263 {
264         const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
265         guint timer_id = 0;
266
267         if (netconfig_device_picker_test == TRUE)
268                 netconfig_device_picker_test = FALSE;
269         else
270                 return;
271
272 #if defined TIZEN_WEARABLE
273         if (aul_app_is_running("org.tizen.wifi") > 0) {
274                 DBG("wifi app is running");
275                 return;
276         }
277 #else
278         int wifi_ug_state;
279
280         vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
281         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
282                 return;
283 #endif
284
285         DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
286         netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
287
288         __netconfig_wifi_device_picker_set_timer_id(timer_id);
289 }
290
291 void netconfig_wifi_device_picker_service_stop(void)
292 {
293         guint timer_id = 0;
294
295         timer_id = __netconfig_wifi_device_picker_get_timer_id();
296         if (timer_id == 0)
297                 return;
298
299         DBG("Clear device picker timer with timer_id %d", timer_id);
300
301         netconfig_stop_timer(&timer_id);
302
303         __netconfig_wifi_device_picker_set_timer_id(timer_id);
304 }
305
306 gboolean netconfig_is_wifi_direct_on(void)
307 {
308 #if defined TIZEN_P2P_ENABLE
309         int wifi_direct_state = 0;
310
311         vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
312
313         DBG("Wi-Fi direct mode %d", wifi_direct_state);
314         return (wifi_direct_state != 0) ? TRUE : FALSE;
315 #else
316         return FALSE;
317 #endif
318 }
319
320 gboolean netconfig_is_wifi_tethering_on(void)
321 {
322 #if defined TIZEN_TETHERING_ENABLE
323         int wifi_tethering_state = 0;
324
325         vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
326
327         DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
328         if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
329                 return TRUE;
330 #endif
331         return FALSE;
332 }
333
334 gboolean netconfig_interface_up(const char *ifname)
335 {
336         int fd;
337         struct ifreq ifr;
338
339         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
340         if (fd < 0)
341                 return FALSE;
342
343         memset(&ifr, 0, sizeof(ifr));
344         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
345
346         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
347                 close(fd);
348                 return FALSE;
349         }
350
351         ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
352         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
353                 close(fd);
354                 return FALSE;
355         }
356
357         close(fd);
358
359         DBG("Successfully activated wireless interface");
360         return TRUE;
361 }
362
363 gboolean netconfig_interface_down(const char *ifname)
364 {
365         int fd;
366         struct ifreq ifr;
367
368         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
369         if (fd < 0)
370                 return FALSE;
371
372         memset(&ifr, 0, sizeof(ifr));
373         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
374
375         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
376                 close(fd);
377                 return FALSE;
378         }
379
380         ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
381         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
382                 close(fd);
383                 return FALSE;
384         }
385
386         close(fd);
387
388         DBG("Successfully de-activated wireless interface");
389         return TRUE;
390 }
391
392 int netconfig_execute_file(const char *file_path,
393                 char *const args[], char *const envs[])
394 {
395         pid_t pid = 0;
396         int status = 0;
397         int rv = 0;
398         errno = 0;
399         register unsigned int index = 0;
400         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
401
402         while (args[index] != NULL) {
403                 DBG("%s", args[index]);
404                 index++;
405         }
406
407         if (!(pid = fork())) {
408                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
409                 DBG("Inside child, exec (%s) command", file_path);
410
411                 errno = 0;
412                 if (execve(file_path, args, envs) == -1) {
413                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
414                         DBG("Fail to execute command (%s)", error_buf);
415                         exit(1);
416                 }
417         } else if (pid > 0) {
418                 if (waitpid(pid, &status, 0) == -1)
419                         DBG("wait pid (%u) status (%d)", pid, status);
420
421                 if (WIFEXITED(status)) {
422                         rv = WEXITSTATUS(status);
423                         DBG("exited, status=%d", rv);
424                 } else if (WIFSIGNALED(status)) {
425                         DBG("killed by signal %d", WTERMSIG(status));
426                 } else if (WIFSTOPPED(status)) {
427                         DBG("stopped by signal %d", WSTOPSIG(status));
428                 } else if (WIFCONTINUED(status)) {
429                         DBG("continued");
430                 }
431
432                 return rv;
433         }
434
435         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
436         DBG("failed to fork(%s)", error_buf);
437         return -EIO;
438 }
439
440 static void on_clat_handler()
441 {
442         pid_t clat_pid = 0;
443         int state = 0;
444
445         clat_pid = waitpid(-1, &state, WNOHANG);
446
447         DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
448 }
449
450 int netconfig_execute_clatd(const char *file_path, char *const args[])
451 {
452         pid_t pid = 0;
453         int rv = 0;
454         errno = 0;
455         register unsigned int index = 0;
456         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
457
458         struct sigaction act;
459         int state = 0;
460
461         act.sa_handler = on_clat_handler;
462         sigemptyset(&act.sa_mask);
463         act.sa_flags = 0;
464
465         state = sigaction(SIGCHLD, &act, 0);
466         if (state != 0) {
467                 DBG("sigaction() : %d");
468                 return -1;
469         }
470
471         while (args[index] != NULL) {
472                 DBG("%s", args[index]);
473                 index++;
474         }
475
476         if (!(pid = fork())) {
477                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
478                 DBG("Inside child, exec (%s) command", file_path);
479
480                 errno = 0;
481                 if (execvp(file_path, args) == -1) {
482                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
483                         ERR("Fail to execute command (%s)", error_buf);
484                         return -1;
485                 }
486         } else if (pid > 0) {
487                 ERR("Success to launch clatd");
488                 return rv;
489         }
490
491         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
492         DBG("failed to fork(%s)", error_buf);
493         return -EIO;
494 }
495
496 int __netconfig_get_interface_index(const char *interface_name)
497 {
498         struct ifreq ifr;
499         int sock = 0;
500         int result = 0;
501         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
502
503         if (interface_name == NULL) {
504                 DBG("Inteface name is NULL");
505                 return -1;
506         }
507
508         errno = 0;
509         sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
510         if (sock < 0) {
511                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
512                 DBG("Failed to create socket : %s", error_buf);
513                 return -1;
514         }
515
516         memset(&ifr, 0, sizeof(ifr));
517         strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
518         result = ioctl(sock, SIOCGIFINDEX, &ifr);
519         close(sock);
520
521         if (result < 0) {
522                 DBG("Failed to get ifr index: %s", error_buf);
523                 return -1;
524         }
525
526         return ifr.ifr_ifindex;
527 }
528
529 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
530 {
531         struct ifreq ifr;
532         struct rtentry rt;
533         struct sockaddr_in addr_in;
534         int sock;
535         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
536
537         memset(&ifr, 0, sizeof(ifr));
538
539         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
540
541         if (ifr.ifr_ifindex < 0)
542                 return -1;
543
544         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
545
546         memset(&rt, 0, sizeof(rt));
547
548         rt.rt_flags = RTF_UP | RTF_HOST;
549         memset(&addr_in, 0, sizeof(struct sockaddr_in));
550         addr_in.sin_family = address_family;
551         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
552         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
553
554         memset(&addr_in, 0, sizeof(struct sockaddr_in));
555         addr_in.sin_family = address_family;
556         addr_in.sin_addr.s_addr = INADDR_ANY;
557         memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
558
559         memset(&addr_in, 0, sizeof(struct sockaddr_in));
560         addr_in.sin_family = AF_INET;
561         addr_in.sin_addr.s_addr = inet_addr(subnet);
562         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
563
564         rt.rt_dev = ifr.ifr_name;
565
566         errno = 0;
567         sock = socket(PF_INET, SOCK_DGRAM, 0);
568
569         if (sock < 0) {
570                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
571                 DBG("Failed to create socket : %s", error_buf);
572                 return -1;
573         }
574
575         if (ioctl(sock, SIOCADDRT, &rt) < 0) {
576                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
577                 DBG("Failed to set route address : %s", error_buf);
578                 close(sock);
579                 return -1;
580         }
581
582         close(sock);
583
584         return 1;
585 }
586
587 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
588 {
589         struct ifreq ifr;
590         struct rtentry rt;
591         struct sockaddr_in addr_in;
592         int sock;
593         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
594
595         memset(&ifr, 0, sizeof(ifr));
596         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
597
598         if (ifr.ifr_ifindex < 0)
599                 return -1;
600
601         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
602
603         memset(&rt, 0, sizeof(rt));
604
605         rt.rt_flags = RTF_UP;
606         memset(&addr_in, 0, sizeof(struct sockaddr_in));
607         addr_in.sin_family = address_family;
608         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
609         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
610
611         memset(&addr_in, 0, sizeof(struct sockaddr_in));
612         addr_in.sin_family = address_family;
613         addr_in.sin_addr.s_addr = inet_addr(subnet);
614         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
615         rt.rt_dev = ifr.ifr_name;
616
617         errno = 0;
618         sock = socket(PF_INET, SOCK_DGRAM, 0);
619
620         if (sock < 0) {
621                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
622                 DBG("Failed to create socket : %s", error_buf);
623                 return -1;
624         }
625
626         if (ioctl(sock, SIOCDELRT, &rt) < 0) {
627                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
628                 DBG("Failed to set route address : %s", error_buf);
629                 close(sock);
630                 return -1;
631         }
632
633         close(sock);
634
635         return 1;
636 }
637
638 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
639 {
640         struct in6_rtmsg rt;
641         int fd = 0;
642         int err = 0;
643         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
644
645         memset(&rt, 0, sizeof(rt));
646
647         rt.rtmsg_dst_len = prefix_len;
648
649         rt.rtmsg_flags = RTF_UP | RTF_HOST;
650
651         errno = 0;
652         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
653                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
654                 DBG("inet_pton failed : %s", error_buf);
655                 return -1;
656         }
657
658         if (gateway != NULL) {
659                 rt.rtmsg_flags |= RTF_GATEWAY;
660                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
661                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
662                         DBG("inet_pton failed : %s", error_buf);
663                         return -1;
664                 }
665         }
666
667         rt.rtmsg_metric = 1;
668
669         fd = socket(AF_INET6, SOCK_DGRAM, 0);
670         if (fd < 0) {
671                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
672                 DBG("Failed to create socket : %s", error_buf);
673                 return -1;
674         }
675
676         rt.rtmsg_ifindex = 0;
677
678         if (interface) {
679                 struct ifreq ifr;
680                 memset(&ifr, 0, sizeof(ifr));
681                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
682                 ioctl(fd, SIOCGIFINDEX, &ifr);
683                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
684         }
685
686         if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
687                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
688                 DBG("Failed to add route: %s", error_buf);
689                 close(fd);
690                 return -1;
691         }
692
693         close(fd);
694
695         return 1;
696 }
697
698 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
699 {
700         struct in6_rtmsg rt;
701         int fd = 0;
702         int err = 0;
703
704         memset(&rt, 0, sizeof(rt));
705
706         rt.rtmsg_dst_len = prefix_len;
707
708         rt.rtmsg_flags = RTF_UP | RTF_HOST;
709
710         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
711                 err = -errno;
712                 return err;
713         }
714
715         if (gateway != NULL) {
716                 rt.rtmsg_flags |= RTF_GATEWAY;
717                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
718                         err = -errno;
719                         return err;
720                 }
721         }
722
723         rt.rtmsg_metric = 1;
724
725         fd = socket(AF_INET6, SOCK_DGRAM, 0);
726         if (fd < 0)
727                 return -1;
728
729         rt.rtmsg_ifindex = 0;
730
731         if (interface) {
732                 struct ifreq ifr;
733                 memset(&ifr, 0, sizeof(ifr));
734                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
735                 ioctl(fd, SIOCGIFINDEX, &ifr);
736                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
737         }
738
739         if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
740                 DBG("Failed to del route: %d\n", err);
741                 close(fd);
742                 return -1;
743         }
744
745         close(fd);
746
747         return 1;
748 }
749
750 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
751 {
752 #if defined TIZEN_P2P_ENABLE
753         int ret = 0;
754         DBG("Launch Wi-Fi direct daemon");
755
756         const char *path = "/usr/bin/wifi-direct-server.sh";
757         char *const args[] = { "wifi-direct-server.sh", "start", NULL };
758         char *const envs[] = { NULL };
759
760         ret = netconfig_execute_file(path, args, envs);
761         if (ret < 0) {
762                 ERR("Failed to launch Wi-Fi direct daemon");
763                 netconfig_error_wifi_direct_failed(context);
764                 return FALSE;
765         }
766         wifi_complete_launch_direct(wifi, context);
767         return TRUE;
768 #else
769         wifi_complete_launch_direct(wifi, context);
770         return FALSE;
771 #endif
772 }
773
774 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
775 {
776         int ret = 0;
777         bundle *b;
778         static gboolean is_found_noti_exists = FALSE;
779         static gboolean is_portal_noti_exists = FALSE;
780
781         if (noti == NULL) {
782                 ERR("Invalid notification");
783                 return FALSE;
784         }
785
786         if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
787                 if (is_found_noti_exists == FALSE)
788                         return TRUE;
789
790                 is_found_noti_exists = FALSE;
791         } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
792                 if (is_found_noti_exists == TRUE)
793                         return TRUE;
794
795                 is_found_noti_exists = TRUE;
796         } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
797                 if (is_portal_noti_exists == TRUE)
798                         return TRUE;
799
800                 is_portal_noti_exists = TRUE;
801         } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
802                 if (is_portal_noti_exists == FALSE)
803                         return TRUE;
804
805                 is_portal_noti_exists = FALSE;
806         }
807
808         b = bundle_create();
809         bundle_add(b, "_SYSPOPUP_TYPE_", noti);
810
811         if (ssid != NULL) {
812                 DBG("ssid (%s)", ssid);
813                 bundle_add(b, "_AP_NAME_", ssid);
814         }
815
816         ret = syspopup_launch("net.netpopup", b);
817
818         bundle_free(b);
819
820         if (ret < 0) {
821                 ERR("Unable to launch noti-popup. Err = %d", ret);
822                 return FALSE;
823         }
824
825         DBG("Successfully sent notification (%s)", noti);
826         return TRUE;
827 }
828
829 int netconfig_send_message_to_net_popup(const char *title,
830                 const char *content, const char *type, const char *ssid)
831 {
832         int ret = 0;
833         bundle *b = bundle_create();
834
835         bundle_add(b, "_SYSPOPUP_TITLE_", title);
836         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
837         bundle_add(b, "_SYSPOPUP_TYPE_", type);
838         bundle_add(b, "_AP_NAME_", ssid);
839
840         ret = syspopup_launch("net.netpopup", b);
841
842         bundle_free(b);
843
844         return ret;
845 }
846
847 void netconfig_set_system_event(const char * sys_evt, const char * evt_key, const char * evt_val)
848 {
849         bundle *b = NULL;
850
851         DBG("System event set [%s : %s : %s]", sys_evt, evt_key, evt_val);
852
853         b = bundle_create();
854         bundle_add_str(b, evt_key, evt_val);
855         eventsystem_send_system_event(sys_evt, b);
856         bundle_free(b);
857 }
858
859 #if defined TIZEN_WEARABLE
860 int wc_launch_syspopup(netconfig_wcpopup_type_e type)
861 {
862         int ret;
863         bundle* b;
864         char *ssid = NULL;
865
866         b = bundle_create();
867         if (!b) {
868                 ERR("Failed to create bundle");
869                 return -1;
870         }
871
872         switch (type) {
873         case WC_POPUP_TYPE_SESSION_OVERLAPPED:
874                 bundle_add(b, "event-type", "wps-session-overlapped");
875                 break;
876         case WC_POPUP_TYPE_WIFI_CONNECTED:
877                 ssid = vconf_get_str(VCONFKEY_WIFI_CONNECTED_AP_NAME);
878                 if (ssid == NULL) {
879                         ERR("Failed to get connected ap ssid");
880                         ssid = g_strdup(" ");
881                 }
882                 bundle_add(b, "event-type", "wifi-connected");
883                 bundle_add(b, "ssid", ssid);
884                 if (ssid)
885                         g_free(ssid);
886                 break;
887         case WC_POPUP_TYPE_WIFI_RESTRICT:
888                                 bundle_add(b, "event-type", "wifi-restrict");
889                                 break;
890         default:
891                 ERR("Popup is not supported[%d]", type);
892                 bundle_free(b);
893                 return -1;
894         }
895
896         ret = syspopup_launch("wc-syspopup", b);
897         if (ret < 0)
898                 ERR("Failed to launch syspopup");
899
900         bundle_free(b);
901
902         return ret;
903 }
904
905 int wc_launch_popup(netconfig_wcpopup_type_e type)
906 {
907         int ret;
908         app_control_h app_control = NULL;
909
910         ret = app_control_create(&app_control);
911         if (ret != APP_CONTROL_ERROR_NONE) {
912                 ERR("Failed to create appcontrol[%d]", ret);
913                 return -1;
914         }
915
916         switch (type) {
917         case WC_POPUP_TYPE_CAPTIVE_PORTAL:
918                 app_control_add_extra_data(app_control, WC_POPUP_EXTRA_DATA_KEY, "captive-portal");
919                 break;
920         default:
921                 ERR("Popup is not supported[%d]", type);
922                 app_control_destroy(app_control);
923                 return -1;
924         }
925
926         app_control_set_app_id(app_control, "com.samsung.weconn-popup");
927         ret = app_control_send_launch_request(app_control, NULL, NULL);
928         if (ret != APP_CONTROL_ERROR_NONE) {
929                 DBG("failed appcontrol launch request [%d]", ret);
930                 app_control_destroy(app_control);
931                 return -1;
932         }
933
934         app_control_destroy(app_control);
935
936         return 0;
937 }
938 #endif
939
940 void netconfig_set_vconf_int(const char * key, int value)
941 {
942         int ret = 0;
943
944         DBG("[%s: %d]", key, value);
945
946         ret = vconf_set_int(key, value);
947         if (ret != VCONF_OK)
948                 ERR("Failed to set");
949 }
950
951 void netconfig_set_vconf_str(const char * key, const char * value)
952 {
953         int ret = 0;
954
955         DBG("[%s: %s]", key, value);
956
957         ret = vconf_set_str(key, value);
958         if (ret != VCONF_OK)
959                 ERR("Failed to set");
960 }
961
962 char* netconfig_get_env(const char *key)
963 {
964         FILE *fp;
965         char buf[256], *entry = NULL, *value = NULL, *last;
966         int len=0;
967
968         if (!key)
969                 return NULL;
970
971         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
972         if (!fp)
973                 return NULL;
974
975         while (fgets(buf, sizeof(buf), fp)) {
976                 entry = buf;
977                 entry = strtok_r(entry, "=", &last);
978                 if (entry) {
979                         if (strstr(entry, key)) {
980                                 entry = strtok_r(NULL, "\n", &last);
981                                 if(entry){
982                                         len = strlen(entry);
983                                         value = (char*)malloc(len+1);
984                                         g_strlcpy(value, entry, len+1);
985                                 }
986                                 else{
987                                         value = (char*)malloc(sizeof(char));
988                                         g_strlcpy(value, "\n", sizeof(char));
989                                 }
990                                 break;
991                         }
992                 }
993         }
994
995         fclose(fp);
996         return value;
997 }
998
999 void netconfig_set_mac_address_from_file(void)
1000 {
1001         FILE *file = NULL;
1002         char mac_str[MAC_ADDRESS_MAX_LEN];
1003         gchar *mac_lower_str = NULL;
1004         int mac_len = 0;
1005
1006         file = fopen(MAC_INFO_FILEPATH, "r");
1007         if (file == NULL) {
1008                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1009                 return;
1010         }
1011         if (fgets(mac_str, sizeof(mac_str), file) == NULL ) {
1012                 ERR("Fail to read mac address");
1013                 fclose(file);
1014                 return;
1015         }
1016
1017         mac_len = strlen(mac_str);
1018         if (mac_len < 17) {
1019                 ERR("mac.info is empty");
1020                 fclose(file);
1021                 return;
1022         }
1023
1024         mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1025         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1026
1027         g_free(mac_lower_str);
1028         fclose(file);
1029 }