Fixed memory leak
[platform/core/connectivity/net-config.git] / src / network-state.c
index 8b80604..9987d1f 100755 (executable)
@@ -28,9 +28,6 @@
 #include <sys/ioctl.h>
 #include <ITapiSim.h>
 #include <TapiUtility.h>
-#include <bundle.h>
-#include <bundle_internal.h>
-#include <eventsystem.h>
 
 #include "log.h"
 #include "util.h"
 #include "wifi-state.h"
 #include "wifi-power.h"
 #include "network-state.h"
+#include "network-dpm.h"
 #include "network-monitor.h"
 #include "netsupplicant.h"
 #include "wifi-tel-intf.h"
-#include "clatd-handler.h"
 
 #include "generated-code.h"
 /* Define TCP buffer sizes for various networks */
@@ -164,20 +161,41 @@ static char *__netconfig_get_default_profile(void)
 static void __netconfig_get_default_connection_info(const char *profile)
 {
        GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
-       GVariantIter *iter = NULL, *iter1 = NULL;
+       GVariantIter *iter = NULL, *iter1 = NULL,  *service = NULL;
        GVariant *next = NULL;
+       gchar *obj_path;
        gchar *key = NULL;
        gchar *key1 = NULL;
        gchar *key2 = NULL;
+       gboolean found_profile = 0;
 
-       message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
-                       CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
+       message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
+                       CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
+                       "GetServices", NULL);
        if (message == NULL) {
-               ERR("Failed to get service properties");
+               ERR("Failed to get services informations");
+               goto done;
+       }
+
+       g_variant_get(message, "(a(oa{sv}))", &service);
+       if (service == NULL) {
+               ERR("Failed to get services iter");
+               goto done;
+       }
+
+       while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
+               if (g_strcmp0(obj_path, profile) == 0) {
+                       g_free(obj_path);
+                       found_profile = 1;
+                       break;
+               }
+       }
+
+       if (iter == NULL || found_profile == 0) {
+               ERR("Profile %s doesn't exist", profile);
                goto done;
        }
 
-       g_variant_get(message, "(a{sv})", &iter);
        while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
                const gchar *value = NULL;
                guint16 freq = 0;
@@ -190,36 +208,42 @@ static void __netconfig_get_default_connection_info(const char *profile)
                        }
                } else if (g_strcmp0(key, "Ethernet") == 0) {
                        g_variant_get(next, "a{sv}", &iter1);
+                       if (iter1 == NULL)
+                               continue;
                        while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
                                if (g_strcmp0(key1, "Interface") == 0) {
                                        value = g_variant_get_string(variant, NULL);
                                        netconfig_default_connection_info.ifname = g_strdup(value);
                                }
                        }
-                       if (iter1)
-                               g_variant_iter_free(iter1);
+                       g_variant_iter_free(iter1);
                } else if (g_strcmp0(key, "IPv4") == 0) {
                        g_variant_get(next, "a{sv}", &iter1);
+                       if (iter1 == NULL)
+                               continue;
                        while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
                                if (g_strcmp0(key1, "Address") == 0) {
                                        value = g_variant_get_string(variant, NULL);
                                        netconfig_default_connection_info.ipaddress = g_strdup(value);
                                }
                        }
-                       if (iter1)
-                               g_variant_iter_free(iter1);
+                       g_variant_iter_free(iter1);
                } else if (g_strcmp0(key, "IPv6") == 0) {
                        g_variant_get(next, "a{sv}", &iter1);
+                       if (iter1 == NULL)
+                               continue;
                        while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
                                if (g_strcmp0(key1, "Address") == 0) {
                                        value = g_variant_get_string(variant, NULL);
                                        netconfig_default_connection_info.ipaddress6 = g_strdup(value);
                                }
                        }
-                       if (iter1)
-                               g_variant_iter_free(iter1);
+                       g_variant_iter_free(iter1);
+
                } else if (g_strcmp0(key, "Proxy") == 0) {
                        g_variant_get(next, "a{sv}", &iter1);
+                       if (iter1 == NULL)
+                               continue;
                        while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
                                GVariantIter *iter_sub = NULL;
 
@@ -253,8 +277,7 @@ static void __netconfig_get_default_connection_info(const char *profile)
                                        }
                                }
                        }
-                       if (iter1)
-                               g_variant_iter_free(iter1);
+                       g_variant_iter_free(iter1);
                } else if (g_strcmp0(key, "Frequency") == 0) {
                        if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
                                freq = g_variant_get_uint16(next);
@@ -270,8 +293,8 @@ done:
        if (iter)
                g_variant_iter_free(iter);
 
-       if (iter1)
-               g_variant_iter_free(iter1);
+       if (service)
+               g_variant_iter_free(service);
 
        return;
 }
@@ -444,8 +467,6 @@ static void __netconfig_update_default_connection_info(void)
 
                DBG("Successfully clear IP and PROXY up");
 
-               /* Disable clatd if it is in running state */
-               netconfig_clatd_disable();
        } else if (profile != NULL) {
                char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
                char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
@@ -454,8 +475,8 @@ static void __netconfig_update_default_connection_info(void)
                        netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
                        netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
 
-                       netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
-                               EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_WIFI);
+                       netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
+                               EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
                } else if (netconfig_is_cellular_profile(profile)) {
 
                        if (!netconfig_is_cellular_internet_profile(profile)) {
@@ -465,24 +486,20 @@ static void __netconfig_update_default_connection_info(void)
 
                        netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
 
-                       netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
-                               EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_CELLULAR);
-
-                       /* Enable clatd if IPv6 is set and no IPv4 address */
-                       if (!ip_addr && ip_addr6)
-                               netconfig_clatd_enable();
+                       netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
+                               EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
                } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
                        netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
-                       netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
-                               EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_ETHERNET);
+                       netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
+                               EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
                } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
                        netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
-                       netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
-                               EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_BT);
+                       netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
+                               EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
                } else{
                        netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
-                       netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
-                               EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_DISCONNECTED);
+                       netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
+                               EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
                }
 
                if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
@@ -506,10 +523,6 @@ static void __netconfig_update_default_connection_info(void)
                netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
 
                DBG("Successfully update default network configuration");
-
-               /* Disable clatd if it is in running state */
-               if (netconfig_is_cellular_profile(profile) != TRUE)
-                       netconfig_clatd_disable();
        }
 
        __netconfig_adjust_tcp_buffer_size();
@@ -809,28 +822,48 @@ void netconfig_update_default(void)
 char *netconfig_get_ifname(const char *profile)
 {
        GVariant *message = NULL, *variant;
-       GVariantIter *iter, *next;
+       GVariantIter *iter, *next, *service;
+       gchar *obj_path;
        gchar *key;
        gchar *key1;
        const gchar *value = NULL;
        gchar *ifname = NULL;
+       gboolean found_profile = 0;
 
        if (profile == NULL)
                return NULL;
 
-       message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
-                       CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
+       message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
+                       CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
+                       "GetServices", NULL);
        if (message == NULL) {
-               ERR("Failed to get service properties");
+               ERR("Failed to get services informations");
+               return NULL;
+       }
+
+       g_variant_get(message, "(a(oa{sv}))", &service);
+       while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
+               if (g_strcmp0(obj_path, profile) == 0) {
+                       g_free(obj_path);
+                       found_profile = 1;
+                       break;
+               }
+       }
+
+       if (found_profile == 0) {
+               ERR("Profile %s doesn't exist", profile);
+               g_variant_iter_free(service);
+               g_variant_unref(message);
                return NULL;
        }
 
-       g_variant_get(message, "(a{sv})", &iter);
        while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
                if (g_strcmp0(key, "Ethernet") == 0) {
                        while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
                                if (g_strcmp0(key1, "Interface") == 0) {
                                        value = g_variant_get_string(variant, NULL);
+                                       if (ifname)
+                                               g_free(ifname);
                                        ifname = g_strdup(value);
                                }
                        }
@@ -839,6 +872,7 @@ char *netconfig_get_ifname(const char *profile)
 
        g_variant_unref(message);
 
+       g_variant_iter_free(service);
        g_variant_iter_free(iter);
 
        return ifname;
@@ -997,6 +1031,13 @@ static gboolean handle_check_profile_privilege(Network *object,
        return TRUE;
 }
 
+static gboolean handle_check_internet_privilege(Network *object,
+               GDBusMethodInvocation *context)
+{
+       network_complete_check_internet_privilege(object, context);
+       return TRUE;
+}
+
 gboolean handle_ethernet_cable_state(Network *object,
        GDBusMethodInvocation *context)
 {
@@ -1037,16 +1078,22 @@ void state_object_create_and_init(void)
                                G_CALLBACK(handle_check_get_privilege), NULL);
        g_signal_connect(netconfigstate, "handle-check-profile-privilege",
                                G_CALLBACK(handle_check_profile_privilege), NULL);
+       g_signal_connect(netconfigstate, "handle-check-internet-privilege",
+                               G_CALLBACK(handle_check_internet_privilege), NULL);
        g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
                                G_CALLBACK(handle_ethernet_cable_state), NULL);
        g_signal_connect(netconfigstate, "handle-remove-route",
                                G_CALLBACK(handle_remove_route), NULL);
        g_signal_connect(netconfigstate, "handle-launch-mdns",
                                G_CALLBACK(handle_launch_mdns), NULL);
-       g_signal_connect(netconfigstate, "handle-ref-mdns",
-                               G_CALLBACK(handle_ref_mdns), NULL);
-       g_signal_connect(netconfigstate, "handle-unref-mdns",
-                               G_CALLBACK(handle_unref_mdns), NULL);
+       g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
+                               G_CALLBACK(handle_device_policy_set_wifi), NULL);
+       g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
+                               G_CALLBACK(handle_device_policy_get_wifi), NULL);
+       g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
+                               G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
+       g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
+                               G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
 
        if (!g_dbus_interface_skeleton_export(interface_network, connection,
                        NETCONFIG_NETWORK_STATE_PATH, NULL)) {