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 MAC_ADDRESS_MAX_LEN 18
54 #define HEADED_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-headed.so"
55 #define TELEPHONY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-telephony.so"
56 #define STC_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-stc.so"
57 #define BATTERY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-battery.so"
59 static gboolean netconfig_device_picker_test = FALSE;
60 static int mdnsd_ref_count = 0;
64 } dnssd_conn_destroy_data;
66 static gboolean netconfig_plugin_headed_enabled = FALSE;
67 static gboolean netconfig_plugin_telephony_enabled = FALSE;
68 static gboolean netconfig_plugin_stc_enabled = FALSE;
69 static gboolean netconfig_plugin_battery_enabled = FALSE;
70 static void *handle_headed;
71 static void *handle_telephony;
72 static void *handle_stc;
73 static void *handle_battery;
74 static struct netconfig_headed_plugin_t *headed_plugin;
75 static struct netconfig_telephony_plugin_t *telephony_plugin;
76 static struct netconfig_stc_plugin_t *stc_plugin;
77 static struct netconfig_battery_plugin_t *battery_plugin;
79 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
80 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
82 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
89 length = strlen(passphrase);
91 if (g_str_has_suffix(service, "psk") == TRUE) {
93 for (int i = 0; i < 64; i++)
94 if (!isxdigit((unsigned char)passphrase[i]))
96 } else if (length < 8 || length > 63)
98 } else if (g_str_has_suffix(service, "wep") == TRUE) {
99 if (length == 10 || length == 26) {
100 for (int i = 0; i < length; i++)
101 if (!isxdigit((unsigned char)passphrase[i]))
103 } else if (length != 5 && length != 13)
110 GKeyFile *netconfig_keyfile_load(const char *pathname)
112 GKeyFile *keyfile = NULL;
113 GError *error = NULL;
115 keyfile = g_key_file_new();
116 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
117 DBG("Unable to open %s, error %s", pathname, error->message);
120 g_key_file_free(keyfile);
124 DBG("loaded keyfile %s", pathname);
128 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
131 GError *error = NULL;
132 gchar *keydata = NULL;
133 gchar *needle = NULL, *directory = NULL;
135 directory = g_strdup(pathname);
136 needle = g_strrstr(directory, "/");
141 if (directory == NULL || (*directory) == '\0') {
143 ERR("directory is NULL");
147 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
148 if (g_mkdir_with_parents(directory,
149 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
151 ERR("failed to make directory");
157 keydata = g_key_file_to_data(keyfile, &size, &error);
158 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
159 ERR("Unable to save %s, error %s", pathname, error->message);
163 if (chmod(pathname, S_IRUSR | S_IWUSR) < 0)
164 DBG("Failed to change mode");
166 DBG("Successfully saved keyfile %s", pathname);
171 void netconfig_start_timer_seconds(guint secs,
172 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
176 if (callback == NULL) {
177 ERR("callback function is NULL");
181 if ((timer_id != NULL && *timer_id != 0)) {
182 ERR("timer already is registered");
186 t_id = g_timeout_add_seconds(secs, callback, user_data);
189 ERR("Can't add timer");
193 if (timer_id != NULL)
197 void netconfig_start_timer(guint msecs,
198 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
202 INFO("Register timer with callback pointer (%p)", callback);
204 if (callback == NULL) {
205 ERR("callback function is NULL");
209 if ((timer_id != NULL && *timer_id != 0)) {
210 ERR("timer already is registered");
214 t_id = g_timeout_add(msecs, callback, user_data);
217 ERR("Can't add timer");
221 if (timer_id != NULL)
225 void netconfig_stop_timer(guint *timer_id)
227 if (timer_id == NULL) {
228 ERR("timer is NULL");
232 if (*timer_id != 0) {
233 g_source_remove(*timer_id);
238 static gboolean __netconfig_test_device_picker()
240 char *favorite_wifi_service = NULL;
242 favorite_wifi_service = wifi_get_favorite_service();
243 if (favorite_wifi_service != NULL) {
244 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
245 g_free(favorite_wifi_service);
252 static void __netconfig_pop_device_picker(void)
254 if (!netconfig_plugin_headed_enabled)
260 headed_plugin->pop_device_picker();
263 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
265 if (__netconfig_test_device_picker() == TRUE)
266 __netconfig_pop_device_picker();
271 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
273 static guint netconfig_wifi_device_picker_service_timer = 0;
275 if (is_set_method != TRUE)
276 return netconfig_wifi_device_picker_service_timer;
278 if (netconfig_wifi_device_picker_service_timer != timer_id)
279 netconfig_wifi_device_picker_service_timer = timer_id;
281 return netconfig_wifi_device_picker_service_timer;
284 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
286 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
289 static guint __netconfig_wifi_device_picker_get_timer_id(void)
291 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
294 void netconfig_wifi_enable_device_picker_test(void)
296 netconfig_device_picker_test = TRUE;
299 void netconfig_wifi_device_picker_service_start(void)
301 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
304 if (netconfig_device_picker_test == TRUE)
305 netconfig_device_picker_test = FALSE;
311 netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
312 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
315 DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
316 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
318 __netconfig_wifi_device_picker_set_timer_id(timer_id);
321 void netconfig_wifi_device_picker_service_stop(void)
325 timer_id = __netconfig_wifi_device_picker_get_timer_id();
329 DBG("Clear device picker timer with timer_id %d", timer_id);
331 netconfig_stop_timer(&timer_id);
333 __netconfig_wifi_device_picker_set_timer_id(timer_id);
336 gboolean netconfig_is_wifi_direct_on(void)
338 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
341 int wifi_direct_state = 0;
343 netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
345 DBG("Wi-Fi direct mode %d", wifi_direct_state);
346 return (wifi_direct_state != 0) ? TRUE : FALSE;
349 gboolean netconfig_is_wifi_tethering_on(void)
351 if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
352 int wifi_tethering_state = 0;
354 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
355 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
356 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
357 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
358 DBG("Mobile AP is on");
363 DBG("Mobile AP is off");
367 gboolean netconfig_interface_up(const char *ifname)
372 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
376 memset(&ifr, 0, sizeof(ifr));
377 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
379 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
384 ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
385 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
392 DBG("Successfully activated wireless interface %s", ifname);
396 gboolean netconfig_interface_down(const char *ifname)
401 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
405 memset(&ifr, 0, sizeof(ifr));
406 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
408 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
413 ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
414 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
421 DBG("Successfully de-activated wireless interface %s", ifname);
425 int netconfig_execute_file(const char *file_path,
426 char *const args[], char *const envs[])
432 register unsigned int index = 0;
433 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
435 while (args[index] != NULL) {
436 DBG("%s", args[index]);
440 if (!(pid = fork())) {
441 DBG("pid(%d), ppid (%d)", getpid(), getppid());
442 DBG("Inside child, exec (%s) command", file_path);
445 if (execve(file_path, args, envs) == -1) {
446 DBG("Fail to execute command (%s)",
447 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
450 } else if (pid > 0) {
451 if (waitpid(pid, &status, 0) == -1)
452 DBG("wait pid (%u) status (%d)", pid, status);
454 if (WIFEXITED(status)) {
455 rv = WEXITSTATUS(status);
456 DBG("exited, status=%d", rv);
457 } else if (WIFSIGNALED(status)) {
458 DBG("killed by signal %d", WTERMSIG(status));
459 } else if (WIFSTOPPED(status)) {
460 DBG("stopped by signal %d", WSTOPSIG(status));
461 } else if (WIFCONTINUED(status)) {
468 DBG("failed to fork(%s)",
469 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
473 int netconfig_execute_cmd(const char *cmd)
482 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
485 DBG("command: %s", cmd);
487 args = g_strsplit_set(cmd, " ", -1);
489 if (!(pid = fork())) {
490 DBG("pid(%d), ppid (%d)", getpid(), getppid());
493 if (execv(args[0], args) == -1) {
494 DBG("Fail to execute command (%s)",
495 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
499 } else if (pid > 0) {
500 if (waitpid(pid, &status, 0) == -1)
501 DBG("wait pid (%u) status (%d)", pid, status);
503 if (WIFEXITED(status)) {
504 rv = WEXITSTATUS(status);
505 DBG("exited, status=%d", rv);
506 } else if (WIFSIGNALED(status)) {
507 DBG("killed by signal %d", WTERMSIG(status));
508 } else if (WIFSTOPPED(status)) {
509 DBG("stopped by signal %d", WSTOPSIG(status));
510 } else if (WIFCONTINUED(status)) {
518 DBG("failed to fork(%s)",
519 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
525 static void on_clat_handler()
530 clat_pid = waitpid(-1, &state, WNOHANG);
532 DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
535 int netconfig_execute_clatd(const char *file_path, char *const args[])
540 register unsigned int index = 0;
541 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
543 struct sigaction act;
546 act.sa_handler = on_clat_handler;
547 sigemptyset(&act.sa_mask);
550 state = sigaction(SIGCHLD, &act, 0);
552 DBG("sigaction() : %d", state);
556 while (args[index] != NULL) {
557 DBG("%s", args[index]);
561 if (!(pid = fork())) {
562 DBG("pid(%d), ppid (%d)", getpid(), getppid());
563 DBG("Inside child, exec (%s) command", file_path);
566 if (execvp(file_path, args) == -1) {
567 ERR("Fail to execute command (%s)",
568 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
571 } else if (pid > 0) {
572 ERR("Success to launch clatd");
576 DBG("failed to fork(%s)",
577 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
581 static void no_wait_signal_handler()
586 child_pid = waitpid(-1, &state, WNOHANG);
588 DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
591 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
596 register unsigned int index = 0;
598 struct sigaction act;
600 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
602 act.sa_handler = no_wait_signal_handler;
603 sigemptyset(&act.sa_mask);
606 state = sigaction(SIGCHLD, &act, 0);
608 DBG("sigaction() : %d", state);
612 while (args[index] != NULL) {
613 DBG("%s", args[index]);
617 if (!(pid = fork())) {
618 DBG("pid(%d), ppid (%d)", getpid(), getppid());
619 DBG("Inside child, exec (%s) command", file_path);
622 if (execvp(file_path, args) == -1) {
623 ERR("Fail to execute command (%s)",
624 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
627 } else if (pid > 0) {
628 ERR("Successfully launched child process");
632 DBG("failed to fork(%s)",
633 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
637 int __netconfig_get_interface_index(const char *interface_name)
642 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
644 if (interface_name == NULL) {
645 DBG("Inteface name is NULL");
650 sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
652 DBG("Failed to create socket : %s",
653 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
657 memset(&ifr, 0, sizeof(ifr));
658 strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
659 result = ioctl(sock, SIOCGIFINDEX, &ifr);
663 DBG("Failed to get ifr index: %s",
664 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
668 return ifr.ifr_ifindex;
671 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
675 struct sockaddr_in addr_in;
677 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
679 memset(&ifr, 0, sizeof(ifr));
681 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
683 if (ifr.ifr_ifindex < 0)
686 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
688 memset(&rt, 0, sizeof(rt));
690 rt.rt_flags = RTF_UP | RTF_HOST;
691 memset(&addr_in, 0, sizeof(struct sockaddr_in));
692 addr_in.sin_family = address_family;
693 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
694 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
696 memset(&addr_in, 0, sizeof(struct sockaddr_in));
697 addr_in.sin_family = address_family;
698 addr_in.sin_addr.s_addr = INADDR_ANY;
699 memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
701 memset(&addr_in, 0, sizeof(struct sockaddr_in));
702 addr_in.sin_family = AF_INET;
703 addr_in.sin_addr.s_addr = inet_addr(subnet);
704 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
706 rt.rt_dev = ifr.ifr_name;
709 sock = socket(PF_INET, SOCK_DGRAM, 0);
712 DBG("Failed to create socket : %s",
713 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
717 if (ioctl(sock, SIOCADDRT, &rt) < 0) {
718 DBG("Failed to set route address : %s",
719 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
729 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
733 struct sockaddr_in addr_in;
735 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
737 memset(&ifr, 0, sizeof(ifr));
738 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
740 if (ifr.ifr_ifindex < 0)
743 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
745 memset(&rt, 0, sizeof(rt));
747 rt.rt_flags = RTF_UP;
748 memset(&addr_in, 0, sizeof(struct sockaddr_in));
749 addr_in.sin_family = address_family;
750 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
751 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
753 memset(&addr_in, 0, sizeof(struct sockaddr_in));
754 addr_in.sin_family = address_family;
755 addr_in.sin_addr.s_addr = inet_addr(subnet);
756 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
757 rt.rt_dev = ifr.ifr_name;
760 sock = socket(PF_INET, SOCK_DGRAM, 0);
763 DBG("Failed to create socket : %s",
764 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
768 if (ioctl(sock, SIOCDELRT, &rt) < 0) {
769 DBG("Failed to set route address : %s",
770 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
780 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
785 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
787 memset(&rt, 0, sizeof(rt));
789 rt.rtmsg_dst_len = prefix_len;
791 rt.rtmsg_flags = RTF_UP | RTF_HOST;
794 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
795 DBG("inet_pton failed : %s",
796 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
800 if (gateway != NULL) {
801 rt.rtmsg_flags |= RTF_GATEWAY;
802 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
803 DBG("inet_pton failed : %s",
804 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
811 fd = socket(AF_INET6, SOCK_DGRAM, 0);
813 DBG("Failed to create socket : %s",
814 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
818 rt.rtmsg_ifindex = 0;
822 memset(&ifr, 0, sizeof(ifr));
823 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
824 ioctl(fd, SIOCGIFINDEX, &ifr);
825 rt.rtmsg_ifindex = ifr.ifr_ifindex;
828 if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
829 DBG("Failed to add route: %s",
830 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
840 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
846 memset(&rt, 0, sizeof(rt));
848 rt.rtmsg_dst_len = prefix_len;
850 rt.rtmsg_flags = RTF_UP | RTF_HOST;
852 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
857 if (gateway != NULL) {
858 rt.rtmsg_flags |= RTF_GATEWAY;
859 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
867 fd = socket(AF_INET6, SOCK_DGRAM, 0);
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, SIOCDELRT, &rt)) < 0) {
882 DBG("Failed to del route: %d\n", err);
892 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
894 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
895 wifi_complete_launch_direct(wifi, context);
900 DBG("Launch Wi-Fi direct daemon");
902 const char *path = "/usr/bin/wifi-direct-server.sh";
903 char *const args[] = { "wifi-direct-server.sh", "start", NULL };
904 char *const envs[] = { NULL };
906 ret = netconfig_execute_file(path, args, envs);
908 ERR("Failed to launch Wi-Fi direct daemon");
909 netconfig_error_wifi_direct_failed(context);
913 wifi_complete_launch_direct(wifi, context);
917 int execute_mdnsd_script(char* op)
919 const char *path = "/usr/bin/mdnsresponder-server.sh";
920 char *const args[] = { "mdnsresponder-server.sh", op, NULL };
921 char *const envs[] = { NULL };
923 return netconfig_execute_file(path, args, envs);
926 static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
927 const gchar *Name, const gchar *path, const gchar *interface,
928 const gchar *sig, GVariant *param, gpointer user_data)
933 dnssd_conn_destroy_data *data = user_data;
934 GDBusConnection *connection = NULL;
935 connection = netdbus_get_connection();
940 g_variant_get(param, "(sss)", &name, &old, &new);
942 if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
943 DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
946 g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
947 if (mdnsd_ref_count == 0) {
948 if (execute_mdnsd_script("stop") < 0)
949 ERR("Failed to stop mdnsresponder daemon");
955 g_free(data->conn_name);
960 static void register_dnssd_conn_destroy_signal(gchar *name)
962 dnssd_conn_destroy_data *data;
963 GDBusConnection *connection = NULL;
964 connection = netdbus_get_connection();
966 if (connection == NULL) {
967 ERR("Failed to get GDbus Connection");
971 data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
974 ERR("Out of Memory!");
978 data->conn_name = g_strdup(name);
980 data->conn_id = g_dbus_connection_signal_subscribe(connection,
981 DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
982 "NameOwnerChanged", NULL, name,
983 G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
988 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
991 DBG("Launch mdnsresponder daemon");
993 if (execute_mdnsd_script("start") < 0) {
994 ERR("Failed to launch mdnsresponder daemon");
995 netconfig_error_invalid_parameter(context);
1000 register_dnssd_conn_destroy_signal(name);
1001 DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
1003 network_complete_launch_mdns(object, context);
1007 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
1009 if (!netconfig_plugin_headed_enabled)
1015 return headed_plugin->send_notification_to_net_popup(noti, ssid);
1018 int netconfig_send_message_to_net_popup(const char *title,
1019 const char *content, const char *type, const char *ssid)
1021 if (!netconfig_plugin_headed_enabled)
1027 return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
1030 int netconfig_send_restriction_to_net_popup(const char *title,
1031 const char *type, const char *restriction)
1033 if (!netconfig_plugin_headed_enabled)
1039 return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
1042 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
1044 if (!netconfig_plugin_headed_enabled)
1050 headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1053 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1055 if (!netconfig_plugin_headed_enabled)
1061 headed_plugin->pop_wifi_connected_poppup(ssid);
1064 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1066 if (!netconfig_plugin_telephony_enabled)
1069 if (!telephony_plugin)
1072 telephony_plugin->get_telephony_network_type(svctype, pstype);
1075 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1077 if (!netconfig_plugin_telephony_enabled)
1080 if (!telephony_plugin)
1083 return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1086 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1087 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1089 if (!netconfig_plugin_telephony_enabled)
1090 return NETCONFIG_ERROR_INTERNAL;
1092 if (!telephony_plugin)
1093 return NETCONFIG_ERROR_INTERNAL;
1095 return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1098 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1099 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1101 if (!netconfig_plugin_telephony_enabled)
1104 if (!telephony_plugin)
1107 return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1110 gboolean netconfig_tapi_check_sim_state(void)
1112 if (!netconfig_plugin_telephony_enabled)
1115 if (!telephony_plugin)
1118 return telephony_plugin->tapi_check_sim_state();
1121 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1122 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1124 if (!netconfig_plugin_telephony_enabled)
1127 if (!telephony_plugin)
1130 return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1133 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1134 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1136 if (!netconfig_plugin_telephony_enabled)
1139 if (!telephony_plugin)
1142 return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1145 static void __netconfig_stc_get_dn_stats(time_t from, time_t to, GSList **list)
1147 if (!netconfig_plugin_stc_enabled)
1153 return stc_plugin->get_stc_dn_stats(from, to, list);
1156 static void __netconfig_stc_get_wifi_stats(time_t from, time_t to, GSList **list)
1158 if (!netconfig_plugin_stc_enabled)
1164 return stc_plugin->get_stc_wifi_stats(from, to, list);
1167 void netconfig_battery_start_dn(void)
1169 if (!netconfig_plugin_battery_enabled)
1172 if (!battery_plugin)
1175 return battery_plugin->start_dn_data();
1178 void netconfig_battery_end_dn(void)
1180 if (!netconfig_plugin_battery_enabled)
1183 if (!battery_plugin)
1186 return battery_plugin->end_dn_data(0, __netconfig_stc_get_dn_stats);
1189 void netconfig_battery_update_dn_rssi(int rssi)
1191 if (!netconfig_plugin_battery_enabled)
1194 if (!battery_plugin)
1197 return battery_plugin->update_dn_rssi(rssi);
1200 void netconfig_battery_start_wifi(void)
1202 if (!netconfig_plugin_battery_enabled)
1205 if (!battery_plugin)
1208 return battery_plugin->start_wifi_data();
1211 void netconfig_battery_end_wifi(void)
1213 if (!netconfig_plugin_battery_enabled)
1216 if (!battery_plugin)
1219 return battery_plugin->end_wifi_data(0, __netconfig_stc_get_wifi_stats);
1222 void netconfig_battery_update_wifi_scan(int state)
1224 if (!netconfig_plugin_battery_enabled)
1227 if (!battery_plugin)
1230 return battery_plugin->update_wifi_scan(state);
1233 void netconfig_battery_update_wifi_rssi(int rssi)
1235 if (!netconfig_plugin_battery_enabled)
1238 if (!battery_plugin)
1241 return battery_plugin->update_wifi_rssi(rssi);
1244 void netconfig_battery_get_dn_list(void *data)
1246 if (!netconfig_plugin_battery_enabled)
1249 if (!battery_plugin)
1252 return battery_plugin->get_battery_dn_list(data, __netconfig_stc_get_dn_stats);
1255 void netconfig_battery_get_wifi_list(void *data)
1257 if (!netconfig_plugin_battery_enabled)
1260 if (!battery_plugin)
1263 return battery_plugin->get_battery_wifi_list(data, __netconfig_stc_get_wifi_stats);
1266 void netconfig_set_vconf_int(const char * key, int value)
1270 DBG("[%s: %d]", key, value);
1272 ret = vconf_set_int(key, value);
1273 if (ret != VCONF_OK)
1274 ERR("Failed to set");
1277 void netconfig_set_vconf_str(const char * key, const char * value)
1281 DBG("[%s: %s]", key, value);
1283 ret = vconf_set_str(key, value);
1284 if (ret != VCONF_OK)
1285 ERR("Failed to set");
1288 int netconfig_vconf_get_int(const char * key, int *value)
1292 ret = vconf_get_int(key, value);
1293 if (ret != VCONF_OK) {
1294 ERR("Failed to get vconfkey [%s] value", key);
1301 int netconfig_vconf_get_bool(const char * key, int *value)
1305 ret = vconf_get_bool(key, value);
1306 if (ret != VCONF_OK) {
1307 ERR("Failed to get vconfkey [%s] value", key);
1314 char* netconfig_get_env(const char *key)
1317 char buf[256], *entry = NULL, *value = NULL, *last;
1323 fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1327 while (fgets(buf, sizeof(buf), fp)) {
1329 entry = strtok_r(entry, "=", &last);
1331 if (strstr(entry, key)) {
1332 entry = strtok_r(NULL, "\n", &last);
1334 len = strlen(entry);
1335 value = (char*)malloc(len+1);
1336 g_strlcpy(value, entry, len+1);
1338 value = (char*)malloc(sizeof(char));
1339 g_strlcpy(value, "\n", sizeof(char));
1350 void netconfig_set_mac_address_from_file(void)
1353 char mac_str[MAC_ADDRESS_MAX_LEN];
1354 gchar *mac_lower_str = NULL;
1357 file = fopen(MAC_INFO_FILEPATH, "r");
1359 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1360 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1362 ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1366 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1367 ERR("Fail to read mac address");
1372 mac_len = strlen(mac_str);
1374 ERR("mac.info is empty");
1379 mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1380 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1382 g_free(mac_lower_str);
1386 char *netconfig_get_mac_address_from_file(const char *ifname)
1389 char file_path[PATH_MAX] = {0, };
1390 char mac_str[MAC_ADDRESS_MAX_LEN];
1396 g_snprintf(file_path, sizeof(file_path),
1397 "/sys/class/net/%s/address", ifname);
1399 DBG("ifname: %s", ifname);
1400 DBG("file_path: %s", file_path);
1401 file = fopen(file_path, "r");
1403 ERR("Fail to open %s", file_path);
1406 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1407 ERR("Fail to read mac address");
1412 mac_len = strlen(mac_str);
1414 ERR("mac is empty");
1418 DBG("address: %s", mac_str);
1421 return g_strdup(mac_str);
1424 int netconfig_freq_to_channel(int freq)
1426 if (freq < 2412 || freq > 5825 ||
1427 (freq > 2484 && freq < 5180)) {
1428 ERR("Invalid Frequence Range");
1432 return 36 + (freq - 5180)/5;
1433 else if (freq <= 2472)
1434 return 1 + (freq - 2412)/5;
1435 else if (freq == 2484)
1441 int netconfig_get_operating_class(int freq)
1446 channel = netconfig_freq_to_channel(freq);
1449 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1450 if (channel >= 1 && channel <= 13)
1452 /* Operating class 115 - 5 GHz, channels 36-48 */
1453 else if (channel >= 36 && channel <= 48)
1455 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1459 INFO("Operating Class is [%d]", oper_class);
1465 tizen_profile_t _get_tizen_profile()
1467 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1468 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1472 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1473 switch (*profileName) {
1476 profile = TIZEN_PROFILE_MOBILE;
1480 profile = TIZEN_PROFILE_WEARABLE;
1484 profile = TIZEN_PROFILE_TV;
1488 profile = TIZEN_PROFILE_IVI;
1490 default: // common or unknown ==> ALL ARE COMMON.
1491 profile = TIZEN_PROFILE_COMMON;
1498 void netconfig_plugin_init()
1500 handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1501 if (!handle_headed) {
1502 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1504 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1505 if (!headed_plugin) {
1506 ERR("Can't load symbol: %s", dlerror());
1507 dlclose(handle_headed);
1509 netconfig_plugin_headed_enabled = TRUE;
1513 handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1514 if (!handle_telephony) {
1515 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1517 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1518 if (!telephony_plugin) {
1519 ERR("Can't load symbol: %s", dlerror());
1520 dlclose(handle_telephony);
1522 netconfig_plugin_telephony_enabled = TRUE;
1526 handle_stc = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
1528 ERR("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
1530 stc_plugin = dlsym(handle_stc, "netconfig_stc_plugin");
1532 ERR("Can't load symbol: %s", dlerror());
1533 dlclose(handle_stc);
1535 netconfig_plugin_stc_enabled = TRUE;
1539 handle_battery = dlopen(BATTERY_PLUGIN_FILEPATH, RTLD_NOW);
1540 if (!handle_battery) {
1541 ERR("Can't load %s: %s", BATTERY_PLUGIN_FILEPATH, dlerror());
1543 battery_plugin = dlsym(handle_battery, "netconfig_battery_plugin");
1544 if (!battery_plugin) {
1545 ERR("Can't load symbol: %s", dlerror());
1546 dlclose(handle_battery);
1548 netconfig_plugin_battery_enabled = TRUE;
1554 void netconfig_plugin_deinit()
1556 if (netconfig_plugin_headed_enabled) {
1557 netconfig_plugin_headed_enabled = FALSE;
1558 dlclose(handle_headed);
1561 if (netconfig_plugin_telephony_enabled) {
1562 netconfig_plugin_telephony_enabled = FALSE;
1563 dlclose(handle_telephony);
1566 if (netconfig_plugin_stc_enabled) {
1567 netconfig_plugin_stc_enabled = FALSE;
1568 dlclose(handle_stc);
1571 if (netconfig_plugin_battery_enabled) {
1572 netconfig_plugin_battery_enabled = FALSE;
1573 dlclose(handle_battery);
1578 gboolean netconfig_get_headed_plugin_flag()
1580 return netconfig_plugin_headed_enabled;
1583 gboolean netconfig_get_telephony_plugin_flag()
1585 return netconfig_plugin_telephony_enabled;
1588 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1590 const char *key = NULL;
1592 if (!is_feature_checked[feature]) {
1594 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1595 key = ETHERNET_FEATURE;
1597 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1598 key = TETHERING_FEATURE;
1600 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1601 key = WIFI_DIRECT_FEATURE;
1603 case NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP:
1604 key = WIFI_SOFTAP_FEATURE;
1607 ERR("Uknown feature");
1611 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1612 ERR("Get feature is failed");
1615 is_feature_checked[feature] = true;
1617 return feature_supported[feature];
1620 void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr)
1625 t = (*bin >> 4) & 0x0f;
1629 else if (t >= 10 && t <= 16)
1630 *hexstr = (t - 10) + 'a';
1638 else if (t >= 10 && t <= 16)
1639 *hexstr = (t - 10) + 'a';
1649 bool __is_hidden_file(const char *file)
1651 /* exclude "." , "..", "settings" and hidden files */
1652 if( g_strcmp0( file, "." ) == 0 ||
1653 g_strcmp0( file, ".." ) == 0 || file[0]=='.' || g_strcmp0(file, "settings")==0 )
1661 int get_files_count(const char *path)
1664 struct dirent *dir = NULL;
1666 dfd = opendir(path);
1668 while ((dir = readdir(dfd)) != NULL) {
1669 if (__is_hidden_file(dir->d_name))
1672 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1683 char * get_least_recently_profile(const char *path)
1686 struct dirent *dir = NULL;
1687 unsigned long lastModified = (unsigned long)~0;
1689 dfd = opendir(path);
1691 while ((dir = readdir(dfd)) != NULL) {
1692 if (__is_hidden_file(dir->d_name))
1695 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1699 gchar *full_path = g_strdup_printf("%s/%s", path, dir->d_name);
1700 if (stat(full_path, &attr)== 0) {
1701 if(lastModified > attr.st_mtime)
1703 lastModified = attr.st_mtime;
1714 DBG("least recently path: [%s]", file);