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