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