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