Merge from tizen_3.0
[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 {
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 static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
786                 const gchar *Name, const gchar *path, const gchar *interface,
787                 const gchar *sig, GVariant *param, gpointer user_data)
788 {
789         gchar *name = NULL;
790         gchar *old = NULL;
791         gchar *new = NULL;
792         dnssd_conn_destroy_data *data = user_data;
793         GDBusConnection *connection = NULL;
794         connection = netdbus_get_connection();
795
796         if (param == NULL)
797                 return;
798
799         g_variant_get(param, "(sss)", &name, &old, &new);
800
801         if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
802                 DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
803                         data->conn_id);
804                 mdnsd_ref_count--;
805                 g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
806                 if (mdnsd_ref_count == 0) {
807                         if (execute_mdnsd_script("stop") < 0)
808                                 ERR("Failed to stop mdnsresponder daemon");
809                 }
810         }
811         g_free(name);
812         g_free(old);
813         g_free(new);
814         g_free(data->conn_name);
815         g_free(data);
816         return;
817 }
818
819 static void register_dnssd_conn_destroy_signal(gchar *name)
820 {
821         dnssd_conn_destroy_data *data;
822         GDBusConnection *connection = NULL;
823         connection = netdbus_get_connection();
824
825         if (connection == NULL) {
826                 ERR("Failed to get GDbus Connection");
827                 return;
828         }
829
830         data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
831         data->conn_name = g_strdup(name);
832
833         data->conn_id = g_dbus_connection_signal_subscribe(connection,
834                                                         DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
835                                                         "NameOwnerChanged", NULL, name,
836                                                         G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
837                                                         data, NULL);
838         return;
839 }
840
841 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
842                                                         gchar *name)
843 {
844         DBG("Launch mdnsresponder daemon");
845
846         if (execute_mdnsd_script("start") < 0) {
847                 ERR("Failed to launch mdnsresponder daemon");
848                 netconfig_error_invalid_parameter(context);
849                 return FALSE;
850         }
851
852         mdnsd_ref_count++;
853         register_dnssd_conn_destroy_signal(name);
854         DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
855
856         network_complete_launch_mdns(object, context);
857         return TRUE;
858 }
859
860 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
861 {
862         int ret = 0;
863         bundle *b;
864         static gboolean is_found_noti_exists = FALSE;
865         static gboolean is_portal_noti_exists = FALSE;
866
867         if (noti == NULL) {
868                 ERR("Invalid notification");
869                 return FALSE;
870         }
871
872         if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
873                 if (is_found_noti_exists == FALSE)
874                         return TRUE;
875
876                 is_found_noti_exists = FALSE;
877         } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
878                 if (is_found_noti_exists == TRUE)
879                         return TRUE;
880
881                 is_found_noti_exists = TRUE;
882         } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
883                 if (is_portal_noti_exists == TRUE)
884                         return TRUE;
885
886                 is_portal_noti_exists = TRUE;
887         } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
888                 if (is_portal_noti_exists == FALSE)
889                         return TRUE;
890
891                 is_portal_noti_exists = FALSE;
892         }
893
894         b = bundle_create();
895         bundle_add(b, "_SYSPOPUP_TYPE_", noti);
896
897         if (ssid != NULL) {
898                 DBG("ssid (%s)", ssid);
899                 bundle_add(b, "_AP_NAME_", ssid);
900         }
901
902         ret = syspopup_launch("net-popup", b);
903
904         bundle_free(b);
905
906         if (ret < 0) {
907                 ERR("Unable to launch noti-popup. Err = %d", ret);
908                 return FALSE;
909         }
910
911         DBG("Successfully sent notification (%s)", noti);
912         return TRUE;
913 }
914
915 int netconfig_send_message_to_net_popup(const char *title,
916                 const char *content, const char *type, const char *ssid)
917 {
918         int ret = 0;
919         bundle *b = bundle_create();
920
921         bundle_add(b, "_SYSPOPUP_TITLE_", title);
922         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
923         bundle_add(b, "_SYSPOPUP_TYPE_", type);
924         bundle_add(b, "_AP_NAME_", ssid);
925
926         ret = syspopup_launch("net-popup", b);
927
928         bundle_free(b);
929
930         return ret;
931 }
932
933 int netconfig_send_restriction_to_net_popup(const char *title,
934                 const char *type, const char *restriction)
935 {
936         int ret = 0;
937         bundle *b = bundle_create();
938
939         bundle_add(b, "_SYSPOPUP_TITLE_", title);
940         bundle_add(b, "_SYSPOPUP_CONTENT_", "security restriction");
941         bundle_add(b, "_SYSPOPUP_TYPE_", type);
942         bundle_add(b, "_RESTRICTED_TYPE_", restriction);
943
944         ret = syspopup_launch("net-popup", b);
945
946         bundle_free(b);
947
948         return ret;
949 }
950
951 void netconfig_set_system_event(const char * sys_evt, const char * evt_key, const char * evt_val)
952 {
953         bundle *b = NULL;
954
955         DBG("System event set [%s : %s : %s]", sys_evt, evt_key, evt_val);
956
957         b = bundle_create();
958         bundle_add_str(b, evt_key, evt_val);
959         eventsystem_send_system_event(sys_evt, b);
960         bundle_free(b);
961 }
962
963 void netconfig_set_vconf_int(const char * key, int value)
964 {
965         int ret = 0;
966
967         DBG("[%s: %d]", key, value);
968
969         ret = vconf_set_int(key, value);
970         if (ret != VCONF_OK)
971                 ERR("Failed to set");
972 }
973
974 void netconfig_set_vconf_str(const char * key, const char * value)
975 {
976         int ret = 0;
977
978         DBG("[%s: %s]", key, value);
979
980         ret = vconf_set_str(key, value);
981         if (ret != VCONF_OK)
982                 ERR("Failed to set");
983 }
984
985 int netconfig_vconf_get_int(const char * key, int *value)
986 {
987         int ret = 0;
988
989         ret = vconf_get_int(key, value);
990         if (ret != VCONF_OK) {
991                 ERR("Failed to get vconfkey [%s] value", key);
992                 return -1;
993         }
994
995         return 0;
996 }
997
998 int netconfig_vconf_get_bool(const char * key, int *value)
999 {
1000         int ret = 0;
1001
1002         ret = vconf_get_bool(key, value);
1003         if (ret != VCONF_OK) {
1004                 ERR("Failed to get vconfkey [%s] value", key);
1005                 return -1;
1006         }
1007
1008         return 0;
1009 }
1010
1011 char* netconfig_get_env(const char *key)
1012 {
1013         FILE *fp;
1014         char buf[256], *entry = NULL, *value = NULL, *last;
1015         int len = 0;
1016
1017         if (!key)
1018                 return NULL;
1019
1020         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1021         if (!fp)
1022                 return NULL;
1023
1024         while (fgets(buf, sizeof(buf), fp)) {
1025                 entry = buf;
1026                 entry = strtok_r(entry, "=", &last);
1027                 if (entry) {
1028                         if (strstr(entry, key)) {
1029                                 entry = strtok_r(NULL, "\n", &last);
1030                                 if (entry) {
1031                                         len = strlen(entry);
1032                                         value = (char*)malloc(len+1);
1033                                         g_strlcpy(value, entry, len+1);
1034                                 } else {
1035                                         value = (char*)malloc(sizeof(char));
1036                                         g_strlcpy(value, "\n", sizeof(char));
1037                                 }
1038                                 break;
1039                         }
1040                 }
1041         }
1042
1043         fclose(fp);
1044         return value;
1045 }
1046
1047 void netconfig_set_mac_address_from_file(void)
1048 {
1049         FILE *file = NULL;
1050         char mac_str[MAC_ADDRESS_MAX_LEN];
1051         gchar *mac_lower_str = NULL;
1052         int mac_len = 0;
1053
1054         file = fopen(MAC_INFO_FILEPATH, "r");
1055         if (file == NULL) {
1056                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1057                 return;
1058         }
1059         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1060                 ERR("Fail to read mac address");
1061                 fclose(file);
1062                 return;
1063         }
1064
1065         mac_len = strlen(mac_str);
1066         if (mac_len < 17) {
1067                 ERR("mac.info is empty");
1068                 fclose(file);
1069                 return;
1070         }
1071
1072         mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1073         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1074
1075         g_free(mac_lower_str);
1076         fclose(file);
1077 }