Merge "Add dbus method for getting wifi passphrase" 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 <linux/limits.h>
34 #include <ctype.h>
35 #include <vconf-keys.h>
36 #include <tzplatform_config.h>
37 #include <system_info.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <dirent.h>
41 #include <time.h>
42
43 #include "log.h"
44 #include "util.h"
45 #include "neterror.h"
46 #include "wifi-state.h"
47 #include "network-state.h"
48 #include "netdbus.h"
49
50 #define DBUS_SERVICE_DBUS              "org.freedesktop.DBus"
51 #define DBUS_INTERFACE_DBUS            "org.freedesktop.DBus"
52 #define MAC_INFO_FILEPATH              tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
53 #define MAC_ADDRESS_FILEPATH           "/sys/class/net/wlan0/address"
54 #if defined(TIZEN_ARCH_64)
55 #define HEADED_PLUGIN_FILEPATH         "/usr/lib64/net-config-plugin-headed.so"
56 #define TELEPHONY_PLUGIN_FILEPATH      "/usr/lib64/net-config-plugin-telephony.so"
57 #define STC_PLUGIN_FILEPATH            "/usr/lib64/net-config-plugin-stc.so"
58 #define BATTERY_PLUGIN_FILEPATH        "/usr/lib64/net-config-plugin-battery.so"
59 #define ONLINE_MONITOR_PLUGIN_FILEPATH "/usr/lib64/net-config-plugin-online-monitor.so"
60 #else
61 #define HEADED_PLUGIN_FILEPATH         "/usr/lib/net-config-plugin-headed.so"
62 #define TELEPHONY_PLUGIN_FILEPATH      "/usr/lib/net-config-plugin-telephony.so"
63 #define STC_PLUGIN_FILEPATH            "/usr/lib/net-config-plugin-stc.so"
64 #define BATTERY_PLUGIN_FILEPATH        "/usr/lib/net-config-plugin-battery.so"
65 #define ONLINE_MONITOR_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-online-monitor.so"
66 #endif
67 #define CONNMAN_MAINFILE               "/etc/connman/main.conf"
68 #define CONNMAN_WIFI_DEF_IFNAME        "DefaultWifiInterface"
69
70 static gboolean netconfig_device_picker_test = FALSE;
71 typedef struct {
72         char *conn_name;
73         int conn_id;
74 } dnssd_conn_destroy_data;
75
76 static gboolean netconfig_plugin_headed_enabled = FALSE;
77 static gboolean netconfig_plugin_telephony_enabled = FALSE;
78 static gboolean netconfig_plugin_stc_enabled = FALSE;
79 static gboolean netconfig_plugin_battery_enabled = FALSE;
80 static gboolean netconfig_plugin_online_monitor_enabled = FALSE;
81 static void *handle_headed;
82 static void *handle_telephony;
83 static void *handle_stc;
84 static void *handle_battery;
85 static void *handle_online_monitor;
86 static struct netconfig_headed_plugin_t *headed_plugin;
87 static struct netconfig_telephony_plugin_t *telephony_plugin;
88 static struct netconfig_stc_plugin_t *stc_plugin;
89 static struct netconfig_battery_plugin_t *battery_plugin;
90 static struct netconfig_online_monitor_plugin_t *online_monitor_plugin;
91
92 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
93 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
94
95 gboolean netconfig_check_mac_address(const char *service, const char *mac_address)
96 {
97         int service_index = 0;
98         int mac_index = 0;
99         const char *org = NULL;
100         const char *dst = NULL;
101         int i = 0;
102
103         if (g_str_has_prefix(service, "wifi_") == FALSE)
104                 return FALSE;
105
106         service_index = strlen("wifi_");
107         for (i = 0; i < 6; i++) {
108                 org = &service[service_index];
109                 dst = &mac_address[mac_index];
110
111                 if (g_ascii_strncasecmp(org, dst, 2) != 0)
112                         return FALSE;
113
114                 service_index += 2;
115                 mac_index += 3;
116         }
117
118         return TRUE;
119 }
120
121 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
122 {
123         gsize length;
124
125         if (!passphrase)
126                 return FALSE;
127
128         length = strlen(passphrase);
129
130         if (g_str_has_suffix(service, "psk") == TRUE) {
131                 if (length == 64) {
132                         for (int i = 0; i < 64; i++)
133                                 if (!isxdigit((unsigned char)passphrase[i]))
134                                         return FALSE;
135                 } else if (length < 8 || length > 63)
136                         return FALSE;
137         } else if (g_str_has_suffix(service, "wep") == TRUE) {
138                 if (length == 10 || length == 26) {
139                         for (int i = 0; i < length; i++)
140                                 if (!isxdigit((unsigned char)passphrase[i]))
141                                         return FALSE;
142                 } else if (length != 5 && length != 13)
143                         return FALSE;
144         }
145
146         return TRUE;
147 }
148
149 GKeyFile *netconfig_keyfile_load(const char *pathname)
150 {
151         GKeyFile *keyfile = NULL;
152         GError *error = NULL;
153
154         keyfile = g_key_file_new();
155         if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
156                 DBG("Unable to open %s, error %s", pathname, error->message);
157                 g_error_free(error);
158
159                 g_key_file_free(keyfile);
160                 keyfile = NULL;
161         }
162
163         DBG("loaded keyfile %s", pathname);
164         return keyfile;
165 }
166
167 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
168 {
169         gsize size = 0;
170         GError *error = NULL;
171         gchar *keydata = NULL;
172         gchar *needle = NULL, *directory = NULL;
173
174         directory = g_strdup(pathname);
175         if (directory == NULL) {
176                 ERR("directory is NULL");
177                 return;
178         }
179
180         needle = g_strrstr(directory, "/");
181         if (needle != NULL)
182                 *needle = '\0';
183
184         if ((*directory) == '\0') {
185                 g_free(directory);
186                 ERR("directory is NULL");
187                 return;
188         }
189
190         if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
191                 if (g_mkdir_with_parents(directory,
192                                 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
193                         g_free(directory);
194                         ERR("failed to make directory");
195                         return;
196                 }
197         }
198         g_free(directory);
199
200         keydata = g_key_file_to_data(keyfile, &size, &error);
201         if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
202                 ERR("Unable to save %s, error %s", pathname, error->message);
203                 g_error_free(error);
204         }
205
206         if (chmod(pathname, S_IRUSR | S_IWUSR) < 0)
207                 DBG("Failed to change mode");
208         else
209                 DBG("Successfully saved keyfile %s", pathname);
210
211         g_free(keydata);
212 }
213
214 void netconfig_start_timer_seconds(guint secs,
215                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
216 {
217         guint t_id = 0;
218
219         if (callback == NULL) {
220                 ERR("callback function is NULL");
221                 return;
222         }
223
224         if ((timer_id != NULL && *timer_id != 0)) {
225                 ERR("timer already is registered");
226                 return;
227         }
228
229         t_id = g_timeout_add_seconds(secs, callback, user_data);
230
231         if (t_id == 0) {
232                 ERR("Can't add timer");
233                 return;
234         }
235
236         if (timer_id != NULL)
237                 *timer_id = t_id;
238 }
239
240 void netconfig_start_timer(guint msecs,
241                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
242 {
243         guint t_id = 0;
244
245         INFO("Register timer with callback pointer (%p)", callback);
246
247         if (callback == NULL) {
248                 ERR("callback function is NULL");
249                 return;
250         }
251
252         if ((timer_id != NULL && *timer_id != 0)) {
253                 ERR("timer already is registered");
254                 return;
255         }
256
257         t_id = g_timeout_add(msecs, callback, user_data);
258
259         if (t_id == 0) {
260                 ERR("Can't add timer");
261                 return;
262         }
263
264         if (timer_id != NULL)
265                 *timer_id = t_id;
266 }
267
268 void netconfig_stop_timer(guint *timer_id)
269 {
270         if (timer_id == NULL) {
271                 ERR("timer is NULL");
272                 return;
273         }
274
275         if (*timer_id != 0) {
276                 g_source_remove(*timer_id);
277                 *timer_id = 0;
278         }
279 }
280
281 static gboolean __netconfig_test_device_picker()
282 {
283         char *favorite_wifi_service = NULL;
284
285         favorite_wifi_service = wifi_get_favorite_service();
286         if (favorite_wifi_service != NULL) {
287                 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
288                 g_free(favorite_wifi_service);
289                 return FALSE;
290         }
291
292         return TRUE;
293 }
294
295 static void __netconfig_pop_device_picker(void)
296 {
297         if (!netconfig_plugin_headed_enabled)
298                 return;
299
300         if (!headed_plugin)
301                 return;
302
303         headed_plugin->pop_device_picker();
304 }
305
306 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
307 {
308         if (__netconfig_test_device_picker() == TRUE)
309                 __netconfig_pop_device_picker();
310
311         return FALSE;
312 }
313
314 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
315 {
316         static guint netconfig_wifi_device_picker_service_timer = 0;
317
318         if (is_set_method != TRUE)
319                 return netconfig_wifi_device_picker_service_timer;
320
321         if (netconfig_wifi_device_picker_service_timer != timer_id)
322                 netconfig_wifi_device_picker_service_timer = timer_id;
323
324         return netconfig_wifi_device_picker_service_timer;
325 }
326
327 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
328 {
329         __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
330 }
331
332 static guint __netconfig_wifi_device_picker_get_timer_id(void)
333 {
334         return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
335 }
336
337 void netconfig_wifi_enable_device_picker_test(void)
338 {
339         netconfig_device_picker_test = TRUE;
340 }
341
342 void netconfig_wifi_device_picker_service_start(void)
343 {
344         const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
345         guint timer_id = 0;
346
347         if (netconfig_device_picker_test == TRUE)
348                 netconfig_device_picker_test = FALSE;
349         else
350                 return;
351
352         int wifi_ug_state;
353
354         netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
355         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
356                 return;
357
358         DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
359         netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
360
361         __netconfig_wifi_device_picker_set_timer_id(timer_id);
362 }
363
364 void netconfig_wifi_device_picker_service_stop(void)
365 {
366         guint timer_id = 0;
367
368         timer_id = __netconfig_wifi_device_picker_get_timer_id();
369         if (timer_id == 0)
370                 return;
371
372         DBG("Clear device picker timer with timer_id %d", timer_id);
373
374         netconfig_stop_timer(&timer_id);
375
376         __netconfig_wifi_device_picker_set_timer_id(timer_id);
377 }
378
379 gboolean netconfig_is_wifi_direct_on(void)
380 {
381         if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
382                 return FALSE;
383
384         int wifi_direct_state = 0;
385
386         netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
387
388         DBG("Wi-Fi direct mode %d", wifi_direct_state);
389         return (wifi_direct_state != 0) ? TRUE : FALSE;
390 }
391
392 gboolean netconfig_is_wifi_tethering_on(void)
393 {
394         if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
395                 int wifi_tethering_state = 0;
396
397                 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
398                 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
399                 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
400                                 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
401                         DBG("Mobile AP is on");
402                         return TRUE;
403                 }
404         }
405
406         DBG("Mobile AP is off");
407         return FALSE;
408 }
409
410 gboolean netconfig_interface_up(const char *ifname)
411 {
412         int fd;
413         struct ifreq ifr;
414
415         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
416         if (fd < 0) {
417                 ERR("socket failed %d", errno);
418                 return FALSE;
419         }
420
421         memset(&ifr, 0, sizeof(ifr));
422         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
423
424         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
425                 ERR("Fail to get IFFLAGS %d", errno);
426                 close(fd);
427                 return FALSE;
428         }
429
430         DBG("IFFLAGS: %x", ifr.ifr_flags);
431         ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
432         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
433                 ERR("Fail to set IFFLAGS %d", errno);
434                 close(fd);
435                 return FALSE;
436         }
437
438         close(fd);
439
440         DBG("Successfully activated wireless interface %s", ifname);
441         return TRUE;
442 }
443
444 gboolean netconfig_interface_down(const char *ifname)
445 {
446         int fd;
447         struct ifreq ifr;
448
449         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
450         if (fd < 0) {
451                 ERR("socket failed %d", errno);
452                 return FALSE;
453         }
454
455         memset(&ifr, 0, sizeof(ifr));
456         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
457
458         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
459                 ERR("Fail to get IFFLAGS %d", errno);
460                 close(fd);
461                 return FALSE;
462         }
463
464         DBG("IFFLAGS: %x", ifr.ifr_flags);
465         ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
466         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
467                 ERR("Fail to set IFFLAGS %d", errno);
468                 close(fd);
469                 return FALSE;
470         }
471
472         close(fd);
473
474         DBG("Successfully de-activated wireless interface %s", ifname);
475         return TRUE;
476 }
477
478 int netconfig_execute_file(const char *file_path,
479                 char *const args[], char *const envs[])
480 {
481         pid_t pid = 0;
482         int status = 0;
483         int rv = 0;
484         errno = 0;
485         register unsigned int index = 0;
486         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
487
488         while (args[index] != NULL) {
489                 DBG("%s", args[index]);
490                 index++;
491         }
492
493         if (!(pid = fork())) {
494                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
495                 DBG("Inside child, exec (%s) command", file_path);
496
497                 errno = 0;
498                 if (execve(file_path, args, envs) == -1) {
499                         DBG("Fail to execute command (%s)",
500                                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
501                         exit(1);
502                 }
503         } else if (pid > 0) {
504                 if (waitpid(pid, &status, 0) == -1)
505                         DBG("wait pid (%u) status (%d)", pid, status);
506
507                 if (WIFEXITED(status)) {
508                         rv = WEXITSTATUS(status);
509                         DBG("exited, status=%d", rv);
510                 } else if (WIFSIGNALED(status)) {
511                         DBG("killed by signal %d", WTERMSIG(status));
512                 } else if (WIFSTOPPED(status)) {
513                         DBG("stopped by signal %d", WSTOPSIG(status));
514                 } else if (WIFCONTINUED(status)) {
515                         DBG("continued");
516                 }
517
518                 return rv;
519         }
520
521         DBG("failed to fork(%s)",
522                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
523         return -EIO;
524 }
525
526 int netconfig_execute_cmd(const char *cmd)
527 {
528         if (cmd == NULL)
529                 return -EIO;
530
531         pid_t pid = 0;
532         int status = 0;
533         int rv = 0;
534         errno = 0;
535         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
536         gchar **args = NULL;
537
538         DBG("command: %s", cmd);
539
540         args = g_strsplit_set(cmd, " ", -1);
541
542         if (!(pid = fork())) {
543                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
544
545                 errno = 0;
546                 if (execv(args[0], args) == -1) {
547                         DBG("Fail to execute command (%s)",
548                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
549                         g_strfreev(args);
550                         exit(1);
551                 }
552         } else if (pid > 0) {
553                 if (waitpid(pid, &status, 0) == -1)
554                         DBG("wait pid (%u) status (%d)", pid, status);
555
556                 if (WIFEXITED(status)) {
557                         rv = WEXITSTATUS(status);
558                         DBG("exited, status=%d", rv);
559                 } else if (WIFSIGNALED(status)) {
560                         DBG("killed by signal %d", WTERMSIG(status));
561                 } else if (WIFSTOPPED(status)) {
562                         DBG("stopped by signal %d", WSTOPSIG(status));
563                 } else if (WIFCONTINUED(status)) {
564                         DBG("continued");
565                 }
566
567                 g_strfreev(args);
568                 return rv;
569         }
570
571         DBG("failed to fork(%s)",
572                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
573         g_strfreev(args);
574
575         return -EIO;
576 }
577
578 static void on_clat_handler()
579 {
580         pid_t clat_pid = 0;
581         int state = 0;
582
583         clat_pid = waitpid(-1, &state, WNOHANG);
584
585         DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
586 }
587
588 int netconfig_execute_clatd(const char *file_path, char *const args[])
589 {
590         pid_t pid = 0;
591         int rv = 0;
592         errno = 0;
593         register unsigned int index = 0;
594         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
595
596         struct sigaction act;
597         int state = 0;
598
599         act.sa_handler = on_clat_handler;
600         sigemptyset(&act.sa_mask);
601         act.sa_flags = 0;
602
603         state = sigaction(SIGCHLD, &act, 0);
604         if (state != 0) {
605                 DBG("sigaction() : %d", state);
606                 return -1;
607         }
608
609         while (args[index] != NULL) {
610                 DBG("%s", args[index]);
611                 index++;
612         }
613
614         if (!(pid = fork())) {
615                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
616                 DBG("Inside child, exec (%s) command", file_path);
617
618                 errno = 0;
619                 if (execvp(file_path, args) == -1) {
620                         ERR("Fail to execute command (%s)",
621                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
622                         return -1;
623                 }
624         } else if (pid > 0) {
625                 ERR("Success to launch clatd");
626                 return rv;
627         }
628
629         DBG("failed to fork(%s)",
630                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
631         return -EIO;
632 }
633
634 static void no_wait_signal_handler()
635 {
636         pid_t child_pid = 0;
637         int state = 0;
638
639         child_pid = waitpid(-1, &state, WNOHANG);
640
641         DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
642 }
643
644 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
645 {
646         pid_t pid = 0;
647         int rv = 0;
648         errno = 0;
649         register unsigned int index = 0;
650
651         struct sigaction act;
652         int state = 0;
653         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
654
655         act.sa_handler = no_wait_signal_handler;
656         sigemptyset(&act.sa_mask);
657         act.sa_flags = 0;
658
659         state = sigaction(SIGCHLD, &act, 0);
660         if (state != 0) {
661                 DBG("sigaction() : %d", state);
662                 return -1;
663         }
664
665         while (args[index] != NULL) {
666                 DBG("%s", args[index]);
667                 index++;
668         }
669
670         if (!(pid = fork())) {
671                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
672                 DBG("Inside child, exec (%s) command", file_path);
673
674                 errno = 0;
675                 if (execvp(file_path, args) == -1) {
676                         ERR("Fail to execute command (%s)",
677                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
678                         return -1;
679                 }
680         } else if (pid > 0) {
681                 ERR("Successfully launched child process");
682                 return rv;
683         }
684
685         DBG("failed to fork(%s)",
686                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
687         return -EIO;
688 }
689
690 int __netconfig_get_interface_index(const char *interface_name)
691 {
692         struct ifreq ifr;
693         int sock = 0;
694         int result = 0;
695         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
696
697         if (interface_name == NULL) {
698                 DBG("Inteface name is NULL");
699                 return -1;
700         }
701
702         errno = 0;
703         sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
704         if (sock < 0) {
705                 DBG("Failed to create socket : %s",
706                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
707                 return -1;
708         }
709
710         memset(&ifr, 0, sizeof(ifr));
711         strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
712         result = ioctl(sock, SIOCGIFINDEX, &ifr);
713         close(sock);
714
715         if (result < 0) {
716                 DBG("Failed to get ifr index: %s",
717                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
718                 return -1;
719         }
720
721         return ifr.ifr_ifindex;
722 }
723
724 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
725 {
726         struct ifreq ifr;
727         struct rtentry rt;
728         struct sockaddr_in addr_in;
729         int sock;
730         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
731
732         memset(&ifr, 0, sizeof(ifr));
733
734         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
735
736         if (ifr.ifr_ifindex < 0)
737                 return -1;
738
739         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
740
741         memset(&rt, 0, sizeof(rt));
742
743         rt.rt_flags = RTF_UP | RTF_HOST;
744         memset(&addr_in, 0, sizeof(struct sockaddr_in));
745         addr_in.sin_family = address_family;
746         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
747         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
748
749         memset(&addr_in, 0, sizeof(struct sockaddr_in));
750         addr_in.sin_family = address_family;
751         addr_in.sin_addr.s_addr = INADDR_ANY;
752         memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
753
754         memset(&addr_in, 0, sizeof(struct sockaddr_in));
755         addr_in.sin_family = AF_INET;
756         addr_in.sin_addr.s_addr = inet_addr(subnet);
757         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
758
759         rt.rt_dev = ifr.ifr_name;
760
761         errno = 0;
762         sock = socket(PF_INET, SOCK_DGRAM, 0);
763
764         if (sock < 0) {
765                 DBG("Failed to create socket : %s",
766                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
767                 return -1;
768         }
769
770         if (ioctl(sock, SIOCADDRT, &rt) < 0) {
771                 DBG("Failed to set route address : %s",
772                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
773                 close(sock);
774                 return -1;
775         }
776
777         close(sock);
778
779         return 1;
780 }
781
782 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
783 {
784         struct ifreq ifr;
785         struct rtentry rt;
786         struct sockaddr_in addr_in;
787         int sock;
788         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
789
790         memset(&ifr, 0, sizeof(ifr));
791         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
792
793         if (ifr.ifr_ifindex < 0)
794                 return -1;
795
796         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
797
798         memset(&rt, 0, sizeof(rt));
799
800         rt.rt_flags = RTF_UP;
801         memset(&addr_in, 0, sizeof(struct sockaddr_in));
802         addr_in.sin_family = address_family;
803         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
804         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
805
806         memset(&addr_in, 0, sizeof(struct sockaddr_in));
807         addr_in.sin_family = address_family;
808         addr_in.sin_addr.s_addr = inet_addr(subnet);
809         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
810         rt.rt_dev = ifr.ifr_name;
811
812         errno = 0;
813         sock = socket(PF_INET, SOCK_DGRAM, 0);
814
815         if (sock < 0) {
816                 DBG("Failed to create socket : %s",
817                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
818                 return -1;
819         }
820
821         if (ioctl(sock, SIOCDELRT, &rt) < 0) {
822                 DBG("Failed to set route address : %s",
823                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
824                 close(sock);
825                 return -1;
826         }
827
828         close(sock);
829
830         return 1;
831 }
832
833 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
834 {
835         struct in6_rtmsg rt;
836         int fd = 0;
837         int err = 0;
838         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
839
840         memset(&rt, 0, sizeof(rt));
841
842         rt.rtmsg_dst_len = prefix_len;
843
844         rt.rtmsg_flags = RTF_UP | RTF_HOST;
845
846         errno = 0;
847         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
848                 DBG("inet_pton failed : %s",
849                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
850                 return -1;
851         }
852
853         if (gateway != NULL && g_strcmp0(gateway, "::") != 0) {
854                 rt.rtmsg_flags |= RTF_GATEWAY;
855                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
856                         DBG("inet_pton failed : %s",
857                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
858                         return -1;
859                 }
860         }
861
862         rt.rtmsg_metric = 1;
863
864         fd = socket(AF_INET6, SOCK_DGRAM, 0);
865         if (fd < 0) {
866                 DBG("Failed to create socket : %s",
867                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
868                 return -1;
869         }
870
871         rt.rtmsg_ifindex = 0;
872
873         if (interface) {
874                 struct ifreq ifr;
875                 memset(&ifr, 0, sizeof(ifr));
876                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
877                 ioctl(fd, SIOCGIFINDEX, &ifr);
878                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
879         }
880
881         if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
882                 DBG("Failed to add route: %s",
883                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
884                 close(fd);
885                 return -1;
886         }
887
888         close(fd);
889
890         return 1;
891 }
892
893 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
894 {
895         struct in6_rtmsg rt;
896         int fd = 0;
897         int err = 0;
898
899         memset(&rt, 0, sizeof(rt));
900
901         rt.rtmsg_dst_len = prefix_len;
902
903         rt.rtmsg_flags = RTF_UP | RTF_HOST;
904
905         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
906                 err = -errno;
907                 return err;
908         }
909
910         if (gateway != NULL) {
911                 rt.rtmsg_flags |= RTF_GATEWAY;
912                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
913                         err = -errno;
914                         return err;
915                 }
916         }
917
918         rt.rtmsg_metric = 1;
919
920         fd = socket(AF_INET6, SOCK_DGRAM, 0);
921         if (fd < 0)
922                 return -1;
923
924         rt.rtmsg_ifindex = 0;
925
926         if (interface) {
927                 struct ifreq ifr;
928                 memset(&ifr, 0, sizeof(ifr));
929                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
930                 ioctl(fd, SIOCGIFINDEX, &ifr);
931                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
932         }
933
934         if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
935                 DBG("Failed to del route: %d\n", err);
936                 close(fd);
937                 return -1;
938         }
939
940         close(fd);
941
942         return 1;
943 }
944
945 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
946 {
947         if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
948                 wifi_complete_launch_direct(wifi, context);
949                 return TRUE;
950         }
951
952         int ret = 0;
953         DBG("Launch Wi-Fi direct daemon");
954
955         const char *path = "/usr/bin/wifi-direct-server.sh";
956         char *const args[] = { "wifi-direct-server.sh", "start", NULL };
957         char *const envs[] = { NULL };
958
959         ret = netconfig_execute_file(path, args, envs);
960         if (ret < 0) {
961                 ERR("Failed to launch Wi-Fi direct daemon");
962                 netconfig_error_wifi_direct_failed(context);
963                 return TRUE;
964         }
965
966         wifi_complete_launch_direct(wifi, context);
967         return TRUE;
968 }
969
970 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
971 {
972         if (!netconfig_plugin_headed_enabled)
973                 return FALSE;
974
975         if (!headed_plugin)
976                 return FALSE;
977
978         return headed_plugin->send_notification_to_net_popup(noti, ssid);
979 }
980
981 int netconfig_send_message_to_net_popup(const char *title,
982                 const char *content, const char *type, const char *ssid)
983 {
984         if (!netconfig_plugin_headed_enabled)
985                 return 0;
986
987         if (!headed_plugin)
988                 return 0;
989
990         return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
991 }
992
993 int netconfig_send_restriction_to_net_popup(const char *title,
994                 const char *type, const char *restriction)
995 {
996         if (!netconfig_plugin_headed_enabled)
997                 return 0;
998
999         if (!headed_plugin)
1000                 return 0;
1001
1002         return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
1003 }
1004
1005 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
1006 {
1007         if (!netconfig_plugin_headed_enabled)
1008                 return;
1009
1010         if (!headed_plugin)
1011                 return;
1012
1013         headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1014 }
1015
1016 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1017 {
1018         if (!netconfig_plugin_headed_enabled)
1019                 return;
1020
1021         if (!headed_plugin)
1022                 return;
1023
1024         headed_plugin->pop_wifi_connected_poppup(ssid);
1025 }
1026
1027 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1028 {
1029         if (!netconfig_plugin_telephony_enabled)
1030                 return;
1031
1032         if (!telephony_plugin)
1033                 return;
1034
1035         telephony_plugin->get_telephony_network_type(svctype, pstype);
1036 }
1037
1038 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1039 {
1040         if (!netconfig_plugin_telephony_enabled)
1041                 return FALSE;
1042
1043         if (!telephony_plugin)
1044                 return FALSE;
1045
1046         return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1047 }
1048
1049 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1050                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1051 {
1052         if (!netconfig_plugin_telephony_enabled)
1053                 return NETCONFIG_ERROR_INTERNAL;
1054
1055         if (!telephony_plugin)
1056                 return NETCONFIG_ERROR_INTERNAL;
1057
1058         return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1059 }
1060
1061 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1062                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1063 {
1064         if (!netconfig_plugin_telephony_enabled)
1065                 return FALSE;
1066
1067         if (!telephony_plugin)
1068                 return FALSE;
1069
1070         return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1071 }
1072
1073 gboolean netconfig_tapi_check_sim_state(void)
1074 {
1075         if (!netconfig_plugin_telephony_enabled)
1076                 return FALSE;
1077
1078         if (!telephony_plugin)
1079                 return FALSE;
1080
1081         return telephony_plugin->tapi_check_sim_state();
1082 }
1083
1084 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1085                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1086 {
1087         if (!netconfig_plugin_telephony_enabled)
1088                 return FALSE;
1089
1090         if (!telephony_plugin)
1091                 return FALSE;
1092
1093         return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1094 }
1095
1096 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1097                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1098 {
1099         if (!netconfig_plugin_telephony_enabled)
1100                 return FALSE;
1101
1102         if (!telephony_plugin)
1103                 return FALSE;
1104
1105         return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1106 }
1107
1108 static void __netconfig_stc_get_dn_stats(time_t from, time_t to, GSList **list)
1109 {
1110         if (!netconfig_plugin_stc_enabled)
1111                 return;
1112
1113         if (!stc_plugin)
1114                 return;
1115
1116         return stc_plugin->get_stc_dn_stats(from, to, list);
1117 }
1118
1119 static void __netconfig_stc_get_wifi_stats(time_t from, time_t to, GSList **list)
1120 {
1121         if (!netconfig_plugin_stc_enabled)
1122                 return;
1123
1124         if (!stc_plugin)
1125                 return;
1126
1127         return stc_plugin->get_stc_wifi_stats(from, to, list);
1128 }
1129
1130 void netconfig_battery_start_dn(void)
1131 {
1132         if (!netconfig_plugin_battery_enabled)
1133                 return;
1134
1135         if (!battery_plugin)
1136                 return;
1137
1138         return battery_plugin->start_dn_data();
1139 }
1140
1141 void netconfig_battery_end_dn(void)
1142 {
1143         if (!netconfig_plugin_battery_enabled)
1144                 return;
1145
1146         if (!battery_plugin)
1147                 return;
1148
1149         return battery_plugin->end_dn_data(0, __netconfig_stc_get_dn_stats);
1150 }
1151
1152 void netconfig_battery_update_dn_rssi(int rssi)
1153 {
1154         if (!netconfig_plugin_battery_enabled)
1155                 return;
1156
1157         if (!battery_plugin)
1158                 return;
1159
1160         return battery_plugin->update_dn_rssi(rssi);
1161 }
1162
1163 void netconfig_battery_start_wifi(void)
1164 {
1165         if (!netconfig_plugin_battery_enabled)
1166                 return;
1167
1168         if (!battery_plugin)
1169                 return;
1170
1171         return battery_plugin->start_wifi_data();
1172 }
1173
1174 void netconfig_battery_end_wifi(void)
1175 {
1176         if (!netconfig_plugin_battery_enabled)
1177                 return;
1178
1179         if (!battery_plugin)
1180                 return;
1181
1182         return battery_plugin->end_wifi_data(0, __netconfig_stc_get_wifi_stats);
1183 }
1184
1185 void netconfig_battery_update_wifi_scan(int state)
1186 {
1187         if (!netconfig_plugin_battery_enabled)
1188                 return;
1189
1190         if (!battery_plugin)
1191                 return;
1192
1193         return battery_plugin->update_wifi_scan(state);
1194 }
1195
1196 void netconfig_battery_update_wifi_rssi(int rssi)
1197 {
1198         if (!netconfig_plugin_battery_enabled)
1199                 return;
1200
1201         if (!battery_plugin)
1202                 return;
1203
1204         return battery_plugin->update_wifi_rssi(rssi);
1205 }
1206
1207 void netconfig_battery_get_dn_list(void *data)
1208 {
1209         if (!netconfig_plugin_battery_enabled)
1210                 return;
1211
1212         if (!battery_plugin)
1213                 return;
1214
1215         return battery_plugin->get_battery_dn_list(data, __netconfig_stc_get_dn_stats);
1216 }
1217
1218 void netconfig_battery_get_wifi_list(void *data)
1219 {
1220         if (!netconfig_plugin_battery_enabled)
1221                 return;
1222
1223         if (!battery_plugin)
1224                 return;
1225
1226         return battery_plugin->get_battery_wifi_list(data, __netconfig_stc_get_wifi_stats);
1227 }
1228
1229 void netconfig_notify_online_state(char *ifname, gboolean online_state)
1230 {
1231         if (!netconfig_plugin_online_monitor_enabled)
1232                 return;
1233
1234         if (!online_monitor_plugin)
1235                 return;
1236
1237         online_monitor_plugin->notify_online_state(ifname, online_state);
1238 }
1239
1240 void netconfig_set_vconf_int(const char *key, int value, gboolean log)
1241 {
1242         int ret = 0;
1243         GVariant *params = NULL;
1244
1245         if (log)
1246                 DBG("[%s: %d]", key, value);
1247
1248         ret = vconf_set_int(key, value);
1249         if (ret != VCONF_OK)
1250                 ERR("Failed to set");
1251
1252         params = g_variant_new("(sssi)", key, "int", "", value);
1253         if (netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
1254                         NETCONFIG_NETWORK_INTERFACE, "VconfChanged",
1255                         params) == FALSE)
1256                 ERR("Failed to emit VconfChanged signal");
1257 }
1258
1259 void netconfig_set_vconf_str(const char *key, const char *value, gboolean log)
1260 {
1261         int ret = 0;
1262         GVariant *params = NULL;
1263
1264         if (log)
1265                 DBG("[%s: %s]", key, value);
1266
1267         ret = vconf_set_str(key, value);
1268         if (ret != VCONF_OK)
1269                 ERR("Failed to set");
1270
1271         params = g_variant_new("(sssi)", key, "string", value, 0);
1272         if (netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
1273                         NETCONFIG_NETWORK_INTERFACE, "VconfChanged",
1274                         params) == FALSE)
1275                 ERR("Failed to emit VconfChanged signal");
1276 }
1277
1278 int netconfig_vconf_get_int(const char *key, int *value)
1279 {
1280         int ret = 0;
1281
1282         ret = vconf_get_int(key, value);
1283         if (ret != VCONF_OK) {
1284                 ERR("Failed to get vconfkey [%s] value", key);
1285                 return -1;
1286         }
1287
1288         return 0;
1289 }
1290
1291 int netconfig_vconf_get_bool(const char *key, int *value)
1292 {
1293         int ret = 0;
1294
1295         ret = vconf_get_bool(key, value);
1296         if (ret != VCONF_OK) {
1297                 ERR("Failed to get vconfkey [%s] value", key);
1298                 return -1;
1299         }
1300
1301         return 0;
1302 }
1303
1304 char *netconfig_vconf_get_str_dbus(const char *key)
1305 {
1306         int ret = 0;
1307         int int_value = 0;
1308         char *str_value = NULL;
1309
1310         if (!netconfig_dbus_get_vconf_value(key, "string", &ret, &int_value, &str_value))
1311                 return NULL;
1312
1313         return str_value;
1314 }
1315
1316 int netconfig_vconf_get_int_dbus(const char *key, int *value)
1317 {
1318         int ret = 0;
1319         int int_value = 0;
1320         char *str_value = NULL;
1321
1322         if (!netconfig_dbus_get_vconf_value(key, "int", &ret, &int_value, &str_value))
1323                 return VCONF_ERROR;
1324
1325         *value = int_value;
1326
1327         if (str_value)
1328                 g_free(str_value);
1329
1330         return ret;
1331 }
1332
1333 int netconfig_vconf_get_bool_dbus(const char *key, int *value)
1334 {
1335         int ret = 0;
1336         int int_value = 0;
1337         char *str_value = NULL;
1338
1339         if (!netconfig_dbus_get_vconf_value(key, "bool", &ret, &int_value, &str_value))
1340                 return VCONF_ERROR;
1341
1342         *value = int_value;
1343
1344         if (str_value)
1345                 g_free(str_value);
1346
1347         return ret;
1348 }
1349
1350 char* netconfig_get_env(const char *key)
1351 {
1352         FILE *fp;
1353         char buf[256], *entry = NULL, *value = NULL, *last;
1354         int len = 0;
1355
1356         if (!key)
1357                 return NULL;
1358
1359         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1360         if (!fp)
1361                 return NULL;
1362
1363         while (fgets(buf, sizeof(buf), fp)) {
1364                 entry = buf;
1365                 entry = strtok_r(entry, "=", &last);
1366                 if (entry) {
1367                         if (strstr(entry, key)) {
1368                                 entry = strtok_r(NULL, "\n", &last);
1369                                 if (entry) {
1370                                         len = strlen(entry);
1371                                         value = (char*)malloc(len+1);
1372                                         g_strlcpy(value, entry, len+1);
1373                                 } else {
1374                                         value = (char*)malloc(sizeof(char));
1375                                         g_strlcpy(value, "\n", sizeof(char));
1376                                 }
1377                                 break;
1378                         }
1379                 }
1380         }
1381
1382         fclose(fp);
1383         return value;
1384 }
1385
1386 void netconfig_set_mac_address_to_vconf(const char *def_mac)
1387 {
1388         int mac_len = 0;
1389         gchar *mac_lower_str = NULL;
1390
1391         mac_len = strlen(def_mac);
1392         if (mac_len < 17) {
1393                 ERR("def_mac is empty");
1394                 return;
1395         }
1396
1397         mac_lower_str = g_ascii_strdown(def_mac, (gssize)mac_len);
1398         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str, TRUE);
1399         g_free(mac_lower_str);
1400 }
1401
1402 void netconfig_set_mac_address_from_file(void)
1403 {
1404         FILE *file = NULL;
1405         char mac_str[MAC_ADDRESS_MAX_LEN];
1406         gchar *mac_lower_str = NULL;
1407         int mac_len = 0;
1408
1409         file = fopen(MAC_INFO_FILEPATH, "r");
1410         if (file == NULL) {
1411                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1412                 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1413                 if (file == NULL) {
1414                         ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1415                         return;
1416                 }
1417         }
1418         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1419                 ERR("Fail to read mac address");
1420                 fclose(file);
1421                 return;
1422         }
1423
1424         mac_len = strlen(mac_str);
1425         if (mac_len < 17) {
1426                 ERR("mac.info is empty");
1427                 fclose(file);
1428                 return;
1429         }
1430
1431         mac_lower_str = g_ascii_strdown(mac_str, (gssize)mac_len);
1432         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str, TRUE);
1433
1434         g_free(mac_lower_str);
1435         fclose(file);
1436 }
1437
1438 char *netconfig_get_mac_address_from_file(const char *ifname)
1439 {
1440         FILE *file = NULL;
1441         char file_path[PATH_MAX] = {0, };
1442         char mac_str[MAC_ADDRESS_MAX_LEN];
1443         int mac_len = 0;
1444
1445         if (!ifname)
1446                 return NULL;
1447
1448         g_snprintf(file_path, sizeof(file_path),
1449                         "/sys/class/net/%s/address", ifname);
1450
1451         DBG("ifname: %s", ifname);
1452         DBG("file_path: %s", file_path);
1453         file = fopen(file_path, "r");
1454         if (file == NULL) {
1455                 ERR("Fail to open %s", file_path);
1456                 return NULL;
1457         }
1458         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1459                 ERR("Fail to read mac address");
1460                 fclose(file);
1461                 return NULL;
1462         }
1463
1464         mac_len = strlen(mac_str);
1465         if (mac_len < 17) {
1466                 ERR("mac is empty");
1467                 fclose(file);
1468                 return NULL;
1469         }
1470         DBG("address: %s", mac_str);
1471
1472         fclose(file);
1473         return g_strdup(mac_str);
1474 }
1475
1476 char *netconfig_get_default_ifname_from_file(void)
1477 {
1478         GKeyFile *keyfile = NULL;
1479
1480         keyfile = netconfig_keyfile_load(CONNMAN_MAINFILE);
1481         if (keyfile == NULL) {
1482                 ERR("keyfile[%s] is NULL", CONNMAN_MAINFILE);
1483                 return NULL;
1484         }
1485
1486         char *str = g_key_file_get_string(keyfile, "General",
1487                         CONNMAN_WIFI_DEF_IFNAME, NULL);
1488         g_key_file_free(keyfile);
1489
1490         if (!str)
1491                 return NULL;
1492
1493         if (*str == '\0') {
1494                 g_free(str);
1495                 return NULL;
1496         }
1497
1498         g_strchomp(str);
1499
1500         if (strlen(str) >= IFNAMSIZ) {
1501                 g_free(str);
1502                 return NULL;
1503         }
1504
1505         DBG("ifname[%s]", str);
1506         return str;
1507 }
1508
1509 int netconfig_freq_to_channel(int freq)
1510 {
1511         /*
1512          * 5825 is max frequency for 5 GHz and 7125 is for 6 GHz
1513          */
1514         if (freq < 2412 || freq > 7125 ||
1515                         (freq > 2484 && freq < 5180) ||
1516                         (freq > 5825 && freq < 5945)) {
1517                 ERR("Invalid Frequence Range");
1518                 return 0;
1519         }
1520
1521         if (freq >= 5945)
1522                 return 189 + (freq - 5945)/5;
1523         else if (freq >= 5180)
1524                 return 36 + (freq - 5180)/5;
1525         else if (freq <= 2472)
1526                 return 1 + (freq - 2412)/5;
1527         else if (freq == 2484)
1528                 return 14;
1529         else
1530                 return 0;
1531 }
1532
1533 int netconfig_get_operating_class(int freq)
1534 {
1535         int channel = 0;
1536         int oper_class = 0;
1537
1538         channel = netconfig_freq_to_channel(freq);
1539
1540         if (channel) {
1541                 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1542                 if (channel >= 1 && channel <= 13)
1543                         oper_class = 81;
1544                 /* Operating class 115 - 5 GHz, channels 36-48 */
1545                 else if (channel >= 36 && channel <= 48)
1546                         oper_class = 115;
1547                 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1548                 else if (channel >= 149 && channel <= 161)
1549                         oper_class = 124;
1550                 /* Operating class - 6 GHz, channels 189, 193, 197, ... 425 */
1551                 else if (channel >= 189 && channel <= 425)
1552                         oper_class = 131;
1553
1554                 INFO("Operating Class  is [%d]", oper_class);
1555                 return oper_class;
1556         }
1557         return 0;
1558 }
1559
1560 tizen_profile_t _get_tizen_profile()
1561 {
1562         static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1563         if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1564                 return profile;
1565
1566         char *profileName;
1567         system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1568         switch (*profileName) {
1569         case 'm':
1570         case 'M':
1571                 profile = TIZEN_PROFILE_MOBILE;
1572                 break;
1573         case 'w':
1574         case 'W':
1575                 profile = TIZEN_PROFILE_WEARABLE;
1576                 break;
1577         case 't':
1578         case 'T':
1579                 profile = TIZEN_PROFILE_TV;
1580                 break;
1581         case 'i':
1582         case 'I':
1583                 profile = TIZEN_PROFILE_IVI;
1584                 break;
1585         default: // common or unknown ==> ALL ARE COMMON.
1586                 profile = TIZEN_PROFILE_COMMON;
1587         }
1588         free(profileName);
1589
1590         return profile;
1591 }
1592
1593 void netconfig_plugin_init()
1594 {
1595         handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1596         if (!handle_headed) {
1597                 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1598         } else {
1599                 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1600                 if (!headed_plugin) {
1601                         ERR("Can't load symbol: %s", dlerror());
1602                         dlclose(handle_headed);
1603                 } else {
1604                         netconfig_plugin_headed_enabled = TRUE;
1605                 }
1606         }
1607
1608         handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1609         if (!handle_telephony) {
1610                 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1611         } else {
1612                 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1613                 if (!telephony_plugin) {
1614                         ERR("Can't load symbol: %s", dlerror());
1615                         dlclose(handle_telephony);
1616                 } else {
1617                         netconfig_plugin_telephony_enabled = TRUE;
1618                 }
1619         }
1620
1621         handle_stc = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
1622         if (!handle_stc) {
1623                 ERR("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
1624         } else {
1625                 stc_plugin = dlsym(handle_stc, "netconfig_stc_plugin");
1626                 if (!stc_plugin) {
1627                         ERR("Can't load symbol: %s", dlerror());
1628                         dlclose(handle_stc);
1629                 } else {
1630                         netconfig_plugin_stc_enabled = TRUE;
1631                 }
1632         }
1633
1634         handle_battery = dlopen(BATTERY_PLUGIN_FILEPATH, RTLD_NOW);
1635         if (!handle_battery) {
1636                 ERR("Can't load %s: %s", BATTERY_PLUGIN_FILEPATH, dlerror());
1637         } else {
1638                 battery_plugin = dlsym(handle_battery, "netconfig_battery_plugin");
1639                 if (!battery_plugin) {
1640                         ERR("Can't load symbol: %s", dlerror());
1641                         dlclose(handle_battery);
1642                 } else {
1643                         netconfig_plugin_battery_enabled = TRUE;
1644                 }
1645         }
1646
1647         handle_online_monitor = dlopen(ONLINE_MONITOR_PLUGIN_FILEPATH, RTLD_NOW);
1648         if (!handle_online_monitor) {
1649                 ERR("Can't load %s: %s", ONLINE_MONITOR_PLUGIN_FILEPATH, dlerror());
1650         } else {
1651                 online_monitor_plugin = dlsym(handle_online_monitor, "netconfig_online_monitor_plugin");
1652                 if (!online_monitor_plugin) {
1653                         ERR("Can't load symbol: %s", dlerror());
1654                         dlclose(handle_online_monitor);
1655                 } else {
1656                         if (online_monitor_plugin->init() == 0)
1657                                 netconfig_plugin_online_monitor_enabled = TRUE;
1658                 }
1659         }
1660 }
1661
1662 void netconfig_plugin_deinit()
1663 {
1664         if (netconfig_plugin_headed_enabled) {
1665                 netconfig_plugin_headed_enabled = FALSE;
1666                 dlclose(handle_headed);
1667         }
1668
1669         if (netconfig_plugin_telephony_enabled) {
1670                 netconfig_plugin_telephony_enabled = FALSE;
1671                 dlclose(handle_telephony);
1672         }
1673
1674         if (netconfig_plugin_stc_enabled) {
1675                 netconfig_plugin_stc_enabled = FALSE;
1676                 dlclose(handle_stc);
1677         }
1678
1679         if (netconfig_plugin_battery_enabled) {
1680                 netconfig_plugin_battery_enabled = FALSE;
1681                 dlclose(handle_battery);
1682         }
1683
1684         if (netconfig_plugin_online_monitor_enabled) {
1685                 netconfig_plugin_online_monitor_enabled = FALSE;
1686                 if (online_monitor_plugin)
1687                         online_monitor_plugin->deinit();
1688                 dlclose(handle_online_monitor);
1689         }
1690 }
1691
1692 gboolean netconfig_get_headed_plugin_flag()
1693 {
1694         return netconfig_plugin_headed_enabled;
1695 }
1696
1697 gboolean netconfig_get_telephony_plugin_flag()
1698 {
1699         return netconfig_plugin_telephony_enabled;
1700 }
1701
1702 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1703 {
1704         const char *key = NULL;
1705
1706         if (!is_feature_checked[feature]) {
1707                 switch (feature) {
1708                 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1709                         key = ETHERNET_FEATURE;
1710                         break;
1711                 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1712                         key = TETHERING_FEATURE;
1713                         break;
1714                 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1715                         key = WIFI_DIRECT_FEATURE;
1716                         break;
1717                 case NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP:
1718                         key = WIFI_SOFTAP_FEATURE;
1719                         break;
1720                 default:
1721                         ERR("Uknown feature");
1722                         return false;
1723                 }
1724
1725                 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1726                         ERR("Get feature is failed");
1727                         return false;
1728                 }
1729                 is_feature_checked[feature] = true;
1730         }
1731         return feature_supported[feature];
1732 }
1733
1734 void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr)
1735 {
1736         char t;
1737
1738         while (blen) {
1739                 t = (*bin >> 4) & 0x0f;
1740
1741                 if (t <= 9)
1742                         *hexstr = t + '0';
1743                 else if (t >= 10 && t <= 16)
1744                         *hexstr = (t - 10) + 'a';
1745
1746                 hexstr++;
1747
1748                 t = *bin & 0x0f;
1749
1750                 if (t <= 9)
1751                         *hexstr = t + '0';
1752                 else if (t >= 10 && t <= 16)
1753                         *hexstr = (t - 10) + 'a';
1754
1755                 hexstr++;
1756                 bin++;
1757                 blen--;
1758         }
1759
1760         *hexstr = '\0';
1761 }
1762
1763 bool __is_hidden_file(const  char *file)
1764 {
1765         /* exclude "." ,  "..", "settings" and hidden files */
1766         if( g_strcmp0( file, "." ) == 0 ||
1767                         g_strcmp0( file, ".." ) == 0 || file[0]=='.' || g_strcmp0(file, "settings")==0 )
1768         {
1769                 return true;
1770         }
1771
1772         return false;
1773 }
1774
1775 int get_files_count(const char *path)
1776 {
1777         DIR *dfd = NULL;
1778         struct dirent *dir = NULL;
1779         int file_count = 0;
1780         dfd = opendir(path);
1781         if (dfd) {
1782                 while ((dir = readdir(dfd)) != NULL) {
1783                         if (__is_hidden_file(dir->d_name))
1784                                 continue;
1785
1786                         if (strncmp(dir->d_name, "wifi_", 5) != 0)
1787                                 continue;
1788
1789                         ++file_count;
1790                 }
1791                 closedir(dfd);
1792         }
1793
1794         return file_count;
1795 }
1796
1797 char * get_least_recently_profile(const char *path)
1798 {
1799         DIR *dfd = NULL;
1800         struct dirent *dir = NULL;
1801         unsigned long lastModified =  (unsigned long)~0;
1802         char *file = NULL;
1803         dfd = opendir(path);
1804         if (dfd) {
1805                 while ((dir = readdir(dfd)) != NULL) {
1806                         if (__is_hidden_file(dir->d_name))
1807                                 continue;
1808
1809                         if (strncmp(dir->d_name, "wifi_", 5) != 0)
1810                                 continue;
1811
1812                         struct stat attr;
1813                         gchar *full_path = g_strdup_printf("%s/%s", path, dir->d_name);
1814                         if (stat(full_path, &attr)== 0) {
1815                                 if(lastModified > attr.st_mtime)
1816                                 {
1817                                         lastModified = attr.st_mtime;
1818                                         file = dir->d_name;
1819                                 }
1820                         } else {
1821                                 ERR("stat failed");
1822                         }
1823                         g_free(full_path);
1824                 }
1825                 closedir(dfd);
1826         }
1827
1828         DBG("least recently path: [%s]",  file);
1829
1830         return file;
1831 }