dispatcher: trivial code rearrangement

Reorganize some code in preparation for the GDBus port. (Most of the
diff is actually just reindentation.)

This also makes explicit something that was probably a bug;
IP4_NUM_ROUTES is always set, even if it's "0" (while the same is not
true of IP6_NUM_ROUTES). (This behavior is preserved for
compatibility.)
This commit is contained in:
Dan Winship
2014-09-03 12:23:45 -04:00
parent ab4199c785
commit 408e86dd35

View File

@@ -91,8 +91,8 @@ add_domains (GSList *items,
static GSList * static GSList *
construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix)
{ {
GSList *addresses = NULL, *routes = NULL, *iter; GSList *addresses, *routes, *iter;
GArray *dns = NULL, *wins = NULL; GArray *dns, *wins;
guint32 num, i; guint32 num, i;
GString *tmp; GString *tmp;
GValue *val; GValue *val;
@@ -107,46 +107,47 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix)
/* IP addresses */ /* IP addresses */
val = g_hash_table_lookup (ip4_config, "addresses"); val = g_hash_table_lookup (ip4_config, "addresses");
if (val) if (val) {
addresses = nm_utils_ip4_addresses_from_gvalue (val); addresses = nm_utils_ip4_addresses_from_gvalue (val);
for (iter = addresses, num = 0; iter; iter = g_slist_next (iter)) { for (iter = addresses, num = 0; iter; iter = g_slist_next (iter)) {
NMIP4Address *addr = (NMIP4Address *) iter->data; NMIP4Address *addr = (NMIP4Address *) iter->data;
guint32 ip_prefix = nm_ip4_address_get_prefix (addr); guint32 ip_prefix = nm_ip4_address_get_prefix (addr);
char *addrtmp; char *addrtmp;
nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), str_addr); nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), str_addr);
nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), str_gw); nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), str_gw);
addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, num++, str_addr, ip_prefix, str_gw); addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, num++, str_addr, ip_prefix, str_gw);
items = g_slist_prepend (items, addrtmp); items = g_slist_prepend (items, addrtmp);
} }
if (num) if (num)
items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ADDRESSES=%d", prefix, num)); items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ADDRESSES=%d", prefix, num));
if (addresses)
g_slist_free_full (addresses, (GDestroyNotify) nm_ip4_address_unref); g_slist_free_full (addresses, (GDestroyNotify) nm_ip4_address_unref);
}
/* DNS servers */ /* DNS servers */
val = g_hash_table_lookup (ip4_config, "nameservers"); val = g_hash_table_lookup (ip4_config, "nameservers");
if (val && G_VALUE_HOLDS (val, DBUS_TYPE_G_UINT_ARRAY)) if (val && G_VALUE_HOLDS (val, DBUS_TYPE_G_UINT_ARRAY)) {
dns = (GArray *) g_value_get_boxed (val); dns = (GArray *) g_value_get_boxed (val);
if (dns && (dns->len > 0)) { if (dns && (dns->len > 0)) {
gboolean first = TRUE; gboolean first = TRUE;
tmp = g_string_new (NULL); tmp = g_string_new (NULL);
g_string_append_printf (tmp, "%sIP4_NAMESERVERS=", prefix); g_string_append_printf (tmp, "%sIP4_NAMESERVERS=", prefix);
for (i = 0; i < dns->len; i++) { for (i = 0; i < dns->len; i++) {
guint32 addr; guint32 addr;
addr = g_array_index (dns, guint32, i); addr = g_array_index (dns, guint32, i);
if (!first) if (!first)
g_string_append_c (tmp, ' '); g_string_append_c (tmp, ' ');
g_string_append (tmp, nm_utils_inet4_ntop (addr, NULL)); g_string_append (tmp, nm_utils_inet4_ntop (addr, NULL));
first = FALSE; first = FALSE;
}
items = g_slist_prepend (items, tmp->str);
g_string_free (tmp, FALSE);
} }
items = g_slist_prepend (items, tmp->str);
g_string_free (tmp, FALSE);
} }
/* Search domains */ /* Search domains */
@@ -154,47 +155,49 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix)
/* WINS servers */ /* WINS servers */
val = g_hash_table_lookup (ip4_config, "wins-servers"); val = g_hash_table_lookup (ip4_config, "wins-servers");
if (val && G_VALUE_HOLDS (val, DBUS_TYPE_G_UINT_ARRAY)) if (val && G_VALUE_HOLDS (val, DBUS_TYPE_G_UINT_ARRAY)) {
wins = (GArray *) g_value_get_boxed (val); wins = (GArray *) g_value_get_boxed (val);
if (wins && wins->len) { if (wins && wins->len) {
gboolean first = TRUE; gboolean first = TRUE;
tmp = g_string_new (NULL); tmp = g_string_new (NULL);
g_string_append_printf (tmp, "%sIP4_WINS_SERVERS=", prefix); g_string_append_printf (tmp, "%sIP4_WINS_SERVERS=", prefix);
for (i = 0; i < wins->len; i++) { for (i = 0; i < wins->len; i++) {
guint32 addr; guint32 addr;
addr = g_array_index (wins, guint32, i); addr = g_array_index (wins, guint32, i);
if (!first) if (!first)
g_string_append_c (tmp, ' '); g_string_append_c (tmp, ' ');
g_string_append (tmp, nm_utils_inet4_ntop (addr, NULL)); g_string_append (tmp, nm_utils_inet4_ntop (addr, NULL));
first = FALSE; first = FALSE;
}
items = g_slist_prepend (items, tmp->str);
g_string_free (tmp, FALSE);
} }
items = g_slist_prepend (items, tmp->str);
g_string_free (tmp, FALSE);
} }
/* Static routes */ /* Static routes */
val = g_hash_table_lookup (ip4_config, "routes"); val = g_hash_table_lookup (ip4_config, "routes");
if (val) if (val) {
routes = nm_utils_ip4_routes_from_gvalue (val); routes = nm_utils_ip4_routes_from_gvalue (val);
for (iter = routes, num = 0; iter; iter = g_slist_next (iter)) { for (iter = routes, num = 0; iter; iter = g_slist_next (iter)) {
NMIP4Route *route = (NMIP4Route *) iter->data; NMIP4Route *route = (NMIP4Route *) iter->data;
guint32 ip_prefix = nm_ip4_route_get_prefix (route); guint32 ip_prefix = nm_ip4_route_get_prefix (route);
guint32 metric = nm_ip4_route_get_metric (route); guint32 metric = nm_ip4_route_get_metric (route);
char *routetmp; char *routetmp;
nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), str_addr); nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), str_addr);
nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), str_gw); nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), str_gw);
routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_gw, metric); routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_gw, metric);
items = g_slist_prepend (items, routetmp); items = g_slist_prepend (items, routetmp);
} }
items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, num)); items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, num));
if (routes)
g_slist_free_full (routes, (GDestroyNotify) nm_ip4_route_unref); g_slist_free_full (routes, (GDestroyNotify) nm_ip4_route_unref);
} else
items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=0", prefix));
return items; return items;
} }
@@ -223,7 +226,7 @@ construct_device_dhcp4_items (GSList *items, GHashTable *dhcp4_config)
static GSList * static GSList *
construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix) construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix)
{ {
GSList *addresses = NULL, *routes = NULL, *dns = NULL, *iter; GSList *addresses, *routes, *dns, *iter;
guint32 num; guint32 num;
GString *tmp; GString *tmp;
GValue *val; GValue *val;
@@ -238,47 +241,48 @@ construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix)
/* IP addresses */ /* IP addresses */
val = g_hash_table_lookup (ip6_config, "addresses"); val = g_hash_table_lookup (ip6_config, "addresses");
if (val) if (val) {
addresses = nm_utils_ip6_addresses_from_gvalue (val); addresses = nm_utils_ip6_addresses_from_gvalue (val);
for (iter = addresses, num = 0; iter; iter = g_slist_next (iter)) { for (iter = addresses, num = 0; iter; iter = g_slist_next (iter)) {
NMIP6Address *addr = (NMIP6Address *) iter->data; NMIP6Address *addr = (NMIP6Address *) iter->data;
guint32 ip_prefix = nm_ip6_address_get_prefix (addr); guint32 ip_prefix = nm_ip6_address_get_prefix (addr);
char *addrtmp; char *addrtmp;
nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), str_addr); nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), str_addr);
nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), str_gw); nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), str_gw);
addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, num++, str_addr, ip_prefix, str_gw); addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, num++, str_addr, ip_prefix, str_gw);
items = g_slist_prepend (items, addrtmp); items = g_slist_prepend (items, addrtmp);
} }
if (num) if (num)
items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ADDRESSES=%d", prefix, num)); items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ADDRESSES=%d", prefix, num));
if (addresses)
g_slist_free_full (addresses, (GDestroyNotify) nm_ip6_address_unref); g_slist_free_full (addresses, (GDestroyNotify) nm_ip6_address_unref);
}
/* DNS servers */ /* DNS servers */
val = g_hash_table_lookup (ip6_config, "nameservers"); val = g_hash_table_lookup (ip6_config, "nameservers");
if (val) if (val) {
dns = nm_utils_ip6_dns_from_gvalue (val); dns = nm_utils_ip6_dns_from_gvalue (val);
if (g_slist_length (dns)) { if (g_slist_length (dns)) {
gboolean first = TRUE; gboolean first = TRUE;
tmp = g_string_new (NULL); tmp = g_string_new (NULL);
g_string_append_printf (tmp, "%sIP6_NAMESERVERS=", prefix); g_string_append_printf (tmp, "%sIP6_NAMESERVERS=", prefix);
for (iter = dns; iter; iter = g_slist_next (iter)) { for (iter = dns; iter; iter = g_slist_next (iter)) {
const struct in6_addr *addr = iter->data; const struct in6_addr *addr = iter->data;
if (!first) if (!first)
g_string_append_c (tmp, ' '); g_string_append_c (tmp, ' ');
g_string_append (tmp, nm_utils_inet6_ntop (addr, NULL)); g_string_append (tmp, nm_utils_inet6_ntop (addr, NULL));
first = FALSE; first = FALSE;
}
items = g_slist_prepend (items, tmp->str);
g_string_free (tmp, FALSE);
} }
items = g_slist_prepend (items, tmp->str);
g_string_free (tmp, FALSE);
} }
/* Search domains */ /* Search domains */
@@ -286,25 +290,25 @@ construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix)
/* Static routes */ /* Static routes */
val = g_hash_table_lookup (ip6_config, "routes"); val = g_hash_table_lookup (ip6_config, "routes");
if (val) if (val) {
routes = nm_utils_ip6_routes_from_gvalue (val); routes = nm_utils_ip6_routes_from_gvalue (val);
for (iter = routes, num = 0; iter; iter = g_slist_next (iter)) { for (iter = routes, num = 0; iter; iter = g_slist_next (iter)) {
NMIP6Route *route = (NMIP6Route *) iter->data; NMIP6Route *route = (NMIP6Route *) iter->data;
guint32 ip_prefix = nm_ip6_route_get_prefix (route); guint32 ip_prefix = nm_ip6_route_get_prefix (route);
guint32 metric = nm_ip6_route_get_metric (route); guint32 metric = nm_ip6_route_get_metric (route);
char *routetmp; char *routetmp;
nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), str_addr); nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), str_addr);
nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), str_gw); nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), str_gw);
routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_gw, metric); routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_gw, metric);
items = g_slist_prepend (items, routetmp); items = g_slist_prepend (items, routetmp);
} }
if (num) if (num)
items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ROUTES=%d", prefix, num)); items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ROUTES=%d", prefix, num));
if (routes)
g_slist_free_full (routes, (GDestroyNotify) nm_ip6_route_unref); g_slist_free_full (routes, (GDestroyNotify) nm_ip6_route_unref);
}
return items; return items;
} }