2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <net/route.h>
29 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
33 #include <linux/limits.h>
35 #include <vconf-keys.h>
36 #include <tzplatform_config.h>
37 #include <system_info.h>
39 #include <sys/types.h>
46 #include "wifi-state.h"
47 #include "network-state.h"
50 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
51 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
52 #define MAC_INFO_FILEPATH tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
53 #define MAC_ADDRESS_FILEPATH "/sys/class/net/wlan0/address"
54 #if defined(TIZEN_ARCH_64)
55 #define HEADED_PLUGIN_FILEPATH "/usr/lib64/net-config-plugin-headed.so"
56 #define TELEPHONY_PLUGIN_FILEPATH "/usr/lib64/net-config-plugin-telephony.so"
57 #define STC_PLUGIN_FILEPATH "/usr/lib64/net-config-plugin-stc.so"
58 #define BATTERY_PLUGIN_FILEPATH "/usr/lib64/net-config-plugin-battery.so"
59 #define ONLINE_MONITOR_PLUGIN_FILEPATH "/usr/lib64/net-config-plugin-online-monitor.so"
61 #define HEADED_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-headed.so"
62 #define TELEPHONY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-telephony.so"
63 #define STC_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-stc.so"
64 #define BATTERY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-battery.so"
65 #define ONLINE_MONITOR_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-online-monitor.so"
67 #define CONNMAN_MAINFILE "/etc/connman/main.conf"
68 #define CONNMAN_WIFI_DEF_IFNAME "DefaultWifiInterface"
70 static gboolean netconfig_device_picker_test = FALSE;
74 } dnssd_conn_destroy_data;
76 static gboolean netconfig_plugin_headed_enabled = FALSE;
77 static gboolean netconfig_plugin_telephony_enabled = FALSE;
78 static gboolean netconfig_plugin_stc_enabled = FALSE;
79 static gboolean netconfig_plugin_battery_enabled = FALSE;
80 static gboolean netconfig_plugin_online_monitor_enabled = FALSE;
81 static void *handle_headed;
82 static void *handle_telephony;
83 static void *handle_stc;
84 static void *handle_battery;
85 static void *handle_online_monitor;
86 static struct netconfig_headed_plugin_t *headed_plugin;
87 static struct netconfig_telephony_plugin_t *telephony_plugin;
88 static struct netconfig_stc_plugin_t *stc_plugin;
89 static struct netconfig_battery_plugin_t *battery_plugin;
90 static struct netconfig_online_monitor_plugin_t *online_monitor_plugin;
92 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
93 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
95 gboolean netconfig_check_mac_address(const char *service, const char *mac_address)
97 int service_index = 0;
99 const char *org = NULL;
100 const char *dst = NULL;
103 if (g_str_has_prefix(service, "wifi_") == FALSE)
106 service_index = strlen("wifi_");
107 for (i = 0; i < 6; i++) {
108 org = &service[service_index];
109 dst = &mac_address[mac_index];
111 if (g_ascii_strncasecmp(org, dst, 2) != 0)
121 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
128 length = strlen(passphrase);
130 if (g_str_has_suffix(service, "psk") == TRUE) {
132 for (int i = 0; i < 64; i++)
133 if (!isxdigit((unsigned char)passphrase[i]))
135 } else if (length < 8 || length > 63)
137 } else if (g_str_has_suffix(service, "wep") == TRUE) {
138 if (length == 10 || length == 26) {
139 for (int i = 0; i < length; i++)
140 if (!isxdigit((unsigned char)passphrase[i]))
142 } else if (length != 5 && length != 13)
149 GKeyFile *netconfig_keyfile_load(const char *pathname)
151 GKeyFile *keyfile = NULL;
152 GError *error = NULL;
154 keyfile = g_key_file_new();
155 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
156 DBG("Unable to open %s, error %s", pathname, error->message);
159 g_key_file_free(keyfile);
163 DBG("loaded keyfile %s", pathname);
167 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
170 GError *error = NULL;
171 gchar *keydata = NULL;
172 gchar *needle = NULL, *directory = NULL;
174 directory = g_strdup(pathname);
175 if (directory == NULL) {
176 ERR("directory is NULL");
180 needle = g_strrstr(directory, "/");
184 if ((*directory) == '\0') {
186 ERR("directory is NULL");
190 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
191 if (g_mkdir_with_parents(directory,
192 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
194 ERR("failed to make directory");
200 keydata = g_key_file_to_data(keyfile, &size, &error);
201 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
202 ERR("Unable to save %s, error %s", pathname, error->message);
206 if (chmod(pathname, S_IRUSR | S_IWUSR) < 0)
207 DBG("Failed to change mode");
209 DBG("Successfully saved keyfile %s", pathname);
214 void netconfig_start_timer_seconds(guint secs,
215 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
219 if (callback == NULL) {
220 ERR("callback function is NULL");
224 if ((timer_id != NULL && *timer_id != 0)) {
225 ERR("timer already is registered");
229 t_id = g_timeout_add_seconds(secs, callback, user_data);
232 ERR("Can't add timer");
236 if (timer_id != NULL)
240 void netconfig_start_timer(guint msecs,
241 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
245 INFO("Register timer with callback pointer (%p)", callback);
247 if (callback == NULL) {
248 ERR("callback function is NULL");
252 if ((timer_id != NULL && *timer_id != 0)) {
253 ERR("timer already is registered");
257 t_id = g_timeout_add(msecs, callback, user_data);
260 ERR("Can't add timer");
264 if (timer_id != NULL)
268 void netconfig_stop_timer(guint *timer_id)
270 if (timer_id == NULL) {
271 ERR("timer is NULL");
275 if (*timer_id != 0) {
276 g_source_remove(*timer_id);
281 static gboolean __netconfig_test_device_picker()
283 char *favorite_wifi_service = NULL;
285 favorite_wifi_service = wifi_get_favorite_service();
286 if (favorite_wifi_service != NULL) {
287 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
288 g_free(favorite_wifi_service);
295 static void __netconfig_pop_device_picker(void)
297 if (!netconfig_plugin_headed_enabled)
303 headed_plugin->pop_device_picker();
306 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
308 if (__netconfig_test_device_picker() == TRUE)
309 __netconfig_pop_device_picker();
314 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
316 static guint netconfig_wifi_device_picker_service_timer = 0;
318 if (is_set_method != TRUE)
319 return netconfig_wifi_device_picker_service_timer;
321 if (netconfig_wifi_device_picker_service_timer != timer_id)
322 netconfig_wifi_device_picker_service_timer = timer_id;
324 return netconfig_wifi_device_picker_service_timer;
327 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
329 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
332 static guint __netconfig_wifi_device_picker_get_timer_id(void)
334 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
337 void netconfig_wifi_enable_device_picker_test(void)
339 netconfig_device_picker_test = TRUE;
342 void netconfig_wifi_device_picker_service_start(void)
344 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
347 if (netconfig_device_picker_test == TRUE)
348 netconfig_device_picker_test = FALSE;
354 netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
355 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
358 DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
359 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
361 __netconfig_wifi_device_picker_set_timer_id(timer_id);
364 void netconfig_wifi_device_picker_service_stop(void)
368 timer_id = __netconfig_wifi_device_picker_get_timer_id();
372 DBG("Clear device picker timer with timer_id %d", timer_id);
374 netconfig_stop_timer(&timer_id);
376 __netconfig_wifi_device_picker_set_timer_id(timer_id);
379 gboolean netconfig_is_wifi_direct_on(void)
381 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
384 int wifi_direct_state = 0;
386 netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
388 DBG("Wi-Fi direct mode %d", wifi_direct_state);
389 return (wifi_direct_state != 0) ? TRUE : FALSE;
392 gboolean netconfig_is_wifi_tethering_on(void)
394 if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
395 int wifi_tethering_state = 0;
397 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
398 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
399 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
400 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
401 DBG("Mobile AP is on");
406 DBG("Mobile AP is off");
410 gboolean netconfig_interface_up(const char *ifname)
415 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
417 ERR("socket failed %d", errno);
421 memset(&ifr, 0, sizeof(ifr));
422 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
424 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
425 ERR("Fail to get IFFLAGS %d", errno);
430 DBG("IFFLAGS: %x", ifr.ifr_flags);
431 ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
432 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
433 ERR("Fail to set IFFLAGS %d", errno);
440 DBG("Successfully activated wireless interface %s", ifname);
444 gboolean netconfig_interface_down(const char *ifname)
449 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
451 ERR("socket failed %d", errno);
455 memset(&ifr, 0, sizeof(ifr));
456 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
458 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
459 ERR("Fail to get IFFLAGS %d", errno);
464 DBG("IFFLAGS: %x", ifr.ifr_flags);
465 ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
466 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
467 ERR("Fail to set IFFLAGS %d", errno);
474 DBG("Successfully de-activated wireless interface %s", ifname);
478 int netconfig_execute_file(const char *file_path,
479 char *const args[], char *const envs[])
485 register unsigned int index = 0;
486 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
488 while (args[index] != NULL) {
489 DBG("%s", args[index]);
493 if (!(pid = fork())) {
494 DBG("pid(%d), ppid (%d)", getpid(), getppid());
495 DBG("Inside child, exec (%s) command", file_path);
498 if (execve(file_path, args, envs) == -1) {
499 DBG("Fail to execute command (%s)",
500 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
503 } else if (pid > 0) {
504 if (waitpid(pid, &status, 0) == -1)
505 DBG("wait pid (%u) status (%d)", pid, status);
507 if (WIFEXITED(status)) {
508 rv = WEXITSTATUS(status);
509 DBG("exited, status=%d", rv);
510 } else if (WIFSIGNALED(status)) {
511 DBG("killed by signal %d", WTERMSIG(status));
512 } else if (WIFSTOPPED(status)) {
513 DBG("stopped by signal %d", WSTOPSIG(status));
514 } else if (WIFCONTINUED(status)) {
521 DBG("failed to fork(%s)",
522 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
526 int netconfig_execute_cmd(const char *cmd)
535 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
538 DBG("command: %s", cmd);
540 args = g_strsplit_set(cmd, " ", -1);
542 if (!(pid = fork())) {
543 DBG("pid(%d), ppid (%d)", getpid(), getppid());
546 if (execv(args[0], args) == -1) {
547 DBG("Fail to execute command (%s)",
548 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
552 } else if (pid > 0) {
553 if (waitpid(pid, &status, 0) == -1)
554 DBG("wait pid (%u) status (%d)", pid, status);
556 if (WIFEXITED(status)) {
557 rv = WEXITSTATUS(status);
558 DBG("exited, status=%d", rv);
559 } else if (WIFSIGNALED(status)) {
560 DBG("killed by signal %d", WTERMSIG(status));
561 } else if (WIFSTOPPED(status)) {
562 DBG("stopped by signal %d", WSTOPSIG(status));
563 } else if (WIFCONTINUED(status)) {
571 DBG("failed to fork(%s)",
572 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
578 static void on_clat_handler()
583 clat_pid = waitpid(-1, &state, WNOHANG);
585 DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
588 int netconfig_execute_clatd(const char *file_path, char *const args[])
593 register unsigned int index = 0;
594 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
596 struct sigaction act;
599 act.sa_handler = on_clat_handler;
600 sigemptyset(&act.sa_mask);
603 state = sigaction(SIGCHLD, &act, 0);
605 DBG("sigaction() : %d", state);
609 while (args[index] != NULL) {
610 DBG("%s", args[index]);
614 if (!(pid = fork())) {
615 DBG("pid(%d), ppid (%d)", getpid(), getppid());
616 DBG("Inside child, exec (%s) command", file_path);
619 if (execvp(file_path, args) == -1) {
620 ERR("Fail to execute command (%s)",
621 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
624 } else if (pid > 0) {
625 ERR("Success to launch clatd");
629 DBG("failed to fork(%s)",
630 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
634 static void no_wait_signal_handler()
639 child_pid = waitpid(-1, &state, WNOHANG);
641 DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
644 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
649 register unsigned int index = 0;
651 struct sigaction act;
653 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
655 act.sa_handler = no_wait_signal_handler;
656 sigemptyset(&act.sa_mask);
659 state = sigaction(SIGCHLD, &act, 0);
661 DBG("sigaction() : %d", state);
665 while (args[index] != NULL) {
666 DBG("%s", args[index]);
670 if (!(pid = fork())) {
671 DBG("pid(%d), ppid (%d)", getpid(), getppid());
672 DBG("Inside child, exec (%s) command", file_path);
675 if (execvp(file_path, args) == -1) {
676 ERR("Fail to execute command (%s)",
677 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
680 } else if (pid > 0) {
681 ERR("Successfully launched child process");
685 DBG("failed to fork(%s)",
686 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
690 int __netconfig_get_interface_index(const char *interface_name)
695 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
697 if (interface_name == NULL) {
698 DBG("Inteface name is NULL");
703 sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
705 DBG("Failed to create socket : %s",
706 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
710 memset(&ifr, 0, sizeof(ifr));
711 strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
712 result = ioctl(sock, SIOCGIFINDEX, &ifr);
716 DBG("Failed to get ifr index: %s",
717 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
721 return ifr.ifr_ifindex;
724 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
728 struct sockaddr_in addr_in;
730 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
732 memset(&ifr, 0, sizeof(ifr));
734 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
736 if (ifr.ifr_ifindex < 0)
739 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
741 memset(&rt, 0, sizeof(rt));
743 rt.rt_flags = RTF_UP | RTF_HOST;
744 memset(&addr_in, 0, sizeof(struct sockaddr_in));
745 addr_in.sin_family = address_family;
746 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
747 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
749 memset(&addr_in, 0, sizeof(struct sockaddr_in));
750 addr_in.sin_family = address_family;
751 addr_in.sin_addr.s_addr = INADDR_ANY;
752 memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
754 memset(&addr_in, 0, sizeof(struct sockaddr_in));
755 addr_in.sin_family = AF_INET;
756 addr_in.sin_addr.s_addr = inet_addr(subnet);
757 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
759 rt.rt_dev = ifr.ifr_name;
762 sock = socket(PF_INET, SOCK_DGRAM, 0);
765 DBG("Failed to create socket : %s",
766 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
770 if (ioctl(sock, SIOCADDRT, &rt) < 0) {
771 DBG("Failed to set route address : %s",
772 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
782 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
786 struct sockaddr_in addr_in;
788 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
790 memset(&ifr, 0, sizeof(ifr));
791 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
793 if (ifr.ifr_ifindex < 0)
796 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
798 memset(&rt, 0, sizeof(rt));
800 rt.rt_flags = RTF_UP;
801 memset(&addr_in, 0, sizeof(struct sockaddr_in));
802 addr_in.sin_family = address_family;
803 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
804 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
806 memset(&addr_in, 0, sizeof(struct sockaddr_in));
807 addr_in.sin_family = address_family;
808 addr_in.sin_addr.s_addr = inet_addr(subnet);
809 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
810 rt.rt_dev = ifr.ifr_name;
813 sock = socket(PF_INET, SOCK_DGRAM, 0);
816 DBG("Failed to create socket : %s",
817 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
821 if (ioctl(sock, SIOCDELRT, &rt) < 0) {
822 DBG("Failed to set route address : %s",
823 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
833 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
838 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
840 memset(&rt, 0, sizeof(rt));
842 rt.rtmsg_dst_len = prefix_len;
844 rt.rtmsg_flags = RTF_UP | RTF_HOST;
847 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
848 DBG("inet_pton failed : %s",
849 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
853 if (gateway != NULL && g_strcmp0(gateway, "::") != 0) {
854 rt.rtmsg_flags |= RTF_GATEWAY;
855 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
856 DBG("inet_pton failed : %s",
857 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
864 fd = socket(AF_INET6, SOCK_DGRAM, 0);
866 DBG("Failed to create socket : %s",
867 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
871 rt.rtmsg_ifindex = 0;
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;
881 if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
882 DBG("Failed to add route: %s",
883 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
893 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
899 memset(&rt, 0, sizeof(rt));
901 rt.rtmsg_dst_len = prefix_len;
903 rt.rtmsg_flags = RTF_UP | RTF_HOST;
905 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
910 if (gateway != NULL) {
911 rt.rtmsg_flags |= RTF_GATEWAY;
912 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
920 fd = socket(AF_INET6, SOCK_DGRAM, 0);
924 rt.rtmsg_ifindex = 0;
928 memset(&ifr, 0, sizeof(ifr));
929 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
930 ioctl(fd, SIOCGIFINDEX, &ifr);
931 rt.rtmsg_ifindex = ifr.ifr_ifindex;
934 if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
935 DBG("Failed to del route: %d\n", err);
945 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
947 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
948 wifi_complete_launch_direct(wifi, context);
953 DBG("Launch Wi-Fi direct daemon");
955 const char *path = "/usr/bin/wifi-direct-server.sh";
956 char *const args[] = { "wifi-direct-server.sh", "start", NULL };
957 char *const envs[] = { NULL };
959 ret = netconfig_execute_file(path, args, envs);
961 ERR("Failed to launch Wi-Fi direct daemon");
962 netconfig_error_wifi_direct_failed(context);
966 wifi_complete_launch_direct(wifi, context);
970 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
972 if (!netconfig_plugin_headed_enabled)
978 return headed_plugin->send_notification_to_net_popup(noti, ssid);
981 int netconfig_send_message_to_net_popup(const char *title,
982 const char *content, const char *type, const char *ssid)
984 if (!netconfig_plugin_headed_enabled)
990 return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
993 int netconfig_send_restriction_to_net_popup(const char *title,
994 const char *type, const char *restriction)
996 if (!netconfig_plugin_headed_enabled)
1002 return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
1005 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
1007 if (!netconfig_plugin_headed_enabled)
1013 headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1016 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1018 if (!netconfig_plugin_headed_enabled)
1024 headed_plugin->pop_wifi_connected_poppup(ssid);
1027 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1029 if (!netconfig_plugin_telephony_enabled)
1032 if (!telephony_plugin)
1035 telephony_plugin->get_telephony_network_type(svctype, pstype);
1038 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1040 if (!netconfig_plugin_telephony_enabled)
1043 if (!telephony_plugin)
1046 return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1049 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1050 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1052 if (!netconfig_plugin_telephony_enabled)
1053 return NETCONFIG_ERROR_INTERNAL;
1055 if (!telephony_plugin)
1056 return NETCONFIG_ERROR_INTERNAL;
1058 return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1061 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1062 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1064 if (!netconfig_plugin_telephony_enabled)
1067 if (!telephony_plugin)
1070 return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1073 gboolean netconfig_tapi_check_sim_state(void)
1075 if (!netconfig_plugin_telephony_enabled)
1078 if (!telephony_plugin)
1081 return telephony_plugin->tapi_check_sim_state();
1084 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1085 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1087 if (!netconfig_plugin_telephony_enabled)
1090 if (!telephony_plugin)
1093 return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1096 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1097 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1099 if (!netconfig_plugin_telephony_enabled)
1102 if (!telephony_plugin)
1105 return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1108 static void __netconfig_stc_get_dn_stats(time_t from, time_t to, GSList **list)
1110 if (!netconfig_plugin_stc_enabled)
1116 return stc_plugin->get_stc_dn_stats(from, to, list);
1119 static void __netconfig_stc_get_wifi_stats(time_t from, time_t to, GSList **list)
1121 if (!netconfig_plugin_stc_enabled)
1127 return stc_plugin->get_stc_wifi_stats(from, to, list);
1130 void netconfig_battery_start_dn(void)
1132 if (!netconfig_plugin_battery_enabled)
1135 if (!battery_plugin)
1138 return battery_plugin->start_dn_data();
1141 void netconfig_battery_end_dn(void)
1143 if (!netconfig_plugin_battery_enabled)
1146 if (!battery_plugin)
1149 return battery_plugin->end_dn_data(0, __netconfig_stc_get_dn_stats);
1152 void netconfig_battery_update_dn_rssi(int rssi)
1154 if (!netconfig_plugin_battery_enabled)
1157 if (!battery_plugin)
1160 return battery_plugin->update_dn_rssi(rssi);
1163 void netconfig_battery_start_wifi(void)
1165 if (!netconfig_plugin_battery_enabled)
1168 if (!battery_plugin)
1171 return battery_plugin->start_wifi_data();
1174 void netconfig_battery_end_wifi(void)
1176 if (!netconfig_plugin_battery_enabled)
1179 if (!battery_plugin)
1182 return battery_plugin->end_wifi_data(0, __netconfig_stc_get_wifi_stats);
1185 void netconfig_battery_update_wifi_scan(int state)
1187 if (!netconfig_plugin_battery_enabled)
1190 if (!battery_plugin)
1193 return battery_plugin->update_wifi_scan(state);
1196 void netconfig_battery_update_wifi_rssi(int rssi)
1198 if (!netconfig_plugin_battery_enabled)
1201 if (!battery_plugin)
1204 return battery_plugin->update_wifi_rssi(rssi);
1207 void netconfig_battery_get_dn_list(void *data)
1209 if (!netconfig_plugin_battery_enabled)
1212 if (!battery_plugin)
1215 return battery_plugin->get_battery_dn_list(data, __netconfig_stc_get_dn_stats);
1218 void netconfig_battery_get_wifi_list(void *data)
1220 if (!netconfig_plugin_battery_enabled)
1223 if (!battery_plugin)
1226 return battery_plugin->get_battery_wifi_list(data, __netconfig_stc_get_wifi_stats);
1229 void netconfig_notify_online_state(char *ifname, gboolean online_state)
1231 if (!netconfig_plugin_online_monitor_enabled)
1234 if (!online_monitor_plugin)
1237 online_monitor_plugin->notify_online_state(ifname, online_state);
1240 void netconfig_set_vconf_int(const char *key, int value, gboolean log)
1243 GVariant *params = NULL;
1246 DBG("[%s: %d]", key, value);
1248 ret = vconf_set_int(key, value);
1249 if (ret != VCONF_OK)
1250 ERR("Failed to set");
1252 params = g_variant_new("(sssi)", key, "int", "", value);
1253 if (netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
1254 NETCONFIG_NETWORK_INTERFACE, "VconfChanged",
1256 ERR("Failed to emit VconfChanged signal");
1259 void netconfig_set_vconf_str(const char *key, const char *value, gboolean log)
1262 GVariant *params = NULL;
1265 DBG("[%s: %s]", key, value);
1267 ret = vconf_set_str(key, value);
1268 if (ret != VCONF_OK)
1269 ERR("Failed to set");
1271 params = g_variant_new("(sssi)", key, "string", value, 0);
1272 if (netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
1273 NETCONFIG_NETWORK_INTERFACE, "VconfChanged",
1275 ERR("Failed to emit VconfChanged signal");
1278 int netconfig_vconf_get_int(const char *key, int *value)
1282 ret = vconf_get_int(key, value);
1283 if (ret != VCONF_OK) {
1284 ERR("Failed to get vconfkey [%s] value", key);
1291 int netconfig_vconf_get_bool(const char *key, int *value)
1295 ret = vconf_get_bool(key, value);
1296 if (ret != VCONF_OK) {
1297 ERR("Failed to get vconfkey [%s] value", key);
1304 char *netconfig_vconf_get_str_dbus(const char *key)
1308 char *str_value = NULL;
1310 if (!netconfig_dbus_get_vconf_value(key, "string", &ret, &int_value, &str_value))
1316 int netconfig_vconf_get_int_dbus(const char *key, int *value)
1320 char *str_value = NULL;
1322 if (!netconfig_dbus_get_vconf_value(key, "int", &ret, &int_value, &str_value))
1333 int netconfig_vconf_get_bool_dbus(const char *key, int *value)
1337 char *str_value = NULL;
1339 if (!netconfig_dbus_get_vconf_value(key, "bool", &ret, &int_value, &str_value))
1350 char* netconfig_get_env(const char *key)
1353 char buf[256], *entry = NULL, *value = NULL, *last;
1359 fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1363 while (fgets(buf, sizeof(buf), fp)) {
1365 entry = strtok_r(entry, "=", &last);
1367 if (strstr(entry, key)) {
1368 entry = strtok_r(NULL, "\n", &last);
1370 len = strlen(entry);
1371 value = (char*)malloc(len+1);
1372 g_strlcpy(value, entry, len+1);
1374 value = (char*)malloc(sizeof(char));
1375 g_strlcpy(value, "\n", sizeof(char));
1386 void netconfig_set_mac_address_to_vconf(const char *def_mac)
1389 gchar *mac_lower_str = NULL;
1391 mac_len = strlen(def_mac);
1393 ERR("def_mac is empty");
1397 mac_lower_str = g_ascii_strdown(def_mac, (gssize)mac_len);
1398 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str, TRUE);
1399 g_free(mac_lower_str);
1402 void netconfig_set_mac_address_from_file(void)
1405 char mac_str[MAC_ADDRESS_MAX_LEN];
1406 gchar *mac_lower_str = NULL;
1409 file = fopen(MAC_INFO_FILEPATH, "r");
1411 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1412 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1414 ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1418 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1419 ERR("Fail to read mac address");
1424 mac_len = strlen(mac_str);
1426 ERR("mac.info is empty");
1431 mac_lower_str = g_ascii_strdown(mac_str, (gssize)mac_len);
1432 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str, TRUE);
1434 g_free(mac_lower_str);
1438 char *netconfig_get_mac_address_from_file(const char *ifname)
1441 char file_path[PATH_MAX] = {0, };
1442 char mac_str[MAC_ADDRESS_MAX_LEN];
1448 g_snprintf(file_path, sizeof(file_path),
1449 "/sys/class/net/%s/address", ifname);
1451 DBG("ifname: %s", ifname);
1452 DBG("file_path: %s", file_path);
1453 file = fopen(file_path, "r");
1455 ERR("Fail to open %s", file_path);
1458 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1459 ERR("Fail to read mac address");
1464 mac_len = strlen(mac_str);
1466 ERR("mac is empty");
1470 DBG("address: %s", mac_str);
1473 return g_strdup(mac_str);
1476 char *netconfig_get_default_ifname_from_file(void)
1478 GKeyFile *keyfile = NULL;
1480 keyfile = netconfig_keyfile_load(CONNMAN_MAINFILE);
1481 if (keyfile == NULL) {
1482 ERR("keyfile[%s] is NULL", CONNMAN_MAINFILE);
1486 char *str = g_key_file_get_string(keyfile, "General",
1487 CONNMAN_WIFI_DEF_IFNAME, NULL);
1488 g_key_file_free(keyfile);
1500 if (strlen(str) >= IFNAMSIZ) {
1505 DBG("ifname[%s]", str);
1509 int netconfig_freq_to_channel(int freq)
1512 * 5825 is max frequency for 5 GHz and 7125 is for 6 GHz
1514 if (freq < 2412 || freq > 7125 ||
1515 (freq > 2484 && freq < 5180) ||
1516 (freq > 5825 && freq < 5945)) {
1517 ERR("Invalid Frequence Range");
1522 return 189 + (freq - 5945)/5;
1523 else if (freq >= 5180)
1524 return 36 + (freq - 5180)/5;
1525 else if (freq <= 2472)
1526 return 1 + (freq - 2412)/5;
1527 else if (freq == 2484)
1533 int netconfig_get_operating_class(int freq)
1538 channel = netconfig_freq_to_channel(freq);
1541 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1542 if (channel >= 1 && channel <= 13)
1544 /* Operating class 115 - 5 GHz, channels 36-48 */
1545 else if (channel >= 36 && channel <= 48)
1547 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1548 else if (channel >= 149 && channel <= 161)
1550 /* Operating class - 6 GHz, channels 189, 193, 197, ... 425 */
1551 else if (channel >= 189 && channel <= 425)
1554 INFO("Operating Class is [%d]", oper_class);
1560 tizen_profile_t _get_tizen_profile()
1562 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1563 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1567 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1568 switch (*profileName) {
1571 profile = TIZEN_PROFILE_MOBILE;
1575 profile = TIZEN_PROFILE_WEARABLE;
1579 profile = TIZEN_PROFILE_TV;
1583 profile = TIZEN_PROFILE_IVI;
1585 default: // common or unknown ==> ALL ARE COMMON.
1586 profile = TIZEN_PROFILE_COMMON;
1593 void netconfig_plugin_init()
1595 handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1596 if (!handle_headed) {
1597 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1599 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1600 if (!headed_plugin) {
1601 ERR("Can't load symbol: %s", dlerror());
1602 dlclose(handle_headed);
1604 netconfig_plugin_headed_enabled = TRUE;
1608 handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1609 if (!handle_telephony) {
1610 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1612 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1613 if (!telephony_plugin) {
1614 ERR("Can't load symbol: %s", dlerror());
1615 dlclose(handle_telephony);
1617 netconfig_plugin_telephony_enabled = TRUE;
1621 handle_stc = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
1623 ERR("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
1625 stc_plugin = dlsym(handle_stc, "netconfig_stc_plugin");
1627 ERR("Can't load symbol: %s", dlerror());
1628 dlclose(handle_stc);
1630 netconfig_plugin_stc_enabled = TRUE;
1634 handle_battery = dlopen(BATTERY_PLUGIN_FILEPATH, RTLD_NOW);
1635 if (!handle_battery) {
1636 ERR("Can't load %s: %s", BATTERY_PLUGIN_FILEPATH, dlerror());
1638 battery_plugin = dlsym(handle_battery, "netconfig_battery_plugin");
1639 if (!battery_plugin) {
1640 ERR("Can't load symbol: %s", dlerror());
1641 dlclose(handle_battery);
1643 netconfig_plugin_battery_enabled = TRUE;
1647 handle_online_monitor = dlopen(ONLINE_MONITOR_PLUGIN_FILEPATH, RTLD_NOW);
1648 if (!handle_online_monitor) {
1649 ERR("Can't load %s: %s", ONLINE_MONITOR_PLUGIN_FILEPATH, dlerror());
1651 online_monitor_plugin = dlsym(handle_online_monitor, "netconfig_online_monitor_plugin");
1652 if (!online_monitor_plugin) {
1653 ERR("Can't load symbol: %s", dlerror());
1654 dlclose(handle_online_monitor);
1656 if (online_monitor_plugin->init() == 0)
1657 netconfig_plugin_online_monitor_enabled = TRUE;
1662 void netconfig_plugin_deinit()
1664 if (netconfig_plugin_headed_enabled) {
1665 netconfig_plugin_headed_enabled = FALSE;
1666 dlclose(handle_headed);
1669 if (netconfig_plugin_telephony_enabled) {
1670 netconfig_plugin_telephony_enabled = FALSE;
1671 dlclose(handle_telephony);
1674 if (netconfig_plugin_stc_enabled) {
1675 netconfig_plugin_stc_enabled = FALSE;
1676 dlclose(handle_stc);
1679 if (netconfig_plugin_battery_enabled) {
1680 netconfig_plugin_battery_enabled = FALSE;
1681 dlclose(handle_battery);
1684 if (netconfig_plugin_online_monitor_enabled) {
1685 netconfig_plugin_online_monitor_enabled = FALSE;
1686 if (online_monitor_plugin)
1687 online_monitor_plugin->deinit();
1688 dlclose(handle_online_monitor);
1692 gboolean netconfig_get_headed_plugin_flag()
1694 return netconfig_plugin_headed_enabled;
1697 gboolean netconfig_get_telephony_plugin_flag()
1699 return netconfig_plugin_telephony_enabled;
1702 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1704 const char *key = NULL;
1706 if (!is_feature_checked[feature]) {
1708 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1709 key = ETHERNET_FEATURE;
1711 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1712 key = TETHERING_FEATURE;
1714 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1715 key = WIFI_DIRECT_FEATURE;
1717 case NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP:
1718 key = WIFI_SOFTAP_FEATURE;
1721 ERR("Uknown feature");
1725 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1726 ERR("Get feature is failed");
1729 is_feature_checked[feature] = true;
1731 return feature_supported[feature];
1734 void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr)
1739 t = (*bin >> 4) & 0x0f;
1743 else if (t >= 10 && t <= 16)
1744 *hexstr = (t - 10) + 'a';
1752 else if (t >= 10 && t <= 16)
1753 *hexstr = (t - 10) + 'a';
1763 bool __is_hidden_file(const char *file)
1765 /* exclude "." , "..", "settings" and hidden files */
1766 if( g_strcmp0( file, "." ) == 0 ||
1767 g_strcmp0( file, ".." ) == 0 || file[0]=='.' || g_strcmp0(file, "settings")==0 )
1775 int get_files_count(const char *path)
1778 struct dirent *dir = NULL;
1780 dfd = opendir(path);
1782 while ((dir = readdir(dfd)) != NULL) {
1783 if (__is_hidden_file(dir->d_name))
1786 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1797 char * get_least_recently_profile(const char *path)
1800 struct dirent *dir = NULL;
1801 unsigned long lastModified = (unsigned long)~0;
1803 dfd = opendir(path);
1805 while ((dir = readdir(dfd)) != NULL) {
1806 if (__is_hidden_file(dir->d_name))
1809 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1813 gchar *full_path = g_strdup_printf("%s/%s", path, dir->d_name);
1814 if (stat(full_path, &attr)== 0) {
1815 if(lastModified > attr.st_mtime)
1817 lastModified = attr.st_mtime;
1828 DBG("least recently path: [%s]", file);