Fixed various memory leaks
[platform/upstream/connman.git] / gsupplicant / supplicant.c
index fbfa6c7..5fedf02 100644 (file)
@@ -321,6 +321,14 @@ struct interface_scan_data {
        void *user_data;
 };
 
+#if defined TIZEN_EXT
+struct g_connman_bssids {
+       char bssid[18];
+       uint16_t strength;
+       uint16_t frequency;
+};
+#endif
+
 static int network_remove(struct interface_data *data);
 
 static inline void debug(const char *format, ...)
@@ -429,6 +437,11 @@ static GSupplicantState string2state(const char *state)
 static bool compare_network_parameters(GSupplicantInterface *interface,
                                GSupplicantSSID *ssid)
 {
+#if defined TIZEN_EXT
+       if (!interface->network_info.ssid)
+               return FALSE;
+#endif
+
        if (memcmp(interface->network_info.ssid, ssid->ssid, ssid->ssid_len))
                return FALSE;
 
@@ -1583,6 +1596,8 @@ void *g_supplicant_network_get_wifi_vsie(GSupplicantNetwork *network)
                unsigned char *vsie = NULL;
                for (list = network->vsie_list; list; list = list->next) {
                        unsigned char *ie = (unsigned char *)list->data;
+                       if (ie == NULL)
+                               continue;
                        vsie = (unsigned char *)g_try_malloc0(ie[1]+2); // tag number size(1), tag length size(1)
 
                        if (vsie) {
@@ -1595,6 +1610,46 @@ void *g_supplicant_network_get_wifi_vsie(GSupplicantNetwork *network)
 
        return vsie_list;
 }
+
+static void update_bssid_list(gpointer key, gpointer value, gpointer user_data)
+{
+       struct g_supplicant_bss *bss = value;
+       struct g_connman_bssids *bssids = NULL;
+       char buff[18];
+       GSList **list = (GSList **)user_data;
+
+       bssids = (struct g_connman_bssids *)g_try_malloc0(sizeof(struct g_connman_bssids));
+
+       if (bssids) {
+               g_snprintf(buff, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
+                               bss->bssid[0], bss->bssid[1], bss->bssid[2], bss->bssid[3],
+                               bss->bssid[4], bss->bssid[5]);
+
+               memcpy(bssids->bssid, buff, 18);
+               bssids->bssid[17] = '\0';
+               bssids->strength = bss->signal;
+               bssids->strength += 120;
+
+               if (bssids->strength > 100)
+                       bssids->strength = 100;
+
+               bssids->frequency = bss->frequency;
+               *list = g_slist_append(*list, bssids);
+       } else
+               SUPPLICANT_DBG("Failed to allocate memory");
+}
+
+void *g_supplicant_network_get_bssid_list(GSupplicantNetwork *network)
+{
+       GSList *bssid_list = NULL;
+
+       if (g_hash_table_size(network->bss_table) < 1)
+               return NULL;
+
+       g_hash_table_foreach(network->bss_table, update_bssid_list, &bssid_list);
+
+       return bssid_list;
+}
 #endif
 
 static void merge_network(GSupplicantNetwork *network)
@@ -1602,7 +1657,6 @@ static void merge_network(GSupplicantNetwork *network)
        GString *str;
        const char *ssid, *mode, *key_mgmt;
 #if defined TIZEN_EXT
-       GSupplicantInterface *interface;
        const char *isHS20AP;
        const char *eap, *identity, *phase2;
 #endif
@@ -1617,7 +1671,6 @@ static void merge_network(GSupplicantNetwork *network)
        eap = g_hash_table_lookup(network->config_table, "eap");
        identity = g_hash_table_lookup(network->config_table, "identity");
        phase2 = g_hash_table_lookup(network->config_table, "phase2");
-       interface = network->interface;
 #endif
 
        SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
@@ -1676,11 +1729,9 @@ static void merge_network(GSupplicantNetwork *network)
        } else
                network->isHS20AP = 0;
 
-       if (interface)
-               interface->network_path = g_strdup(network->path);
-
        network->group = g_strdup(group);
        callback_network_merged(network);
+       g_free(network->group);
 #endif
 
        g_free(group);
@@ -2128,8 +2179,11 @@ static void bss_process_ies(DBusMessageIter *iter, void *user_data)
                }
 
                if(ie[0] == WLAN_EID_COUNTRY && ie[1] >= 2) {
-                       memcpy(bss->country_code, ie+2, COUNTRY_CODE_LENGTH);
-                       continue;
+                       /* Add country code only if it is a valid alphabet */
+                       if (ie[2] >= 65 && ie[2] <= 90 && ie[3] >= 65 && ie[3] <= 90) {
+                               memcpy(bss->country_code, ie+2, COUNTRY_CODE_LENGTH);
+                               continue;
+                       }
                }
 #endif
                if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
@@ -4860,6 +4914,10 @@ static void interface_add_network_result(const char *error,
 
        SUPPLICANT_DBG("PATH: %s", path);
 
+#if defined TIZEN_EXT
+       if (interface->network_path)
+               g_free(interface->network_path);
+#endif
        interface->network_path = g_strdup(path);
 
        store_network_information(interface, data->ssid);
@@ -5857,6 +5915,19 @@ static void network_remove_result(const char *error,
                connect_data->ssid = data->ssid;
                connect_data->user_data = data->user_data;
 
+#if defined TIZEN_EXT
+               int ret;
+               if (data->ssid->passphrase && g_strcmp0(data->ssid->passphrase, "") != 0
+                       && !data->ssid->eap) {
+                       ret = send_decryption_request(data->ssid->passphrase, connect_data);
+                       if (ret < 0) {
+                               SUPPLICANT_DBG("Decryption request failed %d", ret);
+                               g_free(connect_data->ssid);
+                               g_free(connect_data->path);
+                               dbus_free(connect_data);
+                       }
+               } else
+#endif
                supplicant_dbus_method_call(data->interface->path,
                        SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
                        interface_add_network_params,
@@ -6447,29 +6518,6 @@ int g_supplicant_set_widi_ies(GSupplicantP2PServiceParams *p2p_service_params,
        return -EINPROGRESS;
 }
 
-#if defined TIZEN_EXT
-int g_supplicant_interface_remove_network(GSupplicantInterface *interface)
-{
-       struct interface_data *data;
-
-       SUPPLICANT_DBG("");
-
-       if (interface == NULL)
-               return -EINVAL;
-
-       if (system_available == FALSE)
-               return -EFAULT;
-
-       data = dbus_malloc0(sizeof(*data));
-       if (data == NULL)
-               return -ENOMEM;
-
-       data->interface = interface;
-
-       return network_remove(data);
-}
-#endif
-
 static const char *g_supplicant_rule0 = "type=signal,"
                                        "path=" DBUS_PATH_DBUS ","
                                        "sender=" DBUS_SERVICE_DBUS ","