[wearable] Remove weconn dependency
[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
45 #define MAC_INFO_FILEPATH               tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
46 #define MAC_ADDRESS_MAX_LEN             18
47
48 static gboolean netconfig_device_picker_test = FALSE;
49 static int mdnsd_ref_count = 0;
50
51 GKeyFile *netconfig_keyfile_load(const char *pathname)
52 {
53         GKeyFile *keyfile = NULL;
54         GError *error = NULL;
55
56         keyfile = g_key_file_new();
57         if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
58                 DBG("Unable to open %s, error %s", pathname, error->message);
59                 g_error_free(error);
60
61                 g_key_file_free(keyfile);
62                 keyfile = NULL;
63         }
64
65         return keyfile;
66 }
67
68 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
69 {
70         gsize size = 0;
71         GError *error = NULL;
72         gchar *keydata = NULL;
73         gchar *needle = NULL, *directory = NULL;
74
75         directory = g_strdup(pathname);
76         needle = g_strrstr(directory, "/");
77
78         if (needle != NULL)
79                 *needle = '\0';
80
81         if (directory == NULL || (*directory) == '\0') {
82                 g_free(directory);
83                 return;
84         }
85
86         if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
87                 if (g_mkdir_with_parents(directory,
88                                 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
89                         g_free(directory);
90                         return;
91                 }
92         }
93         g_free(directory);
94
95         keydata = g_key_file_to_data(keyfile, &size, &error);
96         if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
97                 DBG("Unable to save %s, error %s", pathname, error->message);
98                 g_error_free(error);
99         }
100
101         chmod(pathname, S_IRUSR | S_IWUSR);
102
103         g_free(keydata);
104 }
105
106 void netconfig_start_timer_seconds(guint secs,
107                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
108 {
109         guint t_id = 0;
110
111         if (callback == NULL) {
112                 ERR("callback function is NULL");
113                 return;
114         }
115
116         if ((timer_id != NULL && *timer_id != 0)) {
117                 ERR("timer already is registered");
118                 return;
119         }
120
121         t_id = g_timeout_add_seconds(secs, callback, user_data);
122
123         if (t_id == 0) {
124                 ERR("Can't add timer");
125                 return;
126         }
127
128         if (timer_id != NULL)
129                 *timer_id = t_id;
130 }
131
132 void netconfig_start_timer(guint msecs,
133                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
134 {
135         guint t_id = 0;
136
137         INFO("Register timer with callback pointer (%p)", callback);
138
139         if (callback == NULL) {
140                 ERR("callback function is NULL");
141                 return;
142         }
143
144         if ((timer_id != NULL && *timer_id != 0)) {
145                 ERR("timer already is registered");
146                 return;
147         }
148
149         t_id = g_timeout_add(msecs, callback, user_data);
150
151         if (t_id == 0) {
152                 ERR("Can't add timer");
153                 return;
154         }
155
156         if (timer_id != NULL)
157                 *timer_id = t_id;
158 }
159
160 void netconfig_stop_timer(guint *timer_id)
161 {
162         if (timer_id == NULL) {
163                 ERR("timer is NULL");
164                 return;
165         }
166
167         if (*timer_id != 0) {
168                 g_source_remove(*timer_id);
169                 *timer_id = 0;
170         }
171 }
172
173 static gboolean __netconfig_test_device_picker()
174 {
175         char *favorite_wifi_service = NULL;
176
177         favorite_wifi_service = wifi_get_favorite_service();
178         if (favorite_wifi_service != NULL) {
179                 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
180                 g_free(favorite_wifi_service);
181                 return FALSE;
182         }
183
184         return TRUE;
185 }
186
187 static void __netconfig_pop_device_picker(void)
188 {
189 #if defined TIZEN_WEARABLE
190         int ret = 0;
191         app_control_h   control = NULL;
192
193         ret = app_control_create(&control);
194         if (APP_CONTROL_ERROR_NONE != ret) {
195                 DBG("failed to create app control");
196                 return ;
197         }
198
199         app_control_add_extra_data(control, "viewtype", "scanlist");
200
201         app_control_set_app_id(control, "org.tizen.wifi");
202         ret = app_control_send_launch_request(control, NULL, NULL);
203         if (APP_CONTROL_ERROR_NONE == ret)
204                 DBG("Launch request sent successfully");
205
206         app_control_destroy(control);
207 #else
208         bundle *b = NULL;
209         int wifi_ug_state = 0;
210
211         netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
212         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
213                 return;
214
215         b = bundle_create();
216
217         DBG("Launch Wi-Fi device picker");
218         syspopup_launch("wifi-qs", b);
219
220         bundle_free(b);
221 #endif
222 }
223
224 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
225 {
226         if (__netconfig_test_device_picker() == TRUE)
227                 __netconfig_pop_device_picker();
228
229         return FALSE;
230 }
231
232 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
233 {
234         static guint netconfig_wifi_device_picker_service_timer = 0;
235
236         if (is_set_method != TRUE)
237                 return netconfig_wifi_device_picker_service_timer;
238
239         if (netconfig_wifi_device_picker_service_timer != timer_id)
240                 netconfig_wifi_device_picker_service_timer = timer_id;
241
242         return netconfig_wifi_device_picker_service_timer;
243 }
244
245 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
246 {
247         __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
248 }
249
250 static guint __netconfig_wifi_device_picker_get_timer_id(void)
251 {
252         return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
253 }
254
255 void netconfig_wifi_enable_device_picker_test(void)
256 {
257         netconfig_device_picker_test = TRUE;
258 }
259
260 void netconfig_wifi_device_picker_service_start(void)
261 {
262         const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
263         guint timer_id = 0;
264
265         if (netconfig_device_picker_test == TRUE)
266                 netconfig_device_picker_test = FALSE;
267         else
268                 return;
269
270         int wifi_ug_state;
271
272         netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
273         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
274                 return;
275
276         DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
277         netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
278
279         __netconfig_wifi_device_picker_set_timer_id(timer_id);
280 }
281
282 void netconfig_wifi_device_picker_service_stop(void)
283 {
284         guint timer_id = 0;
285
286         timer_id = __netconfig_wifi_device_picker_get_timer_id();
287         if (timer_id == 0)
288                 return;
289
290         DBG("Clear device picker timer with timer_id %d", timer_id);
291
292         netconfig_stop_timer(&timer_id);
293
294         __netconfig_wifi_device_picker_set_timer_id(timer_id);
295 }
296
297 gboolean netconfig_is_wifi_direct_on(void)
298 {
299 #if defined TIZEN_P2P_ENABLE
300         int wifi_direct_state = 0;
301
302         netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
303
304         DBG("Wi-Fi direct mode %d", wifi_direct_state);
305         return (wifi_direct_state != 0) ? TRUE : FALSE;
306 #else
307         return FALSE;
308 #endif
309 }
310
311 gboolean netconfig_is_wifi_tethering_on(void)
312 {
313 #if defined TIZEN_TETHERING_ENABLE
314         int wifi_tethering_state = 0;
315
316         netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
317         DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
318         if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
319                 return TRUE;
320 #endif
321         return FALSE;
322 }
323
324 gboolean netconfig_interface_up(const char *ifname)
325 {
326         int fd;
327         struct ifreq ifr;
328
329         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
330         if (fd < 0)
331                 return FALSE;
332
333         memset(&ifr, 0, sizeof(ifr));
334         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
335
336         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
337                 close(fd);
338                 return FALSE;
339         }
340
341         ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
342         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
343                 close(fd);
344                 return FALSE;
345         }
346
347         close(fd);
348
349         DBG("Successfully activated wireless interface");
350         return TRUE;
351 }
352
353 gboolean netconfig_interface_down(const char *ifname)
354 {
355         int fd;
356         struct ifreq ifr;
357
358         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
359         if (fd < 0)
360                 return FALSE;
361
362         memset(&ifr, 0, sizeof(ifr));
363         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
364
365         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
366                 close(fd);
367                 return FALSE;
368         }
369
370         ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
371         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
372                 close(fd);
373                 return FALSE;
374         }
375
376         close(fd);
377
378         DBG("Successfully de-activated wireless interface");
379         return TRUE;
380 }
381
382 int netconfig_execute_file(const char *file_path,
383                 char *const args[], char *const envs[])
384 {
385         pid_t pid = 0;
386         int status = 0;
387         int rv = 0;
388         errno = 0;
389         register unsigned int index = 0;
390         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
391
392         while (args[index] != NULL) {
393                 DBG("%s", args[index]);
394                 index++;
395         }
396
397         if (!(pid = fork())) {
398                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
399                 DBG("Inside child, exec (%s) command", file_path);
400
401                 errno = 0;
402                 if (execve(file_path, args, envs) == -1) {
403                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
404                         DBG("Fail to execute command (%s)", error_buf);
405                         exit(1);
406                 }
407         } else if (pid > 0) {
408                 if (waitpid(pid, &status, 0) == -1)
409                         DBG("wait pid (%u) status (%d)", pid, status);
410
411                 if (WIFEXITED(status)) {
412                         rv = WEXITSTATUS(status);
413                         DBG("exited, status=%d", rv);
414                 } else if (WIFSIGNALED(status)) {
415                         DBG("killed by signal %d", WTERMSIG(status));
416                 } else if (WIFSTOPPED(status)) {
417                         DBG("stopped by signal %d", WSTOPSIG(status));
418                 } else if (WIFCONTINUED(status)) {
419                         DBG("continued");
420                 }
421
422                 return rv;
423         }
424
425         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
426         DBG("failed to fork(%s)", error_buf);
427         return -EIO;
428 }
429
430 static void on_clat_handler()
431 {
432         pid_t clat_pid = 0;
433         int state = 0;
434
435         clat_pid = waitpid(-1, &state, WNOHANG);
436
437         DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
438 }
439
440 int netconfig_execute_clatd(const char *file_path, char *const args[])
441 {
442         pid_t pid = 0;
443         int rv = 0;
444         errno = 0;
445         register unsigned int index = 0;
446         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
447
448         struct sigaction act;
449         int state = 0;
450
451         act.sa_handler = on_clat_handler;
452         sigemptyset(&act.sa_mask);
453         act.sa_flags = 0;
454
455         state = sigaction(SIGCHLD, &act, 0);
456         if (state != 0) {
457                 DBG("sigaction() : %d");
458                 return -1;
459         }
460
461         while (args[index] != NULL) {
462                 DBG("%s", args[index]);
463                 index++;
464         }
465
466         if (!(pid = fork())) {
467                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
468                 DBG("Inside child, exec (%s) command", file_path);
469
470                 errno = 0;
471                 if (execvp(file_path, args) == -1) {
472                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
473                         ERR("Fail to execute command (%s)", error_buf);
474                         return -1;
475                 }
476         } else if (pid > 0) {
477                 ERR("Success to launch clatd");
478                 return rv;
479         }
480
481         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
482         DBG("failed to fork(%s)", error_buf);
483         return -EIO;
484 }
485
486 int __netconfig_get_interface_index(const char *interface_name)
487 {
488         struct ifreq ifr;
489         int sock = 0;
490         int result = 0;
491         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
492
493         if (interface_name == NULL) {
494                 DBG("Inteface name is NULL");
495                 return -1;
496         }
497
498         errno = 0;
499         sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
500         if (sock < 0) {
501                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
502                 DBG("Failed to create socket : %s", error_buf);
503                 return -1;
504         }
505
506         memset(&ifr, 0, sizeof(ifr));
507         strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
508         result = ioctl(sock, SIOCGIFINDEX, &ifr);
509         close(sock);
510
511         if (result < 0) {
512                 DBG("Failed to get ifr index: %s", error_buf);
513                 return -1;
514         }
515
516         return ifr.ifr_ifindex;
517 }
518
519 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
520 {
521         struct ifreq ifr;
522         struct rtentry rt;
523         struct sockaddr_in addr_in;
524         int sock;
525         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
526
527         memset(&ifr, 0, sizeof(ifr));
528
529         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
530
531         if (ifr.ifr_ifindex < 0)
532                 return -1;
533
534         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
535
536         memset(&rt, 0, sizeof(rt));
537
538         rt.rt_flags = RTF_UP | RTF_HOST;
539         memset(&addr_in, 0, sizeof(struct sockaddr_in));
540         addr_in.sin_family = address_family;
541         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
542         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
543
544         memset(&addr_in, 0, sizeof(struct sockaddr_in));
545         addr_in.sin_family = address_family;
546         addr_in.sin_addr.s_addr = INADDR_ANY;
547         memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
548
549         memset(&addr_in, 0, sizeof(struct sockaddr_in));
550         addr_in.sin_family = AF_INET;
551         addr_in.sin_addr.s_addr = inet_addr(subnet);
552         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
553
554         rt.rt_dev = ifr.ifr_name;
555
556         errno = 0;
557         sock = socket(PF_INET, SOCK_DGRAM, 0);
558
559         if (sock < 0) {
560                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
561                 DBG("Failed to create socket : %s", error_buf);
562                 return -1;
563         }
564
565         if (ioctl(sock, SIOCADDRT, &rt) < 0) {
566                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
567                 DBG("Failed to set route address : %s", error_buf);
568                 close(sock);
569                 return -1;
570         }
571
572         close(sock);
573
574         return 1;
575 }
576
577 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
578 {
579         struct ifreq ifr;
580         struct rtentry rt;
581         struct sockaddr_in addr_in;
582         int sock;
583         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
584
585         memset(&ifr, 0, sizeof(ifr));
586         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
587
588         if (ifr.ifr_ifindex < 0)
589                 return -1;
590
591         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
592
593         memset(&rt, 0, sizeof(rt));
594
595         rt.rt_flags = RTF_UP;
596         memset(&addr_in, 0, sizeof(struct sockaddr_in));
597         addr_in.sin_family = address_family;
598         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
599         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
600
601         memset(&addr_in, 0, sizeof(struct sockaddr_in));
602         addr_in.sin_family = address_family;
603         addr_in.sin_addr.s_addr = inet_addr(subnet);
604         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
605         rt.rt_dev = ifr.ifr_name;
606
607         errno = 0;
608         sock = socket(PF_INET, SOCK_DGRAM, 0);
609
610         if (sock < 0) {
611                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
612                 DBG("Failed to create socket : %s", error_buf);
613                 return -1;
614         }
615
616         if (ioctl(sock, SIOCDELRT, &rt) < 0) {
617                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
618                 DBG("Failed to set route address : %s", error_buf);
619                 close(sock);
620                 return -1;
621         }
622
623         close(sock);
624
625         return 1;
626 }
627
628 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
629 {
630         struct in6_rtmsg rt;
631         int fd = 0;
632         int err = 0;
633         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
634
635         memset(&rt, 0, sizeof(rt));
636
637         rt.rtmsg_dst_len = prefix_len;
638
639         rt.rtmsg_flags = RTF_UP | RTF_HOST;
640
641         errno = 0;
642         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
643                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
644                 DBG("inet_pton failed : %s", error_buf);
645                 return -1;
646         }
647
648         if (gateway != NULL) {
649                 rt.rtmsg_flags |= RTF_GATEWAY;
650                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
651                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
652                         DBG("inet_pton failed : %s", error_buf);
653                         return -1;
654                 }
655         }
656
657         rt.rtmsg_metric = 1;
658
659         fd = socket(AF_INET6, SOCK_DGRAM, 0);
660         if (fd < 0) {
661                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
662                 DBG("Failed to create socket : %s", error_buf);
663                 return -1;
664         }
665
666         rt.rtmsg_ifindex = 0;
667
668         if (interface) {
669                 struct ifreq ifr;
670                 memset(&ifr, 0, sizeof(ifr));
671                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
672                 ioctl(fd, SIOCGIFINDEX, &ifr);
673                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
674         }
675
676         if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
677                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
678                 DBG("Failed to add route: %s", error_buf);
679                 close(fd);
680                 return -1;
681         }
682
683         close(fd);
684
685         return 1;
686 }
687
688 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
689 {
690         struct in6_rtmsg rt;
691         int fd = 0;
692         int err = 0;
693
694         memset(&rt, 0, sizeof(rt));
695
696         rt.rtmsg_dst_len = prefix_len;
697
698         rt.rtmsg_flags = RTF_UP | RTF_HOST;
699
700         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
701                 err = -errno;
702                 return err;
703         }
704
705         if (gateway != NULL) {
706                 rt.rtmsg_flags |= RTF_GATEWAY;
707                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
708                         err = -errno;
709                         return err;
710                 }
711         }
712
713         rt.rtmsg_metric = 1;
714
715         fd = socket(AF_INET6, SOCK_DGRAM, 0);
716         if (fd < 0)
717                 return -1;
718
719         rt.rtmsg_ifindex = 0;
720
721         if (interface) {
722                 struct ifreq ifr;
723                 memset(&ifr, 0, sizeof(ifr));
724                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
725                 ioctl(fd, SIOCGIFINDEX, &ifr);
726                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
727         }
728
729         if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
730                 DBG("Failed to del route: %d\n", err);
731                 close(fd);
732                 return -1;
733         }
734
735         close(fd);
736
737         return 1;
738 }
739
740 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
741 {
742 #if defined TIZEN_P2P_ENABLE
743         int ret = 0;
744         DBG("Launch Wi-Fi direct daemon");
745
746         const char *path = "/usr/bin/wifi-direct-server.sh";
747         char *const args[] = { "wifi-direct-server.sh", "start", NULL };
748         char *const envs[] = { NULL };
749
750         ret = netconfig_execute_file(path, args, envs);
751         if (ret < 0) {
752                 ERR("Failed to launch Wi-Fi direct daemon");
753                 netconfig_error_wifi_direct_failed(context);
754                 return FALSE;
755         }
756
757         wifi_complete_launch_direct(wifi, context);
758         return TRUE;
759 #else
760         wifi_complete_launch_direct(wifi, context);
761         return FALSE;
762 #endif
763 }
764
765 int execute_mdnsd_script(char* op) {
766         const char *path = "/usr/bin/mdnsresponder-server.sh";
767         char *const args[] = { "mdnsresponder-server.sh", op, NULL };
768         char *const envs[] = { NULL };
769
770         return netconfig_execute_file(path, args, envs);
771 }
772
773 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context)
774 {
775         DBG("Launch mdnsresponder daemon");
776
777         if (mdnsd_ref_count != 0) {
778                 ERR("Invalid access");
779                 netconfig_error_invalid_parameter(context);
780                 return FALSE;
781         }
782
783         if (execute_mdnsd_script("start") < 0) {
784                 ERR("Failed to launch mdnsresponder daemon");
785                 netconfig_error_invalid_parameter(context);
786                 return FALSE;
787         }
788
789         network_complete_launch_mdns(object, context);
790         return TRUE;
791 }
792
793 gboolean handle_ref_mdns(Network *object, GDBusMethodInvocation *context)
794 {
795         mdnsd_ref_count++;
796
797         DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
798         network_complete_ref_mdns(object, context);
799         return TRUE;
800 }
801
802 gboolean handle_unref_mdns(Network *object, GDBusMethodInvocation *context)
803 {
804         DBG("Unef mdnsresponder daemon");
805
806         if (mdnsd_ref_count <= 0) {
807                 ERR("Invalid access");
808                 netconfig_error_invalid_parameter(context);
809                 return FALSE;
810         }
811
812         mdnsd_ref_count--;
813
814         DBG("Unref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
815         if (mdnsd_ref_count == 0) {
816                 if (execute_mdnsd_script("stop") < 0) {
817                         ERR("Failed to stop mdnsresponder daemon");
818                         netconfig_error_invalid_parameter(context);
819                         return FALSE;
820                 }
821         }
822
823         network_complete_unref_mdns(object, context);
824         return TRUE;
825 }
826
827 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
828 {
829         int ret = 0;
830         bundle *b;
831         static gboolean is_found_noti_exists = FALSE;
832         static gboolean is_portal_noti_exists = FALSE;
833
834         if (noti == NULL) {
835                 ERR("Invalid notification");
836                 return FALSE;
837         }
838
839         if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
840                 if (is_found_noti_exists == FALSE)
841                         return TRUE;
842
843                 is_found_noti_exists = FALSE;
844         } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
845                 if (is_found_noti_exists == TRUE)
846                         return TRUE;
847
848                 is_found_noti_exists = TRUE;
849         } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
850                 if (is_portal_noti_exists == TRUE)
851                         return TRUE;
852
853                 is_portal_noti_exists = TRUE;
854         } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
855                 if (is_portal_noti_exists == FALSE)
856                         return TRUE;
857
858                 is_portal_noti_exists = FALSE;
859         }
860
861         b = bundle_create();
862         bundle_add(b, "_SYSPOPUP_TYPE_", noti);
863
864         if (ssid != NULL) {
865                 DBG("ssid (%s)", ssid);
866                 bundle_add(b, "_AP_NAME_", ssid);
867         }
868
869         ret = syspopup_launch("net-popup", b);
870
871         bundle_free(b);
872
873         if (ret < 0) {
874                 ERR("Unable to launch noti-popup. Err = %d", ret);
875                 return FALSE;
876         }
877
878         DBG("Successfully sent notification (%s)", noti);
879         return TRUE;
880 }
881
882 int netconfig_send_message_to_net_popup(const char *title,
883                 const char *content, const char *type, const char *ssid)
884 {
885         int ret = 0;
886         bundle *b = bundle_create();
887
888         bundle_add(b, "_SYSPOPUP_TITLE_", title);
889         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
890         bundle_add(b, "_SYSPOPUP_TYPE_", type);
891         bundle_add(b, "_AP_NAME_", ssid);
892
893         ret = syspopup_launch("net-popup", b);
894
895         bundle_free(b);
896
897         return ret;
898 }
899
900 int netconfig_send_restriction_to_net_popup(const char *title,
901                 const char *type, const char *restriction)
902 {
903         int ret = 0;
904         bundle *b = bundle_create();
905
906         bundle_add(b, "_SYSPOPUP_TITLE_", title);
907         bundle_add(b, "_SYSPOPUP_CONTENT_", "security restriction");
908         bundle_add(b, "_SYSPOPUP_TYPE_", type);
909         bundle_add(b, "_RESTRICTED_TYPE_", restriction);
910
911         ret = syspopup_launch("net-popup", b);
912
913         bundle_free(b);
914
915         return ret;
916 }
917
918 void netconfig_set_system_event(const char * sys_evt, const char * evt_key, const char * evt_val)
919 {
920         bundle *b = NULL;
921
922         DBG("System event set [%s : %s : %s]", sys_evt, evt_key, evt_val);
923
924         b = bundle_create();
925         bundle_add_str(b, evt_key, evt_val);
926         eventsystem_send_system_event(sys_evt, b);
927         bundle_free(b);
928 }
929
930 void netconfig_set_vconf_int(const char * key, int value)
931 {
932         int ret = 0;
933
934         DBG("[%s: %d]", key, value);
935
936         ret = vconf_set_int(key, value);
937         if (ret != VCONF_OK)
938                 ERR("Failed to set");
939 }
940
941 void netconfig_set_vconf_str(const char * key, const char * value)
942 {
943         int ret = 0;
944
945         DBG("[%s: %s]", key, value);
946
947         ret = vconf_set_str(key, value);
948         if (ret != VCONF_OK)
949                 ERR("Failed to set");
950 }
951
952 int netconfig_vconf_get_int(const char * key, int *value)
953 {
954         int ret = 0;
955
956         ret = vconf_get_int(key, value);
957         if (ret != VCONF_OK) {
958                 ERR("Failed to get vconfkey [%s] value", key);
959                 return -1;
960         }
961
962         return 0;
963 }
964
965 int netconfig_vconf_get_bool(const char * key, int *value)
966 {
967         int ret = 0;
968
969         ret = vconf_get_bool(key, value);
970         if (ret != VCONF_OK) {
971                 ERR("Failed to get vconfkey [%s] value", key);
972                 return -1;
973         }
974
975         return 0;
976 }
977
978 char* netconfig_get_env(const char *key)
979 {
980         FILE *fp;
981         char buf[256], *entry = NULL, *value = NULL, *last;
982         int len = 0;
983
984         if (!key)
985                 return NULL;
986
987         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
988         if (!fp)
989                 return NULL;
990
991         while (fgets(buf, sizeof(buf), fp)) {
992                 entry = buf;
993                 entry = strtok_r(entry, "=", &last);
994                 if (entry) {
995                         if (strstr(entry, key)) {
996                                 entry = strtok_r(NULL, "\n", &last);
997                                 if (entry) {
998                                         len = strlen(entry);
999                                         value = (char*)malloc(len+1);
1000                                         g_strlcpy(value, entry, len+1);
1001                                 } else {
1002                                         value = (char*)malloc(sizeof(char));
1003                                         g_strlcpy(value, "\n", sizeof(char));
1004                                 }
1005                                 break;
1006                         }
1007                 }
1008         }
1009
1010         fclose(fp);
1011         return value;
1012 }
1013
1014 void netconfig_set_mac_address_from_file(void)
1015 {
1016         FILE *file = NULL;
1017         char mac_str[MAC_ADDRESS_MAX_LEN];
1018         gchar *mac_lower_str = NULL;
1019         int mac_len = 0;
1020
1021         file = fopen(MAC_INFO_FILEPATH, "r");
1022         if (file == NULL) {
1023                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1024                 return;
1025         }
1026         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1027                 ERR("Fail to read mac address");
1028                 fclose(file);
1029                 return;
1030         }
1031
1032         mac_len = strlen(mac_str);
1033         if (mac_len < 17) {
1034                 ERR("mac.info is empty");
1035                 fclose(file);
1036                 return;
1037         }
1038
1039         mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1040         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1041
1042         g_free(mac_lower_str);
1043         fclose(file);
1044 }