From cca9cfc84d7f458c38ade150754b320ceebc4a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0imerda?= Date: Wed, 3 Oct 2012 14:50:59 +0200 Subject: [PATCH] keyfile: read and write a nicer format for IPv4 and IPv6 addresses and routes You can now use 'address=' even for IPv6 and it's the encouraged way to set up a single address manually. For multiple addresses, 'address0=', 'address1=', etc, should be preferred. Example: address=10.0.0.15/24/10.0.0.1 address0=192.168.0.1/24 address1=10.0.0.16/32 Example (backward compatibility): addresses=10.0.0.15/24/10.0.0.1 addresses0=192.168.0.1/24 addresses1=10.0.0.16/32 --- src/settings/plugins/keyfile/reader.c | 85 ++++++++++----------------- src/settings/plugins/keyfile/writer.c | 8 +-- 2 files changed, 35 insertions(+), 58 deletions(-) diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 70a790c0f1..6ddf50d57d 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -364,69 +364,46 @@ error: } static void -ip_addr_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path) +ip_address_or_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path) { const char *setting_name = nm_setting_get_name (setting); gboolean ipv6 = !strcmp (setting_name, "ipv6"); - GPtrArray *addresses; + gboolean routes = !strcmp (key, "routes"); + static const char *key_names_routes[] = { "route", "routes", NULL }; + static const char *key_names_addresses[] = { "address", "addresses", NULL }; + const char **key_names = routes ? key_names_routes : key_names_addresses; + GPtrArray *list; int i; - addresses = g_ptr_array_new_with_free_func ( + list = g_ptr_array_new_with_free_func ( ipv6 ? (GDestroyNotify) g_value_array_free : (GDestroyNotify) g_array_unref); - /* Look for individual addresses */ - for (i = 0; i < 1000; i++) { - char *key_name; - /* address is a GArray of three guint32 for IPv4 and a GValueArray consisting - * of GByteArray, guint32 and GByteArray for IPv6. - */ - gpointer address; - - key_name = g_strdup_printf ("%s%d", key, i); - address = read_one_ip_address_or_route (keyfile, setting_name, key_name, ipv6, FALSE); - if (address) { - g_ptr_array_add (addresses, address); - } - g_free (key_name); - } - - if (addresses->len >= 1) - g_object_set (setting, key, addresses, NULL); - - g_ptr_array_unref (addresses); -} - -static void -ip_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path) -{ - const char *setting_name = nm_setting_get_name (setting); - gboolean ipv6 = !strcmp (setting_name, "ipv6"); - GPtrArray *routes; - int i; - - routes = g_ptr_array_new_with_free_func ( - ipv6 ? (GDestroyNotify) g_value_array_free : (GDestroyNotify) g_array_unref); - - /* Look for individual routes */ for (i = -1; i < 1000; i++) { - char *key_name; - /* route is a GArray of three or four guint32 for IPv4 and a GValueArray - * consisting of GByteArray, guint32, GByteArray and optional guint32 - * for IPv6. - */ - gpointer route; + const char **key_basename; - key_name = g_strdup_printf ("%s%d", key, i); - route = read_one_ip_address_or_route (keyfile, setting_name, key_name, ipv6, TRUE); - if (route) { - g_ptr_array_add (routes, route); + for (key_basename = key_names; *key_basename; key_basename++) { + char *key_name; + gpointer item; + + /* -1 means no suffix */ + if (i >= 0) + key_name = g_strdup_printf ("%s%d", *key_basename, i); + else + key_name = g_strdup (*key_basename); + + item = read_one_ip_address_or_route (keyfile, setting_name, key_name, ipv6, routes); + + if (item) + g_ptr_array_add (list, item); + + g_free (key_name); } } - if (routes->len >= 1) - g_object_set (setting, key, routes, NULL); + if (list->len >= 1) + g_object_set (setting, key, list, NULL); - g_ptr_array_unref (routes); + g_ptr_array_unref (list); } static void @@ -844,19 +821,19 @@ static KeyParser key_parsers[] = { { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES, FALSE, - ip_addr_parser }, + ip_address_or_route_parser }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ADDRESSES, FALSE, - ip_addr_parser }, + ip_address_or_route_parser }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ROUTES, FALSE, - ip_route_parser }, + ip_address_or_route_parser }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ROUTES, FALSE, - ip_route_parser }, + ip_address_or_route_parser }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS, FALSE, diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 8e99b32c4f..0b8c15929f 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -173,7 +173,7 @@ ip4_addr_writer (GKeyFile *file, array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip4_values (file, setting_name, key, array, 3, 0, 2); + write_ip4_values (file, setting_name, "address", array, 3, 0, 2); } static void @@ -191,7 +191,7 @@ ip4_route_writer (GKeyFile *file, array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip4_values (file, setting_name, key, array, 4, 0, 2); + write_ip4_values (file, setting_name, "route", array, 4, 0, 2); } static void @@ -336,7 +336,7 @@ ip6_addr_writer (GKeyFile *file, ip6_addr = ip6_array_to_addr_prefix (values); if (ip6_addr) { /* Write it out */ - key_name = g_strdup_printf ("%s%d", key, j++); + key_name = g_strdup_printf ("address%d", j++); g_key_file_set_string (file, setting_name, key_name, ip6_addr); g_free (key_name); g_free (ip6_addr); @@ -379,7 +379,7 @@ ip6_route_writer (GKeyFile *file, g_string_append_printf (output, ",%d", int_val); /* Write it out */ - key_name = g_strdup_printf ("%s%d", key, j++); + key_name = g_strdup_printf ("route%d", j++); g_key_file_set_string (file, setting_name, key_name, output->str); g_free (key_name);