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