From: Seonah Moon Date: Mon, 11 Jan 2016 09:53:34 +0000 (+0900) Subject: [capi-tethering] Enabling/disabling the mac-filtering feature X-Git-Tag: submit/tizen/20160211.121512^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6b22cacbc3d40ff3c8eff6d2944334a775b600e0;p=platform%2Fcore%2Fapi%2Ftethering.git [capi-tethering] Enabling/disabling the mac-filtering feature 1. Set/Get enable mac-filtering feature 2. Add/Remove mac address allowed/white list 3. Add/Remove mac address blocked/black list Change-Id: If58575eb32f4bb31be19e8073d572d2689e0c67e Signed-off-by: Seonah Moon --- diff --git a/include/tethering_private.h b/include/tethering_private.h index d82c263..f2b78b3 100644 --- a/include/tethering_private.h +++ b/include/tethering_private.h @@ -295,6 +295,7 @@ typedef struct { char passphrase[TETHERING_WIFI_KEY_MAX_LEN + 1]; tethering_wifi_security_type_e sec_type; bool visibility; + bool mac_filter; int channel; tethering_wifi_mode_type_e mode_type; } __tethering_h; @@ -321,6 +322,7 @@ typedef struct { char mode[TETHERING_WIFI_MODE_MAX_LEN + 1]; tethering_wifi_security_type_e sec_type; bool visibility; + bool mac_filter; int channel; } _softap_settings_t; diff --git a/src/tethering.c b/src/tethering.c index 2efc80b..684290e 100755 --- a/src/tethering.c +++ b/src/tethering.c @@ -31,6 +31,12 @@ #include #include "tethering_private.h" +#define ALLOWED_LIST "/etc/hostapd.accept" +#define BLOCKED_LIST "/etc/hostapd.deny" +#define TEMP_LIST "/etc/hostapd_tmp" +#define MAC_ADDR_LEN 18 +#define MAX_BUF_SIZE 80 + static void __handle_wifi_tether_on(GDBusConnection *connection, const gchar *sender_name, const gchar *object_path, const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data); @@ -1375,6 +1381,7 @@ static int __prepare_wifi_settings(tethering_h tethering, _softap_settings_t *se if (ret != TETHERING_ERROR_NONE) set->visibility = th->visibility; + set->mac_filter = th->mac_filter; set->channel = th->channel; __get_wifi_mode_type (th->mode_type, &ptr); @@ -1410,7 +1417,7 @@ static int __prepare_wifi_settings(tethering_h tethering, _softap_settings_t *se } if (parameters != NULL) { - g_variant_get(parameters, "(siu)", passphrase, &len, &ret); + g_variant_get(parameters, "(siu)", &passphrase, &len, &ret); g_variant_unref(parameters); } @@ -1509,6 +1516,7 @@ API int tethering_create(tethering_h *tethering) memset(th, 0x00, sizeof(__tethering_h)); th->sec_type = TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK; th->visibility = true; + th->mac_filter = false; th->channel = 6; th->mode_type = TETHERING_WIFI_MODE_TYPE_G; @@ -1670,7 +1678,7 @@ API int tethering_enable(tethering_h tethering, tethering_type_e type) sigs[E_SIGNAL_WIFI_TETHER_ON].sig_id); g_dbus_proxy_call(proxy, "enable_wifi_tethering", - g_variant_new("(sssiii)", set.ssid, set.key, set.mode, set.channel, set.visibility, set.sec_type), + g_variant_new("(sssiiii)", set.ssid, set.key, set.mode, set.channel, set.visibility, set.mac_filter, set.sec_type), G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable, (GAsyncReadyCallback) __wifi_enabled_cfm_cb, (gpointer)tethering); break; @@ -3195,13 +3203,146 @@ API int tethering_wifi_reload_settings(tethering_h tethering, tethering_wifi_set th->settings_reloaded_user_data = user_data; g_dbus_proxy_call(proxy, "reload_wifi_settings", - g_variant_new("(sssiii)", set.ssid, set.key, set.mode, set.channel, set.visibility, set.sec_type), + g_variant_new("(sssiiii)", set.ssid, set.key, set.mode, set.channel, set.visibility, set.mac_filter, set.sec_type), G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable, (GAsyncReadyCallback) __settings_reloaded_cb, (gpointer)tethering); return TETHERING_ERROR_NONE; } +API int tethering_wifi_set_mac_filter(tethering_h tethering, bool mac_filter) +{ + CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE); + + _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(tethering) is NULL\n"); + + __tethering_h *th = (__tethering_h *)tethering; + th->mac_filter = mac_filter; + + return TETHERING_ERROR_NONE; +} + +API int tethering_wifi_get_mac_filter(tethering_h tethering, bool *mac_filter) +{ + CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE); + + _retvm_if(mac_filter == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(mac_filter) is NULL\n"); + + __tethering_h *th = (__tethering_h *)tethering; + *mac_filter = th->mac_filter; + + return TETHERING_ERROR_NONE; +} + +static int __add_mac_to_file(const char *filepath, const char *mac) +{ + FILE *fp = NULL; + char line[MAX_BUF_SIZE] = "\0"; + bool mac_exist = false; + + fp = fopen(filepath, "a+"); + if (!fp) { + ERR("fopen is failed\n"); + return TETHERING_ERROR_OPERATION_FAILED; + } + + while (fgets(line, MAX_BUF_SIZE, fp) != NULL) { + if (strncmp(mac, line, 17) == 0) { + DBG("MAC %s already exist in the list\n", mac); + mac_exist = true; + break; + } + } + + if (!mac_exist) + fprintf(fp, "%s\n", mac); + + fclose(fp); + + return TETHERING_ERROR_NONE; +} + +static int __remove_mac_from_file(const char *filepath, const char *mac) +{ + FILE *fp = NULL; + FILE *fp1 = NULL; + char line[MAX_BUF_SIZE] = "\0"; + + fp = fopen(filepath, "r"); + if (!fp) { + ERR("fopen is failed\n"); + return TETHERING_ERROR_OPERATION_FAILED; + } + + fp1 = fopen(TEMP_LIST, "w+"); + if (!fp1) { + fclose(fp); + ERR("fopen is failed\n"); + return TETHERING_ERROR_OPERATION_FAILED; + } + + while (fgets(line, MAX_BUF_SIZE, fp) != NULL) { + if (strncmp(mac, line, 17) == 0) + DBG("MAC %s found in the list\n", mac); + else + fprintf(fp1, "%s", line); + } + + fclose(fp); + fclose(fp1); + + if ((strcmp(filepath, ALLOWED_LIST) == 0)) + rename(TEMP_LIST, ALLOWED_LIST); + else if ((strcmp(filepath, BLOCKED_LIST) == 0)) + rename(TEMP_LIST, BLOCKED_LIST); + + return TETHERING_ERROR_NONE; +} + +API int tethering_wifi_add_allowed_mac_list(tethering_h tethering, const char *mac) +{ + CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE); + + _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(tethering) is NULL\n"); + _retvm_if(mac == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(mac) is NULL\n"); + + return __add_mac_to_file(ALLOWED_LIST, mac); +} + +API int tethering_wifi_remove_allowed_mac_list(tethering_h tethering, const char *mac) +{ + _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(tethering) is NULL\n"); + _retvm_if(mac == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(mac) is NULL\n"); + + return __remove_mac_from_file(ALLOWED_LIST, mac); +} + +API int tethering_wifi_add_blocked_mac_list(tethering_h tethering, const char *mac) +{ + _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(tethering) is NULL\n"); + _retvm_if(mac == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(mac) is NULL\n"); + + return __add_mac_to_file(BLOCKED_LIST, mac); +} + +API int tethering_wifi_remove_blocked_mac_list(tethering_h tethering, const char *mac) +{ + _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(tethering) is NULL\n"); + _retvm_if(mac == NULL, TETHERING_ERROR_INVALID_PARAMETER, + "parameter(mac) is NULL\n"); + + return __remove_mac_from_file(BLOCKED_LIST, mac); +} + /** * @internal * @brief Sets the security type of Wi-Fi AP. diff --git a/test/tethering_test.c b/test/tethering_test.c index e29182c..e2481be 100755 --- a/test/tethering_test.c +++ b/test/tethering_test.c @@ -487,6 +487,7 @@ static void __print_wifi_tethering_setting(tethering_h th) char *ssid = NULL; char *passphrase = NULL; bool visibility = false; + bool mac_filter = 0; tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE; int error = TETHERING_ERROR_NONE; @@ -519,6 +520,13 @@ static void __print_wifi_tethering_setting(tethering_h th) TETHERING_WIFI_SECURITY_TYPE_NONE ? "open" : "wpa2-psk"); + error = tethering_wifi_get_mac_filter(th, &mac_filter); + if (error != TETHERING_ERROR_NONE) + __is_err(error); + else + g_print("\t** WiFi tethering mac filter : %s\n", + mac_filter ? "enable" : "disable"); + if (ssid) free(ssid); if (passphrase) @@ -811,7 +819,8 @@ static int test_tethering_wifi_set_passphrase(void) return 1; } -static int test_tethering_wifi_set_channel(void){ +static int test_tethering_wifi_set_channel(void) +{ int ret; int channel; @@ -827,7 +836,8 @@ static int test_tethering_wifi_set_channel(void){ return 1; } -static int test_tethering_wifi_set_mode(void) { +static int test_tethering_wifi_set_mode(void) +{ int ret; int type; @@ -839,6 +849,66 @@ static int test_tethering_wifi_set_mode(void) { printf("Fail to set mode!!\n"); return -1; } + + return 1; +} + +static int test_tethering_wifi_set_mac_filtering(void) +{ + int ret; + int enable; + + printf("Input mac filtering option (0: disable, 1: enable): "); + ret = scanf("%d", &enable); + + ret = tethering_wifi_set_mac_filter(th, enable); + if (__is_err(ret) == true) { + printf("Fail to set mac filtering!!\n"); + return -1; + } + + return 1; +} + +static int test_tethering_manage_mac_list(void) +{ + int ret = 0; + int list, option; + char mac[100]; + + printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): "); + ret = scanf("%d", &list); + + printf("Select option (0: Add, 1: Remove): "); + ret = scanf("%d", &option); + + printf("Input MAC Address to add/remove allowed/blocked mac list: "); + ret = scanf("%99s", mac); + if (ret < 0) { + printf("scanf is failed!!\n"); + return -1; + } + + if (!list && !option) { + /* Add to allowed mac list*/ + ret = tethering_wifi_add_allowed_mac_list(th, mac); + } else if (!list && option) { + /* Remove from allowed mac list */ + ret = tethering_wifi_remove_allowed_mac_list(th, mac); + } else if (list && !option) { + /* Add to blocked mac list */ + ret = tethering_wifi_add_blocked_mac_list(th, mac); + } else if (list && option) { + /* Remove from blocked mac list */ + ret = tethering_wifi_remove_blocked_mac_list(th, mac); + } else { + printf("Input Failed!!\n"); + return -1; + } + + if (ret < 0) + return -1; + return 1; } @@ -989,12 +1059,14 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) printf("b - Set Wi-Fi tethering security type\n"); printf("c - Set Wi-Fi tethering visibility\n"); printf("d - Set Wi-Fi tethering passphrase\n"); - printf("e - Set Wi-Fi AP SSID\n"); - printf("f - Set Wi-Fi AP security type\n"); - printf("g - Set Wi-Fi AP visibility\n"); - printf("h - Set Wi-Fi AP passphrase\n"); - printf("i - Reload Wi-Fi tethering\n"); - printf("j - Reload Wi-Fi AP\n"); + printf("e - Set Wi-Fi tethering mac filtering\n"); + printf("f - Add/Remove MAC adress to/from allowed/blocked list\n"); + printf("g - Set Wi-Fi AP SSID\n"); + printf("h - Set Wi-Fi AP security type\n"); + printf("i - Set Wi-Fi AP visibility\n"); + printf("j - Set Wi-Fi AP passphrase\n"); + printf("k - Reload Wi-Fi tethering\n"); + printf("l - Reload Wi-Fi AP\n"); printf("m - Set Wi-Fi channel\n"); printf("n - Set Wi-Fi hw_mode\n"); printf("0 - Exit \n"); @@ -1042,21 +1114,27 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) rv = test_tethering_wifi_set_passphrase(); break; case 'e': - rv = test_tethering_wifi_ap_set_ssid(); + rv = test_tethering_wifi_set_mac_filtering(); break; case 'f': - rv = test_tethering_wifi_ap_set_security_type(); + rv = test_tethering_manage_mac_list(); break; case 'g': - rv = test_tethering_wifi_ap_set_visibility(); + rv = test_tethering_wifi_ap_set_ssid(); break; case 'h': - rv = test_tethering_wifi_ap_set_passphrase(); + rv = test_tethering_wifi_ap_set_security_type(); break; case 'i': - rv = test_tethering_wifi_reload_settings(); + rv = test_tethering_wifi_ap_set_visibility(); break; case 'j': + rv = test_tethering_wifi_ap_set_passphrase(); + break; + case 'k': + rv = test_tethering_wifi_reload_settings(); + break; + case 'l': rv = test_tethering_wifi_ap_reload_settings(); break; case 'm':