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