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.
29 #include <net/route.h>
30 #include <arpa/inet.h>
33 #include <sys/ioctl.h>
34 #include <vconf-keys.h>
35 #include <syspopup_caller.h>
37 #include <bundle_internal.h>
38 #include <eventsystem.h>
43 #include "wifi-state.h"
45 #define WC_POPUP_EXTRA_DATA_KEY "http://samsung.com/appcontrol/data/connection_type"
47 static gboolean netconfig_device_picker_test = FALSE;
49 GKeyFile *netconfig_keyfile_load(const char *pathname)
51 GKeyFile *keyfile = NULL;
54 keyfile = g_key_file_new();
55 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
56 DBG("Unable to open %s, error %s", pathname, error->message);
59 g_key_file_free(keyfile);
66 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
70 gchar *keydata = NULL;
71 gchar *needle = NULL, *directory = NULL;
73 directory = g_strdup(pathname);
74 needle = g_strrstr(directory, "/");
79 if (directory == NULL || (*directory) == '\0') {
84 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
85 if (g_mkdir_with_parents(directory,
86 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
93 keydata = g_key_file_to_data(keyfile, &size, &error);
94 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
95 DBG("Unable to save %s, error %s", pathname, error->message);
99 chmod(pathname, S_IRUSR | S_IWUSR);
103 g_key_file_free(keyfile);
106 void netconfig_start_timer_seconds(guint secs,
107 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
111 if (callback == NULL) {
112 ERR("callback function is NULL");
116 if ((timer_id != NULL && *timer_id != 0)) {
117 ERR("timer already is registered");
121 t_id = g_timeout_add_seconds(secs, callback, user_data);
124 ERR("Can't add timer");
128 if (timer_id != NULL)
132 void netconfig_start_timer(guint msecs,
133 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
137 INFO("Register timer with callback pointer (%p)", callback);
139 if (callback == NULL) {
140 ERR("callback function is NULL");
144 if ((timer_id != NULL && *timer_id != 0)) {
145 ERR("timer already is registered");
149 t_id = g_timeout_add(msecs, callback, user_data);
152 ERR("Can't add timer");
156 if (timer_id != NULL)
160 void netconfig_stop_timer(guint *timer_id)
162 if (timer_id == NULL) {
163 ERR("timer is NULL");
167 if (*timer_id != 0) {
168 g_source_remove(*timer_id);
173 static gboolean __netconfig_test_device_picker()
175 char *favorite_wifi_service = NULL;
177 favorite_wifi_service = netconfig_wifi_get_favorite_service();
178 if (favorite_wifi_service != NULL) {
179 g_free(favorite_wifi_service);
186 static void __netconfig_pop_device_picker(void)
188 #if defined TIZEN_WEARABLE
190 app_control_h control = NULL;
192 ret = app_control_create(&control);
193 if (APP_CONTROL_ERROR_NONE != ret) {
194 DBG("failed to create app control");
198 app_control_add_extra_data(control, "viewtype", "scanlist");
200 app_control_set_app_id(control, "org.tizen.wifi");
201 ret = app_control_send_launch_request(control, NULL, NULL);
202 if (APP_CONTROL_ERROR_NONE == ret)
203 DBG("Launch request sent successfully");
205 app_control_destroy(control);
208 int wifi_ug_state = 0;
210 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
211 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
216 DBG("Launch Wi-Fi device picker");
217 syspopup_launch("wifi-qs", b);
223 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
225 if (__netconfig_test_device_picker() == TRUE)
226 __netconfig_pop_device_picker();
231 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method,
234 static guint netconfig_wifi_device_picker_service_timer = 0;
236 if (is_set_method != TRUE)
237 return netconfig_wifi_device_picker_service_timer;
239 if (netconfig_wifi_device_picker_service_timer != timer_id)
240 netconfig_wifi_device_picker_service_timer = timer_id;
242 return netconfig_wifi_device_picker_service_timer;
245 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
247 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
250 static guint __netconfig_wifi_device_picker_get_timer_id(void)
252 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
255 void netconfig_wifi_enable_device_picker_test(void)
257 netconfig_device_picker_test = TRUE;
260 void netconfig_wifi_device_picker_service_start(void)
262 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
265 if (netconfig_device_picker_test == TRUE)
266 netconfig_device_picker_test = FALSE;
270 #if defined TIZEN_WEARABLE
271 if (aul_app_is_running("org.tizen.wifi") > 0) {
272 DBG("wifi app is running");
278 if (netconfig_device_picker_test == TRUE)
279 netconfig_device_picker_test = FALSE;
283 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
284 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
288 DBG("Register device picker timer with %d milliseconds",
289 NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
291 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL,
292 __netconfig_wifi_try_device_picker, NULL, &timer_id);
294 __netconfig_wifi_device_picker_set_timer_id(timer_id);
297 void netconfig_wifi_device_picker_service_stop(void)
301 timer_id = __netconfig_wifi_device_picker_get_timer_id();
305 DBG("Clear device picker timer with timer_id %d", timer_id);
307 netconfig_stop_timer(&timer_id);
309 __netconfig_wifi_device_picker_set_timer_id(timer_id);
312 gboolean netconfig_is_wifi_direct_on(void)
314 #if defined TIZEN_P2P_ENABLE
315 int wifi_direct_state = 0;
317 vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
319 DBG("Wi-Fi direct mode %d", wifi_direct_state);
320 return (wifi_direct_state != 0) ? TRUE : FALSE;
326 gboolean netconfig_is_wifi_tethering_on(void)
328 #if defined TIZEN_TETHERING_ENABLE
329 int wifi_tethering_state = 0;
331 vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
333 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
334 if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
340 gboolean netconfig_interface_up(const char *ifname)
345 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
349 memset(&ifr, 0, sizeof(ifr));
350 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
352 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
357 ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
358 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
365 DBG("Successfully activated wireless interface");
369 gboolean netconfig_interface_down(const char *ifname)
374 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
378 memset(&ifr, 0, sizeof(ifr));
379 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
381 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
386 ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
387 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
394 DBG("Successfully de-activated wireless interface");
398 int netconfig_execute_file(const char *file_path,
399 char *const args[], char *const envs[])
405 register unsigned int index = 0;
407 while (args[index] != NULL) {
408 DBG("%s", args[index]);
412 if (!(pid = fork())) {
413 DBG("pid(%d), ppid (%d)", getpid(), getppid());
414 DBG("Inside child, exec (%s) command", file_path);
417 if (execve(file_path, args, envs) == -1) {
418 DBG("Fail to execute command (%s)", strerror(errno));
421 } else if (pid > 0) {
422 if (waitpid(pid, &status, 0) == -1)
423 DBG("wait pid (%u) status (%d)", pid, status);
425 if (WIFEXITED(status)) {
426 rv = WEXITSTATUS(status);
427 DBG("exited, status=%d", rv);
428 } else if (WIFSIGNALED(status)) {
429 DBG("killed by signal %d", WTERMSIG(status));
430 } else if (WIFSTOPPED(status)) {
431 DBG("stopped by signal %d", WSTOPSIG(status));
432 } else if (WIFCONTINUED(status)) {
439 DBG("failed to fork(%s)", strerror(errno));
443 static void on_clat_handler()
448 clat_pid = waitpid(-1, &state, WNOHANG);
450 DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
453 int netconfig_execute_clatd(const char *file_path, char *const args[])
458 register unsigned int index = 0;
460 struct sigaction act;
463 act.sa_handler = on_clat_handler;
464 sigemptyset(&act.sa_mask);
467 state = sigaction(SIGCHLD, &act, 0);
469 DBG("sigaction() : %d");
473 while (args[index] != NULL) {
474 DBG("%s", args[index]);
478 if (!(pid = fork())) {
479 DBG("pid(%d), ppid (%d)", getpid(), getppid());
480 DBG("Inside child, exec (%s) command", file_path);
483 if (execvp(file_path, args) == -1) {
484 ERR("Fail to execute command (%s)", strerror(errno));
487 } else if (pid > 0) {
488 ERR("Success to launch clatd");
492 DBG("failed to fork(%s)", strerror(errno));
496 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
502 memset(&rt, 0, sizeof(rt));
504 rt.rtmsg_dst_len = prefix_len;
506 rt.rtmsg_flags = RTF_UP | RTF_HOST;
508 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
513 if (gateway != NULL) {
514 rt.rtmsg_flags |= RTF_GATEWAY;
515 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
523 fd = socket(AF_INET6, SOCK_DGRAM, 0);
527 rt.rtmsg_ifindex = 0;
531 memset(&ifr, 0, sizeof(ifr));
532 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
533 ioctl(fd, SIOCGIFINDEX, &ifr);
534 rt.rtmsg_ifindex = ifr.ifr_ifindex;
537 if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
538 DBG("Failed to add route: %d\n", err);
548 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
554 memset(&rt, 0, sizeof(rt));
556 rt.rtmsg_dst_len = prefix_len;
558 rt.rtmsg_flags = RTF_UP | RTF_HOST;
560 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
565 if (gateway != NULL) {
566 rt.rtmsg_flags |= RTF_GATEWAY;
567 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
575 fd = socket(AF_INET6, SOCK_DGRAM, 0);
579 rt.rtmsg_ifindex = 0;
583 memset(&ifr, 0, sizeof(ifr));
584 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
585 ioctl(fd, SIOCGIFINDEX, &ifr);
586 rt.rtmsg_ifindex = ifr.ifr_ifindex;
589 if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
590 DBG("Failed to add route: %d\n", err);
600 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
602 #if defined TIZEN_P2P_ENABLE
604 DBG("Launch Wi-Fi direct daemon");
606 const char *path = "/usr/bin/wifi-direct-server.sh";
607 char *const args[] = { "wifi-direct-server.sh", "start", NULL };
608 char *const envs[] = { NULL };
610 ret = netconfig_execute_file(path, args, envs);
612 ERR("Failed to launch Wi-Fi direct daemon");
613 netconfig_error_wifi_direct_failed(context);
616 wifi_complete_launch_direct(wifi, context);
619 wifi_complete_launch_direct(wifi, context);
624 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
628 static gboolean is_found_noti_exists = FALSE;
629 static gboolean is_portal_noti_exists = FALSE;
632 ERR("Invalid notification");
636 if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
637 if (is_found_noti_exists == FALSE)
640 is_found_noti_exists = FALSE;
641 } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
642 if (is_found_noti_exists == TRUE)
645 is_found_noti_exists = TRUE;
646 } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
647 if (is_portal_noti_exists == TRUE)
650 is_portal_noti_exists = TRUE;
651 } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
652 if (is_portal_noti_exists == FALSE)
655 is_portal_noti_exists = FALSE;
659 bundle_add(b, "_SYSPOPUP_TYPE_", noti);
662 DBG("ssid (%s)", ssid);
663 bundle_add(b, "_AP_NAME_", ssid);
666 ret = aul_launch_app("net.netpopup", b);
671 ERR("Unable to launch noti-popup. Err = %d", ret);
675 DBG("Successfully sent notification (%s)", noti);
679 int netconfig_send_message_to_net_popup(const char *title,
680 const char *content, const char *type, const char *ssid)
683 bundle *b = bundle_create();
685 bundle_add(b, "_SYSPOPUP_TITLE_", title);
686 bundle_add(b, "_SYSPOPUP_CONTENT_", content);
687 bundle_add(b, "_SYSPOPUP_TYPE_", type);
688 bundle_add(b, "_AP_NAME_", ssid);
690 ret = aul_launch_app("net.netpopup", b);
697 void netconfig_set_system_event(const char * sys_evt, const char * evt_key, const char * evt_val)
701 DBG("System event set [%s : %s : %s]", sys_evt, evt_key, evt_val);
704 bundle_add_str(b, evt_key, evt_val);
705 eventsystem_send_system_event(sys_evt, b);
709 #if defined TIZEN_WEARABLE
710 int wc_launch_syspopup(netconfig_wcpopup_type_e type)
718 ERR("Failed to create bundle");
723 case WC_POPUP_TYPE_SESSION_OVERLAPPED:
724 bundle_add(b, "event-type", "wps-session-overlapped");
726 case WC_POPUP_TYPE_WIFI_CONNECTED:
727 ssid = vconf_get_str(VCONFKEY_WIFI_CONNECTED_AP_NAME);
729 ERR("Failed to get connected ap ssid");
730 ssid = g_strdup(" ");
732 bundle_add(b, "event-type", "wifi-connected");
733 bundle_add(b, "ssid", ssid);
737 case WC_POPUP_TYPE_WIFI_RESTRICT:
738 bundle_add(b, "event-type", "wifi-restrict");
741 ERR("Popup is not supported[%d]", type);
746 ret = syspopup_launch("wc-syspopup", b);
748 ERR("Failed to launch syspopup");
755 int wc_launch_popup(netconfig_wcpopup_type_e type)
758 app_control_h app_control = NULL;
760 ret = app_control_create(&app_control);
761 if (ret != APP_CONTROL_ERROR_NONE) {
762 ERR("Failed to create appcontrol[%d]", ret);
767 case WC_POPUP_TYPE_CAPTIVE_PORTAL:
768 app_control_add_extra_data(app_control, WC_POPUP_EXTRA_DATA_KEY, "captive-portal");
771 ERR("Popup is not supported[%d]", type);
772 app_control_destroy(app_control);
776 app_control_set_app_id(app_control, "com.samsung.weconn-popup");
777 ret = app_control_send_launch_request(app_control, NULL, NULL);
778 if (ret != APP_CONTROL_ERROR_NONE) {
779 DBG("failed appcontrol launch request [%d]", ret);
780 app_control_destroy(app_control);
784 app_control_destroy(app_control);
790 void netconfig_set_vconf_int(const char * key, int value)
794 DBG("[%s: %d]", key, value);
796 ret = vconf_set_int(key, value);
798 ERR("Failed to set");
801 void netconfig_set_vconf_str(const char * key, const char * value)
805 DBG("[%s: %s]", key, value);
807 ret = vconf_set_str(key, value);
809 ERR("Failed to set");
812 char* netconfig_get_env(const char *key)
815 char buf[256], *entry = NULL, *value = NULL, *last;
821 fp = fopen(NETCONFIG_TIZENMOBILEENV, "r");
825 while (fgets(buf, sizeof(buf), fp)) {
827 entry = strtok_r(entry, "=", &last);
829 if (strstr(entry, key)) {
830 entry = strtok_r(NULL, "\n", &last);
833 value = (char*)malloc(len+1);
834 g_strlcpy(value, entry, len+1);
837 value = (char*)malloc(sizeof(char));
838 g_strlcpy(value, "\n", sizeof(char));