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>
42 #include "wifi-state.h"
45 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
46 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
47 #define MAC_INFO_FILEPATH tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
48 #define MAC_ADDRESS_FILEPATH "/sys/class/net/wlan0/address"
49 #define MAC_ADDRESS_MAX_LEN 18
50 #define HEADED_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-headed.so"
51 #define TELEPHONY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-telephony.so"
52 #define STC_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-stc.so"
53 #define BATTERY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-battery.so"
55 static gboolean netconfig_device_picker_test = FALSE;
56 static int mdnsd_ref_count = 0;
60 } dnssd_conn_destroy_data;
62 static gboolean netconfig_plugin_headed_enabled = FALSE;
63 static gboolean netconfig_plugin_telephony_enabled = FALSE;
64 static gboolean netconfig_plugin_stc_enabled = FALSE;
65 static gboolean netconfig_plugin_battery_enabled = FALSE;
66 static void *handle_headed;
67 static void *handle_telephony;
68 static void *handle_stc;
69 static void *handle_battery;
70 static struct netconfig_headed_plugin_t *headed_plugin;
71 static struct netconfig_telephony_plugin_t *telephony_plugin;
72 static struct netconfig_stc_plugin_t *stc_plugin;
73 static struct netconfig_battery_plugin_t *battery_plugin;
75 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
76 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
78 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
85 length = strlen(passphrase);
87 if (g_str_has_suffix(service, "psk") == TRUE) {
89 for (int i = 0; i < 64; i++)
90 if (!isxdigit((unsigned char)passphrase[i]))
92 } else if (length < 8 || length > 63)
94 } else if (g_str_has_suffix(service, "wep") == TRUE) {
95 if (length == 10 || length == 26) {
96 for (int i = 0; i < length; i++)
97 if (!isxdigit((unsigned char)passphrase[i]))
99 } else if (length != 5 && length != 13)
106 GKeyFile *netconfig_keyfile_load(const char *pathname)
108 GKeyFile *keyfile = NULL;
109 GError *error = NULL;
111 keyfile = g_key_file_new();
112 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
113 DBG("Unable to open %s, error %s", pathname, error->message);
116 g_key_file_free(keyfile);
120 DBG("loaded keyfile %s", pathname);
124 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
127 GError *error = NULL;
128 gchar *keydata = NULL;
129 gchar *needle = NULL, *directory = NULL;
131 directory = g_strdup(pathname);
132 needle = g_strrstr(directory, "/");
137 if (directory == NULL || (*directory) == '\0') {
139 ERR("directory is NULL");
143 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
144 if (g_mkdir_with_parents(directory,
145 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
147 ERR("failed to make directory");
153 keydata = g_key_file_to_data(keyfile, &size, &error);
154 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
155 ERR("Unable to save %s, error %s", pathname, error->message);
159 if (chmod(pathname, S_IRUSR | S_IWUSR) < 0)
160 DBG("Failed to change mode");
162 DBG("Successfully saved keyfile %s", pathname);
167 void netconfig_start_timer_seconds(guint secs,
168 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
172 if (callback == NULL) {
173 ERR("callback function is NULL");
177 if ((timer_id != NULL && *timer_id != 0)) {
178 ERR("timer already is registered");
182 t_id = g_timeout_add_seconds(secs, callback, user_data);
185 ERR("Can't add timer");
189 if (timer_id != NULL)
193 void netconfig_start_timer(guint msecs,
194 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
198 INFO("Register timer with callback pointer (%p)", callback);
200 if (callback == NULL) {
201 ERR("callback function is NULL");
205 if ((timer_id != NULL && *timer_id != 0)) {
206 ERR("timer already is registered");
210 t_id = g_timeout_add(msecs, callback, user_data);
213 ERR("Can't add timer");
217 if (timer_id != NULL)
221 void netconfig_stop_timer(guint *timer_id)
223 if (timer_id == NULL) {
224 ERR("timer is NULL");
228 if (*timer_id != 0) {
229 g_source_remove(*timer_id);
234 static gboolean __netconfig_test_device_picker()
236 char *favorite_wifi_service = NULL;
238 favorite_wifi_service = wifi_get_favorite_service();
239 if (favorite_wifi_service != NULL) {
240 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
241 g_free(favorite_wifi_service);
248 static void __netconfig_pop_device_picker(void)
250 if (!netconfig_plugin_headed_enabled)
256 headed_plugin->pop_device_picker();
259 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
261 if (__netconfig_test_device_picker() == TRUE)
262 __netconfig_pop_device_picker();
267 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
269 static guint netconfig_wifi_device_picker_service_timer = 0;
271 if (is_set_method != TRUE)
272 return netconfig_wifi_device_picker_service_timer;
274 if (netconfig_wifi_device_picker_service_timer != timer_id)
275 netconfig_wifi_device_picker_service_timer = timer_id;
277 return netconfig_wifi_device_picker_service_timer;
280 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
282 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
285 static guint __netconfig_wifi_device_picker_get_timer_id(void)
287 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
290 void netconfig_wifi_enable_device_picker_test(void)
292 netconfig_device_picker_test = TRUE;
295 void netconfig_wifi_device_picker_service_start(void)
297 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
300 if (netconfig_device_picker_test == TRUE)
301 netconfig_device_picker_test = FALSE;
307 netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
308 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
311 DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
312 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
314 __netconfig_wifi_device_picker_set_timer_id(timer_id);
317 void netconfig_wifi_device_picker_service_stop(void)
321 timer_id = __netconfig_wifi_device_picker_get_timer_id();
325 DBG("Clear device picker timer with timer_id %d", timer_id);
327 netconfig_stop_timer(&timer_id);
329 __netconfig_wifi_device_picker_set_timer_id(timer_id);
332 gboolean netconfig_is_wifi_direct_on(void)
334 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
337 int wifi_direct_state = 0;
339 netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
341 DBG("Wi-Fi direct mode %d", wifi_direct_state);
342 return (wifi_direct_state != 0) ? TRUE : FALSE;
345 gboolean netconfig_is_wifi_tethering_on(void)
347 if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
348 int wifi_tethering_state = 0;
350 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
351 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
352 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
353 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
354 DBG("Mobile AP is on");
359 DBG("Mobile AP is off");
363 gboolean netconfig_interface_up(const char *ifname)
368 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
372 memset(&ifr, 0, sizeof(ifr));
373 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
375 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
380 ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
381 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
388 DBG("Successfully activated wireless interface %s", ifname);
392 gboolean netconfig_interface_down(const char *ifname)
397 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
401 memset(&ifr, 0, sizeof(ifr));
402 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
404 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
409 ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
410 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
417 DBG("Successfully de-activated wireless interface %s", ifname);
421 int netconfig_execute_file(const char *file_path,
422 char *const args[], char *const envs[])
428 register unsigned int index = 0;
429 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
431 while (args[index] != NULL) {
432 DBG("%s", args[index]);
436 if (!(pid = fork())) {
437 DBG("pid(%d), ppid (%d)", getpid(), getppid());
438 DBG("Inside child, exec (%s) command", file_path);
441 if (execve(file_path, args, envs) == -1) {
442 DBG("Fail to execute command (%s)",
443 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
446 } else if (pid > 0) {
447 if (waitpid(pid, &status, 0) == -1)
448 DBG("wait pid (%u) status (%d)", pid, status);
450 if (WIFEXITED(status)) {
451 rv = WEXITSTATUS(status);
452 DBG("exited, status=%d", rv);
453 } else if (WIFSIGNALED(status)) {
454 DBG("killed by signal %d", WTERMSIG(status));
455 } else if (WIFSTOPPED(status)) {
456 DBG("stopped by signal %d", WSTOPSIG(status));
457 } else if (WIFCONTINUED(status)) {
464 DBG("failed to fork(%s)",
465 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
469 int netconfig_execute_cmd(const char *cmd)
478 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
481 DBG("command: %s", cmd);
483 args = g_strsplit_set(cmd, " ", -1);
485 if (!(pid = fork())) {
486 DBG("pid(%d), ppid (%d)", getpid(), getppid());
489 if (execv(args[0], args) == -1) {
490 DBG("Fail to execute command (%s)",
491 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
495 } else if (pid > 0) {
496 if (waitpid(pid, &status, 0) == -1)
497 DBG("wait pid (%u) status (%d)", pid, status);
499 if (WIFEXITED(status)) {
500 rv = WEXITSTATUS(status);
501 DBG("exited, status=%d", rv);
502 } else if (WIFSIGNALED(status)) {
503 DBG("killed by signal %d", WTERMSIG(status));
504 } else if (WIFSTOPPED(status)) {
505 DBG("stopped by signal %d", WSTOPSIG(status));
506 } else if (WIFCONTINUED(status)) {
514 DBG("failed to fork(%s)",
515 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
521 static void on_clat_handler()
526 clat_pid = waitpid(-1, &state, WNOHANG);
528 DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
531 int netconfig_execute_clatd(const char *file_path, char *const args[])
536 register unsigned int index = 0;
537 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
539 struct sigaction act;
542 act.sa_handler = on_clat_handler;
543 sigemptyset(&act.sa_mask);
546 state = sigaction(SIGCHLD, &act, 0);
548 DBG("sigaction() : %d", state);
552 while (args[index] != NULL) {
553 DBG("%s", args[index]);
557 if (!(pid = fork())) {
558 DBG("pid(%d), ppid (%d)", getpid(), getppid());
559 DBG("Inside child, exec (%s) command", file_path);
562 if (execvp(file_path, args) == -1) {
563 ERR("Fail to execute command (%s)",
564 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
567 } else if (pid > 0) {
568 ERR("Success to launch clatd");
572 DBG("failed to fork(%s)",
573 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
577 static void no_wait_signal_handler()
582 child_pid = waitpid(-1, &state, WNOHANG);
584 DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
587 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
592 register unsigned int index = 0;
594 struct sigaction act;
596 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
598 act.sa_handler = no_wait_signal_handler;
599 sigemptyset(&act.sa_mask);
602 state = sigaction(SIGCHLD, &act, 0);
604 DBG("sigaction() : %d", state);
608 while (args[index] != NULL) {
609 DBG("%s", args[index]);
613 if (!(pid = fork())) {
614 DBG("pid(%d), ppid (%d)", getpid(), getppid());
615 DBG("Inside child, exec (%s) command", file_path);
618 if (execvp(file_path, args) == -1) {
619 ERR("Fail to execute command (%s)",
620 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
623 } else if (pid > 0) {
624 ERR("Successfully launched child process");
628 DBG("failed to fork(%s)",
629 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
633 int __netconfig_get_interface_index(const char *interface_name)
638 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
640 if (interface_name == NULL) {
641 DBG("Inteface name is NULL");
646 sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
648 DBG("Failed to create socket : %s",
649 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
653 memset(&ifr, 0, sizeof(ifr));
654 strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
655 result = ioctl(sock, SIOCGIFINDEX, &ifr);
659 DBG("Failed to get ifr index: %s",
660 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
664 return ifr.ifr_ifindex;
667 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
671 struct sockaddr_in addr_in;
673 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
675 memset(&ifr, 0, sizeof(ifr));
677 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
679 if (ifr.ifr_ifindex < 0)
682 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
684 memset(&rt, 0, sizeof(rt));
686 rt.rt_flags = RTF_UP | RTF_HOST;
687 memset(&addr_in, 0, sizeof(struct sockaddr_in));
688 addr_in.sin_family = address_family;
689 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
690 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
692 memset(&addr_in, 0, sizeof(struct sockaddr_in));
693 addr_in.sin_family = address_family;
694 addr_in.sin_addr.s_addr = INADDR_ANY;
695 memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
697 memset(&addr_in, 0, sizeof(struct sockaddr_in));
698 addr_in.sin_family = AF_INET;
699 addr_in.sin_addr.s_addr = inet_addr(subnet);
700 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
702 rt.rt_dev = ifr.ifr_name;
705 sock = socket(PF_INET, SOCK_DGRAM, 0);
708 DBG("Failed to create socket : %s",
709 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
713 if (ioctl(sock, SIOCADDRT, &rt) < 0) {
714 DBG("Failed to set route address : %s",
715 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
725 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
729 struct sockaddr_in addr_in;
731 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
733 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;
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 = inet_addr(subnet);
752 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
753 rt.rt_dev = ifr.ifr_name;
756 sock = socket(PF_INET, SOCK_DGRAM, 0);
759 DBG("Failed to create socket : %s",
760 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
764 if (ioctl(sock, SIOCDELRT, &rt) < 0) {
765 DBG("Failed to set route address : %s",
766 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
776 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
781 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
783 memset(&rt, 0, sizeof(rt));
785 rt.rtmsg_dst_len = prefix_len;
787 rt.rtmsg_flags = RTF_UP | RTF_HOST;
790 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
791 DBG("inet_pton failed : %s",
792 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
796 if (gateway != NULL) {
797 rt.rtmsg_flags |= RTF_GATEWAY;
798 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
799 DBG("inet_pton failed : %s",
800 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
807 fd = socket(AF_INET6, SOCK_DGRAM, 0);
809 DBG("Failed to create socket : %s",
810 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
814 rt.rtmsg_ifindex = 0;
818 memset(&ifr, 0, sizeof(ifr));
819 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
820 ioctl(fd, SIOCGIFINDEX, &ifr);
821 rt.rtmsg_ifindex = ifr.ifr_ifindex;
824 if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
825 DBG("Failed to add route: %s",
826 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
836 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
842 memset(&rt, 0, sizeof(rt));
844 rt.rtmsg_dst_len = prefix_len;
846 rt.rtmsg_flags = RTF_UP | RTF_HOST;
848 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
853 if (gateway != NULL) {
854 rt.rtmsg_flags |= RTF_GATEWAY;
855 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
863 fd = socket(AF_INET6, SOCK_DGRAM, 0);
867 rt.rtmsg_ifindex = 0;
871 memset(&ifr, 0, sizeof(ifr));
872 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
873 ioctl(fd, SIOCGIFINDEX, &ifr);
874 rt.rtmsg_ifindex = ifr.ifr_ifindex;
877 if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
878 DBG("Failed to del route: %d\n", err);
888 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
890 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
891 wifi_complete_launch_direct(wifi, context);
896 DBG("Launch Wi-Fi direct daemon");
898 const char *path = "/usr/bin/wifi-direct-server.sh";
899 char *const args[] = { "wifi-direct-server.sh", "start", NULL };
900 char *const envs[] = { NULL };
902 ret = netconfig_execute_file(path, args, envs);
904 ERR("Failed to launch Wi-Fi direct daemon");
905 netconfig_error_wifi_direct_failed(context);
909 wifi_complete_launch_direct(wifi, context);
913 int execute_mdnsd_script(char* op)
915 const char *path = "/usr/bin/mdnsresponder-server.sh";
916 char *const args[] = { "mdnsresponder-server.sh", op, NULL };
917 char *const envs[] = { NULL };
919 return netconfig_execute_file(path, args, envs);
922 static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
923 const gchar *Name, const gchar *path, const gchar *interface,
924 const gchar *sig, GVariant *param, gpointer user_data)
929 dnssd_conn_destroy_data *data = user_data;
930 GDBusConnection *connection = NULL;
931 connection = netdbus_get_connection();
936 g_variant_get(param, "(sss)", &name, &old, &new);
938 if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
939 DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
942 g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
943 if (mdnsd_ref_count == 0) {
944 if (execute_mdnsd_script("stop") < 0)
945 ERR("Failed to stop mdnsresponder daemon");
951 g_free(data->conn_name);
956 static void register_dnssd_conn_destroy_signal(gchar *name)
958 dnssd_conn_destroy_data *data;
959 GDBusConnection *connection = NULL;
960 connection = netdbus_get_connection();
962 if (connection == NULL) {
963 ERR("Failed to get GDbus Connection");
967 data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
970 ERR("Out of Memory!");
974 data->conn_name = g_strdup(name);
976 data->conn_id = g_dbus_connection_signal_subscribe(connection,
977 DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
978 "NameOwnerChanged", NULL, name,
979 G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
984 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
987 DBG("Launch mdnsresponder daemon");
989 if (execute_mdnsd_script("start") < 0) {
990 ERR("Failed to launch mdnsresponder daemon");
991 netconfig_error_invalid_parameter(context);
996 register_dnssd_conn_destroy_signal(name);
997 DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
999 network_complete_launch_mdns(object, context);
1003 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
1005 if (!netconfig_plugin_headed_enabled)
1011 return headed_plugin->send_notification_to_net_popup(noti, ssid);
1014 int netconfig_send_message_to_net_popup(const char *title,
1015 const char *content, const char *type, const char *ssid)
1017 if (!netconfig_plugin_headed_enabled)
1023 return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
1026 int netconfig_send_restriction_to_net_popup(const char *title,
1027 const char *type, const char *restriction)
1029 if (!netconfig_plugin_headed_enabled)
1035 return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
1038 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
1040 if (!netconfig_plugin_headed_enabled)
1046 headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1049 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1051 if (!netconfig_plugin_headed_enabled)
1057 headed_plugin->pop_wifi_connected_poppup(ssid);
1060 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1062 if (!netconfig_plugin_telephony_enabled)
1065 if (!telephony_plugin)
1068 telephony_plugin->get_telephony_network_type(svctype, pstype);
1071 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1073 if (!netconfig_plugin_telephony_enabled)
1076 if (!telephony_plugin)
1079 return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1082 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1083 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1085 if (!netconfig_plugin_telephony_enabled)
1086 return NETCONFIG_ERROR_INTERNAL;
1088 if (!telephony_plugin)
1089 return NETCONFIG_ERROR_INTERNAL;
1091 return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1094 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1095 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1097 if (!netconfig_plugin_telephony_enabled)
1100 if (!telephony_plugin)
1103 return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1106 gboolean netconfig_tapi_check_sim_state(void)
1108 if (!netconfig_plugin_telephony_enabled)
1111 if (!telephony_plugin)
1114 return telephony_plugin->tapi_check_sim_state();
1117 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1118 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1120 if (!netconfig_plugin_telephony_enabled)
1123 if (!telephony_plugin)
1126 return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1129 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1130 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1132 if (!netconfig_plugin_telephony_enabled)
1135 if (!telephony_plugin)
1138 return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1141 static void __netconfig_stc_get_dn_stats(time_t from, time_t to, GSList **list)
1143 if (!netconfig_plugin_stc_enabled)
1149 return stc_plugin->get_stc_dn_stats(from, to, list);
1152 static void __netconfig_stc_get_wifi_stats(time_t from, time_t to, GSList **list)
1154 if (!netconfig_plugin_stc_enabled)
1160 return stc_plugin->get_stc_wifi_stats(from, to, list);
1163 void netconfig_battery_start_dn(void)
1165 if (!netconfig_plugin_battery_enabled)
1168 if (!battery_plugin)
1171 return battery_plugin->start_dn_data();
1174 void netconfig_battery_end_dn(void)
1176 if (!netconfig_plugin_battery_enabled)
1179 if (!battery_plugin)
1182 return battery_plugin->end_dn_data(0, __netconfig_stc_get_dn_stats);
1185 void netconfig_battery_update_dn_rssi(int rssi)
1187 if (!netconfig_plugin_battery_enabled)
1190 if (!battery_plugin)
1193 return battery_plugin->update_dn_rssi(rssi);
1196 void netconfig_battery_start_wifi(void)
1198 if (!netconfig_plugin_battery_enabled)
1201 if (!battery_plugin)
1204 return battery_plugin->start_wifi_data();
1207 void netconfig_battery_end_wifi(void)
1209 if (!netconfig_plugin_battery_enabled)
1212 if (!battery_plugin)
1215 return battery_plugin->end_wifi_data(0, __netconfig_stc_get_wifi_stats);
1218 void netconfig_battery_update_wifi_scan(int state)
1220 if (!netconfig_plugin_battery_enabled)
1223 if (!battery_plugin)
1226 return battery_plugin->update_wifi_scan(state);
1229 void netconfig_battery_update_wifi_rssi(int rssi)
1231 if (!netconfig_plugin_battery_enabled)
1234 if (!battery_plugin)
1237 return battery_plugin->update_wifi_rssi(rssi);
1240 void netconfig_battery_get_dn_list(void *data)
1242 if (!netconfig_plugin_battery_enabled)
1245 if (!battery_plugin)
1248 return battery_plugin->get_battery_dn_list(data, __netconfig_stc_get_dn_stats);
1251 void netconfig_battery_get_wifi_list(void *data)
1253 if (!netconfig_plugin_battery_enabled)
1256 if (!battery_plugin)
1259 return battery_plugin->get_battery_wifi_list(data, __netconfig_stc_get_wifi_stats);
1262 void netconfig_set_vconf_int(const char * key, int value)
1266 DBG("[%s: %d]", key, value);
1268 ret = vconf_set_int(key, value);
1269 if (ret != VCONF_OK)
1270 ERR("Failed to set");
1273 void netconfig_set_vconf_str(const char * key, const char * value)
1277 DBG("[%s: %s]", key, value);
1279 ret = vconf_set_str(key, value);
1280 if (ret != VCONF_OK)
1281 ERR("Failed to set");
1284 int netconfig_vconf_get_int(const char * key, int *value)
1288 ret = vconf_get_int(key, value);
1289 if (ret != VCONF_OK) {
1290 ERR("Failed to get vconfkey [%s] value", key);
1297 int netconfig_vconf_get_bool(const char * key, int *value)
1301 ret = vconf_get_bool(key, value);
1302 if (ret != VCONF_OK) {
1303 ERR("Failed to get vconfkey [%s] value", key);
1310 char* netconfig_get_env(const char *key)
1313 char buf[256], *entry = NULL, *value = NULL, *last;
1319 fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1323 while (fgets(buf, sizeof(buf), fp)) {
1325 entry = strtok_r(entry, "=", &last);
1327 if (strstr(entry, key)) {
1328 entry = strtok_r(NULL, "\n", &last);
1330 len = strlen(entry);
1331 value = (char*)malloc(len+1);
1332 g_strlcpy(value, entry, len+1);
1334 value = (char*)malloc(sizeof(char));
1335 g_strlcpy(value, "\n", sizeof(char));
1346 void netconfig_set_mac_address_from_file(void)
1349 char mac_str[MAC_ADDRESS_MAX_LEN];
1350 gchar *mac_lower_str = NULL;
1353 file = fopen(MAC_INFO_FILEPATH, "r");
1355 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1356 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1358 ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1362 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1363 ERR("Fail to read mac address");
1368 mac_len = strlen(mac_str);
1370 ERR("mac.info is empty");
1375 mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1376 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1378 g_free(mac_lower_str);
1382 char *netconfig_get_mac_address_from_file(const char *ifname)
1385 char file_path[PATH_MAX] = {0, };
1386 char mac_str[MAC_ADDRESS_MAX_LEN];
1392 g_snprintf(file_path, sizeof(file_path),
1393 "/sys/class/net/%s/address", ifname);
1395 DBG("ifname: %s", ifname);
1396 DBG("file_path: %s", file_path);
1397 file = fopen(file_path, "r");
1399 ERR("Fail to open %s", file_path);
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 is empty");
1414 DBG("address: %s", mac_str);
1417 return g_strdup(mac_str);
1420 int netconfig_freq_to_channel(int freq)
1422 if (freq < 2412 || freq > 5825 ||
1423 (freq > 2484 && freq < 5180)) {
1424 ERR("Invalid Frequence Range");
1428 return 36 + (freq - 5180)/5;
1429 else if (freq <= 2472)
1430 return 1 + (freq - 2412)/5;
1431 else if (freq == 2484)
1437 int netconfig_get_operating_class(int freq)
1442 channel = netconfig_freq_to_channel(freq);
1445 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1446 if (channel >= 1 && channel <= 13)
1448 /* Operating class 115 - 5 GHz, channels 36-48 */
1449 else if (channel >= 36 && channel <= 48)
1451 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1455 INFO("Operating Class is [%d]", oper_class);
1461 tizen_profile_t _get_tizen_profile()
1463 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1464 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1468 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1469 switch (*profileName) {
1472 profile = TIZEN_PROFILE_MOBILE;
1476 profile = TIZEN_PROFILE_WEARABLE;
1480 profile = TIZEN_PROFILE_TV;
1484 profile = TIZEN_PROFILE_IVI;
1486 default: // common or unknown ==> ALL ARE COMMON.
1487 profile = TIZEN_PROFILE_COMMON;
1494 void netconfig_plugin_init()
1496 handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1497 if (!handle_headed) {
1498 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1500 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1501 if (!headed_plugin) {
1502 ERR("Can't load symbol: %s", dlerror());
1503 dlclose(handle_headed);
1505 netconfig_plugin_headed_enabled = TRUE;
1509 handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1510 if (!handle_telephony) {
1511 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1513 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1514 if (!telephony_plugin) {
1515 ERR("Can't load symbol: %s", dlerror());
1516 dlclose(handle_telephony);
1518 netconfig_plugin_telephony_enabled = TRUE;
1522 handle_stc = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
1524 ERR("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
1526 stc_plugin = dlsym(handle_stc, "netconfig_stc_plugin");
1528 ERR("Can't load symbol: %s", dlerror());
1529 dlclose(handle_stc);
1531 netconfig_plugin_stc_enabled = TRUE;
1535 handle_battery = dlopen(BATTERY_PLUGIN_FILEPATH, RTLD_NOW);
1536 if (!handle_battery) {
1537 ERR("Can't load %s: %s", BATTERY_PLUGIN_FILEPATH, dlerror());
1539 battery_plugin = dlsym(handle_battery, "netconfig_battery_plugin");
1540 if (!battery_plugin) {
1541 ERR("Can't load symbol: %s", dlerror());
1542 dlclose(handle_battery);
1544 netconfig_plugin_battery_enabled = TRUE;
1550 void netconfig_plugin_deinit()
1552 if (netconfig_plugin_headed_enabled) {
1553 netconfig_plugin_headed_enabled = FALSE;
1554 dlclose(handle_headed);
1557 if (netconfig_plugin_telephony_enabled) {
1558 netconfig_plugin_telephony_enabled = FALSE;
1559 dlclose(handle_telephony);
1562 if (netconfig_plugin_stc_enabled) {
1563 netconfig_plugin_stc_enabled = FALSE;
1564 dlclose(handle_stc);
1567 if (netconfig_plugin_battery_enabled) {
1568 netconfig_plugin_battery_enabled = FALSE;
1569 dlclose(handle_battery);
1574 gboolean netconfig_get_headed_plugin_flag()
1576 return netconfig_plugin_headed_enabled;
1579 gboolean netconfig_get_telephony_plugin_flag()
1581 return netconfig_plugin_telephony_enabled;
1584 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1586 const char *key = NULL;
1588 if (!is_feature_checked[feature]) {
1590 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1591 key = ETHERNET_FEATURE;
1593 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1594 key = TETHERING_FEATURE;
1596 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1597 key = WIFI_DIRECT_FEATURE;
1599 case NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP:
1600 key = WIFI_SOFTAP_FEATURE;
1603 ERR("Uknown feature");
1607 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1608 ERR("Get feature is failed");
1611 is_feature_checked[feature] = true;
1613 return feature_supported[feature];
1616 void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr)
1621 t = (*bin >> 4) & 0x0f;
1625 else if (t >= 10 && t <= 16)
1626 *hexstr = (t - 10) + 'a';
1634 else if (t >= 10 && t <= 16)
1635 *hexstr = (t - 10) + 'a';