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