Added APIs get the tcp dump and network state
[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                 ERR("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 static void no_wait_signal_handler()
535 {
536         pid_t child_pid = 0;
537         int state = 0;
538
539         child_pid = waitpid(-1, &state, WNOHANG);
540
541         DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
542 }
543
544 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
545 {
546         pid_t pid = 0;
547         int rv = 0;
548         errno = 0;
549         register unsigned int index = 0;
550
551         struct sigaction act;
552         int state = 0;
553
554         act.sa_handler = no_wait_signal_handler;
555         sigemptyset(&act.sa_mask);
556         act.sa_flags = 0;
557
558         state = sigaction(SIGCHLD, &act, 0);
559         if (state != 0) {
560                 DBG("sigaction() : %d");
561                 return -1;
562         }
563
564         while (args[index] != NULL) {
565                 DBG("%s", args[index]);
566                 index++;
567         }
568
569         if (!(pid = fork())) {
570                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
571                 DBG("Inside child, exec (%s) command", file_path);
572
573                 errno = 0;
574                 if (execvp(file_path, args) == -1) {
575                         ERR("Fail to execute command (%s)", strerror(errno));
576                         return -1;
577                 }
578         } else if (pid > 0) {
579                 ERR("Successfully launched child process");
580                 return rv;
581         }
582
583         DBG("failed to fork(%s)", strerror(errno));
584         return -EIO;
585 }
586
587 int __netconfig_get_interface_index(const char *interface_name)
588 {
589         struct ifreq ifr;
590         int sock = 0;
591         int result = 0;
592         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
593
594         if (interface_name == NULL) {
595                 DBG("Inteface name is NULL");
596                 return -1;
597         }
598
599         errno = 0;
600         sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
601         if (sock < 0) {
602                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
603                 DBG("Failed to create socket : %s", error_buf);
604                 return -1;
605         }
606
607         memset(&ifr, 0, sizeof(ifr));
608         strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
609         result = ioctl(sock, SIOCGIFINDEX, &ifr);
610         close(sock);
611
612         if (result < 0) {
613                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
614                 DBG("Failed to get ifr index: %s", error_buf);
615                 return -1;
616         }
617
618         return ifr.ifr_ifindex;
619 }
620
621 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
622 {
623         struct ifreq ifr;
624         struct rtentry rt;
625         struct sockaddr_in addr_in;
626         int sock;
627         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
628
629         memset(&ifr, 0, sizeof(ifr));
630
631         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
632
633         if (ifr.ifr_ifindex < 0)
634                 return -1;
635
636         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
637
638         memset(&rt, 0, sizeof(rt));
639
640         rt.rt_flags = RTF_UP | RTF_HOST;
641         memset(&addr_in, 0, sizeof(struct sockaddr_in));
642         addr_in.sin_family = address_family;
643         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
644         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
645
646         memset(&addr_in, 0, sizeof(struct sockaddr_in));
647         addr_in.sin_family = address_family;
648         addr_in.sin_addr.s_addr = INADDR_ANY;
649         memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
650
651         memset(&addr_in, 0, sizeof(struct sockaddr_in));
652         addr_in.sin_family = AF_INET;
653         addr_in.sin_addr.s_addr = inet_addr(subnet);
654         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
655
656         rt.rt_dev = ifr.ifr_name;
657
658         errno = 0;
659         sock = socket(PF_INET, SOCK_DGRAM, 0);
660
661         if (sock < 0) {
662                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
663                 DBG("Failed to create socket : %s", error_buf);
664                 return -1;
665         }
666
667         if (ioctl(sock, SIOCADDRT, &rt) < 0) {
668                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
669                 DBG("Failed to set route address : %s", error_buf);
670                 close(sock);
671                 return -1;
672         }
673
674         close(sock);
675
676         return 1;
677 }
678
679 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
680 {
681         struct ifreq ifr;
682         struct rtentry rt;
683         struct sockaddr_in addr_in;
684         int sock;
685         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
686
687         memset(&ifr, 0, sizeof(ifr));
688         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
689
690         if (ifr.ifr_ifindex < 0)
691                 return -1;
692
693         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
694
695         memset(&rt, 0, sizeof(rt));
696
697         rt.rt_flags = RTF_UP;
698         memset(&addr_in, 0, sizeof(struct sockaddr_in));
699         addr_in.sin_family = address_family;
700         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
701         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
702
703         memset(&addr_in, 0, sizeof(struct sockaddr_in));
704         addr_in.sin_family = address_family;
705         addr_in.sin_addr.s_addr = inet_addr(subnet);
706         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
707         rt.rt_dev = ifr.ifr_name;
708
709         errno = 0;
710         sock = socket(PF_INET, SOCK_DGRAM, 0);
711
712         if (sock < 0) {
713                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
714                 DBG("Failed to create socket : %s", error_buf);
715                 return -1;
716         }
717
718         if (ioctl(sock, SIOCDELRT, &rt) < 0) {
719                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
720                 DBG("Failed to set route address : %s", error_buf);
721                 close(sock);
722                 return -1;
723         }
724
725         close(sock);
726
727         return 1;
728 }
729
730 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
731 {
732         struct in6_rtmsg rt;
733         int fd = 0;
734         int err = 0;
735         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
736
737         memset(&rt, 0, sizeof(rt));
738
739         rt.rtmsg_dst_len = prefix_len;
740
741         rt.rtmsg_flags = RTF_UP | RTF_HOST;
742
743         errno = 0;
744         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
745                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
746                 DBG("inet_pton failed : %s", error_buf);
747                 return -1;
748         }
749
750         if (gateway != NULL) {
751                 rt.rtmsg_flags |= RTF_GATEWAY;
752                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
753                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
754                         DBG("inet_pton failed : %s", error_buf);
755                         return -1;
756                 }
757         }
758
759         rt.rtmsg_metric = 1;
760
761         fd = socket(AF_INET6, SOCK_DGRAM, 0);
762         if (fd < 0) {
763                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
764                 DBG("Failed to create socket : %s", error_buf);
765                 return -1;
766         }
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, SIOCADDRT, &rt)) < 0) {
779                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
780                 DBG("Failed to add route: %s", error_buf);
781                 close(fd);
782                 return -1;
783         }
784
785         close(fd);
786
787         return 1;
788 }
789
790 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
791 {
792         struct in6_rtmsg rt;
793         int fd = 0;
794         int err = 0;
795
796         memset(&rt, 0, sizeof(rt));
797
798         rt.rtmsg_dst_len = prefix_len;
799
800         rt.rtmsg_flags = RTF_UP | RTF_HOST;
801
802         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
803                 err = -errno;
804                 return err;
805         }
806
807         if (gateway != NULL) {
808                 rt.rtmsg_flags |= RTF_GATEWAY;
809                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
810                         err = -errno;
811                         return err;
812                 }
813         }
814
815         rt.rtmsg_metric = 1;
816
817         fd = socket(AF_INET6, SOCK_DGRAM, 0);
818         if (fd < 0)
819                 return -1;
820
821         rt.rtmsg_ifindex = 0;
822
823         if (interface) {
824                 struct ifreq ifr;
825                 memset(&ifr, 0, sizeof(ifr));
826                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
827                 ioctl(fd, SIOCGIFINDEX, &ifr);
828                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
829         }
830
831         if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
832                 DBG("Failed to del route: %d\n", err);
833                 close(fd);
834                 return -1;
835         }
836
837         close(fd);
838
839         return 1;
840 }
841
842 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
843 {
844 #if defined TIZEN_P2P_ENABLE
845         int ret = 0;
846         DBG("Launch Wi-Fi direct daemon");
847
848         const char *path = "/usr/bin/wifi-direct-server.sh";
849         char *const args[] = { "wifi-direct-server.sh", "start", NULL };
850         char *const envs[] = { NULL };
851
852         ret = netconfig_execute_file(path, args, envs);
853         if (ret < 0) {
854                 ERR("Failed to launch Wi-Fi direct daemon");
855                 netconfig_error_wifi_direct_failed(context);
856                 return FALSE;
857         }
858
859         wifi_complete_launch_direct(wifi, context);
860         return TRUE;
861 #else
862         wifi_complete_launch_direct(wifi, context);
863         return FALSE;
864 #endif
865 }
866
867 int execute_mdnsd_script(char* op)
868 {
869         const char *path = "/usr/bin/mdnsresponder-server.sh";
870         char *const args[] = { "mdnsresponder-server.sh", op, NULL };
871         char *const envs[] = { NULL };
872
873         return netconfig_execute_file(path, args, envs);
874 }
875
876 static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
877                 const gchar *Name, const gchar *path, const gchar *interface,
878                 const gchar *sig, GVariant *param, gpointer user_data)
879 {
880         gchar *name = NULL;
881         gchar *old = NULL;
882         gchar *new = NULL;
883         dnssd_conn_destroy_data *data = user_data;
884         GDBusConnection *connection = NULL;
885         connection = netdbus_get_connection();
886
887         if (param == NULL)
888                 return;
889
890         g_variant_get(param, "(sss)", &name, &old, &new);
891
892         if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
893                 DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
894                         data->conn_id);
895                 mdnsd_ref_count--;
896                 g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
897                 if (mdnsd_ref_count == 0) {
898                         if (execute_mdnsd_script("stop") < 0)
899                                 ERR("Failed to stop mdnsresponder daemon");
900                 }
901         }
902         g_free(name);
903         g_free(old);
904         g_free(new);
905         g_free(data->conn_name);
906         g_free(data);
907         return;
908 }
909
910 static void register_dnssd_conn_destroy_signal(gchar *name)
911 {
912         dnssd_conn_destroy_data *data;
913         GDBusConnection *connection = NULL;
914         connection = netdbus_get_connection();
915
916         if (connection == NULL) {
917                 ERR("Failed to get GDbus Connection");
918                 return;
919         }
920
921         data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
922
923         if (data == NULL) {
924                 ERR("Out of Memory!");
925                 return;
926         }
927
928         data->conn_name = g_strdup(name);
929
930         data->conn_id = g_dbus_connection_signal_subscribe(connection,
931                                                         DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
932                                                         "NameOwnerChanged", NULL, name,
933                                                         G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
934                                                         data, NULL);
935         return;
936 }
937
938 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
939                                                         gchar *name)
940 {
941         DBG("Launch mdnsresponder daemon");
942
943         if (execute_mdnsd_script("start") < 0) {
944                 ERR("Failed to launch mdnsresponder daemon");
945                 netconfig_error_invalid_parameter(context);
946                 return FALSE;
947         }
948
949         mdnsd_ref_count++;
950         register_dnssd_conn_destroy_signal(name);
951         DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
952
953         network_complete_launch_mdns(object, context);
954         return TRUE;
955 }
956
957 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
958 {
959         if (!netconfig_plugin_headed_enabled)
960                 return FALSE;
961
962         if (!headed_plugin)
963                 return FALSE;
964
965         return headed_plugin->send_notification_to_net_popup(noti, ssid);
966 }
967
968 int netconfig_send_message_to_net_popup(const char *title,
969                 const char *content, const char *type, const char *ssid)
970 {
971         if (!netconfig_plugin_headed_enabled)
972                 return 0;
973
974         if (!headed_plugin)
975                 return 0;
976
977         return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
978 }
979
980 int netconfig_send_restriction_to_net_popup(const char *title,
981                 const char *type, const char *restriction)
982 {
983         if (!netconfig_plugin_headed_enabled)
984                 return 0;
985
986         if (!headed_plugin)
987                 return 0;
988
989         return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
990 }
991
992 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
993 {
994         if (!netconfig_plugin_headed_enabled)
995                 return;
996
997         if (!headed_plugin)
998                 return;
999
1000         headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1001 }
1002
1003 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1004 {
1005         if (!netconfig_plugin_headed_enabled)
1006                 return;
1007
1008         if (!headed_plugin)
1009                 return;
1010
1011         headed_plugin->pop_wifi_connected_poppup(ssid);
1012 }
1013
1014 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1015 {
1016         if (!netconfig_plugin_telephony_enabled)
1017                 return;
1018
1019         if (!telephony_plugin)
1020                 return;
1021
1022         telephony_plugin->get_telephony_network_type(svctype, pstype);
1023 }
1024
1025 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1026 {
1027         if (!netconfig_plugin_telephony_enabled)
1028                 return FALSE;
1029
1030         if (!telephony_plugin)
1031                 return FALSE;
1032
1033         return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1034 }
1035
1036 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1037                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1038 {
1039         if (!netconfig_plugin_telephony_enabled)
1040                 return NETCONFIG_ERROR_INTERNAL;
1041
1042         if (!telephony_plugin)
1043                 return NETCONFIG_ERROR_INTERNAL;
1044
1045         return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1046 }
1047
1048 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1049                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1050 {
1051         if (!netconfig_plugin_telephony_enabled)
1052                 return FALSE;
1053
1054         if (!telephony_plugin)
1055                 return FALSE;
1056
1057         return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1058 }
1059
1060 gboolean netconfig_tapi_check_sim_state(void)
1061 {
1062         if (!netconfig_plugin_telephony_enabled)
1063                 return FALSE;
1064
1065         if (!telephony_plugin)
1066                 return FALSE;
1067
1068         return telephony_plugin->tapi_check_sim_state();
1069 }
1070
1071 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1072                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1073 {
1074         if (!netconfig_plugin_telephony_enabled)
1075                 return FALSE;
1076
1077         if (!telephony_plugin)
1078                 return FALSE;
1079
1080         return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1081 }
1082
1083 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1084                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1085 {
1086         if (!netconfig_plugin_telephony_enabled)
1087                 return FALSE;
1088
1089         if (!telephony_plugin)
1090                 return FALSE;
1091
1092         return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1093 }
1094
1095 void netconfig_set_vconf_int(const char * key, int value)
1096 {
1097         int ret = 0;
1098
1099         DBG("[%s: %d]", key, value);
1100
1101         ret = vconf_set_int(key, value);
1102         if (ret != VCONF_OK)
1103                 ERR("Failed to set");
1104 }
1105
1106 void netconfig_set_vconf_str(const char * key, const char * value)
1107 {
1108         int ret = 0;
1109
1110         DBG("[%s: %s]", key, value);
1111
1112         ret = vconf_set_str(key, value);
1113         if (ret != VCONF_OK)
1114                 ERR("Failed to set");
1115 }
1116
1117 int netconfig_vconf_get_int(const char * key, int *value)
1118 {
1119         int ret = 0;
1120
1121         ret = vconf_get_int(key, value);
1122         if (ret != VCONF_OK) {
1123                 ERR("Failed to get vconfkey [%s] value", key);
1124                 return -1;
1125         }
1126
1127         return 0;
1128 }
1129
1130 int netconfig_vconf_get_bool(const char * key, int *value)
1131 {
1132         int ret = 0;
1133
1134         ret = vconf_get_bool(key, value);
1135         if (ret != VCONF_OK) {
1136                 ERR("Failed to get vconfkey [%s] value", key);
1137                 return -1;
1138         }
1139
1140         return 0;
1141 }
1142
1143 char* netconfig_get_env(const char *key)
1144 {
1145         FILE *fp;
1146         char buf[256], *entry = NULL, *value = NULL, *last;
1147         int len = 0;
1148
1149         if (!key)
1150                 return NULL;
1151
1152         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1153         if (!fp)
1154                 return NULL;
1155
1156         while (fgets(buf, sizeof(buf), fp)) {
1157                 entry = buf;
1158                 entry = strtok_r(entry, "=", &last);
1159                 if (entry) {
1160                         if (strstr(entry, key)) {
1161                                 entry = strtok_r(NULL, "\n", &last);
1162                                 if (entry) {
1163                                         len = strlen(entry);
1164                                         value = (char*)malloc(len+1);
1165                                         g_strlcpy(value, entry, len+1);
1166                                 } else {
1167                                         value = (char*)malloc(sizeof(char));
1168                                         g_strlcpy(value, "\n", sizeof(char));
1169                                 }
1170                                 break;
1171                         }
1172                 }
1173         }
1174
1175         fclose(fp);
1176         return value;
1177 }
1178
1179 void netconfig_set_mac_address_from_file(void)
1180 {
1181         FILE *file = NULL;
1182         char mac_str[MAC_ADDRESS_MAX_LEN];
1183         gchar *mac_lower_str = NULL;
1184         int mac_len = 0;
1185
1186         file = fopen(MAC_INFO_FILEPATH, "r");
1187         if (file == NULL) {
1188                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1189                 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1190                 if (file == NULL) {
1191                         ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1192                         return;
1193                 }
1194         }
1195         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1196                 ERR("Fail to read mac address");
1197                 fclose(file);
1198                 return;
1199         }
1200
1201         mac_len = strlen(mac_str);
1202         if (mac_len < 17) {
1203                 ERR("mac.info is empty");
1204                 fclose(file);
1205                 return;
1206         }
1207
1208         mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1209         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1210
1211         g_free(mac_lower_str);
1212         fclose(file);
1213 }
1214
1215 tizen_profile_t _get_tizen_profile()
1216 {
1217         static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1218         if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1219                 return profile;
1220
1221         char *profileName;
1222         system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1223         switch (*profileName) {
1224         case 'm':
1225         case 'M':
1226                 profile = TIZEN_PROFILE_MOBILE;
1227                 break;
1228         case 'w':
1229         case 'W':
1230                 profile = TIZEN_PROFILE_WEARABLE;
1231                 break;
1232         case 't':
1233         case 'T':
1234                 profile = TIZEN_PROFILE_TV;
1235                 break;
1236         case 'i':
1237         case 'I':
1238                 profile = TIZEN_PROFILE_IVI;
1239                 break;
1240         default: // common or unknown ==> ALL ARE COMMON.
1241                 profile = TIZEN_PROFILE_COMMON;
1242         }
1243         free(profileName);
1244
1245         return profile;
1246 }
1247
1248 void netconfig_plugin_init()
1249 {
1250         handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1251         if (!handle_headed) {
1252                 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1253         } else {
1254                 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1255                 if (!headed_plugin) {
1256                         ERR("Can't load symbol: %s", dlerror());
1257                         dlclose(handle_headed);
1258                 } else {
1259                         netconfig_plugin_headed_enabled = TRUE;
1260                 }
1261         }
1262
1263         handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1264         if (!handle_telephony) {
1265                 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1266         } else {
1267                 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1268                 if (!telephony_plugin) {
1269                         ERR("Can't load symbol: %s", dlerror());
1270                         dlclose(handle_telephony);
1271                 } else {
1272                         netconfig_plugin_telephony_enabled = TRUE;
1273                 }
1274         }
1275 }
1276
1277 void netconfig_plugin_deinit()
1278 {
1279         if (netconfig_plugin_headed_enabled) {
1280                 netconfig_plugin_headed_enabled = FALSE;
1281                 dlclose(handle_headed);
1282         }
1283
1284         if (netconfig_plugin_telephony_enabled) {
1285                 netconfig_plugin_telephony_enabled = FALSE;
1286                 dlclose(handle_telephony);
1287         }
1288 }
1289
1290 gboolean netconfig_get_headed_plugin_flag()
1291 {
1292         return netconfig_plugin_headed_enabled;
1293 }
1294
1295 gboolean netconfig_get_telephony_plugin_flag()
1296 {
1297         return netconfig_plugin_telephony_enabled;
1298 }
1299