X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fipconfig.c;h=ec4c396fc11103a5ba52f45ea892f81394827171;hb=3b0014134b2153de07f0c0b953a243a07d9029a9;hp=ae70745fabd372150a0ba207e0ddebbe3aa55203;hpb=bcae74da8fa2958b3fec9153fc33e41f0e0317bf;p=platform%2Fupstream%2Fconnman.git diff --git a/src/ipconfig.c b/src/ipconfig.c old mode 100644 new mode 100755 index ae70745..ec4c396 --- a/src/ipconfig.c +++ b/src/ipconfig.c @@ -45,16 +45,17 @@ struct connman_ipconfig { int index; enum connman_ipconfig_type type; - struct connman_ipconfig *origin; - const struct connman_ipconfig_ops *ops; void *ops_data; - bool enabled; enum connman_ipconfig_method method; struct connman_ipaddress *address; struct connman_ipaddress *system; +#if defined TIZEN_EXT + int dhcp_lease_duration; +#endif + int ipv6_privacy_config; char *last_dhcp_address; char **last_dhcpv6_prefixes; @@ -88,16 +89,101 @@ struct connman_ipdevice { int ipv6_privacy; }; +struct ipconfig_store { + GKeyFile *file; + const char *group; + const char *prefix; +}; + static GHashTable *ipdevice_hash = NULL; static GList *ipconfig_list = NULL; static bool is_ipv6_supported = false; -void __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig) +static void store_set_str(struct ipconfig_store *store, + const char *key, const char *val) + { - if (!ipconfig) + char *pk; + + if (!val || strlen(val) == 0) + return; + + pk = g_strdup_printf("%s%s", store->prefix, key); + g_key_file_set_string(store->file, store->group, pk, val); + g_free(pk); +} + +static char *store_get_str(struct ipconfig_store *store, const char *key) +{ + char *pk, *val; + + pk = g_strdup_printf("%s%s", store->prefix, key); + val = g_key_file_get_string(store->file, store->group, pk, NULL); + g_free(pk); + + return val; +} + +static void store_set_strs(struct ipconfig_store *store, + const char *key, char **val) +{ + guint len; + char *pk; + + if (!val) + return; + + len = g_strv_length(val); + if (len == 0) return; - connman_ipaddress_clear(ipconfig->address); + pk = g_strdup_printf("%s%s", store->prefix, key); + g_key_file_set_string_list(store->file, store->group, + pk, (const gchar **)val, len); + g_free(pk); +} + +static char **store_get_strs(struct ipconfig_store *store, const char *key) +{ + gsize len; + char *pk, **val; + + pk = g_strdup_printf("%s%s", store->prefix, key); + val = g_key_file_get_string_list(store->file, store->group, + pk, &len, NULL); + g_free(pk); + + if (val && len == 0) { + g_free(val); + return NULL; + } + + return val; +} + +static void store_set_int(struct ipconfig_store *store, + const char *key, int val) +{ + char *pk; + + if (val == 0) + return; + + pk = g_strdup_printf("%s%s", store->prefix, key); + g_key_file_set_integer(store->file, store->group, pk, val); + g_free(pk); +} + +static int store_get_int(struct ipconfig_store *store, const char *key) +{ + int val; + char *pk; + + pk = g_strdup_printf("%s%s", store->prefix, key); + val = g_key_file_get_integer(store->file, store->group, pk, 0); + g_free(pk); + + return val; } static void free_address_list(struct connman_ipdevice *ipdevice) @@ -135,6 +221,7 @@ const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type) { switch (type) { case CONNMAN_IPCONFIG_TYPE_UNKNOWN: + case CONNMAN_IPCONFIG_TYPE_ALL: return "unknown"; case CONNMAN_IPCONFIG_TYPE_IPV4: return "IPv4"; @@ -401,34 +488,15 @@ static void free_ipdevice(gpointer data) g_free(ipdevice->address); - set_ipv6_state(ifname, ipdevice->ipv6_enabled); - set_ipv6_privacy(ifname, ipdevice->ipv6_privacy); + if (ifname) { + set_ipv6_state(ifname, ipdevice->ipv6_enabled); + set_ipv6_privacy(ifname, ipdevice->ipv6_privacy); + } g_free(ifname); g_free(ipdevice); } -static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice) -{ - DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4, - ipdevice->config_ipv6); -} - -static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice) -{ - DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4, - ipdevice->config_ipv6); - - if (ipdevice->config_ipv4) - connman_inet_clear_address(ipdevice->index, - ipdevice->config_ipv4->address); - - if (ipdevice->config_ipv6) - connman_inet_clear_ipv6_address(ipdevice->index, - ipdevice->config_ipv6->address->local, - ipdevice->config_ipv6->address->prefixlen); -} - static void update_stats(struct connman_ipdevice *ipdevice, const char *ifname, struct rtnl_link_stats *stats) { @@ -445,12 +513,9 @@ static void update_stats(struct connman_ipdevice *ipdevice, if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6) return; - if (ipdevice->config_ipv4) - service = __connman_ipconfig_get_data(ipdevice->config_ipv4); - else if (ipdevice->config_ipv6) - service = __connman_ipconfig_get_data(ipdevice->config_ipv6); - else - return; + service = __connman_service_lookup_from_index(ipdevice->index); + + DBG("service %p", service); if (!service) return; @@ -512,6 +577,16 @@ void __connman_ipconfig_newlink(int index, unsigned short type, index, type, type2str(type)); update: +#if defined TIZEN_EXT + if (g_strcmp0(ipdevice->address, address) != 0) { + /* If an original address is built-in physical device, + * it's hardly get an address at a initial creation + */ + g_free(ipdevice->address); + ipdevice->address = g_strdup(address); + } +#endif + ipdevice->mtu = mtu; update_stats(ipdevice, ifname, stats); @@ -581,11 +656,6 @@ update: g_list_free(ipconfig_copy); - if (lower_up) - __connman_ipconfig_lower_up(ipdevice); - if (lower_down) - __connman_ipconfig_lower_down(ipdevice); - out: g_free(ifname); } @@ -626,8 +696,6 @@ void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats) g_free(ifname); - __connman_ipconfig_lower_down(ipdevice); - g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index)); } @@ -642,7 +710,7 @@ static inline gint check_duplicate_address(gconstpointer a, gconstpointer b) return g_strcmp0(addr1->local, addr2->local); } -void __connman_ipconfig_newaddr(int index, int family, const char *label, +int __connman_ipconfig_newaddr(int index, int family, const char *label, unsigned char prefixlen, const char *address) { struct connman_ipdevice *ipdevice; @@ -655,11 +723,11 @@ void __connman_ipconfig_newaddr(int index, int family, const char *label, ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index)); if (!ipdevice) - return; + return -ENXIO; ipaddress = connman_ipaddress_alloc(family); if (!ipaddress) - return; + return -ENOMEM; ipaddress->prefixlen = prefixlen; ipaddress->local = g_strdup(address); @@ -667,7 +735,7 @@ void __connman_ipconfig_newaddr(int index, int family, const char *label, if (g_slist_find_custom(ipdevice->address_list, ipaddress, check_duplicate_address)) { connman_ipaddress_free(ipaddress); - return; + return -EALREADY; } if (family == AF_INET) @@ -675,7 +743,7 @@ void __connman_ipconfig_newaddr(int index, int family, const char *label, else if (family == AF_INET6) type = CONNMAN_IPCONFIG_TYPE_IPV6; else - return; + return -EINVAL; ipdevice->address_list = g_slist_prepend(ipdevice->address_list, ipaddress); @@ -698,7 +766,7 @@ void __connman_ipconfig_newaddr(int index, int family, const char *label, goto out; if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP)) - return; + goto out; for (list = g_list_first(ipconfig_list); list; list = g_list_next(list)) { @@ -717,8 +785,33 @@ void __connman_ipconfig_newaddr(int index, int family, const char *label, ipconfig->ops->ip_bound(ipconfig, ifname); } +#if defined TIZEN_EXT + const char *local; + struct connman_service *service; + struct connman_ipconfig *local_ipconfig; + + service = connman_service_get_default(); + if (!service) + goto out; + + local_ipconfig = __connman_service_get_ip4config(service); + if (!local_ipconfig) + goto out; + + local = __connman_ipconfig_get_local(local_ipconfig); + if (!local) + goto out; + + DBG("local %s", local); + + if (g_strcmp0(local, address) != 0) + goto out; + + __connman_connection_update_default_gateway(); +#endif out: g_free(ifname); + return 0; } void __connman_ipconfig_deladdr(int index, int family, const char *label, @@ -1077,16 +1170,32 @@ void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, ipconfig->address->gateway = g_strdup(gateway); } +#if defined TIZEN_EXT +void __connman_ipconfig_set_dhcp_lease_duration(struct connman_ipconfig *ipconfig, + int dhcp_lease_duration) +{ + ipconfig->dhcp_lease_duration = dhcp_lease_duration; +} +#endif + +#if defined TIZEN_EXT +int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service) +#else int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig) +#endif { +#if !defined TIZEN_EXT struct connman_service *service; +#endif DBG(""); if (!ipconfig->address) return -EINVAL; +#if !defined TIZEN_EXT service = __connman_service_lookup_from_index(ipconfig->index); +#endif if (!service) return -EINVAL; @@ -1136,8 +1245,6 @@ static struct connman_ipconfig *create_ipv6config(int index) struct connman_ipconfig *ipv6config; struct connman_ipdevice *ipdevice; - DBG("index %d", index); - ipv6config = g_try_new0(struct connman_ipconfig, 1); if (!ipv6config) return NULL; @@ -1145,7 +1252,6 @@ static struct connman_ipconfig *create_ipv6config(int index) ipv6config->refcount = 1; ipv6config->index = index; - ipv6config->enabled = false; ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6; if (!is_ipv6_supported) @@ -1155,7 +1261,11 @@ static struct connman_ipconfig *create_ipv6config(int index) ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index)); if (ipdevice) +#if !defined TIZEN_EXT ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy; +#else + ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy = 2; +#endif ipv6config->address = connman_ipaddress_alloc(AF_INET6); if (!ipv6config->address) { @@ -1165,7 +1275,7 @@ static struct connman_ipconfig *create_ipv6config(int index) ipv6config->system = connman_ipaddress_alloc(AF_INET6); - DBG("ipconfig %p method %s", ipv6config, + DBG("ipconfig %p index %d method %s", ipv6config, index, __connman_ipconfig_method2string(ipv6config->method)); return ipv6config; @@ -1186,8 +1296,6 @@ struct connman_ipconfig *__connman_ipconfig_create(int index, if (type == CONNMAN_IPCONFIG_TYPE_IPV6) return create_ipv6config(index); - DBG("index %d", index); - ipconfig = g_try_new0(struct connman_ipconfig, 1); if (!ipconfig) return NULL; @@ -1195,7 +1303,6 @@ struct connman_ipconfig *__connman_ipconfig_create(int index, ipconfig->refcount = 1; ipconfig->index = index; - ipconfig->enabled = false; ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4; ipconfig->address = connman_ipaddress_alloc(AF_INET); @@ -1205,8 +1312,10 @@ struct connman_ipconfig *__connman_ipconfig_create(int index, } ipconfig->system = connman_ipaddress_alloc(AF_INET); - - DBG("ipconfig %p", ipconfig); +#if defined TIZEN_EXT + if (!simplified_log) +#endif + DBG("ipconfig %p index %d", ipconfig, index); return ipconfig; } @@ -1241,7 +1350,9 @@ void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig, { if (!ipconfig) return; - +#if defined TIZEN_EXT + if (!simplified_log) +#endif DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1, file, line, caller); @@ -1253,11 +1364,6 @@ void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig, __connman_ipconfig_set_ops(ipconfig, NULL); - if (ipconfig->origin && ipconfig->origin != ipconfig) { - __connman_ipconfig_unref(ipconfig->origin); - ipconfig->origin = NULL; - } - connman_ipaddress_free(ipconfig->system); connman_ipaddress_free(ipconfig->address); g_free(ipconfig->last_dhcp_address); @@ -1302,9 +1408,6 @@ int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig) if (!ipconfig) return -1; - if (ipconfig->origin) - return ipconfig->origin->index; - return ipconfig->index; } @@ -1347,8 +1450,6 @@ enum connman_ipconfig_method __connman_ipconfig_get_method( int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig) { - DBG(""); - switch (ipconfig->method) { case CONNMAN_IPCONFIG_METHOD_UNKNOWN: case CONNMAN_IPCONFIG_METHOD_OFF: @@ -1372,25 +1473,22 @@ int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig) { int err; - DBG(""); - if (!ipconfig) return 0; - DBG("method %d", ipconfig->method); - switch (ipconfig->method) { case CONNMAN_IPCONFIG_METHOD_UNKNOWN: case CONNMAN_IPCONFIG_METHOD_OFF: break; case CONNMAN_IPCONFIG_METHOD_AUTO: - case CONNMAN_IPCONFIG_METHOD_FIXED: case CONNMAN_IPCONFIG_METHOD_DHCP: - case CONNMAN_IPCONFIG_METHOD_MANUAL: err = __connman_ipconfig_address_unset(ipconfig); connman_ipaddress_clear(ipconfig->address); return err; + case CONNMAN_IPCONFIG_METHOD_FIXED: + case CONNMAN_IPCONFIG_METHOD_MANUAL: + return __connman_ipconfig_address_unset(ipconfig); } return 0; @@ -1400,12 +1498,14 @@ int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig) { int err; - DBG(""); - if (!ipconfig) return 0; +#if defined TIZEN_EXT + DBG("ipconfig method %d type %d", ipconfig->method, ipconfig->type); +#else DBG("method %d", ipconfig->method); +#endif switch (ipconfig->method) { case CONNMAN_IPCONFIG_METHOD_UNKNOWN: @@ -1437,8 +1537,6 @@ int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig, { struct connman_ipdevice *ipdevice; - DBG("ipconfig %p", ipconfig); - if (!ipconfig || ipconfig->index < 0) return -ENODEV; @@ -1457,8 +1555,6 @@ const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipc { struct connman_ipdevice *ipdevice; - DBG("ipconfig %p", ipconfig); - if (!ipconfig || ipconfig->index < 0) return NULL; @@ -1611,8 +1707,6 @@ int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig) } else return -EINVAL; - ipconfig->enabled = true; - if (type == CONNMAN_IPCONFIG_TYPE_IPV4 && ipdevice->config_ipv4) { ipconfig_list = g_list_remove(ipconfig_list, @@ -1673,7 +1767,9 @@ int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig) int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig) { struct connman_ipdevice *ipdevice; - +#if defined TIZEN_EXT + if (!simplified_log) +#endif DBG("ipconfig %p", ipconfig); if (!ipconfig || ipconfig->index < 0) @@ -1687,8 +1783,6 @@ int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig) if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6) return -EINVAL; - ipconfig->enabled = false; - if (ipdevice->config_ipv4 == ipconfig) { ipconfig_list = g_list_remove(ipconfig_list, ipconfig); @@ -1701,6 +1795,11 @@ int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig) if (ipdevice->config_ipv6 == ipconfig) { ipconfig_list = g_list_remove(ipconfig_list, ipconfig); +#if defined TIZEN_EXT + if (ipdevice->config_ipv6->method == + CONNMAN_IPCONFIG_METHOD_AUTO) + disable_ipv6(ipdevice->config_ipv6); +#endif connman_ipaddress_clear(ipdevice->config_ipv6->system); __connman_ipconfig_unref(ipdevice->config_ipv6); ipdevice->config_ipv6 = NULL; @@ -1797,8 +1896,6 @@ int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig, if (!ipconfig) return -EINVAL; - DBG("ipconfig %p privacy %s", ipconfig, value); - privacy = string2privacy(value); ipconfig->ipv6_privacy_config = privacy; @@ -1814,8 +1911,6 @@ void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig, struct connman_ipaddress *append_addr = NULL; const char *str; - DBG(""); - if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4) return; @@ -1828,16 +1923,21 @@ void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig, switch (ipconfig->method) { case CONNMAN_IPCONFIG_METHOD_UNKNOWN: case CONNMAN_IPCONFIG_METHOD_OFF: - case CONNMAN_IPCONFIG_METHOD_AUTO: return; case CONNMAN_IPCONFIG_METHOD_FIXED: append_addr = ipconfig->address; break; + case CONNMAN_IPCONFIG_METHOD_AUTO: case CONNMAN_IPCONFIG_METHOD_MANUAL: case CONNMAN_IPCONFIG_METHOD_DHCP: append_addr = ipconfig->system; +#if defined TIZEN_EXT + /* TIZEN enables get_properties before __connman_ipconfig_newaddr */ + if (append_addr && append_addr->local == NULL) + append_addr = ipconfig->address; +#endif break; } @@ -1862,6 +1962,20 @@ void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig, if (append_addr->gateway) connman_dbus_dict_append_basic(iter, "Gateway", DBUS_TYPE_STRING, &append_addr->gateway); + +#if defined TIZEN_EXT + if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_DHCP) { + char *server_ip; + server_ip = __connman_dhcp_get_server_address(ipconfig); + if (server_ip) { + connman_dbus_dict_append_basic(iter, "DHCPServerIP", + DBUS_TYPE_STRING, &server_ip); + g_free(server_ip); + } + connman_dbus_dict_append_basic(iter, "DHCPLeaseDuration", + DBUS_TYPE_INT32, &ipconfig->dhcp_lease_duration); + } +#endif } void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig, @@ -1871,8 +1985,6 @@ void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig, struct connman_ipaddress *append_addr = NULL; const char *str, *privacy; - DBG(""); - if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6) return; @@ -1901,6 +2013,11 @@ void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig, case CONNMAN_IPCONFIG_METHOD_DHCP: case CONNMAN_IPCONFIG_METHOD_AUTO: append_addr = ipconfig->system; +#if defined TIZEN_EXT + /* TIZEN enables get_properties before __connman_ipconfig_newaddr */ + if (append_addr && append_addr->local == NULL) + append_addr = ipconfig->address; +#endif break; } @@ -1929,8 +2046,6 @@ void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig, { const char *str, *privacy; - DBG(""); - str = __connman_ipconfig_method2string(ipconfig->method); if (!str) return; @@ -1973,8 +2088,6 @@ void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig, { const char *str; - DBG(""); - str = __connman_ipconfig_method2string(ipconfig->method); if (!str) return; @@ -2025,8 +2138,6 @@ int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig, DBusMessageIter dict; int type = -1; - DBG("ipconfig %p", ipconfig); - if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) return -EINVAL; @@ -2106,6 +2217,10 @@ int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig, case CONNMAN_IPCONFIG_METHOD_OFF: ipconfig->method = method; +#if defined TIZEN_EXT + if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) + disable_ipv6(ipconfig); +#endif break; @@ -2116,6 +2231,9 @@ int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig, ipconfig->method = method; if (privacy_string) ipconfig->ipv6_privacy_config = privacy; +#if defined TIZEN_EXT + enable_ipv6(ipconfig); +#endif break; @@ -2128,6 +2246,7 @@ int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig, type = AF_INET6; break; case CONNMAN_IPCONFIG_TYPE_UNKNOWN: + case CONNMAN_IPCONFIG_TYPE_ALL: type = -1; break; } @@ -2197,202 +2316,153 @@ void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig, DBUS_TYPE_UINT16, &ipdevice->mtu); } -int __connman_ipconfig_load(struct connman_ipconfig *ipconfig, +void __connman_ipconfig_load(struct connman_ipconfig *ipconfig, GKeyFile *keyfile, const char *identifier, const char *prefix) { char *method; - char *key; char *str; + struct ipconfig_store is = { .file = keyfile, + .group = identifier, + .prefix = prefix }; DBG("ipconfig %p identifier %s", ipconfig, identifier); - key = g_strdup_printf("%smethod", prefix); - method = g_key_file_get_string(keyfile, identifier, key, NULL); + method = store_get_str(&is, "method"); if (!method) { switch (ipconfig->type) { case CONNMAN_IPCONFIG_TYPE_IPV4: ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP; break; + case CONNMAN_IPCONFIG_TYPE_IPV6: ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO; break; + case CONNMAN_IPCONFIG_TYPE_UNKNOWN: + case CONNMAN_IPCONFIG_TYPE_ALL: ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF; break; } - } else + } else { ipconfig->method = __connman_ipconfig_string2method(method); + g_free(method); + } if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF; if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) { - gsize length; - char *pprefix; - if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO || - ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) { + ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) { char *privacy; - pprefix = g_strdup_printf("%sprivacy", prefix); - privacy = g_key_file_get_string(keyfile, identifier, - pprefix, NULL); + privacy = store_get_str(&is, "privacy"); ipconfig->ipv6_privacy_config = string2privacy(privacy); - g_free(pprefix); g_free(privacy); } - pprefix = g_strdup_printf("%sDHCP.LastPrefixes", prefix); + g_strfreev(ipconfig->last_dhcpv6_prefixes); ipconfig->last_dhcpv6_prefixes = - g_key_file_get_string_list(keyfile, identifier, pprefix, - &length, NULL); - if (ipconfig->last_dhcpv6_prefixes && length == 0) { - g_free(ipconfig->last_dhcpv6_prefixes); - ipconfig->last_dhcpv6_prefixes = NULL; - } - g_free(pprefix); + store_get_strs(&is, "DHCP.LastPrefixes"); } - g_free(method); - g_free(key); - key = g_strdup_printf("%snetmask_prefixlen", prefix); - ipconfig->address->prefixlen = g_key_file_get_integer( - keyfile, identifier, key, NULL); - g_free(key); + switch (ipconfig->method) { + case CONNMAN_IPCONFIG_METHOD_UNKNOWN: + case CONNMAN_IPCONFIG_METHOD_OFF: + break; - key = g_strdup_printf("%slocal_address", prefix); - g_free(ipconfig->address->local); - ipconfig->address->local = g_key_file_get_string( - keyfile, identifier, key, NULL); - g_free(key); + case CONNMAN_IPCONFIG_METHOD_FIXED: + case CONNMAN_IPCONFIG_METHOD_MANUAL: + ipconfig->address->prefixlen = + store_get_int(&is, "netmask_prefixlen"); - key = g_strdup_printf("%speer_address", prefix); - g_free(ipconfig->address->peer); - ipconfig->address->peer = g_key_file_get_string( - keyfile, identifier, key, NULL); - g_free(key); + g_free(ipconfig->address->local); + ipconfig->address->local = + store_get_str(&is, "local_address"); - key = g_strdup_printf("%sbroadcast_address", prefix); - g_free(ipconfig->address->broadcast); - ipconfig->address->broadcast = g_key_file_get_string( - keyfile, identifier, key, NULL); - g_free(key); + g_free(ipconfig->address->peer); + ipconfig->address->peer = + store_get_str(&is, "peer_address"); - key = g_strdup_printf("%sgateway", prefix); - g_free(ipconfig->address->gateway); - ipconfig->address->gateway = g_key_file_get_string( - keyfile, identifier, key, NULL); - g_free(key); - - key = g_strdup_printf("%sDHCP.LastAddress", prefix); - str = g_key_file_get_string(keyfile, identifier, key, NULL); - if (str) { - g_free(ipconfig->last_dhcp_address); - ipconfig->last_dhcp_address = str; - } - g_free(key); + g_free(ipconfig->address->broadcast); + ipconfig->address->broadcast = + store_get_str(&is, "broadcast_address"); - return 0; + g_free(ipconfig->address->gateway); + ipconfig->address->gateway = + store_get_str(&is, "gateway"); + break; + + case CONNMAN_IPCONFIG_METHOD_AUTO: + if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4) + break; + + /* + * If the last used method for IPv4 was AUTO then we + * try first DHCP. We will try also to use the last + * used DHCP address, if exits. + */ + __connman_ipconfig_set_method(ipconfig, + CONNMAN_IPCONFIG_METHOD_DHCP); + /* fall through */ + + case CONNMAN_IPCONFIG_METHOD_DHCP: + str = store_get_str(&is, "DHCP.LastAddress"); + if (str) { + g_free(ipconfig->last_dhcp_address); + ipconfig->last_dhcp_address = str; + } + + break; + } } -int __connman_ipconfig_save(struct connman_ipconfig *ipconfig, +void __connman_ipconfig_save(struct connman_ipconfig *ipconfig, GKeyFile *keyfile, const char *identifier, const char *prefix) { const char *method; - char *key; + struct ipconfig_store is = { .file = keyfile, + .group = identifier, + .prefix = prefix }; method = __connman_ipconfig_method2string(ipconfig->method); - DBG("ipconfig %p identifier %s method %s", ipconfig, identifier, method); - if (method) { - key = g_strdup_printf("%smethod", prefix); - g_key_file_set_string(keyfile, identifier, key, method); - g_free(key); - } + store_set_str(&is, "method", method); if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) { - const char *privacy; - privacy = privacy2string(ipconfig->ipv6_privacy_config); - key = g_strdup_printf("%sprivacy", prefix); - g_key_file_set_string(keyfile, identifier, key, privacy); - g_free(key); - - key = g_strdup_printf("%sDHCP.LastAddress", prefix); - if (ipconfig->last_dhcp_address && - strlen(ipconfig->last_dhcp_address) > 0) - g_key_file_set_string(keyfile, identifier, key, - ipconfig->last_dhcp_address); - else - g_key_file_remove_key(keyfile, identifier, key, NULL); - g_free(key); - - key = g_strdup_printf("%sDHCP.LastPrefixes", prefix); - if (ipconfig->last_dhcpv6_prefixes && - ipconfig->last_dhcpv6_prefixes[0]) { - guint len = - g_strv_length(ipconfig->last_dhcpv6_prefixes); - - g_key_file_set_string_list(keyfile, identifier, key, - (const gchar **)ipconfig->last_dhcpv6_prefixes, - len); - } else - g_key_file_remove_key(keyfile, identifier, key, NULL); - g_free(key); + store_set_str(&is, "privacy", + privacy2string(ipconfig->ipv6_privacy_config)); + + store_set_str(&is, "DHCP.LastAddress", + ipconfig->last_dhcp_address); + + store_set_strs(&is, "DHCP.LastPrefixes", + ipconfig->last_dhcpv6_prefixes); } switch (ipconfig->method) { case CONNMAN_IPCONFIG_METHOD_FIXED: case CONNMAN_IPCONFIG_METHOD_MANUAL: break; + case CONNMAN_IPCONFIG_METHOD_DHCP: - key = g_strdup_printf("%sDHCP.LastAddress", prefix); - if (ipconfig->last_dhcp_address && - strlen(ipconfig->last_dhcp_address) > 0) - g_key_file_set_string(keyfile, identifier, key, - ipconfig->last_dhcp_address); - else - g_key_file_remove_key(keyfile, identifier, key, NULL); - g_free(key); + store_set_str(&is, "DHCP.LastAddress", + ipconfig->last_dhcp_address); /* fall through */ + case CONNMAN_IPCONFIG_METHOD_UNKNOWN: case CONNMAN_IPCONFIG_METHOD_OFF: case CONNMAN_IPCONFIG_METHOD_AUTO: - return 0; + return; } - key = g_strdup_printf("%snetmask_prefixlen", prefix); - if (ipconfig->address->prefixlen != 0) - g_key_file_set_integer(keyfile, identifier, - key, ipconfig->address->prefixlen); - g_free(key); - - key = g_strdup_printf("%slocal_address", prefix); - if (ipconfig->address->local) - g_key_file_set_string(keyfile, identifier, - key, ipconfig->address->local); - g_free(key); - - key = g_strdup_printf("%speer_address", prefix); - if (ipconfig->address->peer) - g_key_file_set_string(keyfile, identifier, - key, ipconfig->address->peer); - g_free(key); - - key = g_strdup_printf("%sbroadcast_address", prefix); - if (ipconfig->address->broadcast) - g_key_file_set_string(keyfile, identifier, - key, ipconfig->address->broadcast); - g_free(key); - - key = g_strdup_printf("%sgateway", prefix); - if (ipconfig->address->gateway) - g_key_file_set_string(keyfile, identifier, - key, ipconfig->address->gateway); - g_free(key); - - return 0; + store_set_int(&is, "netmask_prefixlen", ipconfig->address->prefixlen); + store_set_str(&is, "local_address", ipconfig->address->local); + store_set_str(&is, "peer_address", ipconfig->address->peer); + store_set_str(&is, "broadcast_address", ipconfig->address->broadcast); + store_set_str(&is, "gateway", ipconfig->address->gateway); } int __connman_ipconfig_init(void)