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