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"
49 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
50 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
51 #define MAC_INFO_FILEPATH tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
52 #define MAC_ADDRESS_FILEPATH "/sys/class/net/wlan0/address"
53 #define HEADED_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-headed.so"
54 #define TELEPHONY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-telephony.so"
55 #define STC_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-stc.so"
56 #define BATTERY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-battery.so"
58 static gboolean netconfig_device_picker_test = FALSE;
59 static int mdnsd_ref_count = 0;
63 } dnssd_conn_destroy_data;
65 static gboolean netconfig_plugin_headed_enabled = FALSE;
66 static gboolean netconfig_plugin_telephony_enabled = FALSE;
67 static gboolean netconfig_plugin_stc_enabled = FALSE;
68 static gboolean netconfig_plugin_battery_enabled = FALSE;
69 static void *handle_headed;
70 static void *handle_telephony;
71 static void *handle_stc;
72 static void *handle_battery;
73 static struct netconfig_headed_plugin_t *headed_plugin;
74 static struct netconfig_telephony_plugin_t *telephony_plugin;
75 static struct netconfig_stc_plugin_t *stc_plugin;
76 static struct netconfig_battery_plugin_t *battery_plugin;
78 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
79 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
81 gboolean netconfig_check_mac_address(const char *service, const char *mac_address)
83 int service_index = 0;
85 const char *org = NULL;
86 const char *dst = NULL;
89 if (g_str_has_prefix(service, "wifi_") == FALSE)
92 service_index = strlen("wifi_");
93 for (i = 0; i < 6; i++) {
94 org = &service[service_index];
95 dst = &mac_address[mac_index];
97 if (g_ascii_strncasecmp(org, dst, 2) != 0)
107 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
114 length = strlen(passphrase);
116 if (g_str_has_suffix(service, "psk") == TRUE) {
118 for (int i = 0; i < 64; i++)
119 if (!isxdigit((unsigned char)passphrase[i]))
121 } else if (length < 8 || length > 63)
123 } else if (g_str_has_suffix(service, "wep") == TRUE) {
124 if (length == 10 || length == 26) {
125 for (int i = 0; i < length; i++)
126 if (!isxdigit((unsigned char)passphrase[i]))
128 } else if (length != 5 && length != 13)
135 GKeyFile *netconfig_keyfile_load(const char *pathname)
137 GKeyFile *keyfile = NULL;
138 GError *error = NULL;
140 keyfile = g_key_file_new();
141 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
142 DBG("Unable to open %s, error %s", pathname, error->message);
145 g_key_file_free(keyfile);
149 DBG("loaded keyfile %s", pathname);
153 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
156 GError *error = NULL;
157 gchar *keydata = NULL;
158 gchar *needle = NULL, *directory = NULL;
160 directory = g_strdup(pathname);
161 needle = g_strrstr(directory, "/");
166 if (directory == NULL || (*directory) == '\0') {
168 ERR("directory is NULL");
172 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
173 if (g_mkdir_with_parents(directory,
174 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
176 ERR("failed to make directory");
182 keydata = g_key_file_to_data(keyfile, &size, &error);
183 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
184 ERR("Unable to save %s, error %s", pathname, error->message);
188 if (chmod(pathname, S_IRUSR | S_IWUSR) < 0)
189 DBG("Failed to change mode");
191 DBG("Successfully saved keyfile %s", pathname);
196 void netconfig_start_timer_seconds(guint secs,
197 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
201 if (callback == NULL) {
202 ERR("callback function is NULL");
206 if ((timer_id != NULL && *timer_id != 0)) {
207 ERR("timer already is registered");
211 t_id = g_timeout_add_seconds(secs, callback, user_data);
214 ERR("Can't add timer");
218 if (timer_id != NULL)
222 void netconfig_start_timer(guint msecs,
223 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
227 INFO("Register timer with callback pointer (%p)", callback);
229 if (callback == NULL) {
230 ERR("callback function is NULL");
234 if ((timer_id != NULL && *timer_id != 0)) {
235 ERR("timer already is registered");
239 t_id = g_timeout_add(msecs, callback, user_data);
242 ERR("Can't add timer");
246 if (timer_id != NULL)
250 void netconfig_stop_timer(guint *timer_id)
252 if (timer_id == NULL) {
253 ERR("timer is NULL");
257 if (*timer_id != 0) {
258 g_source_remove(*timer_id);
263 static gboolean __netconfig_test_device_picker()
265 char *favorite_wifi_service = NULL;
267 favorite_wifi_service = wifi_get_favorite_service();
268 if (favorite_wifi_service != NULL) {
269 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
270 g_free(favorite_wifi_service);
277 static void __netconfig_pop_device_picker(void)
279 if (!netconfig_plugin_headed_enabled)
285 headed_plugin->pop_device_picker();
288 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
290 if (__netconfig_test_device_picker() == TRUE)
291 __netconfig_pop_device_picker();
296 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
298 static guint netconfig_wifi_device_picker_service_timer = 0;
300 if (is_set_method != TRUE)
301 return netconfig_wifi_device_picker_service_timer;
303 if (netconfig_wifi_device_picker_service_timer != timer_id)
304 netconfig_wifi_device_picker_service_timer = timer_id;
306 return netconfig_wifi_device_picker_service_timer;
309 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
311 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
314 static guint __netconfig_wifi_device_picker_get_timer_id(void)
316 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
319 void netconfig_wifi_enable_device_picker_test(void)
321 netconfig_device_picker_test = TRUE;
324 void netconfig_wifi_device_picker_service_start(void)
326 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
329 if (netconfig_device_picker_test == TRUE)
330 netconfig_device_picker_test = FALSE;
336 netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
337 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
340 DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
341 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
343 __netconfig_wifi_device_picker_set_timer_id(timer_id);
346 void netconfig_wifi_device_picker_service_stop(void)
350 timer_id = __netconfig_wifi_device_picker_get_timer_id();
354 DBG("Clear device picker timer with timer_id %d", timer_id);
356 netconfig_stop_timer(&timer_id);
358 __netconfig_wifi_device_picker_set_timer_id(timer_id);
361 gboolean netconfig_is_wifi_direct_on(void)
363 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
366 int wifi_direct_state = 0;
368 netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
370 DBG("Wi-Fi direct mode %d", wifi_direct_state);
371 return (wifi_direct_state != 0) ? TRUE : FALSE;
374 gboolean netconfig_is_wifi_tethering_on(void)
376 if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
377 int wifi_tethering_state = 0;
379 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
380 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
381 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
382 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
383 DBG("Mobile AP is on");
388 DBG("Mobile AP is off");
392 gboolean netconfig_interface_up(const char *ifname)
397 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
399 ERR("socket failed %d", errno);
403 memset(&ifr, 0, sizeof(ifr));
404 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
406 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
407 ERR("Fail to get IFFLAGS %d", errno);
412 DBG("IFFLAGS: %x", ifr.ifr_flags);
413 ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
414 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
415 ERR("Fail to set IFFLAGS %d", errno);
422 DBG("Successfully activated wireless interface %s", ifname);
426 gboolean netconfig_interface_down(const char *ifname)
431 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
433 ERR("socket failed %d", errno);
437 memset(&ifr, 0, sizeof(ifr));
438 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
440 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
441 ERR("Fail to get IFFLAGS %d", errno);
446 DBG("IFFLAGS: %x", ifr.ifr_flags);
447 ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
448 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
449 ERR("Fail to set IFFLAGS %d", errno);
456 DBG("Successfully de-activated wireless interface %s", ifname);
460 int netconfig_execute_file(const char *file_path,
461 char *const args[], char *const envs[])
467 register unsigned int index = 0;
468 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
470 while (args[index] != NULL) {
471 DBG("%s", args[index]);
475 if (!(pid = fork())) {
476 DBG("pid(%d), ppid (%d)", getpid(), getppid());
477 DBG("Inside child, exec (%s) command", file_path);
480 if (execve(file_path, args, envs) == -1) {
481 DBG("Fail to execute command (%s)",
482 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
485 } else if (pid > 0) {
486 if (waitpid(pid, &status, 0) == -1)
487 DBG("wait pid (%u) status (%d)", pid, status);
489 if (WIFEXITED(status)) {
490 rv = WEXITSTATUS(status);
491 DBG("exited, status=%d", rv);
492 } else if (WIFSIGNALED(status)) {
493 DBG("killed by signal %d", WTERMSIG(status));
494 } else if (WIFSTOPPED(status)) {
495 DBG("stopped by signal %d", WSTOPSIG(status));
496 } else if (WIFCONTINUED(status)) {
503 DBG("failed to fork(%s)",
504 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
508 int netconfig_execute_cmd(const char *cmd)
517 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
520 DBG("command: %s", cmd);
522 args = g_strsplit_set(cmd, " ", -1);
524 if (!(pid = fork())) {
525 DBG("pid(%d), ppid (%d)", getpid(), getppid());
528 if (execv(args[0], args) == -1) {
529 DBG("Fail to execute command (%s)",
530 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
534 } else if (pid > 0) {
535 if (waitpid(pid, &status, 0) == -1)
536 DBG("wait pid (%u) status (%d)", pid, status);
538 if (WIFEXITED(status)) {
539 rv = WEXITSTATUS(status);
540 DBG("exited, status=%d", rv);
541 } else if (WIFSIGNALED(status)) {
542 DBG("killed by signal %d", WTERMSIG(status));
543 } else if (WIFSTOPPED(status)) {
544 DBG("stopped by signal %d", WSTOPSIG(status));
545 } else if (WIFCONTINUED(status)) {
553 DBG("failed to fork(%s)",
554 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
560 static void on_clat_handler()
565 clat_pid = waitpid(-1, &state, WNOHANG);
567 DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
570 int netconfig_execute_clatd(const char *file_path, char *const args[])
575 register unsigned int index = 0;
576 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
578 struct sigaction act;
581 act.sa_handler = on_clat_handler;
582 sigemptyset(&act.sa_mask);
585 state = sigaction(SIGCHLD, &act, 0);
587 DBG("sigaction() : %d", state);
591 while (args[index] != NULL) {
592 DBG("%s", args[index]);
596 if (!(pid = fork())) {
597 DBG("pid(%d), ppid (%d)", getpid(), getppid());
598 DBG("Inside child, exec (%s) command", file_path);
601 if (execvp(file_path, args) == -1) {
602 ERR("Fail to execute command (%s)",
603 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
606 } else if (pid > 0) {
607 ERR("Success to launch clatd");
611 DBG("failed to fork(%s)",
612 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
616 static void no_wait_signal_handler()
621 child_pid = waitpid(-1, &state, WNOHANG);
623 DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
626 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
631 register unsigned int index = 0;
633 struct sigaction act;
635 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
637 act.sa_handler = no_wait_signal_handler;
638 sigemptyset(&act.sa_mask);
641 state = sigaction(SIGCHLD, &act, 0);
643 DBG("sigaction() : %d", state);
647 while (args[index] != NULL) {
648 DBG("%s", args[index]);
652 if (!(pid = fork())) {
653 DBG("pid(%d), ppid (%d)", getpid(), getppid());
654 DBG("Inside child, exec (%s) command", file_path);
657 if (execvp(file_path, args) == -1) {
658 ERR("Fail to execute command (%s)",
659 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
662 } else if (pid > 0) {
663 ERR("Successfully launched child process");
667 DBG("failed to fork(%s)",
668 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
672 int __netconfig_get_interface_index(const char *interface_name)
677 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
679 if (interface_name == NULL) {
680 DBG("Inteface name is NULL");
685 sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
687 DBG("Failed to create socket : %s",
688 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
692 memset(&ifr, 0, sizeof(ifr));
693 strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
694 result = ioctl(sock, SIOCGIFINDEX, &ifr);
698 DBG("Failed to get ifr index: %s",
699 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
703 return ifr.ifr_ifindex;
706 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
710 struct sockaddr_in addr_in;
712 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
714 memset(&ifr, 0, sizeof(ifr));
716 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
718 if (ifr.ifr_ifindex < 0)
721 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
723 memset(&rt, 0, sizeof(rt));
725 rt.rt_flags = RTF_UP | RTF_HOST;
726 memset(&addr_in, 0, sizeof(struct sockaddr_in));
727 addr_in.sin_family = address_family;
728 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
729 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
731 memset(&addr_in, 0, sizeof(struct sockaddr_in));
732 addr_in.sin_family = address_family;
733 addr_in.sin_addr.s_addr = INADDR_ANY;
734 memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
736 memset(&addr_in, 0, sizeof(struct sockaddr_in));
737 addr_in.sin_family = AF_INET;
738 addr_in.sin_addr.s_addr = inet_addr(subnet);
739 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
741 rt.rt_dev = ifr.ifr_name;
744 sock = socket(PF_INET, SOCK_DGRAM, 0);
747 DBG("Failed to create socket : %s",
748 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
752 if (ioctl(sock, SIOCADDRT, &rt) < 0) {
753 DBG("Failed to set route address : %s",
754 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
764 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
768 struct sockaddr_in addr_in;
770 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
772 memset(&ifr, 0, sizeof(ifr));
773 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
775 if (ifr.ifr_ifindex < 0)
778 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
780 memset(&rt, 0, sizeof(rt));
782 rt.rt_flags = RTF_UP;
783 memset(&addr_in, 0, sizeof(struct sockaddr_in));
784 addr_in.sin_family = address_family;
785 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
786 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
788 memset(&addr_in, 0, sizeof(struct sockaddr_in));
789 addr_in.sin_family = address_family;
790 addr_in.sin_addr.s_addr = inet_addr(subnet);
791 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
792 rt.rt_dev = ifr.ifr_name;
795 sock = socket(PF_INET, SOCK_DGRAM, 0);
798 DBG("Failed to create socket : %s",
799 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
803 if (ioctl(sock, SIOCDELRT, &rt) < 0) {
804 DBG("Failed to set route address : %s",
805 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
815 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
820 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
822 memset(&rt, 0, sizeof(rt));
824 rt.rtmsg_dst_len = prefix_len;
826 rt.rtmsg_flags = RTF_UP | RTF_HOST;
829 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
830 DBG("inet_pton failed : %s",
831 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
835 if (gateway != NULL) {
836 rt.rtmsg_flags |= RTF_GATEWAY;
837 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
838 DBG("inet_pton failed : %s",
839 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
846 fd = socket(AF_INET6, SOCK_DGRAM, 0);
848 DBG("Failed to create socket : %s",
849 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
853 rt.rtmsg_ifindex = 0;
857 memset(&ifr, 0, sizeof(ifr));
858 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
859 ioctl(fd, SIOCGIFINDEX, &ifr);
860 rt.rtmsg_ifindex = ifr.ifr_ifindex;
863 if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
864 DBG("Failed to add route: %s",
865 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
875 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
881 memset(&rt, 0, sizeof(rt));
883 rt.rtmsg_dst_len = prefix_len;
885 rt.rtmsg_flags = RTF_UP | RTF_HOST;
887 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
892 if (gateway != NULL) {
893 rt.rtmsg_flags |= RTF_GATEWAY;
894 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
902 fd = socket(AF_INET6, SOCK_DGRAM, 0);
906 rt.rtmsg_ifindex = 0;
910 memset(&ifr, 0, sizeof(ifr));
911 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
912 ioctl(fd, SIOCGIFINDEX, &ifr);
913 rt.rtmsg_ifindex = ifr.ifr_ifindex;
916 if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
917 DBG("Failed to del route: %d\n", err);
927 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
929 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
930 wifi_complete_launch_direct(wifi, context);
935 DBG("Launch Wi-Fi direct daemon");
937 const char *path = "/usr/bin/wifi-direct-server.sh";
938 char *const args[] = { "wifi-direct-server.sh", "start", NULL };
939 char *const envs[] = { NULL };
941 ret = netconfig_execute_file(path, args, envs);
943 ERR("Failed to launch Wi-Fi direct daemon");
944 netconfig_error_wifi_direct_failed(context);
948 wifi_complete_launch_direct(wifi, context);
952 int execute_mdnsd_script(char* op)
954 const char *path = "/usr/bin/mdnsresponder-server.sh";
955 char *const args[] = { "mdnsresponder-server.sh", op, NULL };
956 char *const envs[] = { NULL };
958 return netconfig_execute_file(path, args, envs);
961 static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
962 const gchar *Name, const gchar *path, const gchar *interface,
963 const gchar *sig, GVariant *param, gpointer user_data)
968 dnssd_conn_destroy_data *data = user_data;
969 GDBusConnection *connection = NULL;
970 connection = netdbus_get_connection();
975 g_variant_get(param, "(sss)", &name, &old, &new);
977 if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
978 DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
981 g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
982 if (mdnsd_ref_count == 0) {
983 if (execute_mdnsd_script("stop") < 0)
984 ERR("Failed to stop mdnsresponder daemon");
990 g_free(data->conn_name);
995 static void register_dnssd_conn_destroy_signal(gchar *name)
997 dnssd_conn_destroy_data *data;
998 GDBusConnection *connection = NULL;
999 connection = netdbus_get_connection();
1001 if (connection == NULL) {
1002 ERR("Failed to get GDbus Connection");
1006 data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
1009 ERR("Out of Memory!");
1013 data->conn_name = g_strdup(name);
1015 data->conn_id = g_dbus_connection_signal_subscribe(connection,
1016 DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
1017 "NameOwnerChanged", NULL, name,
1018 G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
1023 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
1026 DBG("Launch mdnsresponder daemon");
1028 if (execute_mdnsd_script("start") < 0) {
1029 ERR("Failed to launch mdnsresponder daemon");
1030 netconfig_error_invalid_parameter(context);
1035 register_dnssd_conn_destroy_signal(name);
1036 DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
1038 network_complete_launch_mdns(object, context);
1042 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
1044 if (!netconfig_plugin_headed_enabled)
1050 return headed_plugin->send_notification_to_net_popup(noti, ssid);
1053 int netconfig_send_message_to_net_popup(const char *title,
1054 const char *content, const char *type, const char *ssid)
1056 if (!netconfig_plugin_headed_enabled)
1062 return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
1065 int netconfig_send_restriction_to_net_popup(const char *title,
1066 const char *type, const char *restriction)
1068 if (!netconfig_plugin_headed_enabled)
1074 return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
1077 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
1079 if (!netconfig_plugin_headed_enabled)
1085 headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1088 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1090 if (!netconfig_plugin_headed_enabled)
1096 headed_plugin->pop_wifi_connected_poppup(ssid);
1099 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1101 if (!netconfig_plugin_telephony_enabled)
1104 if (!telephony_plugin)
1107 telephony_plugin->get_telephony_network_type(svctype, pstype);
1110 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1112 if (!netconfig_plugin_telephony_enabled)
1115 if (!telephony_plugin)
1118 return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1121 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1122 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1124 if (!netconfig_plugin_telephony_enabled)
1125 return NETCONFIG_ERROR_INTERNAL;
1127 if (!telephony_plugin)
1128 return NETCONFIG_ERROR_INTERNAL;
1130 return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1133 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1134 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1136 if (!netconfig_plugin_telephony_enabled)
1139 if (!telephony_plugin)
1142 return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1145 gboolean netconfig_tapi_check_sim_state(void)
1147 if (!netconfig_plugin_telephony_enabled)
1150 if (!telephony_plugin)
1153 return telephony_plugin->tapi_check_sim_state();
1156 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1157 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1159 if (!netconfig_plugin_telephony_enabled)
1162 if (!telephony_plugin)
1165 return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1168 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1169 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1171 if (!netconfig_plugin_telephony_enabled)
1174 if (!telephony_plugin)
1177 return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1180 static void __netconfig_stc_get_dn_stats(time_t from, time_t to, GSList **list)
1182 if (!netconfig_plugin_stc_enabled)
1188 return stc_plugin->get_stc_dn_stats(from, to, list);
1191 static void __netconfig_stc_get_wifi_stats(time_t from, time_t to, GSList **list)
1193 if (!netconfig_plugin_stc_enabled)
1199 return stc_plugin->get_stc_wifi_stats(from, to, list);
1202 void netconfig_battery_start_dn(void)
1204 if (!netconfig_plugin_battery_enabled)
1207 if (!battery_plugin)
1210 return battery_plugin->start_dn_data();
1213 void netconfig_battery_end_dn(void)
1215 if (!netconfig_plugin_battery_enabled)
1218 if (!battery_plugin)
1221 return battery_plugin->end_dn_data(0, __netconfig_stc_get_dn_stats);
1224 void netconfig_battery_update_dn_rssi(int rssi)
1226 if (!netconfig_plugin_battery_enabled)
1229 if (!battery_plugin)
1232 return battery_plugin->update_dn_rssi(rssi);
1235 void netconfig_battery_start_wifi(void)
1237 if (!netconfig_plugin_battery_enabled)
1240 if (!battery_plugin)
1243 return battery_plugin->start_wifi_data();
1246 void netconfig_battery_end_wifi(void)
1248 if (!netconfig_plugin_battery_enabled)
1251 if (!battery_plugin)
1254 return battery_plugin->end_wifi_data(0, __netconfig_stc_get_wifi_stats);
1257 void netconfig_battery_update_wifi_scan(int state)
1259 if (!netconfig_plugin_battery_enabled)
1262 if (!battery_plugin)
1265 return battery_plugin->update_wifi_scan(state);
1268 void netconfig_battery_update_wifi_rssi(int rssi)
1270 if (!netconfig_plugin_battery_enabled)
1273 if (!battery_plugin)
1276 return battery_plugin->update_wifi_rssi(rssi);
1279 void netconfig_battery_get_dn_list(void *data)
1281 if (!netconfig_plugin_battery_enabled)
1284 if (!battery_plugin)
1287 return battery_plugin->get_battery_dn_list(data, __netconfig_stc_get_dn_stats);
1290 void netconfig_battery_get_wifi_list(void *data)
1292 if (!netconfig_plugin_battery_enabled)
1295 if (!battery_plugin)
1298 return battery_plugin->get_battery_wifi_list(data, __netconfig_stc_get_wifi_stats);
1301 void netconfig_set_vconf_int(const char * key, int value)
1305 if (g_strcmp0(key, VCONFKEY_WIFI_TRANSFER_STATE) != 0)
1306 DBG("[%s: %d]", key, value);
1308 ret = vconf_set_int(key, value);
1309 if (ret != VCONF_OK)
1310 ERR("Failed to set");
1313 void netconfig_set_vconf_str(const char * key, const char * value)
1317 DBG("[%s: %s]", key, value);
1319 ret = vconf_set_str(key, value);
1320 if (ret != VCONF_OK)
1321 ERR("Failed to set");
1324 int netconfig_vconf_get_int(const char * key, int *value)
1328 ret = vconf_get_int(key, value);
1329 if (ret != VCONF_OK) {
1330 ERR("Failed to get vconfkey [%s] value", key);
1337 int netconfig_vconf_get_bool(const char * key, int *value)
1341 ret = vconf_get_bool(key, value);
1342 if (ret != VCONF_OK) {
1343 ERR("Failed to get vconfkey [%s] value", key);
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_from_file(void)
1389 char mac_str[MAC_ADDRESS_MAX_LEN];
1390 gchar *mac_lower_str = NULL;
1393 file = fopen(MAC_INFO_FILEPATH, "r");
1395 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1396 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1398 ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1402 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1403 ERR("Fail to read mac address");
1408 mac_len = strlen(mac_str);
1410 ERR("mac.info is empty");
1415 mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1416 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1418 g_free(mac_lower_str);
1422 char *netconfig_get_mac_address_from_file(const char *ifname)
1425 char file_path[PATH_MAX] = {0, };
1426 char mac_str[MAC_ADDRESS_MAX_LEN];
1432 g_snprintf(file_path, sizeof(file_path),
1433 "/sys/class/net/%s/address", ifname);
1435 DBG("ifname: %s", ifname);
1436 DBG("file_path: %s", file_path);
1437 file = fopen(file_path, "r");
1439 ERR("Fail to open %s", file_path);
1442 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1443 ERR("Fail to read mac address");
1448 mac_len = strlen(mac_str);
1450 ERR("mac is empty");
1454 DBG("address: %s", mac_str);
1457 return g_strdup(mac_str);
1460 int netconfig_freq_to_channel(int freq)
1462 if (freq < 2412 || freq > 5825 ||
1463 (freq > 2484 && freq < 5180)) {
1464 ERR("Invalid Frequence Range");
1468 return 36 + (freq - 5180)/5;
1469 else if (freq <= 2472)
1470 return 1 + (freq - 2412)/5;
1471 else if (freq == 2484)
1477 int netconfig_get_operating_class(int freq)
1482 channel = netconfig_freq_to_channel(freq);
1485 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1486 if (channel >= 1 && channel <= 13)
1488 /* Operating class 115 - 5 GHz, channels 36-48 */
1489 else if (channel >= 36 && channel <= 48)
1491 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1495 INFO("Operating Class is [%d]", oper_class);
1501 tizen_profile_t _get_tizen_profile()
1503 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1504 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1508 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1509 switch (*profileName) {
1512 profile = TIZEN_PROFILE_MOBILE;
1516 profile = TIZEN_PROFILE_WEARABLE;
1520 profile = TIZEN_PROFILE_TV;
1524 profile = TIZEN_PROFILE_IVI;
1526 default: // common or unknown ==> ALL ARE COMMON.
1527 profile = TIZEN_PROFILE_COMMON;
1534 void netconfig_plugin_init()
1536 handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1537 if (!handle_headed) {
1538 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1540 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1541 if (!headed_plugin) {
1542 ERR("Can't load symbol: %s", dlerror());
1543 dlclose(handle_headed);
1545 netconfig_plugin_headed_enabled = TRUE;
1549 handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1550 if (!handle_telephony) {
1551 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1553 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1554 if (!telephony_plugin) {
1555 ERR("Can't load symbol: %s", dlerror());
1556 dlclose(handle_telephony);
1558 netconfig_plugin_telephony_enabled = TRUE;
1562 handle_stc = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
1564 ERR("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
1566 stc_plugin = dlsym(handle_stc, "netconfig_stc_plugin");
1568 ERR("Can't load symbol: %s", dlerror());
1569 dlclose(handle_stc);
1571 netconfig_plugin_stc_enabled = TRUE;
1575 handle_battery = dlopen(BATTERY_PLUGIN_FILEPATH, RTLD_NOW);
1576 if (!handle_battery) {
1577 ERR("Can't load %s: %s", BATTERY_PLUGIN_FILEPATH, dlerror());
1579 battery_plugin = dlsym(handle_battery, "netconfig_battery_plugin");
1580 if (!battery_plugin) {
1581 ERR("Can't load symbol: %s", dlerror());
1582 dlclose(handle_battery);
1584 netconfig_plugin_battery_enabled = TRUE;
1590 void netconfig_plugin_deinit()
1592 if (netconfig_plugin_headed_enabled) {
1593 netconfig_plugin_headed_enabled = FALSE;
1594 dlclose(handle_headed);
1597 if (netconfig_plugin_telephony_enabled) {
1598 netconfig_plugin_telephony_enabled = FALSE;
1599 dlclose(handle_telephony);
1602 if (netconfig_plugin_stc_enabled) {
1603 netconfig_plugin_stc_enabled = FALSE;
1604 dlclose(handle_stc);
1607 if (netconfig_plugin_battery_enabled) {
1608 netconfig_plugin_battery_enabled = FALSE;
1609 dlclose(handle_battery);
1614 gboolean netconfig_get_headed_plugin_flag()
1616 return netconfig_plugin_headed_enabled;
1619 gboolean netconfig_get_telephony_plugin_flag()
1621 return netconfig_plugin_telephony_enabled;
1624 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1626 const char *key = NULL;
1628 if (!is_feature_checked[feature]) {
1630 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1631 key = ETHERNET_FEATURE;
1633 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1634 key = TETHERING_FEATURE;
1636 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1637 key = WIFI_DIRECT_FEATURE;
1639 case NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP:
1640 key = WIFI_SOFTAP_FEATURE;
1643 ERR("Uknown feature");
1647 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1648 ERR("Get feature is failed");
1651 is_feature_checked[feature] = true;
1653 return feature_supported[feature];
1656 void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr)
1661 t = (*bin >> 4) & 0x0f;
1665 else if (t >= 10 && t <= 16)
1666 *hexstr = (t - 10) + 'a';
1674 else if (t >= 10 && t <= 16)
1675 *hexstr = (t - 10) + 'a';
1685 bool __is_hidden_file(const char *file)
1687 /* exclude "." , "..", "settings" and hidden files */
1688 if( g_strcmp0( file, "." ) == 0 ||
1689 g_strcmp0( file, ".." ) == 0 || file[0]=='.' || g_strcmp0(file, "settings")==0 )
1697 int get_files_count(const char *path)
1700 struct dirent *dir = NULL;
1702 dfd = opendir(path);
1704 while ((dir = readdir(dfd)) != NULL) {
1705 if (__is_hidden_file(dir->d_name))
1708 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1719 char * get_least_recently_profile(const char *path)
1722 struct dirent *dir = NULL;
1723 unsigned long lastModified = (unsigned long)~0;
1725 dfd = opendir(path);
1727 while ((dir = readdir(dfd)) != NULL) {
1728 if (__is_hidden_file(dir->d_name))
1731 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1735 gchar *full_path = g_strdup_printf("%s/%s", path, dir->d_name);
1736 if (stat(full_path, &attr)== 0) {
1737 if(lastModified > attr.st_mtime)
1739 lastModified = attr.st_mtime;
1750 DBG("least recently path: [%s]", file);