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"
57 #define CONNMAN_MAINFILE "/etc/connman/main.conf"
58 #define CONNMAN_WIFI_DEF_IFNAME "DefaultWifiInterface"
60 static gboolean netconfig_device_picker_test = FALSE;
61 static int mdnsd_ref_count = 0;
65 } dnssd_conn_destroy_data;
67 static gboolean netconfig_plugin_headed_enabled = FALSE;
68 static gboolean netconfig_plugin_telephony_enabled = FALSE;
69 static gboolean netconfig_plugin_stc_enabled = FALSE;
70 static gboolean netconfig_plugin_battery_enabled = FALSE;
71 static void *handle_headed;
72 static void *handle_telephony;
73 static void *handle_stc;
74 static void *handle_battery;
75 static struct netconfig_headed_plugin_t *headed_plugin;
76 static struct netconfig_telephony_plugin_t *telephony_plugin;
77 static struct netconfig_stc_plugin_t *stc_plugin;
78 static struct netconfig_battery_plugin_t *battery_plugin;
80 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
81 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
83 gboolean netconfig_check_mac_address(const char *service, const char *mac_address)
85 int service_index = 0;
87 const char *org = NULL;
88 const char *dst = NULL;
91 if (g_str_has_prefix(service, "wifi_") == FALSE)
94 service_index = strlen("wifi_");
95 for (i = 0; i < 6; i++) {
96 org = &service[service_index];
97 dst = &mac_address[mac_index];
99 if (g_ascii_strncasecmp(org, dst, 2) != 0)
109 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
116 length = strlen(passphrase);
118 if (g_str_has_suffix(service, "psk") == TRUE) {
120 for (int i = 0; i < 64; i++)
121 if (!isxdigit((unsigned char)passphrase[i]))
123 } else if (length < 8 || length > 63)
125 } else if (g_str_has_suffix(service, "wep") == TRUE) {
126 if (length == 10 || length == 26) {
127 for (int i = 0; i < length; i++)
128 if (!isxdigit((unsigned char)passphrase[i]))
130 } else if (length != 5 && length != 13)
137 GKeyFile *netconfig_keyfile_load(const char *pathname)
139 GKeyFile *keyfile = NULL;
140 GError *error = NULL;
142 keyfile = g_key_file_new();
143 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
144 DBG("Unable to open %s, error %s", pathname, error->message);
147 g_key_file_free(keyfile);
151 DBG("loaded keyfile %s", pathname);
155 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
158 GError *error = NULL;
159 gchar *keydata = NULL;
160 gchar *needle = NULL, *directory = NULL;
162 directory = g_strdup(pathname);
163 if (directory == NULL) {
164 ERR("directory is NULL");
168 needle = g_strrstr(directory, "/");
172 if ((*directory) == '\0') {
174 ERR("directory is NULL");
178 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
179 if (g_mkdir_with_parents(directory,
180 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
182 ERR("failed to make directory");
188 keydata = g_key_file_to_data(keyfile, &size, &error);
189 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
190 ERR("Unable to save %s, error %s", pathname, error->message);
194 if (chmod(pathname, S_IRUSR | S_IWUSR) < 0)
195 DBG("Failed to change mode");
197 DBG("Successfully saved keyfile %s", pathname);
202 void netconfig_start_timer_seconds(guint secs,
203 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
207 if (callback == NULL) {
208 ERR("callback function is NULL");
212 if ((timer_id != NULL && *timer_id != 0)) {
213 ERR("timer already is registered");
217 t_id = g_timeout_add_seconds(secs, callback, user_data);
220 ERR("Can't add timer");
224 if (timer_id != NULL)
228 void netconfig_start_timer(guint msecs,
229 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
233 INFO("Register timer with callback pointer (%p)", callback);
235 if (callback == NULL) {
236 ERR("callback function is NULL");
240 if ((timer_id != NULL && *timer_id != 0)) {
241 ERR("timer already is registered");
245 t_id = g_timeout_add(msecs, callback, user_data);
248 ERR("Can't add timer");
252 if (timer_id != NULL)
256 void netconfig_stop_timer(guint *timer_id)
258 if (timer_id == NULL) {
259 ERR("timer is NULL");
263 if (*timer_id != 0) {
264 g_source_remove(*timer_id);
269 static gboolean __netconfig_test_device_picker()
271 char *favorite_wifi_service = NULL;
273 favorite_wifi_service = wifi_get_favorite_service();
274 if (favorite_wifi_service != NULL) {
275 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
276 g_free(favorite_wifi_service);
283 static void __netconfig_pop_device_picker(void)
285 if (!netconfig_plugin_headed_enabled)
291 headed_plugin->pop_device_picker();
294 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
296 if (__netconfig_test_device_picker() == TRUE)
297 __netconfig_pop_device_picker();
302 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
304 static guint netconfig_wifi_device_picker_service_timer = 0;
306 if (is_set_method != TRUE)
307 return netconfig_wifi_device_picker_service_timer;
309 if (netconfig_wifi_device_picker_service_timer != timer_id)
310 netconfig_wifi_device_picker_service_timer = timer_id;
312 return netconfig_wifi_device_picker_service_timer;
315 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
317 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
320 static guint __netconfig_wifi_device_picker_get_timer_id(void)
322 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
325 void netconfig_wifi_enable_device_picker_test(void)
327 netconfig_device_picker_test = TRUE;
330 void netconfig_wifi_device_picker_service_start(void)
332 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
335 if (netconfig_device_picker_test == TRUE)
336 netconfig_device_picker_test = FALSE;
342 netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
343 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
346 DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
347 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
349 __netconfig_wifi_device_picker_set_timer_id(timer_id);
352 void netconfig_wifi_device_picker_service_stop(void)
356 timer_id = __netconfig_wifi_device_picker_get_timer_id();
360 DBG("Clear device picker timer with timer_id %d", timer_id);
362 netconfig_stop_timer(&timer_id);
364 __netconfig_wifi_device_picker_set_timer_id(timer_id);
367 gboolean netconfig_is_wifi_direct_on(void)
369 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
372 int wifi_direct_state = 0;
374 netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
376 DBG("Wi-Fi direct mode %d", wifi_direct_state);
377 return (wifi_direct_state != 0) ? TRUE : FALSE;
380 gboolean netconfig_is_wifi_tethering_on(void)
382 if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
383 int wifi_tethering_state = 0;
385 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
386 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
387 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
388 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
389 DBG("Mobile AP is on");
394 DBG("Mobile AP is off");
398 gboolean netconfig_interface_up(const char *ifname)
403 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
405 ERR("socket failed %d", errno);
409 memset(&ifr, 0, sizeof(ifr));
410 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
412 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
413 ERR("Fail to get IFFLAGS %d", errno);
418 DBG("IFFLAGS: %x", ifr.ifr_flags);
419 ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
420 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
421 ERR("Fail to set IFFLAGS %d", errno);
428 DBG("Successfully activated wireless interface %s", ifname);
432 gboolean netconfig_interface_down(const char *ifname)
437 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
439 ERR("socket failed %d", errno);
443 memset(&ifr, 0, sizeof(ifr));
444 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
446 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
447 ERR("Fail to get IFFLAGS %d", errno);
452 DBG("IFFLAGS: %x", ifr.ifr_flags);
453 ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
454 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
455 ERR("Fail to set IFFLAGS %d", errno);
462 DBG("Successfully de-activated wireless interface %s", ifname);
466 int netconfig_execute_file(const char *file_path,
467 char *const args[], char *const envs[])
473 register unsigned int index = 0;
474 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
476 while (args[index] != NULL) {
477 DBG("%s", args[index]);
481 if (!(pid = fork())) {
482 DBG("pid(%d), ppid (%d)", getpid(), getppid());
483 DBG("Inside child, exec (%s) command", file_path);
486 if (execve(file_path, args, envs) == -1) {
487 DBG("Fail to execute command (%s)",
488 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
491 } else if (pid > 0) {
492 if (waitpid(pid, &status, 0) == -1)
493 DBG("wait pid (%u) status (%d)", pid, status);
495 if (WIFEXITED(status)) {
496 rv = WEXITSTATUS(status);
497 DBG("exited, status=%d", rv);
498 } else if (WIFSIGNALED(status)) {
499 DBG("killed by signal %d", WTERMSIG(status));
500 } else if (WIFSTOPPED(status)) {
501 DBG("stopped by signal %d", WSTOPSIG(status));
502 } else if (WIFCONTINUED(status)) {
509 DBG("failed to fork(%s)",
510 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
514 int netconfig_execute_cmd(const char *cmd)
523 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
526 DBG("command: %s", cmd);
528 args = g_strsplit_set(cmd, " ", -1);
530 if (!(pid = fork())) {
531 DBG("pid(%d), ppid (%d)", getpid(), getppid());
534 if (execv(args[0], args) == -1) {
535 DBG("Fail to execute command (%s)",
536 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
540 } else if (pid > 0) {
541 if (waitpid(pid, &status, 0) == -1)
542 DBG("wait pid (%u) status (%d)", pid, status);
544 if (WIFEXITED(status)) {
545 rv = WEXITSTATUS(status);
546 DBG("exited, status=%d", rv);
547 } else if (WIFSIGNALED(status)) {
548 DBG("killed by signal %d", WTERMSIG(status));
549 } else if (WIFSTOPPED(status)) {
550 DBG("stopped by signal %d", WSTOPSIG(status));
551 } else if (WIFCONTINUED(status)) {
559 DBG("failed to fork(%s)",
560 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
566 static void on_clat_handler()
571 clat_pid = waitpid(-1, &state, WNOHANG);
573 DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
576 int netconfig_execute_clatd(const char *file_path, char *const args[])
581 register unsigned int index = 0;
582 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
584 struct sigaction act;
587 act.sa_handler = on_clat_handler;
588 sigemptyset(&act.sa_mask);
591 state = sigaction(SIGCHLD, &act, 0);
593 DBG("sigaction() : %d", state);
597 while (args[index] != NULL) {
598 DBG("%s", args[index]);
602 if (!(pid = fork())) {
603 DBG("pid(%d), ppid (%d)", getpid(), getppid());
604 DBG("Inside child, exec (%s) command", file_path);
607 if (execvp(file_path, args) == -1) {
608 ERR("Fail to execute command (%s)",
609 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
612 } else if (pid > 0) {
613 ERR("Success to launch clatd");
617 DBG("failed to fork(%s)",
618 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
622 static void no_wait_signal_handler()
627 child_pid = waitpid(-1, &state, WNOHANG);
629 DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
632 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
637 register unsigned int index = 0;
639 struct sigaction act;
641 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
643 act.sa_handler = no_wait_signal_handler;
644 sigemptyset(&act.sa_mask);
647 state = sigaction(SIGCHLD, &act, 0);
649 DBG("sigaction() : %d", state);
653 while (args[index] != NULL) {
654 DBG("%s", args[index]);
658 if (!(pid = fork())) {
659 DBG("pid(%d), ppid (%d)", getpid(), getppid());
660 DBG("Inside child, exec (%s) command", file_path);
663 if (execvp(file_path, args) == -1) {
664 ERR("Fail to execute command (%s)",
665 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
668 } else if (pid > 0) {
669 ERR("Successfully launched child process");
673 DBG("failed to fork(%s)",
674 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
678 int __netconfig_get_interface_index(const char *interface_name)
683 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
685 if (interface_name == NULL) {
686 DBG("Inteface name is NULL");
691 sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
693 DBG("Failed to create socket : %s",
694 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
698 memset(&ifr, 0, sizeof(ifr));
699 strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
700 result = ioctl(sock, SIOCGIFINDEX, &ifr);
704 DBG("Failed to get ifr index: %s",
705 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
709 return ifr.ifr_ifindex;
712 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
716 struct sockaddr_in addr_in;
718 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
720 memset(&ifr, 0, sizeof(ifr));
722 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
724 if (ifr.ifr_ifindex < 0)
727 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
729 memset(&rt, 0, sizeof(rt));
731 rt.rt_flags = RTF_UP | RTF_HOST;
732 memset(&addr_in, 0, sizeof(struct sockaddr_in));
733 addr_in.sin_family = address_family;
734 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
735 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
737 memset(&addr_in, 0, sizeof(struct sockaddr_in));
738 addr_in.sin_family = address_family;
739 addr_in.sin_addr.s_addr = INADDR_ANY;
740 memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
742 memset(&addr_in, 0, sizeof(struct sockaddr_in));
743 addr_in.sin_family = AF_INET;
744 addr_in.sin_addr.s_addr = inet_addr(subnet);
745 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
747 rt.rt_dev = ifr.ifr_name;
750 sock = socket(PF_INET, SOCK_DGRAM, 0);
753 DBG("Failed to create socket : %s",
754 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
758 if (ioctl(sock, SIOCADDRT, &rt) < 0) {
759 DBG("Failed to set route address : %s",
760 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
770 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
774 struct sockaddr_in addr_in;
776 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
778 memset(&ifr, 0, sizeof(ifr));
779 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
781 if (ifr.ifr_ifindex < 0)
784 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
786 memset(&rt, 0, sizeof(rt));
788 rt.rt_flags = RTF_UP;
789 memset(&addr_in, 0, sizeof(struct sockaddr_in));
790 addr_in.sin_family = address_family;
791 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
792 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
794 memset(&addr_in, 0, sizeof(struct sockaddr_in));
795 addr_in.sin_family = address_family;
796 addr_in.sin_addr.s_addr = inet_addr(subnet);
797 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
798 rt.rt_dev = ifr.ifr_name;
801 sock = socket(PF_INET, SOCK_DGRAM, 0);
804 DBG("Failed to create socket : %s",
805 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
809 if (ioctl(sock, SIOCDELRT, &rt) < 0) {
810 DBG("Failed to set route address : %s",
811 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
821 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
826 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
828 memset(&rt, 0, sizeof(rt));
830 rt.rtmsg_dst_len = prefix_len;
832 rt.rtmsg_flags = RTF_UP | RTF_HOST;
835 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
836 DBG("inet_pton failed : %s",
837 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
841 if (gateway != NULL) {
842 rt.rtmsg_flags |= RTF_GATEWAY;
843 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
844 DBG("inet_pton failed : %s",
845 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
852 fd = socket(AF_INET6, SOCK_DGRAM, 0);
854 DBG("Failed to create socket : %s",
855 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
859 rt.rtmsg_ifindex = 0;
863 memset(&ifr, 0, sizeof(ifr));
864 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
865 ioctl(fd, SIOCGIFINDEX, &ifr);
866 rt.rtmsg_ifindex = ifr.ifr_ifindex;
869 if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
870 DBG("Failed to add route: %s",
871 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
881 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
887 memset(&rt, 0, sizeof(rt));
889 rt.rtmsg_dst_len = prefix_len;
891 rt.rtmsg_flags = RTF_UP | RTF_HOST;
893 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
898 if (gateway != NULL) {
899 rt.rtmsg_flags |= RTF_GATEWAY;
900 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
908 fd = socket(AF_INET6, SOCK_DGRAM, 0);
912 rt.rtmsg_ifindex = 0;
916 memset(&ifr, 0, sizeof(ifr));
917 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
918 ioctl(fd, SIOCGIFINDEX, &ifr);
919 rt.rtmsg_ifindex = ifr.ifr_ifindex;
922 if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
923 DBG("Failed to del route: %d\n", err);
933 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
935 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
936 wifi_complete_launch_direct(wifi, context);
941 DBG("Launch Wi-Fi direct daemon");
943 const char *path = "/usr/bin/wifi-direct-server.sh";
944 char *const args[] = { "wifi-direct-server.sh", "start", NULL };
945 char *const envs[] = { NULL };
947 ret = netconfig_execute_file(path, args, envs);
949 ERR("Failed to launch Wi-Fi direct daemon");
950 netconfig_error_wifi_direct_failed(context);
954 wifi_complete_launch_direct(wifi, context);
958 int execute_mdnsd_script(char* op)
960 const char *path = "/usr/bin/mdnsresponder-server.sh";
961 char *const args[] = { "mdnsresponder-server.sh", op, NULL };
962 char *const envs[] = { NULL };
964 return netconfig_execute_file(path, args, envs);
967 static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
968 const gchar *Name, const gchar *path, const gchar *interface,
969 const gchar *sig, GVariant *param, gpointer user_data)
974 dnssd_conn_destroy_data *data = user_data;
975 GDBusConnection *connection = NULL;
976 connection = netdbus_get_connection();
981 g_variant_get(param, "(sss)", &name, &old, &new);
983 if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
984 DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
987 g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
988 if (mdnsd_ref_count == 0) {
989 if (execute_mdnsd_script("stop") < 0)
990 ERR("Failed to stop mdnsresponder daemon");
996 g_free(data->conn_name);
1001 static void register_dnssd_conn_destroy_signal(gchar *name)
1003 dnssd_conn_destroy_data *data;
1004 GDBusConnection *connection = NULL;
1005 connection = netdbus_get_connection();
1007 if (connection == NULL) {
1008 ERR("Failed to get GDbus Connection");
1012 data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
1015 ERR("Out of Memory!");
1019 data->conn_name = g_strdup(name);
1021 data->conn_id = g_dbus_connection_signal_subscribe(connection,
1022 DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
1023 "NameOwnerChanged", NULL, name,
1024 G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
1029 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
1032 DBG("Launch mdnsresponder daemon");
1034 if (execute_mdnsd_script("start") < 0) {
1035 ERR("Failed to launch mdnsresponder daemon");
1036 netconfig_error_invalid_parameter(context);
1041 register_dnssd_conn_destroy_signal(name);
1042 DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
1044 network_complete_launch_mdns(object, context);
1048 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
1050 if (!netconfig_plugin_headed_enabled)
1056 return headed_plugin->send_notification_to_net_popup(noti, ssid);
1059 int netconfig_send_message_to_net_popup(const char *title,
1060 const char *content, const char *type, const char *ssid)
1062 if (!netconfig_plugin_headed_enabled)
1068 return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
1071 int netconfig_send_restriction_to_net_popup(const char *title,
1072 const char *type, const char *restriction)
1074 if (!netconfig_plugin_headed_enabled)
1080 return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
1083 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
1085 if (!netconfig_plugin_headed_enabled)
1091 headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1094 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1096 if (!netconfig_plugin_headed_enabled)
1102 headed_plugin->pop_wifi_connected_poppup(ssid);
1105 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1107 if (!netconfig_plugin_telephony_enabled)
1110 if (!telephony_plugin)
1113 telephony_plugin->get_telephony_network_type(svctype, pstype);
1116 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1118 if (!netconfig_plugin_telephony_enabled)
1121 if (!telephony_plugin)
1124 return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1127 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1128 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1130 if (!netconfig_plugin_telephony_enabled)
1131 return NETCONFIG_ERROR_INTERNAL;
1133 if (!telephony_plugin)
1134 return NETCONFIG_ERROR_INTERNAL;
1136 return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1139 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1140 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1142 if (!netconfig_plugin_telephony_enabled)
1145 if (!telephony_plugin)
1148 return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1151 gboolean netconfig_tapi_check_sim_state(void)
1153 if (!netconfig_plugin_telephony_enabled)
1156 if (!telephony_plugin)
1159 return telephony_plugin->tapi_check_sim_state();
1162 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1163 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1165 if (!netconfig_plugin_telephony_enabled)
1168 if (!telephony_plugin)
1171 return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1174 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1175 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1177 if (!netconfig_plugin_telephony_enabled)
1180 if (!telephony_plugin)
1183 return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1186 static void __netconfig_stc_get_dn_stats(time_t from, time_t to, GSList **list)
1188 if (!netconfig_plugin_stc_enabled)
1194 return stc_plugin->get_stc_dn_stats(from, to, list);
1197 static void __netconfig_stc_get_wifi_stats(time_t from, time_t to, GSList **list)
1199 if (!netconfig_plugin_stc_enabled)
1205 return stc_plugin->get_stc_wifi_stats(from, to, list);
1208 void netconfig_battery_start_dn(void)
1210 if (!netconfig_plugin_battery_enabled)
1213 if (!battery_plugin)
1216 return battery_plugin->start_dn_data();
1219 void netconfig_battery_end_dn(void)
1221 if (!netconfig_plugin_battery_enabled)
1224 if (!battery_plugin)
1227 return battery_plugin->end_dn_data(0, __netconfig_stc_get_dn_stats);
1230 void netconfig_battery_update_dn_rssi(int rssi)
1232 if (!netconfig_plugin_battery_enabled)
1235 if (!battery_plugin)
1238 return battery_plugin->update_dn_rssi(rssi);
1241 void netconfig_battery_start_wifi(void)
1243 if (!netconfig_plugin_battery_enabled)
1246 if (!battery_plugin)
1249 return battery_plugin->start_wifi_data();
1252 void netconfig_battery_end_wifi(void)
1254 if (!netconfig_plugin_battery_enabled)
1257 if (!battery_plugin)
1260 return battery_plugin->end_wifi_data(0, __netconfig_stc_get_wifi_stats);
1263 void netconfig_battery_update_wifi_scan(int state)
1265 if (!netconfig_plugin_battery_enabled)
1268 if (!battery_plugin)
1271 return battery_plugin->update_wifi_scan(state);
1274 void netconfig_battery_update_wifi_rssi(int rssi)
1276 if (!netconfig_plugin_battery_enabled)
1279 if (!battery_plugin)
1282 return battery_plugin->update_wifi_rssi(rssi);
1285 void netconfig_battery_get_dn_list(void *data)
1287 if (!netconfig_plugin_battery_enabled)
1290 if (!battery_plugin)
1293 return battery_plugin->get_battery_dn_list(data, __netconfig_stc_get_dn_stats);
1296 void netconfig_battery_get_wifi_list(void *data)
1298 if (!netconfig_plugin_battery_enabled)
1301 if (!battery_plugin)
1304 return battery_plugin->get_battery_wifi_list(data, __netconfig_stc_get_wifi_stats);
1307 void netconfig_set_vconf_int(const char * key, int value, gboolean log)
1312 DBG("[%s: %d]", key, value);
1314 ret = vconf_set_int(key, value);
1315 if (ret != VCONF_OK)
1316 ERR("Failed to set");
1319 void netconfig_set_vconf_str(const char * key, const char * value, gboolean log)
1324 DBG("[%s: %s]", key, value);
1326 ret = vconf_set_str(key, value);
1327 if (ret != VCONF_OK)
1328 ERR("Failed to set");
1331 int netconfig_vconf_get_int(const char * key, int *value)
1335 ret = vconf_get_int(key, value);
1336 if (ret != VCONF_OK) {
1337 ERR("Failed to get vconfkey [%s] value", key);
1344 int netconfig_vconf_get_bool(const char * key, int *value)
1348 ret = vconf_get_bool(key, value);
1349 if (ret != VCONF_OK) {
1350 ERR("Failed to get vconfkey [%s] value", key);
1357 char* netconfig_get_env(const char *key)
1360 char buf[256], *entry = NULL, *value = NULL, *last;
1366 fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1370 while (fgets(buf, sizeof(buf), fp)) {
1372 entry = strtok_r(entry, "=", &last);
1374 if (strstr(entry, key)) {
1375 entry = strtok_r(NULL, "\n", &last);
1377 len = strlen(entry);
1378 value = (char*)malloc(len+1);
1379 g_strlcpy(value, entry, len+1);
1381 value = (char*)malloc(sizeof(char));
1382 g_strlcpy(value, "\n", sizeof(char));
1393 void netconfig_set_mac_address_to_vconf(const char *def_mac)
1397 mac_len = strlen(def_mac);
1399 ERR("def_mac is empty");
1403 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, def_mac, TRUE);
1406 void netconfig_set_mac_address_from_file(void)
1409 char mac_str[MAC_ADDRESS_MAX_LEN];
1410 gchar *mac_lower_str = NULL;
1413 file = fopen(MAC_INFO_FILEPATH, "r");
1415 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1416 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1418 ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1422 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1423 ERR("Fail to read mac address");
1428 mac_len = strlen(mac_str);
1430 ERR("mac.info is empty");
1435 mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1436 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str, TRUE);
1438 g_free(mac_lower_str);
1442 char *netconfig_get_mac_address_from_file(const char *ifname)
1445 char file_path[PATH_MAX] = {0, };
1446 char mac_str[MAC_ADDRESS_MAX_LEN];
1452 g_snprintf(file_path, sizeof(file_path),
1453 "/sys/class/net/%s/address", ifname);
1455 DBG("ifname: %s", ifname);
1456 DBG("file_path: %s", file_path);
1457 file = fopen(file_path, "r");
1459 ERR("Fail to open %s", file_path);
1462 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1463 ERR("Fail to read mac address");
1468 mac_len = strlen(mac_str);
1470 ERR("mac is empty");
1474 DBG("address: %s", mac_str);
1477 return g_strdup(mac_str);
1480 char *netconfig_get_default_ifname_from_file(void)
1482 GKeyFile *keyfile = NULL;
1484 keyfile = netconfig_keyfile_load(CONNMAN_MAINFILE);
1485 if (keyfile == NULL) {
1486 ERR("keyfile[%s] is NULL", CONNMAN_MAINFILE);
1490 char *str = g_key_file_get_string(keyfile, "General",
1491 CONNMAN_WIFI_DEF_IFNAME, NULL);
1492 g_key_file_free(keyfile);
1504 if (strlen(str) >= IFNAMSIZ) {
1509 DBG("ifname[%s]", str);
1513 int netconfig_freq_to_channel(int freq)
1515 if (freq < 2412 || freq > 5825 ||
1516 (freq > 2484 && freq < 5180)) {
1517 ERR("Invalid Frequence Range");
1521 return 36 + (freq - 5180)/5;
1522 else if (freq <= 2472)
1523 return 1 + (freq - 2412)/5;
1524 else if (freq == 2484)
1530 int netconfig_get_operating_class(int freq)
1535 channel = netconfig_freq_to_channel(freq);
1538 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1539 if (channel >= 1 && channel <= 13)
1541 /* Operating class 115 - 5 GHz, channels 36-48 */
1542 else if (channel >= 36 && channel <= 48)
1544 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1548 INFO("Operating Class is [%d]", oper_class);
1554 tizen_profile_t _get_tizen_profile()
1556 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1557 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1561 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1562 switch (*profileName) {
1565 profile = TIZEN_PROFILE_MOBILE;
1569 profile = TIZEN_PROFILE_WEARABLE;
1573 profile = TIZEN_PROFILE_TV;
1577 profile = TIZEN_PROFILE_IVI;
1579 default: // common or unknown ==> ALL ARE COMMON.
1580 profile = TIZEN_PROFILE_COMMON;
1587 void netconfig_plugin_init()
1589 handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1590 if (!handle_headed) {
1591 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1593 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1594 if (!headed_plugin) {
1595 ERR("Can't load symbol: %s", dlerror());
1596 dlclose(handle_headed);
1598 netconfig_plugin_headed_enabled = TRUE;
1602 handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1603 if (!handle_telephony) {
1604 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1606 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1607 if (!telephony_plugin) {
1608 ERR("Can't load symbol: %s", dlerror());
1609 dlclose(handle_telephony);
1611 netconfig_plugin_telephony_enabled = TRUE;
1615 handle_stc = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
1617 ERR("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
1619 stc_plugin = dlsym(handle_stc, "netconfig_stc_plugin");
1621 ERR("Can't load symbol: %s", dlerror());
1622 dlclose(handle_stc);
1624 netconfig_plugin_stc_enabled = TRUE;
1628 handle_battery = dlopen(BATTERY_PLUGIN_FILEPATH, RTLD_NOW);
1629 if (!handle_battery) {
1630 ERR("Can't load %s: %s", BATTERY_PLUGIN_FILEPATH, dlerror());
1632 battery_plugin = dlsym(handle_battery, "netconfig_battery_plugin");
1633 if (!battery_plugin) {
1634 ERR("Can't load symbol: %s", dlerror());
1635 dlclose(handle_battery);
1637 netconfig_plugin_battery_enabled = TRUE;
1643 void netconfig_plugin_deinit()
1645 if (netconfig_plugin_headed_enabled) {
1646 netconfig_plugin_headed_enabled = FALSE;
1647 dlclose(handle_headed);
1650 if (netconfig_plugin_telephony_enabled) {
1651 netconfig_plugin_telephony_enabled = FALSE;
1652 dlclose(handle_telephony);
1655 if (netconfig_plugin_stc_enabled) {
1656 netconfig_plugin_stc_enabled = FALSE;
1657 dlclose(handle_stc);
1660 if (netconfig_plugin_battery_enabled) {
1661 netconfig_plugin_battery_enabled = FALSE;
1662 dlclose(handle_battery);
1667 gboolean netconfig_get_headed_plugin_flag()
1669 return netconfig_plugin_headed_enabled;
1672 gboolean netconfig_get_telephony_plugin_flag()
1674 return netconfig_plugin_telephony_enabled;
1677 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1679 const char *key = NULL;
1681 if (!is_feature_checked[feature]) {
1683 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1684 key = ETHERNET_FEATURE;
1686 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1687 key = TETHERING_FEATURE;
1689 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1690 key = WIFI_DIRECT_FEATURE;
1692 case NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP:
1693 key = WIFI_SOFTAP_FEATURE;
1696 ERR("Uknown feature");
1700 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1701 ERR("Get feature is failed");
1704 is_feature_checked[feature] = true;
1706 return feature_supported[feature];
1709 void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr)
1714 t = (*bin >> 4) & 0x0f;
1718 else if (t >= 10 && t <= 16)
1719 *hexstr = (t - 10) + 'a';
1727 else if (t >= 10 && t <= 16)
1728 *hexstr = (t - 10) + 'a';
1738 bool __is_hidden_file(const char *file)
1740 /* exclude "." , "..", "settings" and hidden files */
1741 if( g_strcmp0( file, "." ) == 0 ||
1742 g_strcmp0( file, ".." ) == 0 || file[0]=='.' || g_strcmp0(file, "settings")==0 )
1750 int get_files_count(const char *path)
1753 struct dirent *dir = NULL;
1755 dfd = opendir(path);
1757 while ((dir = readdir(dfd)) != NULL) {
1758 if (__is_hidden_file(dir->d_name))
1761 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1772 char * get_least_recently_profile(const char *path)
1775 struct dirent *dir = NULL;
1776 unsigned long lastModified = (unsigned long)~0;
1778 dfd = opendir(path);
1780 while ((dir = readdir(dfd)) != NULL) {
1781 if (__is_hidden_file(dir->d_name))
1784 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1788 gchar *full_path = g_strdup_printf("%s/%s", path, dir->d_name);
1789 if (stat(full_path, &attr)== 0) {
1790 if(lastModified > attr.st_mtime)
1792 lastModified = attr.st_mtime;
1803 DBG("least recently path: [%s]", file);