Apply dpm poilcy for wifi and wifi profile
[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         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         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         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         vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
328
329         DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
330         if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
331                 return TRUE;
332 #endif
333         return FALSE;
334 }
335
336 gboolean netconfig_interface_up(const char *ifname)
337 {
338         int fd;
339         struct ifreq ifr;
340
341         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
342         if (fd < 0)
343                 return FALSE;
344
345         memset(&ifr, 0, sizeof(ifr));
346         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
347
348         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
349                 close(fd);
350                 return FALSE;
351         }
352
353         ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
354         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
355                 close(fd);
356                 return FALSE;
357         }
358
359         close(fd);
360
361         DBG("Successfully activated wireless interface");
362         return TRUE;
363 }
364
365 gboolean netconfig_interface_down(const char *ifname)
366 {
367         int fd;
368         struct ifreq ifr;
369
370         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
371         if (fd < 0)
372                 return FALSE;
373
374         memset(&ifr, 0, sizeof(ifr));
375         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
376
377         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
378                 close(fd);
379                 return FALSE;
380         }
381
382         ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
383         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
384                 close(fd);
385                 return FALSE;
386         }
387
388         close(fd);
389
390         DBG("Successfully de-activated wireless interface");
391         return TRUE;
392 }
393
394 int netconfig_execute_file(const char *file_path,
395                 char *const args[], char *const envs[])
396 {
397         pid_t pid = 0;
398         int status = 0;
399         int rv = 0;
400         errno = 0;
401         register unsigned int index = 0;
402         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
403
404         while (args[index] != NULL) {
405                 DBG("%s", args[index]);
406                 index++;
407         }
408
409         if (!(pid = fork())) {
410                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
411                 DBG("Inside child, exec (%s) command", file_path);
412
413                 errno = 0;
414                 if (execve(file_path, args, envs) == -1) {
415                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
416                         DBG("Fail to execute command (%s)", error_buf);
417                         exit(1);
418                 }
419         } else if (pid > 0) {
420                 if (waitpid(pid, &status, 0) == -1)
421                         DBG("wait pid (%u) status (%d)", pid, status);
422
423                 if (WIFEXITED(status)) {
424                         rv = WEXITSTATUS(status);
425                         DBG("exited, status=%d", rv);
426                 } else if (WIFSIGNALED(status)) {
427                         DBG("killed by signal %d", WTERMSIG(status));
428                 } else if (WIFSTOPPED(status)) {
429                         DBG("stopped by signal %d", WSTOPSIG(status));
430                 } else if (WIFCONTINUED(status)) {
431                         DBG("continued");
432                 }
433
434                 return rv;
435         }
436
437         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
438         DBG("failed to fork(%s)", error_buf);
439         return -EIO;
440 }
441
442 static void on_clat_handler()
443 {
444         pid_t clat_pid = 0;
445         int state = 0;
446
447         clat_pid = waitpid(-1, &state, WNOHANG);
448
449         DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
450 }
451
452 int netconfig_execute_clatd(const char *file_path, char *const args[])
453 {
454         pid_t pid = 0;
455         int rv = 0;
456         errno = 0;
457         register unsigned int index = 0;
458         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
459
460         struct sigaction act;
461         int state = 0;
462
463         act.sa_handler = on_clat_handler;
464         sigemptyset(&act.sa_mask);
465         act.sa_flags = 0;
466
467         state = sigaction(SIGCHLD, &act, 0);
468         if (state != 0) {
469                 DBG("sigaction() : %d");
470                 return -1;
471         }
472
473         while (args[index] != NULL) {
474                 DBG("%s", args[index]);
475                 index++;
476         }
477
478         if (!(pid = fork())) {
479                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
480                 DBG("Inside child, exec (%s) command", file_path);
481
482                 errno = 0;
483                 if (execvp(file_path, args) == -1) {
484                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
485                         ERR("Fail to execute command (%s)", error_buf);
486                         return -1;
487                 }
488         } else if (pid > 0) {
489                 ERR("Success to launch clatd");
490                 return rv;
491         }
492
493         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
494         DBG("failed to fork(%s)", error_buf);
495         return -EIO;
496 }
497
498 int __netconfig_get_interface_index(const char *interface_name)
499 {
500         struct ifreq ifr;
501         int sock = 0;
502         int result = 0;
503         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
504
505         if (interface_name == NULL) {
506                 DBG("Inteface name is NULL");
507                 return -1;
508         }
509
510         errno = 0;
511         sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
512         if (sock < 0) {
513                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
514                 DBG("Failed to create socket : %s", error_buf);
515                 return -1;
516         }
517
518         memset(&ifr, 0, sizeof(ifr));
519         strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
520         result = ioctl(sock, SIOCGIFINDEX, &ifr);
521         close(sock);
522
523         if (result < 0) {
524                 DBG("Failed to get ifr index: %s", error_buf);
525                 return -1;
526         }
527
528         return ifr.ifr_ifindex;
529 }
530
531 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
532 {
533         struct ifreq ifr;
534         struct rtentry rt;
535         struct sockaddr_in addr_in;
536         int sock;
537         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
538
539         memset(&ifr, 0, sizeof(ifr));
540
541         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
542
543         if (ifr.ifr_ifindex < 0)
544                 return -1;
545
546         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
547
548         memset(&rt, 0, sizeof(rt));
549
550         rt.rt_flags = RTF_UP | RTF_HOST;
551         memset(&addr_in, 0, sizeof(struct sockaddr_in));
552         addr_in.sin_family = address_family;
553         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
554         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
555
556         memset(&addr_in, 0, sizeof(struct sockaddr_in));
557         addr_in.sin_family = address_family;
558         addr_in.sin_addr.s_addr = INADDR_ANY;
559         memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
560
561         memset(&addr_in, 0, sizeof(struct sockaddr_in));
562         addr_in.sin_family = AF_INET;
563         addr_in.sin_addr.s_addr = inet_addr(subnet);
564         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
565
566         rt.rt_dev = ifr.ifr_name;
567
568         errno = 0;
569         sock = socket(PF_INET, SOCK_DGRAM, 0);
570
571         if (sock < 0) {
572                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
573                 DBG("Failed to create socket : %s", error_buf);
574                 return -1;
575         }
576
577         if (ioctl(sock, SIOCADDRT, &rt) < 0) {
578                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
579                 DBG("Failed to set route address : %s", error_buf);
580                 close(sock);
581                 return -1;
582         }
583
584         close(sock);
585
586         return 1;
587 }
588
589 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
590 {
591         struct ifreq ifr;
592         struct rtentry rt;
593         struct sockaddr_in addr_in;
594         int sock;
595         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
596
597         memset(&ifr, 0, sizeof(ifr));
598         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
599
600         if (ifr.ifr_ifindex < 0)
601                 return -1;
602
603         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
604
605         memset(&rt, 0, sizeof(rt));
606
607         rt.rt_flags = RTF_UP;
608         memset(&addr_in, 0, sizeof(struct sockaddr_in));
609         addr_in.sin_family = address_family;
610         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
611         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
612
613         memset(&addr_in, 0, sizeof(struct sockaddr_in));
614         addr_in.sin_family = address_family;
615         addr_in.sin_addr.s_addr = inet_addr(subnet);
616         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
617         rt.rt_dev = ifr.ifr_name;
618
619         errno = 0;
620         sock = socket(PF_INET, SOCK_DGRAM, 0);
621
622         if (sock < 0) {
623                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
624                 DBG("Failed to create socket : %s", error_buf);
625                 return -1;
626         }
627
628         if (ioctl(sock, SIOCDELRT, &rt) < 0) {
629                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
630                 DBG("Failed to set route address : %s", error_buf);
631                 close(sock);
632                 return -1;
633         }
634
635         close(sock);
636
637         return 1;
638 }
639
640 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
641 {
642         struct in6_rtmsg rt;
643         int fd = 0;
644         int err = 0;
645         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
646
647         memset(&rt, 0, sizeof(rt));
648
649         rt.rtmsg_dst_len = prefix_len;
650
651         rt.rtmsg_flags = RTF_UP | RTF_HOST;
652
653         errno = 0;
654         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
655                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
656                 DBG("inet_pton failed : %s", error_buf);
657                 return -1;
658         }
659
660         if (gateway != NULL) {
661                 rt.rtmsg_flags |= RTF_GATEWAY;
662                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
663                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
664                         DBG("inet_pton failed : %s", error_buf);
665                         return -1;
666                 }
667         }
668
669         rt.rtmsg_metric = 1;
670
671         fd = socket(AF_INET6, SOCK_DGRAM, 0);
672         if (fd < 0) {
673                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
674                 DBG("Failed to create socket : %s", error_buf);
675                 return -1;
676         }
677
678         rt.rtmsg_ifindex = 0;
679
680         if (interface) {
681                 struct ifreq ifr;
682                 memset(&ifr, 0, sizeof(ifr));
683                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
684                 ioctl(fd, SIOCGIFINDEX, &ifr);
685                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
686         }
687
688         if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
689                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
690                 DBG("Failed to add route: %s", error_buf);
691                 close(fd);
692                 return -1;
693         }
694
695         close(fd);
696
697         return 1;
698 }
699
700 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
701 {
702         struct in6_rtmsg rt;
703         int fd = 0;
704         int err = 0;
705
706         memset(&rt, 0, sizeof(rt));
707
708         rt.rtmsg_dst_len = prefix_len;
709
710         rt.rtmsg_flags = RTF_UP | RTF_HOST;
711
712         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
713                 err = -errno;
714                 return err;
715         }
716
717         if (gateway != NULL) {
718                 rt.rtmsg_flags |= RTF_GATEWAY;
719                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
720                         err = -errno;
721                         return err;
722                 }
723         }
724
725         rt.rtmsg_metric = 1;
726
727         fd = socket(AF_INET6, SOCK_DGRAM, 0);
728         if (fd < 0)
729                 return -1;
730
731         rt.rtmsg_ifindex = 0;
732
733         if (interface) {
734                 struct ifreq ifr;
735                 memset(&ifr, 0, sizeof(ifr));
736                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
737                 ioctl(fd, SIOCGIFINDEX, &ifr);
738                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
739         }
740
741         if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
742                 DBG("Failed to del route: %d\n", err);
743                 close(fd);
744                 return -1;
745         }
746
747         close(fd);
748
749         return 1;
750 }
751
752 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
753 {
754 #if defined TIZEN_P2P_ENABLE
755         int ret = 0;
756         DBG("Launch Wi-Fi direct daemon");
757
758         const char *path = "/usr/bin/wifi-direct-server.sh";
759         char *const args[] = { "wifi-direct-server.sh", "start", NULL };
760         char *const envs[] = { NULL };
761
762         ret = netconfig_execute_file(path, args, envs);
763         if (ret < 0) {
764                 ERR("Failed to launch Wi-Fi direct daemon");
765                 netconfig_error_wifi_direct_failed(context);
766                 return FALSE;
767         }
768
769         wifi_complete_launch_direct(wifi, context);
770         return TRUE;
771 #else
772         wifi_complete_launch_direct(wifi, context);
773         return FALSE;
774 #endif
775 }
776
777 int execute_mdnsd_script(char* op) {
778         const char *path = "/usr/bin/mdnsresponder-server.sh";
779         char *const args[] = { "mdnsresponder-server.sh", op, NULL };
780         char *const envs[] = { NULL };
781
782         return netconfig_execute_file(path, args, envs);
783 }
784
785 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context)
786 {
787         DBG("Launch mdnsresponder daemon");
788
789         if (mdnsd_ref_count != 0) {
790                 ERR("Invalid access");
791                 netconfig_error_invalid_parameter(context);
792                 return FALSE;
793         }
794
795         if (execute_mdnsd_script("start") < 0) {
796                 ERR("Failed to launch mdnsresponder daemon");
797                 netconfig_error_invalid_parameter(context);
798                 return FALSE;
799         }
800
801         network_complete_launch_mdns(object, context);
802         return TRUE;
803 }
804
805 gboolean handle_ref_mdns(Network *object, GDBusMethodInvocation *context)
806 {
807         mdnsd_ref_count++;
808
809         DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
810         network_complete_ref_mdns(object, context);
811         return TRUE;
812 }
813
814 gboolean handle_unref_mdns(Network *object, GDBusMethodInvocation *context)
815 {
816         DBG("Unef mdnsresponder daemon");
817
818         if (mdnsd_ref_count <= 0) {
819                 ERR("Invalid access");
820                 netconfig_error_invalid_parameter(context);
821                 return FALSE;
822         }
823
824         mdnsd_ref_count--;
825
826         DBG("Unref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
827         if (mdnsd_ref_count == 0) {
828                 if (execute_mdnsd_script("stop") < 0) {
829                         ERR("Failed to stop mdnsresponder daemon");
830                         netconfig_error_invalid_parameter(context);
831                         return FALSE;
832                 }
833         }
834
835         network_complete_unref_mdns(object, context);
836         return TRUE;
837 }
838
839 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
840 {
841         int ret = 0;
842         bundle *b;
843         static gboolean is_found_noti_exists = FALSE;
844         static gboolean is_portal_noti_exists = FALSE;
845
846         if (noti == NULL) {
847                 ERR("Invalid notification");
848                 return FALSE;
849         }
850
851         if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
852                 if (is_found_noti_exists == FALSE)
853                         return TRUE;
854
855                 is_found_noti_exists = FALSE;
856         } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
857                 if (is_found_noti_exists == TRUE)
858                         return TRUE;
859
860                 is_found_noti_exists = TRUE;
861         } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
862                 if (is_portal_noti_exists == TRUE)
863                         return TRUE;
864
865                 is_portal_noti_exists = TRUE;
866         } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
867                 if (is_portal_noti_exists == FALSE)
868                         return TRUE;
869
870                 is_portal_noti_exists = FALSE;
871         }
872
873         b = bundle_create();
874         bundle_add(b, "_SYSPOPUP_TYPE_", noti);
875
876         if (ssid != NULL) {
877                 DBG("ssid (%s)", ssid);
878                 bundle_add(b, "_AP_NAME_", ssid);
879         }
880
881         ret = syspopup_launch("net-popup", b);
882
883         bundle_free(b);
884
885         if (ret < 0) {
886                 ERR("Unable to launch noti-popup. Err = %d", ret);
887                 return FALSE;
888         }
889
890         DBG("Successfully sent notification (%s)", noti);
891         return TRUE;
892 }
893
894 int netconfig_send_message_to_net_popup(const char *title,
895                 const char *content, const char *type, const char *ssid)
896 {
897         int ret = 0;
898         bundle *b = bundle_create();
899
900         bundle_add(b, "_SYSPOPUP_TITLE_", title);
901         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
902         bundle_add(b, "_SYSPOPUP_TYPE_", type);
903         bundle_add(b, "_AP_NAME_", ssid);
904
905         ret = syspopup_launch("net-popup", b);
906
907         bundle_free(b);
908
909         return ret;
910 }
911
912 int netconfig_send_restriction_to_net_popup(const char *title,
913                 const char *type, const char *restriction)
914 {
915         int ret = 0;
916         bundle *b = bundle_create();
917
918         bundle_add(b, "_SYSPOPUP_TITLE_", title);
919         bundle_add(b, "_SYSPOPUP_CONTENT_", "security restriction");
920         bundle_add(b, "_SYSPOPUP_TYPE_", type);
921         bundle_add(b, "_RESTRICTED_TYPE_", restriction);
922
923         ret = syspopup_launch("net-popup", b);
924
925         bundle_free(b);
926
927         return ret;
928 }
929
930 void netconfig_set_system_event(const char * sys_evt, const char * evt_key, const char * evt_val)
931 {
932         bundle *b = NULL;
933
934         DBG("System event set [%s : %s : %s]", sys_evt, evt_key, evt_val);
935
936         b = bundle_create();
937         bundle_add_str(b, evt_key, evt_val);
938         eventsystem_send_system_event(sys_evt, b);
939         bundle_free(b);
940 }
941
942 #if defined TIZEN_WEARABLE
943 int wc_launch_syspopup(netconfig_wcpopup_type_e type)
944 {
945         int ret;
946         bundle* b;
947         char *ssid = NULL;
948
949         b = bundle_create();
950         if (!b) {
951                 ERR("Failed to create bundle");
952                 return -1;
953         }
954
955         switch (type) {
956         case WC_POPUP_TYPE_SESSION_OVERLAPPED:
957                 bundle_add(b, "event-type", "wps-session-overlapped");
958                 break;
959         case WC_POPUP_TYPE_WIFI_CONNECTED:
960                 ssid = vconf_get_str(VCONFKEY_WIFI_CONNECTED_AP_NAME);
961                 if (ssid == NULL) {
962                         ERR("Failed to get connected ap ssid");
963                         ssid = g_strdup(" ");
964                 }
965                 bundle_add(b, "event-type", "wifi-connected");
966                 bundle_add(b, "ssid", ssid);
967                 if (ssid)
968                         g_free(ssid);
969                 break;
970         case WC_POPUP_TYPE_WIFI_RESTRICT:
971                 bundle_add(b, "event-type", "wifi-restrict");
972                 break;
973         default:
974                 ERR("Popup is not supported[%d]", type);
975                 bundle_free(b);
976                 return -1;
977         }
978
979         ret = syspopup_launch("wc-syspopup", b);
980         if (ret < 0)
981                 ERR("Failed to launch syspopup");
982
983         bundle_free(b);
984         return ret;
985 }
986
987 int wc_launch_popup(netconfig_wcpopup_type_e type)
988 {
989         int ret;
990         app_control_h app_control = NULL;
991
992         ret = app_control_create(&app_control);
993         if (ret != APP_CONTROL_ERROR_NONE) {
994                 ERR("Failed to create appcontrol[%d]", ret);
995                 return -1;
996         }
997
998         switch (type) {
999         case WC_POPUP_TYPE_CAPTIVE_PORTAL:
1000                 app_control_add_extra_data(app_control, WC_POPUP_EXTRA_DATA_KEY, "captive-portal");
1001                 break;
1002         default:
1003                 ERR("Popup is not supported[%d]", type);
1004                 app_control_destroy(app_control);
1005                 return -1;
1006         }
1007
1008         app_control_set_app_id(app_control, "com.samsung.weconn-popup");
1009         ret = app_control_send_launch_request(app_control, NULL, NULL);
1010         if (ret != APP_CONTROL_ERROR_NONE) {
1011                 DBG("failed appcontrol launch request [%d]", ret);
1012                 app_control_destroy(app_control);
1013                 return -1;
1014         }
1015
1016         app_control_destroy(app_control);
1017
1018         return 0;
1019 }
1020 #endif
1021
1022 void netconfig_set_vconf_int(const char * key, int value)
1023 {
1024         int ret = 0;
1025
1026         DBG("[%s: %d]", key, value);
1027
1028         ret = vconf_set_int(key, value);
1029         if (ret != VCONF_OK)
1030                 ERR("Failed to set");
1031 }
1032
1033 void netconfig_set_vconf_str(const char * key, const char * value)
1034 {
1035         int ret = 0;
1036
1037         DBG("[%s: %s]", key, value);
1038
1039         ret = vconf_set_str(key, value);
1040         if (ret != VCONF_OK)
1041                 ERR("Failed to set");
1042 }
1043
1044 char* netconfig_get_env(const char *key)
1045 {
1046         FILE *fp;
1047         char buf[256], *entry = NULL, *value = NULL, *last;
1048         int len = 0;
1049
1050         if (!key)
1051                 return NULL;
1052
1053         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1054         if (!fp)
1055                 return NULL;
1056
1057         while (fgets(buf, sizeof(buf), fp)) {
1058                 entry = buf;
1059                 entry = strtok_r(entry, "=", &last);
1060                 if (entry) {
1061                         if (strstr(entry, key)) {
1062                                 entry = strtok_r(NULL, "\n", &last);
1063                                 if (entry) {
1064                                         len = strlen(entry);
1065                                         value = (char*)malloc(len+1);
1066                                         g_strlcpy(value, entry, len+1);
1067                                 } else {
1068                                         value = (char*)malloc(sizeof(char));
1069                                         g_strlcpy(value, "\n", sizeof(char));
1070                                 }
1071                                 break;
1072                         }
1073                 }
1074         }
1075
1076         fclose(fp);
1077         return value;
1078 }
1079
1080 void netconfig_set_mac_address_from_file(void)
1081 {
1082         FILE *file = NULL;
1083         char mac_str[MAC_ADDRESS_MAX_LEN];
1084         gchar *mac_lower_str = NULL;
1085         int mac_len = 0;
1086
1087         file = fopen(MAC_INFO_FILEPATH, "r");
1088         if (file == NULL) {
1089                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1090                 return;
1091         }
1092         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1093                 ERR("Fail to read mac address");
1094                 fclose(file);
1095                 return;
1096         }
1097
1098         mac_len = strlen(mac_str);
1099         if (mac_len < 17) {
1100                 ERR("mac.info is empty");
1101                 fclose(file);
1102                 return;
1103         }
1104
1105         mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1106         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1107
1108         g_free(mac_lower_str);
1109         fclose(file);
1110 }