[net-config] Use wrapper function to get vconfkeys value.
[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 static int mdnsd_ref_count = 0;
54
55 GKeyFile *netconfig_keyfile_load(const char *pathname)
56 {
57         GKeyFile *keyfile = NULL;
58         GError *error = NULL;
59
60         keyfile = g_key_file_new();
61         if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
62                 DBG("Unable to open %s, error %s", pathname, error->message);
63                 g_error_free(error);
64
65                 g_key_file_free(keyfile);
66                 keyfile = NULL;
67         }
68
69         return keyfile;
70 }
71
72 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
73 {
74         gsize size = 0;
75         GError *error = NULL;
76         gchar *keydata = NULL;
77         gchar *needle = NULL, *directory = NULL;
78
79         directory = g_strdup(pathname);
80         needle = g_strrstr(directory, "/");
81
82         if (needle != NULL)
83                 *needle = '\0';
84
85         if (directory == NULL || (*directory) == '\0') {
86                 g_free(directory);
87                 return;
88         }
89
90         if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
91                 if (g_mkdir_with_parents(directory,
92                                 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
93                         g_free(directory);
94                         return;
95                 }
96         }
97         g_free(directory);
98
99         keydata = g_key_file_to_data(keyfile, &size, &error);
100         if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
101                 DBG("Unable to save %s, error %s", pathname, error->message);
102                 g_error_free(error);
103         }
104
105         chmod(pathname, S_IRUSR | S_IWUSR);
106
107         g_free(keydata);
108 }
109
110 void netconfig_start_timer_seconds(guint secs,
111                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
112 {
113         guint t_id = 0;
114
115         if (callback == NULL) {
116                 ERR("callback function is NULL");
117                 return;
118         }
119
120         if ((timer_id != NULL && *timer_id != 0)) {
121                 ERR("timer already is registered");
122                 return;
123         }
124
125         t_id = g_timeout_add_seconds(secs, callback, user_data);
126
127         if (t_id == 0) {
128                 ERR("Can't add timer");
129                 return;
130         }
131
132         if (timer_id != NULL)
133                 *timer_id = t_id;
134 }
135
136 void netconfig_start_timer(guint msecs,
137                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
138 {
139         guint t_id = 0;
140
141         INFO("Register timer with callback pointer (%p)", callback);
142
143         if (callback == NULL) {
144                 ERR("callback function is NULL");
145                 return;
146         }
147
148         if ((timer_id != NULL && *timer_id != 0)) {
149                 ERR("timer already is registered");
150                 return;
151         }
152
153         t_id = g_timeout_add(msecs, callback, user_data);
154
155         if (t_id == 0) {
156                 ERR("Can't add timer");
157                 return;
158         }
159
160         if (timer_id != NULL)
161                 *timer_id = t_id;
162 }
163
164 void netconfig_stop_timer(guint *timer_id)
165 {
166         if (timer_id == NULL) {
167                 ERR("timer is NULL");
168                 return;
169         }
170
171         if (*timer_id != 0) {
172                 g_source_remove(*timer_id);
173                 *timer_id = 0;
174         }
175 }
176
177 static gboolean __netconfig_test_device_picker()
178 {
179         char *favorite_wifi_service = NULL;
180
181         favorite_wifi_service = wifi_get_favorite_service();
182         if (favorite_wifi_service != NULL) {
183                 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
184                 g_free(favorite_wifi_service);
185                 return FALSE;
186         }
187
188         return TRUE;
189 }
190
191 static void __netconfig_pop_device_picker(void)
192 {
193 #if defined TIZEN_WEARABLE
194         int ret = 0;
195         app_control_h   control = NULL;
196
197         ret = app_control_create(&control);
198         if (APP_CONTROL_ERROR_NONE != ret) {
199                 DBG("failed to create app control");
200                 return ;
201         }
202
203         app_control_add_extra_data(control, "viewtype", "scanlist");
204
205         app_control_set_app_id(control, "org.tizen.wifi");
206         ret = app_control_send_launch_request(control, NULL, NULL);
207         if (APP_CONTROL_ERROR_NONE == ret)
208                 DBG("Launch request sent successfully");
209
210         app_control_destroy(control);
211 #else
212         bundle *b = NULL;
213         int wifi_ug_state = 0;
214
215         netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
216         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
217                 return;
218
219         b = bundle_create();
220
221         DBG("Launch Wi-Fi device picker");
222         syspopup_launch("wifi-qs", b);
223
224         bundle_free(b);
225 #endif
226 }
227
228 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
229 {
230         if (__netconfig_test_device_picker() == TRUE)
231                 __netconfig_pop_device_picker();
232
233         return FALSE;
234 }
235
236 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
237 {
238         static guint netconfig_wifi_device_picker_service_timer = 0;
239
240         if (is_set_method != TRUE)
241                 return netconfig_wifi_device_picker_service_timer;
242
243         if (netconfig_wifi_device_picker_service_timer != timer_id)
244                 netconfig_wifi_device_picker_service_timer = timer_id;
245
246         return netconfig_wifi_device_picker_service_timer;
247 }
248
249 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
250 {
251         __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
252 }
253
254 static guint __netconfig_wifi_device_picker_get_timer_id(void)
255 {
256         return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
257 }
258
259 void netconfig_wifi_enable_device_picker_test(void)
260 {
261         netconfig_device_picker_test = TRUE;
262 }
263
264 void netconfig_wifi_device_picker_service_start(void)
265 {
266         const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
267         guint timer_id = 0;
268
269         if (netconfig_device_picker_test == TRUE)
270                 netconfig_device_picker_test = FALSE;
271         else
272                 return;
273
274 #if defined TIZEN_WEARABLE
275         if (aul_app_is_running("org.tizen.wifi") > 0) {
276                 DBG("wifi app is running");
277                 return;
278         }
279 #else
280         int wifi_ug_state;
281
282         netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
283         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
284                 return;
285 #endif
286
287         DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
288         netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
289
290         __netconfig_wifi_device_picker_set_timer_id(timer_id);
291 }
292
293 void netconfig_wifi_device_picker_service_stop(void)
294 {
295         guint timer_id = 0;
296
297         timer_id = __netconfig_wifi_device_picker_get_timer_id();
298         if (timer_id == 0)
299                 return;
300
301         DBG("Clear device picker timer with timer_id %d", timer_id);
302
303         netconfig_stop_timer(&timer_id);
304
305         __netconfig_wifi_device_picker_set_timer_id(timer_id);
306 }
307
308 gboolean netconfig_is_wifi_direct_on(void)
309 {
310 #if defined TIZEN_P2P_ENABLE
311         int wifi_direct_state = 0;
312
313         netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
314
315         DBG("Wi-Fi direct mode %d", wifi_direct_state);
316         return (wifi_direct_state != 0) ? TRUE : FALSE;
317 #else
318         return FALSE;
319 #endif
320 }
321
322 gboolean netconfig_is_wifi_tethering_on(void)
323 {
324 #if defined TIZEN_TETHERING_ENABLE
325         int wifi_tethering_state = 0;
326
327         netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
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
768         wifi_complete_launch_direct(wifi, context);
769         return TRUE;
770 #else
771         wifi_complete_launch_direct(wifi, context);
772         return FALSE;
773 #endif
774 }
775
776 int execute_mdnsd_script(char* op) {
777         const char *path = "/usr/bin/mdnsresponder-server.sh";
778         char *const args[] = { "mdnsresponder-server.sh", op, NULL };
779         char *const envs[] = { NULL };
780
781         return netconfig_execute_file(path, args, envs);
782 }
783
784 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context)
785 {
786         DBG("Launch mdnsresponder daemon");
787
788         if (mdnsd_ref_count != 0) {
789                 ERR("Invalid access");
790                 netconfig_error_invalid_parameter(context);
791                 return FALSE;
792         }
793
794         if (execute_mdnsd_script("start") < 0) {
795                 ERR("Failed to launch mdnsresponder daemon");
796                 netconfig_error_invalid_parameter(context);
797                 return FALSE;
798         }
799
800         network_complete_launch_mdns(object, context);
801         return TRUE;
802 }
803
804 gboolean handle_ref_mdns(Network *object, GDBusMethodInvocation *context)
805 {
806         mdnsd_ref_count++;
807
808         DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
809         network_complete_ref_mdns(object, context);
810         return TRUE;
811 }
812
813 gboolean handle_unref_mdns(Network *object, GDBusMethodInvocation *context)
814 {
815         DBG("Unef mdnsresponder daemon");
816
817         if (mdnsd_ref_count <= 0) {
818                 ERR("Invalid access");
819                 netconfig_error_invalid_parameter(context);
820                 return FALSE;
821         }
822
823         mdnsd_ref_count--;
824
825         DBG("Unref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
826         if (mdnsd_ref_count == 0) {
827                 if (execute_mdnsd_script("stop") < 0) {
828                         ERR("Failed to stop mdnsresponder daemon");
829                         netconfig_error_invalid_parameter(context);
830                         return FALSE;
831                 }
832         }
833
834         network_complete_unref_mdns(object, context);
835         return TRUE;
836 }
837
838 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
839 {
840         int ret = 0;
841         bundle *b;
842         static gboolean is_found_noti_exists = FALSE;
843         static gboolean is_portal_noti_exists = FALSE;
844
845         if (noti == NULL) {
846                 ERR("Invalid notification");
847                 return FALSE;
848         }
849
850         if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
851                 if (is_found_noti_exists == FALSE)
852                         return TRUE;
853
854                 is_found_noti_exists = FALSE;
855         } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
856                 if (is_found_noti_exists == TRUE)
857                         return TRUE;
858
859                 is_found_noti_exists = TRUE;
860         } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
861                 if (is_portal_noti_exists == TRUE)
862                         return TRUE;
863
864                 is_portal_noti_exists = TRUE;
865         } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
866                 if (is_portal_noti_exists == FALSE)
867                         return TRUE;
868
869                 is_portal_noti_exists = FALSE;
870         }
871
872         b = bundle_create();
873         bundle_add(b, "_SYSPOPUP_TYPE_", noti);
874
875         if (ssid != NULL) {
876                 DBG("ssid (%s)", ssid);
877                 bundle_add(b, "_AP_NAME_", ssid);
878         }
879
880         ret = syspopup_launch("net-popup", b);
881
882         bundle_free(b);
883
884         if (ret < 0) {
885                 ERR("Unable to launch noti-popup. Err = %d", ret);
886                 return FALSE;
887         }
888
889         DBG("Successfully sent notification (%s)", noti);
890         return TRUE;
891 }
892
893 int netconfig_send_message_to_net_popup(const char *title,
894                 const char *content, const char *type, const char *ssid)
895 {
896         int ret = 0;
897         bundle *b = bundle_create();
898
899         bundle_add(b, "_SYSPOPUP_TITLE_", title);
900         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
901         bundle_add(b, "_SYSPOPUP_TYPE_", type);
902         bundle_add(b, "_AP_NAME_", ssid);
903
904         ret = syspopup_launch("net-popup", b);
905
906         bundle_free(b);
907
908         return ret;
909 }
910
911 void netconfig_set_system_event(const char * sys_evt, const char * evt_key, const char * evt_val)
912 {
913         bundle *b = NULL;
914
915         DBG("System event set [%s : %s : %s]", sys_evt, evt_key, evt_val);
916
917         b = bundle_create();
918         bundle_add_str(b, evt_key, evt_val);
919         eventsystem_send_system_event(sys_evt, b);
920         bundle_free(b);
921 }
922
923 #if defined TIZEN_WEARABLE
924 int wc_launch_syspopup(netconfig_wcpopup_type_e type)
925 {
926         int ret;
927         bundle* b;
928         char *ssid = NULL;
929
930         b = bundle_create();
931         if (!b) {
932                 ERR("Failed to create bundle");
933                 return -1;
934         }
935
936         switch (type) {
937         case WC_POPUP_TYPE_SESSION_OVERLAPPED:
938                 bundle_add(b, "event-type", "wps-session-overlapped");
939                 break;
940         case WC_POPUP_TYPE_WIFI_CONNECTED:
941                 ssid = vconf_get_str(VCONFKEY_WIFI_CONNECTED_AP_NAME);
942                 if (ssid == NULL) {
943                         ERR("Failed to get connected ap ssid");
944                         ssid = g_strdup(" ");
945                 }
946                 bundle_add(b, "event-type", "wifi-connected");
947                 bundle_add(b, "ssid", ssid);
948                 if (ssid)
949                         g_free(ssid);
950                 break;
951         case WC_POPUP_TYPE_WIFI_RESTRICT:
952                 bundle_add(b, "event-type", "wifi-restrict");
953                 break;
954         default:
955                 ERR("Popup is not supported[%d]", type);
956                 bundle_free(b);
957                 return -1;
958         }
959
960         ret = syspopup_launch("wc-syspopup", b);
961         if (ret < 0)
962                 ERR("Failed to launch syspopup");
963
964         bundle_free(b);
965         return ret;
966 }
967
968 int wc_launch_popup(netconfig_wcpopup_type_e type)
969 {
970         int ret;
971         app_control_h app_control = NULL;
972
973         ret = app_control_create(&app_control);
974         if (ret != APP_CONTROL_ERROR_NONE) {
975                 ERR("Failed to create appcontrol[%d]", ret);
976                 return -1;
977         }
978
979         switch (type) {
980         case WC_POPUP_TYPE_CAPTIVE_PORTAL:
981                 app_control_add_extra_data(app_control, WC_POPUP_EXTRA_DATA_KEY, "captive-portal");
982                 break;
983         default:
984                 ERR("Popup is not supported[%d]", type);
985                 app_control_destroy(app_control);
986                 return -1;
987         }
988
989         app_control_set_app_id(app_control, "com.samsung.weconn-popup");
990         ret = app_control_send_launch_request(app_control, NULL, NULL);
991         if (ret != APP_CONTROL_ERROR_NONE) {
992                 DBG("failed appcontrol launch request [%d]", ret);
993                 app_control_destroy(app_control);
994                 return -1;
995         }
996
997         app_control_destroy(app_control);
998
999         return 0;
1000 }
1001 #endif
1002
1003 void netconfig_set_vconf_int(const char * key, int value)
1004 {
1005         int ret = 0;
1006
1007         DBG("[%s: %d]", key, value);
1008
1009         ret = vconf_set_int(key, value);
1010         if (ret != VCONF_OK)
1011                 ERR("Failed to set");
1012 }
1013
1014 void netconfig_set_vconf_str(const char * key, const char * value)
1015 {
1016         int ret = 0;
1017
1018         DBG("[%s: %s]", key, value);
1019
1020         ret = vconf_set_str(key, value);
1021         if (ret != VCONF_OK)
1022                 ERR("Failed to set");
1023 }
1024
1025 int netconfig_vconf_get_int(const char * key, int *value)
1026 {
1027         int ret = 0;
1028
1029         ret = vconf_get_int(key, value);
1030         if (ret != VCONF_OK) {
1031                 ERR("Failed to get vconfkey [%s] value", key);
1032                 return -1;
1033         }
1034
1035         return 0;
1036 }
1037
1038 int netconfig_vconf_get_bool(const char * key, int *value)
1039 {
1040         int ret = 0;
1041
1042         ret = vconf_get_bool(key, value);
1043         if (ret != VCONF_OK) {
1044                 ERR("Failed to get vconfkey [%s] value", key);
1045                 return -1;
1046         }
1047
1048         return 0;
1049 }
1050
1051 char* netconfig_get_env(const char *key)
1052 {
1053         FILE *fp;
1054         char buf[256], *entry = NULL, *value = NULL, *last;
1055         int len = 0;
1056
1057         if (!key)
1058                 return NULL;
1059
1060         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1061         if (!fp)
1062                 return NULL;
1063
1064         while (fgets(buf, sizeof(buf), fp)) {
1065                 entry = buf;
1066                 entry = strtok_r(entry, "=", &last);
1067                 if (entry) {
1068                         if (strstr(entry, key)) {
1069                                 entry = strtok_r(NULL, "\n", &last);
1070                                 if (entry) {
1071                                         len = strlen(entry);
1072                                         value = (char*)malloc(len+1);
1073                                         g_strlcpy(value, entry, len+1);
1074                                 } else {
1075                                         value = (char*)malloc(sizeof(char));
1076                                         g_strlcpy(value, "\n", sizeof(char));
1077                                 }
1078                                 break;
1079                         }
1080                 }
1081         }
1082
1083         fclose(fp);
1084         return value;
1085 }
1086
1087 void netconfig_set_mac_address_from_file(void)
1088 {
1089         FILE *file = NULL;
1090         char mac_str[MAC_ADDRESS_MAX_LEN];
1091         gchar *mac_lower_str = NULL;
1092         int mac_len = 0;
1093
1094         file = fopen(MAC_INFO_FILEPATH, "r");
1095         if (file == NULL) {
1096                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1097                 return;
1098         }
1099         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1100                 ERR("Fail to read mac address");
1101                 fclose(file);
1102                 return;
1103         }
1104
1105         mac_len = strlen(mac_str);
1106         if (mac_len < 17) {
1107                 ERR("mac.info is empty");
1108                 fclose(file);
1109                 return;
1110         }
1111
1112         mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1113         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1114
1115         g_free(mac_lower_str);
1116         fclose(file);
1117 }