Fix memory leak when loading global settings
[platform/upstream/connman.git] / src / technology.c
index b358600..2ba02e2 100644 (file)
@@ -100,6 +100,9 @@ struct connman_technology {
        bool dbus_registered;
 #if defined TIZEN_EXT
        char **enabled_devices;
+       unsigned int mac_policy;
+       unsigned int preassoc_mac_policy;
+       unsigned int random_mac_lifetime;
 #endif
 #if defined TIZEN_EXT_WIFI_MESH
        DBusMessage *mesh_dbus_msg;
@@ -213,10 +216,25 @@ static void technology_save(struct connman_technology *technology)
                                        "Tethering.Identifier",
                                        technology->tethering_ident);
 
-       if (technology->tethering_passphrase)
+       if (technology->tethering_passphrase) {
+               char *enc = g_strescape(technology->tethering_passphrase, NULL);
                g_key_file_set_string(keyfile, identifier,
-                                       "Tethering.Passphrase",
-                                       technology->tethering_passphrase);
+                                       "Tethering.Passphrase", enc);
+               g_free(enc);
+       }
+
+#ifdef TIZEN_EXT
+       if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
+               g_key_file_set_uint64(keyfile, identifier, "MacPolicy",
+                                       technology->mac_policy);
+
+               g_key_file_set_uint64(keyfile, identifier, "PreassocMacPolicy",
+                                       technology->preassoc_mac_policy);
+
+               g_key_file_set_uint64(keyfile, identifier, "RandomMacLifetime",
+                                       technology->random_mac_lifetime);
+       }
+#endif /* TIZEN_EXT */
 
 done:
        g_free(identifier);
@@ -430,6 +448,11 @@ static void technology_load(struct connman_technology *technology)
        gchar *identifier;
        GError *error = NULL;
        bool enable, need_saving = false;
+#ifdef TIZEN_EXT
+       char *enc = NULL;
+#else
+       char *enc;
+#endif
 
        DBG("technology %p", technology);
 
@@ -486,11 +509,43 @@ static void technology_load(struct connman_technology *technology)
        technology->tethering_ident = g_key_file_get_string(keyfile,
                                identifier, "Tethering.Identifier", NULL);
 
-       technology->tethering_passphrase = g_key_file_get_string(keyfile,
+       enc = g_key_file_get_string(keyfile,
                                identifier, "Tethering.Passphrase", NULL);
+       if (enc)
+               technology->tethering_passphrase = g_strcompress(enc);
+#ifdef TIZEN_EXT
+       if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
+               unsigned int val = 0;
+
+               val = g_key_file_get_uint64(keyfile,
+                               identifier, "MacPolicy", NULL);
+               if (val <= 2)
+                       technology->mac_policy = val;
+               else
+                       technology->mac_policy = 0;
+
+               val = g_key_file_get_uint64(keyfile,
+                               identifier, "PreassocMacPolicy", NULL);
+               if (val <= 2)
+                       technology->preassoc_mac_policy = val;
+               else
+                       technology->preassoc_mac_policy = 0;
+
+               val = g_key_file_get_uint64(keyfile,
+                               identifier, "RandomMacLifetime", NULL);
+               if (val > 0)
+                       technology->random_mac_lifetime = val;
+               else
+                       technology->random_mac_lifetime = 60;
+       }
+#endif /* TIZEN_EXT */
+
 done:
        g_free(identifier);
 
+#ifdef TIZEN_EXT
+       g_free(enc);
+#endif
        g_key_file_free(keyfile);
 }
 
@@ -589,6 +644,37 @@ static void append_devices(DBusMessageIter *iter, void *user_data)
                                DBUS_TYPE_STRING, &str);
        }
 }
+
+void __connman_technology_append_interfaces(DBusMessageIter *array,
+                               enum connman_service_type type, const char *ifname)
+{
+       GSList *list;
+       struct connman_technology *technology = NULL;
+
+       for (list = technology_list; list; list = list->next) {
+               struct connman_technology *local_tech = list->data;
+
+               if (local_tech->type != type)
+                       continue;
+
+               technology = local_tech;
+               break;
+       }
+
+       if (!technology)
+               return;
+
+       for (list = technology->device_list; list; list = list->next) {
+               struct connman_device *device = list->data;
+               const char *str = connman_device_get_string(device, "Interface");
+
+               if (g_strcmp0(ifname, str) == 0)
+                       continue;
+
+               dbus_message_iter_append_basic(array,
+                               DBUS_TYPE_STRING, &str);
+       }
+}
 #endif
 
 static void append_properties(DBusMessageIter *iter,
@@ -635,10 +721,27 @@ static void append_properties(DBusMessageIter *iter,
                connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
                                        DBUS_TYPE_STRING,
                                        &technology->tethering_passphrase);
+
 #if defined TIZEN_EXT
+       connman_dbus_dict_append_basic(&dict, "MacPolicy",
+                                       DBUS_TYPE_UINT32,
+                                       &(technology->mac_policy));
+
+       connman_dbus_dict_append_basic(&dict, "PreassocMacPolicy",
+                                       DBUS_TYPE_UINT32,
+                                       &(technology->preassoc_mac_policy));
+
+       connman_dbus_dict_append_basic(&dict, "RandomMacLifetime",
+                                       DBUS_TYPE_UINT32,
+                                       &(technology->random_mac_lifetime));
+
        if (technology->type == CONNMAN_SERVICE_TYPE_WIFI)
                connman_dbus_dict_append_dict(&dict, "Device.List",
                                        append_devices, technology);
+       if (technology->regdom)
+               connman_dbus_dict_append_basic(&dict, "CountryCode",
+                                       DBUS_TYPE_STRING,
+                                       &technology->regdom);
 #endif
        connman_dbus_dict_close(iter, &dict);
 }
@@ -1047,6 +1150,190 @@ int set_connman_bssid(enum bssid_type mode, char *bssid, const char *ifname)
 
        return bssid_len;
 }
+
+void connman_technology_mac_policy_notify(struct connman_technology *technology,
+                                                       unsigned int policy)
+{
+       DBG("Mac polict set to %u", policy);
+
+       technology->mac_policy = policy;
+       technology_save(technology);
+}
+
+void __connman_technology_notify_mac_policy_by_device(struct connman_device *device,
+                                               int result, unsigned int policy)
+{
+       struct connman_technology *technology;
+       enum connman_service_type type;
+
+       type = __connman_device_get_service_type(device);
+       technology = technology_find(type);
+
+       if (!technology)
+               return;
+
+       connman_technology_mac_policy_notify(technology, policy);
+}
+
+static DBusMessage *set_mac_policy(struct connman_technology *technology,
+                               DBusMessage *msg, unsigned int policy)
+{
+       DBusMessage *reply = NULL;
+       int err = 0;
+       unsigned int last_policy = technology->mac_policy;
+
+       if (technology->rfkill_driven && technology->hardblocked) {
+               err = -EACCES;
+               goto make_reply;
+       }
+
+       for (GSList *list = technology->device_list; list; list = list->next) {
+               struct connman_device *device = list->data;
+
+               err = connman_device_set_mac_policy(device, policy);
+               if (err < 0)
+                       break;
+       }
+
+make_reply:
+       if (err < 0) {
+               if (err != -EACCES && err != -EOPNOTSUPP) {
+                       for (GSList *list = technology->device_list; list; list = list->next) {
+                               struct connman_device *device = list->data;
+
+                               connman_device_set_mac_policy(device, last_policy);
+                       }
+               }
+
+               reply = __connman_error_failed(msg, -err);
+       } else {
+               reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+       }
+
+       return reply;
+}
+
+void connman_technology_preassoc_mac_policy_notify(struct connman_technology *technology,
+                                                       unsigned int policy)
+{
+       DBG("Preassoc mac polict set to %u", policy);
+
+       technology->preassoc_mac_policy = policy;
+       technology_save(technology);
+}
+
+void __connman_technology_notify_preassoc_mac_policy_by_device(struct connman_device *device,
+                                               int result, unsigned int policy)
+{
+       struct connman_technology *technology;
+       enum connman_service_type type;
+
+       type = __connman_device_get_service_type(device);
+       technology = technology_find(type);
+
+       if (!technology)
+               return;
+
+       connman_technology_preassoc_mac_policy_notify(technology, policy);
+}
+
+static DBusMessage *set_preassoc_mac_policy(struct connman_technology *technology,
+                               DBusMessage *msg, unsigned int policy)
+{
+       DBusMessage *reply = NULL;
+       int err = 0;
+       unsigned int last_policy = technology->preassoc_mac_policy;
+
+       if (technology->rfkill_driven && technology->hardblocked) {
+               err = -EACCES;
+               goto make_reply;
+       }
+
+       for (GSList *list = technology->device_list; list; list = list->next) {
+               struct connman_device *device = list->data;
+
+               err = connman_device_set_preassoc_mac_policy(device, policy);
+               if (err < 0)
+                       break;
+       }
+
+make_reply:
+       if (err < 0) {
+               if (err != -EACCES && err != -EOPNOTSUPP) {
+                       for (GSList *list = technology->device_list; list; list = list->next) {
+                               struct connman_device *device = list->data;
+
+                               connman_device_set_preassoc_mac_policy(device, last_policy);
+                       }
+               }
+
+               reply = __connman_error_failed(msg, -err);
+       } else {
+               reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+       }
+
+       return reply;
+}
+
+void connman_technology_random_mac_lifetime_notify(struct connman_technology *technology,
+                                                       unsigned int lifetime)
+{
+       DBG("Random mac lifetime set to %u", lifetime);
+
+       technology->random_mac_lifetime = lifetime;
+       technology_save(technology);
+}
+
+void __connman_technology_notify_random_mac_lifetime_by_device(struct connman_device *device,
+                                               int result, unsigned int lifetime)
+{
+       struct connman_technology *technology;
+       enum connman_service_type type;
+
+       type = __connman_device_get_service_type(device);
+       technology = technology_find(type);
+
+       if (!technology)
+               return;
+
+       connman_technology_random_mac_lifetime_notify(technology, lifetime);
+}
+
+static DBusMessage *set_random_mac_lifetime(struct connman_technology *technology,
+                               DBusMessage *msg, unsigned int lifetime)
+{
+       DBusMessage *reply = NULL;
+       int err = 0;
+       unsigned int last_lifetime = technology->random_mac_lifetime;
+
+       if (technology->rfkill_driven && technology->hardblocked) {
+               err = -EACCES;
+               goto make_reply;
+       }
+
+       for (GSList *list = technology->device_list; list; list = list->next) {
+               struct connman_device *device = list->data;
+
+               err = connman_device_set_random_mac_lifetime(device, lifetime);
+       }
+
+make_reply:
+       if (err < 0) {
+               if (err != -EACCES && err != -EOPNOTSUPP) {
+                       for (GSList *list = technology->device_list; list; list = list->next) {
+                               struct connman_device *device = list->data;
+
+                               connman_device_set_random_mac_lifetime(device, last_lifetime);
+                       }
+               }
+
+               reply = __connman_error_failed(msg, -err);
+       } else {
+               reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+       }
+
+       return reply;
+}
 #endif
 
 static DBusMessage *set_property(DBusConnection *conn,
@@ -1172,6 +1459,60 @@ static DBusMessage *set_property(DBusConnection *conn,
                dbus_message_iter_get_basic(&value, &key);
                DBG("BSSID %s", key);
                set_connman_bssid(SET_BSSID, key, NULL);
+       } else if (g_str_equal(name, "MacPolicy")) {
+               dbus_uint32_t mac_policy;
+
+               if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
+                       return __connman_error_not_supported(msg);
+
+               if (type != DBUS_TYPE_UINT32)
+                       return __connman_error_invalid_arguments(msg);
+
+               dbus_message_iter_get_basic(&value, &mac_policy);
+
+               if (mac_policy <= 2)
+                       return set_mac_policy(technology, msg, mac_policy);
+               else
+                       return __connman_error_invalid_arguments(msg);
+
+       } else if (g_str_equal(name, "PreassocMacPolicy")) {
+               dbus_uint32_t preassoc_mac_policy;
+
+               if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
+                       return __connman_error_not_supported(msg);
+
+               if (type != DBUS_TYPE_UINT32)
+                       return __connman_error_invalid_arguments(msg);
+
+               dbus_message_iter_get_basic(&value, &preassoc_mac_policy);
+
+               if (preassoc_mac_policy <= 2)
+                       return set_preassoc_mac_policy(technology, msg, preassoc_mac_policy);
+               else
+                       return __connman_error_invalid_arguments(msg);
+
+       } else if (g_str_equal(name, "RandomMacLifetime")) {
+               dbus_uint32_t random_mac_lifetime;
+
+               if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
+                       return __connman_error_not_supported(msg);
+
+               if (type != DBUS_TYPE_UINT32)
+                       return __connman_error_invalid_arguments(msg);
+
+               dbus_message_iter_get_basic(&value, &random_mac_lifetime);
+
+               if (random_mac_lifetime > 0)
+                       return set_random_mac_lifetime(technology, msg, random_mac_lifetime);
+               else
+                       return __connman_error_invalid_arguments(msg);
+
+       } else if (g_str_equal(name, "CountryCode")) {
+               const char *str;
+
+               dbus_message_iter_get_basic(&value, &str);
+               DBG("country code %s", str);
+               connman_technology_set_regdom(str);
 #endif
        } else
                return __connman_error_invalid_property(msg);
@@ -1271,7 +1612,7 @@ static void reply_scan_pending_device(
        dbus_bool_t status = 0;
        connman_scan_type_e scan_type = CONNMAN_SCAN_TYPE_UNKNOWN;
 
-       DBG("technology %p ifname %d count %d", technology, ifname, count);
+       DBG("technology %p ifname %s count %d", technology, ifname, count);
 
        list = technology->scan_pending;
 
@@ -1333,6 +1674,43 @@ static void __connman_technology_notify_device_detected(
 
        DBG("Successfuly sent DeviceDetected signal");
 }
+
+void __connman_technology_notify_roaming_state(const char *ifname,
+               const char *state, const char *cur_bssid, const char *dst_bssid)
+{
+       DBG("");
+       DBusMessage *signal;
+       DBusMessageIter array, dict;
+
+       signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
+                       CONNMAN_MANAGER_INTERFACE, "RoamingStateChanged");
+       if (!signal)
+               return;
+
+       dbus_message_iter_init_append(signal, &array);
+
+       connman_dbus_dict_open(&array, &dict);
+
+       if (ifname)
+               connman_dbus_dict_append_basic(&dict, "Interface",
+                                       DBUS_TYPE_STRING, &ifname);
+       if (state)
+               connman_dbus_dict_append_basic(&dict, "State",
+                                       DBUS_TYPE_STRING, &state);
+       if (cur_bssid)
+               connman_dbus_dict_append_basic(&dict, "ConnectedBSSID",
+                                       DBUS_TYPE_STRING, &cur_bssid);
+       if (dst_bssid)
+               connman_dbus_dict_append_basic(&dict, "TargetBSSID",
+                                       DBUS_TYPE_STRING, &dst_bssid);
+
+       connman_dbus_dict_close(&array, &dict);
+
+       dbus_connection_send(connection, signal, NULL);
+       dbus_message_unref(signal);
+
+       DBG("Successfully sent Roaming State Changed signal");
+}
 #endif
 
 void __connman_technology_scan_started(struct connman_device *device)
@@ -1375,9 +1753,10 @@ void __connman_technology_scan_stopped(struct connman_device *device,
        reply_scan_pending_device(technology, ifname, count);
 
        return;
-#endif
+#else
        if (count == 0)
                reply_scan_pending(technology, 0);
+#endif
 }
 
 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
@@ -1755,45 +2134,6 @@ static DBusMessage *get_max_scan_ssid(DBusConnection *conn, DBusMessage *msg, vo
        return reply;
 }
 
-static DBusMessage *get_interfaces(DBusConnection *conn, DBusMessage *msg, void *data)
-{
-       DBusMessage *reply;
-       DBusMessageIter iter, array;
-       GSList *list;
-       struct connman_technology *technology = data;
-       const char *default_interface = connman_option_get_string("DefaultWifiInterface");
-
-       DBG("technology %p", technology);
-
-       reply = dbus_message_new_method_return(msg);
-       if (!reply)
-               return NULL;
-
-       if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
-               return NULL;
-
-       dbus_message_iter_init_append(reply, &iter);
-       dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
-                                       DBUS_TYPE_STRING_AS_STRING, &array);
-
-       dbus_message_iter_append_basic(&array,
-                               DBUS_TYPE_STRING, &default_interface);
-
-       for (list = technology->device_list; list; list = list->next) {
-               struct connman_device *device = list->data;
-               const char *str = connman_device_get_string(device, "Interface");
-
-               if (g_strcmp0(default_interface, str) == 0)
-                       continue;
-
-               dbus_message_iter_append_basic(&array,
-                                               DBUS_TYPE_STRING, &str);
-       }
-
-       dbus_message_iter_close_container(&iter, &array);
-       return reply;
-}
-
 static int technology_enable_device(struct connman_technology *technology,
                                bool enable_device, const char *ifname, struct connman_device **device_out)
 {
@@ -1923,10 +2263,17 @@ void technology_save_device(struct connman_device *device)
        enum connman_service_type type;
 
        type = __connman_device_get_service_type(device);
+
+       if (type != CONNMAN_SERVICE_TYPE_WIFI)
+               return;
+
        technology = technology_get(type);
        if (!technology)
                return;
 
+       if (!g_slist_find(technology->device_list, device))
+               return;
+
        GKeyFile *keyfile;
        gchar *identifier;
        const char *name = get_name(technology->type);
@@ -1948,7 +2295,7 @@ void technology_save_device(struct connman_device *device)
        gchar **ifname_list = NULL;
        guint dev_count = g_slist_length(technology->device_list);
 
-       if (dev_count > 1) {
+       if (dev_count >= 1) {
                GString *ifname_str = g_string_new(NULL);
 
                if (ifname_str) {
@@ -2497,8 +2844,6 @@ static const GDBusMethodTable technology_methods[] = {
                        get_5ghz_supported) },
        { GDBUS_METHOD("GetMaxScanSsid", NULL, GDBUS_ARGS({ "maxscanssid", "a{sv}" }),
                        get_max_scan_ssid) },
-       { GDBUS_METHOD("GetInterfaces", NULL, GDBUS_ARGS({ "interface_list", "as" }),
-                       get_interfaces) },
        { GDBUS_ASYNC_METHOD("SetDevicePower",
                        GDBUS_ARGS({ "ifname", "s" }, { "value", "b" }),
                        NULL, set_device_power) },
@@ -2930,7 +3275,7 @@ int __connman_technology_add_device(struct connman_device *device)
 #if defined TIZEN_EXT
                bool found = true;
                int err = 0;
-               if (technology->enabled_devices) {
+               if (technology->enabled_devices && type == CONNMAN_SERVICE_TYPE_WIFI) {
                        int i = 0;
                        found = false;
                        const char *ifname = connman_device_get_string(device, "Interface");
@@ -2967,8 +3312,14 @@ done:
                                                                device);
 
 #if defined TIZEN_EXT
+       technology_save_device(device);
+
        const char *ifname = connman_device_get_string(device, "Interface");
        __connman_technology_notify_device_detected(technology, ifname, true);
+
+       connman_device_set_mac_policy(device, technology->mac_policy);
+       connman_device_set_preassoc_mac_policy(device, technology->preassoc_mac_policy);
+       connman_device_set_random_mac_lifetime(device, technology->random_mac_lifetime);
 #endif
        return 0;
 }
@@ -2993,6 +3344,8 @@ int __connman_technology_remove_device(struct connman_device *device)
                                                                device);
 
 #if defined TIZEN_EXT
+       technology_save_device(device);
+
        const char *ifname = connman_device_get_string(device, "Interface");
        __connman_technology_notify_device_detected(technology, ifname, false);
 #endif