Fix resource leak for 108826
[platform/core/connectivity/net-config.git] / src / network-state.c
index cc6a608..4460017 100755 (executable)
 
 #define ROUTE_EXEC_PATH                                                "/sbin/route"
 
+#define TELEPHONY_SERVICE                      "com.tcore.ps"
+#define TELEPHONY_MASTER_INTERFACE             TELEPHONY_SERVICE ".master"
+#define TELEPHONY_MODEM_INTERFACE              TELEPHONY_SERVICE ".modem"
+#define TELEPHONY_PROFILE_INTERFACE            TELEPHONY_SERVICE ".context"
+#define TELEPHONY_MASTER_PATH                  "/"
+#define NET_PROFILE_NAME_LEN_MAX 512
+
+typedef struct {
+       char                    profile_name[NET_PROFILE_NAME_LEN_MAX];
+} net_profile_name_t;
+
 static Network *netconfigstate = NULL;
 
 struct netconfig_default_connection {
@@ -86,6 +97,7 @@ struct netconfig_default_connection {
        char *proxy;
        char *essid;
        unsigned int freq;
+       gboolean is_metered;
 };
 
 static struct netconfig_default_connection
@@ -155,6 +167,192 @@ static char *__netconfig_get_default_profile(void)
        return default_profile;
 }
 
+static int __netconfig_telephony_get_modem_object_path(GSList **modem_path_list)
+{
+       GVariant *result;
+       GVariantIter *iter_modem = NULL;
+       GVariantIter *modem_properties = NULL;
+       const char *modem_path;
+
+       result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, TELEPHONY_MASTER_PATH,
+                       TELEPHONY_MASTER_INTERFACE, "GetModems", NULL);
+       if (result == NULL) {
+               ERR("Failed to get modem path list");
+               return -1;
+       }
+
+       g_variant_get(result, "(a{sa{ss}})", &iter_modem);
+       while (g_variant_iter_loop(iter_modem, "{sa{ss}}", &modem_path, &modem_properties)) {
+               *modem_path_list = g_slist_append(*modem_path_list, g_strdup(modem_path));
+               DBG("modem object path: %s",    modem_path);
+       }
+
+       g_variant_iter_free(iter_modem);
+       g_variant_unref(result);
+
+       return 0;
+}
+
+static int __netconfig_telephony_get_profile_list(net_profile_name_t **profile_list,
+               int *profile_count)
+{
+       int ret = 0;
+       int count = 0, i = 0;
+       const char *str = NULL;
+       GVariant *result;
+       GVariantIter *iter = NULL;
+       GSList *profiles = NULL, *list = NULL;
+       net_profile_name_t *plist = NULL;
+
+       GSList *modem_path_list = NULL;
+       const char *path = NULL;
+
+       ret = __netconfig_telephony_get_modem_object_path(&modem_path_list);
+       if (ret < 0) {
+               ERR("Failed to get modems path list");
+
+               g_slist_free_full(modem_path_list, g_free);
+               return ret;
+       }
+
+       for (list = modem_path_list; list != NULL; list = list->next) {
+               path = (const char *)list->data;
+
+               DBG("path: %s", path);
+               result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, path,
+                               TELEPHONY_MODEM_INTERFACE, "GetProfileList", NULL);
+               if (result == NULL) {
+                       DBG("Failed to get profiles: %s", path);
+                       continue;
+               }
+
+               g_variant_get(result, "(as)", &iter);
+               while (g_variant_iter_loop(iter, "s", &str))
+                       profiles = g_slist_append(profiles, g_strdup(str));
+
+               g_variant_iter_free(iter);
+               g_variant_unref(result);
+       }
+
+       g_slist_free_full(modem_path_list, g_free);
+
+       count = g_slist_length(profiles);
+       if (count > 0) {
+               plist = (net_profile_name_t*)malloc(sizeof(net_profile_name_t) * count);
+       } else {
+               *profile_count = 0;
+               goto out;
+       }
+
+       if (plist == NULL) {
+               ERR("Failed to allocate memory");
+               *profile_count = 0;
+               ret = -1;
+               goto out;
+       }
+
+       for (list = profiles, i = 0; list != NULL; list = list->next, i++)
+               g_strlcpy(plist[i].profile_name,
+                               (const char *)list->data, NET_PROFILE_NAME_LEN_MAX);
+
+       *profile_list = plist;
+       *profile_count = count;
+
+out:
+       g_slist_free_full(profiles, g_free);
+
+       return ret;
+}
+
+static int __netconfig_telephony_search_pdp_profile(const char* profile_name, net_profile_name_t* pdp_name)
+{
+       int ret;
+       net_profile_name_t* profile_list = NULL;
+       char* prof_name = NULL;
+       char* tel_prof_name = NULL;
+       char* found_ptr = NULL;
+       int profile_count = 0;
+       int i;
+
+       /* Get pdp profile list from telephony service */
+       ret = __netconfig_telephony_get_profile_list(&profile_list, &profile_count);
+       if (ret < 0) {
+               ERR("Failed to get profile list from telephony service");
+               g_free(profile_list);
+               return ret;
+       }
+
+       if (profile_list == NULL || profile_count <= 0) {
+               ERR("There is no PDP profiles");
+               g_free(profile_list);
+               return -1;
+       }
+
+       /* Find matching profile */
+       prof_name = strrchr(profile_name, '/') + 1;
+       for (i = 0; i < profile_count; i++) {
+               tel_prof_name = strrchr(profile_list[i].profile_name, '/') + 1;
+               found_ptr = strstr(prof_name, tel_prof_name);
+
+               if (found_ptr != NULL && g_strcmp0(found_ptr, tel_prof_name) == 0) {
+                       g_strlcpy(pdp_name->profile_name,
+                                       profile_list[i].profile_name, NET_PROFILE_NAME_LEN_MAX);
+
+                       DBG("PDP profile name found in cellular profile: %s", pdp_name->profile_name);
+                       break;
+               }
+       }
+
+       if (i >= profile_count) {
+               ERR("There is no matching PDP profiles");
+               g_free(profile_list);
+               return -1;
+       }
+
+       g_free(profile_list);
+
+       return ret;
+}
+
+static gboolean __netconfig_telephony_get_metered_info(net_profile_name_t* pdp_name)
+{
+       GVariant *result;
+       GVariantIter *iter;
+       const gchar *key = NULL;
+       const gchar *value = NULL;
+       gboolean ret = FALSE;
+
+       if (pdp_name == NULL) {
+               ERR("Invalid parameter!");
+               return ret;
+       }
+
+       result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, pdp_name->profile_name,
+                       TELEPHONY_PROFILE_INTERFACE, "GetProfile", NULL);
+       if (result == NULL) {
+               ERR("_net_invoke_dbus_method failed");
+               return ret;
+       }
+
+       g_variant_get(result, "(a{ss})", &iter);
+       while (g_variant_iter_next(iter, "{ss}", &key, &value)) {
+               if (g_strcmp0(key, "is_metered") == 0) {
+                       if (value == NULL)
+                               continue;
+
+                       if (g_strcmp0(value, "TRUE") == 0)
+                               ret = TRUE;
+               }
+       }
+
+       g_variant_iter_free(iter);
+       g_variant_unref(result);
+
+       DBG("is_metered = %s", ret ? "TRUE" : "FALSE");
+
+       return ret;
+}
+
 static void __netconfig_get_default_connection_info(const char *profile)
 {
        GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
@@ -283,6 +481,16 @@ static void __netconfig_get_default_connection_info(const char *profile)
                }
        }
 
+       if (netconfig_is_cellular_profile(profile) == TRUE) {
+               net_profile_name_t pdp_name;
+               int ret;
+
+               ret = __netconfig_telephony_search_pdp_profile(profile, &pdp_name);
+               if (ret >= 0 && strlen(pdp_name.profile_name) > 0)
+                       if (__netconfig_telephony_get_metered_info(&pdp_name))
+                               netconfig_default_connection_info.is_metered = TRUE;
+       }
+
 done:
        if (message)
                g_variant_unref(message);
@@ -296,6 +504,74 @@ done:
        return;
 }
 
+static char *__netconfig_get_preferred_ipv6_address(char *profile)
+{
+       GVariant *message = NULL, *variant = NULL, *next = NULL;
+       GVariantIter *iter = NULL, *sub_iter = NULL, *service = NULL;
+       gchar *obj_path;
+       gchar *key = NULL;
+       gchar *sub_key = NULL;
+       gchar *preferred_address6 = NULL;
+       gboolean found_profile = 0;
+
+       message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
+                                       CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
+                                       "GetServices", NULL);
+       if (message == NULL) {
+               ERR("Failed to get service 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;
+       }
+
+       while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
+               const gchar *value = NULL;
+               if (g_strcmp0(key, "IPv6") == 0) {
+                       g_variant_get(next, "a{sv}", &sub_iter);
+                       if (sub_iter == NULL)
+                               continue;
+                       while (g_variant_iter_loop(sub_iter, "{sv}", &sub_key, &variant)) {
+                               if (g_strcmp0(sub_key, "Address") == 0) {
+                                       value = g_variant_get_string(variant, NULL);
+                                       preferred_address6 = g_strdup(value);
+                                       g_variant_iter_free(sub_iter);
+                                       goto done;
+                               }
+                       }
+                       g_variant_iter_free(sub_iter);
+               }
+       }
+
+done:
+       if (message)
+               g_variant_unref(message);
+
+       if (iter)
+               g_variant_iter_free(iter);
+
+       if (service)
+               g_variant_iter_free(service);
+
+       return preferred_address6;
+}
+
 static void __netconfig_adjust_tcp_buffer_size(void)
 {
        int fdr = 0, fdw = 0;
@@ -450,7 +726,7 @@ static void __netconfig_update_default_connection_info(void)
        if (profile == NULL)
                DBG("Reset network state configuration");
        else
-               DBG("%s: ip(%s) ip6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
+               DBG("profile[%s] ipv4(%s) ipv6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
 
        netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
 
@@ -719,6 +995,11 @@ const char *netconfig_wifi_get_connected_essid(const char *default_profile)
        return netconfig_default_connection_info.essid;
 }
 
+gboolean netconfig_get_default_is_metered(void)
+{
+       return netconfig_default_connection_info.is_metered;
+}
+
 static int __netconfig_reset_ipv4_socket(void)
 {
        int ret;
@@ -797,33 +1078,38 @@ void netconfig_update_default_profile(const char *profile)
                netconfig_default_connection_info.proxy = NULL;
 
                netconfig_default_connection_info.freq = 0;
+               netconfig_default_connection_info.is_metered = FALSE;
 
-               if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
-                       g_free(netconfig_default_connection_info.essid);
-                       netconfig_default_connection_info.essid = NULL;
-               }
+               g_free(netconfig_default_connection_info.essid);
+               netconfig_default_connection_info.essid = NULL;
        }
 
        /* default profile is NULL and new connected profile is NULL */
        if (!profile) {
-               profile = __netconfig_get_default_profile();
+               char *tmp_profile = __netconfig_get_default_profile();
 
-               if (profile && netconfig_is_cellular_profile(profile) &&
-                       !netconfig_is_cellular_internet_profile(profile)) {
+               if (tmp_profile && netconfig_is_cellular_profile(tmp_profile) &&
+                       !netconfig_is_cellular_internet_profile(tmp_profile)) {
                        DBG("not a default cellular profile");
-                       profile = NULL;
+                       g_free(tmp_profile);
+                       tmp_profile = NULL;
                }
 
-               if (!profile) {
+               if (!tmp_profile) {
                        __netconfig_update_default_connection_info();
                        return;
                }
+
+               netconfig_default_connection_info.profile = g_strdup(tmp_profile);
+               __netconfig_get_default_connection_info(tmp_profile);
+               __netconfig_update_default_connection_info();
+               g_free(tmp_profile);
+               return;
        }
 
        netconfig_default_connection_info.profile = g_strdup(profile);
        __netconfig_get_default_connection_info(profile);
        __netconfig_update_default_connection_info();
-
 }
 
 void netconfig_update_default(void)
@@ -877,9 +1163,11 @@ char *netconfig_get_ifname(const char *profile)
                        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);
+                                       if (!ifname)
+                                               ifname = g_strdup(value);
+                                       g_free(key1);
+                                       g_variant_unref(variant);
+                                       break;
                                }
                        }
                }
@@ -1071,6 +1359,35 @@ gboolean handle_ethernet_cable_state(Network *object,
        return TRUE;
 }
 
+gboolean handle_get_metered_info(Network *object,
+       GDBusMethodInvocation *context)
+{
+       gboolean state = 0;
+
+       state = netconfig_get_default_is_metered();
+
+       DBG("Default metered state [%s]", state ? "TRUE" : "FALSE");
+       network_complete_get_metered_info(object, context, state);
+       return TRUE;
+}
+
+gboolean handle_preferred_ipv6_address(Network *object,
+       GDBusMethodInvocation *context, gchar *profile)
+{
+       char *address = NULL;
+
+       address = __netconfig_get_preferred_ipv6_address(profile);
+       if (address == NULL) {
+               DBG("Failed to get preferred IPv6 address");
+               netconfig_error_fail_preferred_ipv6_address(context);
+               return FALSE;
+       }
+
+       DBG("Successfully get preferred IPv6 address[%s]", address);
+       network_complete_preferred_ipv6_address(object, context, address);
+       return TRUE;
+}
+
 void state_object_create_and_init(void)
 {
        DBG("Creating network state object");
@@ -1097,6 +1414,8 @@ void state_object_create_and_init(void)
                                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-preferred-ipv6-address",
+                               G_CALLBACK(handle_preferred_ipv6_address), NULL);
        g_signal_connect(netconfigstate, "handle-remove-route",
                                G_CALLBACK(handle_remove_route), NULL);
        g_signal_connect(netconfigstate, "handle-launch-mdns",
@@ -1109,6 +1428,8 @@ void state_object_create_and_init(void)
                                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);
+       g_signal_connect(netconfigstate, "handle-get-metered-info",
+                               G_CALLBACK(handle_get_metered_info), NULL);
 
        if (!g_dbus_interface_skeleton_export(interface_network, connection,
                        NETCONFIG_NETWORK_STATE_PATH, NULL)) {