diff options
Diffstat (limited to 'libnm-core/nm-setting-ip-config.c')
-rw-r--r-- | libnm-core/nm-setting-ip-config.c | 164 |
1 files changed, 87 insertions, 77 deletions
diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index bc81129177..a843ba99f7 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -11,6 +11,7 @@ #include <arpa/inet.h> #include <linux/fib_rules.h> +#include "nm-glib-aux/nm-str-buf.h" #include "nm-setting-ip4-config.h" #include "nm-setting-ip6-config.h" #include "nm-utils.h" @@ -745,7 +746,7 @@ nm_ip_route_unref (NMIPRoute *route) * nm_ip_route_equal_full: * @route: the #NMIPRoute * @other: the #NMIPRoute to compare @route to. - * @cmp_flags: tune how to compare attributes. Currently only + * @cmp_flags: tune how to compare attributes. Currently, only * NM_IP_ROUTE_EQUAL_CMP_FLAGS_NONE (0) and NM_IP_ROUTE_EQUAL_CMP_FLAGS_WITH_ATTRS (1) * is supported. * @@ -1363,9 +1364,12 @@ nm_ip_route_attribute_validate (const char *name, gboolean _nm_ip_route_attribute_validate_all (const NMIPRoute *route, GError **error) { - GHashTableIter iter; - const char *key; + NMUtilsNamedValue attrs_static[G_N_ELEMENTS (ip_route_attribute_spec)]; + gs_free NMUtilsNamedValue *attrs_free = NULL; + const NMUtilsNamedValue *attrs; + guint attrs_len; GVariant *val; + guint i; guint8 u8; g_return_val_if_fail (route, FALSE); @@ -1374,9 +1378,15 @@ _nm_ip_route_attribute_validate_all (const NMIPRoute *route, GError **error) if (!route->attributes) return TRUE; - g_hash_table_iter_init (&iter, route->attributes); - while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &val)) { - if (!nm_ip_route_attribute_validate (key, val, route->family, NULL, error)) + attrs = nm_utils_named_values_from_strdict (route->attributes, + &attrs_len, + attrs_static, + &attrs_free); + for (i = 0; i < attrs_len; i++) { + const char *key = attrs[i].name; + GVariant *val2 = attrs[i].value_ptr; + + if (!nm_ip_route_attribute_validate (key, val2, route->family, NULL, NULL)) return FALSE; } @@ -3021,7 +3031,7 @@ _rr_string_addr_family_from_flags (NMIPRoutingRuleAsStringFlags to_string_flags) * @to_string_flags: #NMIPRoutingRuleAsStringFlags for controlling the * string conversion. * @extra_args: (allow-none): extra arguments for controlling the string - * conversion. Currently not extra arguments are supported. + * conversion. Currently, not extra arguments are supported. * @error: (allow-none) (out): the error reason. * * Returns: (transfer full): the new #NMIPRoutingRule or %NULL on error. @@ -3081,7 +3091,7 @@ nm_ip_routing_rule_from_string (const char *str, * - iproute2 in may regards is flexible about the command lines. For example * - for tables it accepts table names from /etc/iproute2/rt_tables. We only * accept the special aliases "main", "local", and "default". - * - key names like "preference" can be abbreviated to "prefe", we don't do that. + * - key names like "preference" can be abbreviated to "pref", we don't do that. * - the "preference"/"priority" may be unspecified, in which kernel automatically * chooses an unused priority (during `ip rule add`). We don't allow for that, the * priority must be explicitly set. @@ -3117,7 +3127,7 @@ nm_ip_routing_rule_from_string (const char *str, } /* iproute2 matches keywords with any partial prefix. We don't allow - * for that flexiblity. */ + * for that flexibility. */ if (NM_IN_STRSET (word0, "from")) { if (!word1) @@ -3395,7 +3405,7 @@ next_words_consumed: } static void -_rr_string_append_inet_addr (GString *str, +_rr_string_append_inet_addr (NMStrBuf *str, gboolean is_from /* or else is-to */, gboolean required, int addr_family, @@ -3406,26 +3416,26 @@ _rr_string_append_inet_addr (GString *str, if (addr_len == 0) { if (required) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "%s %s/0", - is_from ? "from" : "to", - (addr_family == AF_INET) - ? "0.0.0.0" - : "::"); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (str, ' '), + "%s %s/0", + is_from ? "from" : "to", + (addr_family == AF_INET) + ? "0.0.0.0" + : "::"); } return; } - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "%s %s", - is_from ? "from" : "to", - nm_utils_inet_ntop (addr_family, - addr_bin, - addr_str)); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (str, ' '), + "%s %s", + is_from ? "from" : "to", + nm_utils_inet_ntop (addr_family, + addr_bin, + addr_str)); if (addr_len != nm_utils_addr_family_to_size (addr_family) * 8) { - g_string_append_printf (str, - "/%u", - addr_len); + nm_str_buf_append_printf (str, + "/%u", + addr_len); } } @@ -3435,7 +3445,7 @@ _rr_string_append_inet_addr (GString *str, * @to_string_flags: #NMIPRoutingRuleAsStringFlags for controlling the * string conversion. * @extra_args: (allow-none): extra arguments for controlling the string - * conversion. Currently not extra arguments are supported. + * conversion. Currently, not extra arguments are supported. * @error: (allow-none) (out): the error reason. * * Returns: (transfer full): the string representation or %NULL on error. @@ -3448,8 +3458,8 @@ nm_ip_routing_rule_to_string (const NMIPRoutingRule *self, GHashTable *extra_args, GError **error) { - nm_auto_free_gstring GString *str = NULL; int addr_family; + NMStrBuf str; g_return_val_if_fail (NM_IS_IP_ROUTING_RULE (self, TRUE), NULL); @@ -3485,18 +3495,18 @@ nm_ip_routing_rule_to_string (const NMIPRoutingRule *self, } } - str = g_string_sized_new (30); + nm_str_buf_init (&str, NM_UTILS_GET_NEXT_REALLOC_SIZE_32, FALSE); if (self->priority_has) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "priority %u", - (guint) self->priority); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "priority %u", + (guint) self->priority); } if (self->invert) - g_string_append (nm_gstring_add_space_delimiter (str), "not"); + nm_str_buf_append (nm_str_buf_append_required_delimiter (&str, ' '), "not"); - _rr_string_append_inet_addr (str, + _rr_string_append_inet_addr (&str, TRUE, ( !self->to_has || !self->to_valid), @@ -3506,7 +3516,7 @@ nm_ip_routing_rule_to_string (const NMIPRoutingRule *self, ? self->from_len : 0); - _rr_string_append_inet_addr (str, + _rr_string_append_inet_addr (&str, FALSE, FALSE, addr_family, @@ -3516,88 +3526,88 @@ nm_ip_routing_rule_to_string (const NMIPRoutingRule *self, : 0); if (self->tos != 0) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "tos 0x%02x", - (guint) self->tos); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "tos 0x%02x", + (guint) self->tos); } if (self->ipproto != 0) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "ipproto %u", - (guint) self->ipproto); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "ipproto %u", + (guint) self->ipproto); } if ( self->fwmark != 0 || self->fwmask != 0) { if (self->fwmark != 0) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "fwmark 0x%x", - self->fwmark); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "fwmark 0x%x", + self->fwmark); } else { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "fwmark 0"); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "fwmark 0"); } if (self->fwmask != 0xFFFFFFFFu) { if (self->fwmask != 0) - g_string_append_printf (str, "/0x%x", self->fwmask); + nm_str_buf_append_printf (&str, "/0x%x", self->fwmask); else - g_string_append_printf (str, "/0"); + nm_str_buf_append_printf (&str, "/0"); } } if ( self->sport_start != 0 || self->sport_end != 0) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "sport %u", - self->sport_start); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "sport %u", + self->sport_start); if (self->sport_start != self->sport_end) { - g_string_append_printf (str, - "-%u", - self->sport_end); + nm_str_buf_append_printf (&str, + "-%u", + self->sport_end); } } if ( self->dport_start != 0 || self->dport_end != 0) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "dport %u", - self->dport_start); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "dport %u", + self->dport_start); if (self->dport_start != self->dport_end) { - g_string_append_printf (str, - "-%u", - self->dport_end); + nm_str_buf_append_printf (&str, + "-%u", + self->dport_end); } } if (self->iifname) { - g_string_append (nm_gstring_add_space_delimiter (str), - "iif "); - nm_utils_escaped_tokens_escape_gstr (self->iifname, - NM_ASCII_SPACES, - str); + nm_str_buf_append (nm_str_buf_append_required_delimiter (&str, ' '), + "iif "); + nm_utils_escaped_tokens_escape_strbuf (self->iifname, + NM_ASCII_SPACES, + &str); } if (self->oifname) { - g_string_append (nm_gstring_add_space_delimiter (str), - "oif "); - nm_utils_escaped_tokens_escape_gstr (self->oifname, - NM_ASCII_SPACES, - str); + nm_str_buf_append (nm_str_buf_append_required_delimiter (&str, ' '), + "oif "); + nm_utils_escaped_tokens_escape_strbuf (self->oifname, + NM_ASCII_SPACES, + &str); } if (self->table != 0) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "table %u", - (guint) self->table); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "table %u", + (guint) self->table); } if (self->suppress_prefixlength != -1) { - g_string_append_printf (nm_gstring_add_space_delimiter (str), - "suppress_prefixlength %d", - (int) self->suppress_prefixlength); + nm_str_buf_append_printf (nm_str_buf_append_required_delimiter (&str, ' '), + "suppress_prefixlength %d", + (int) self->suppress_prefixlength); } - return g_string_free (g_steal_pointer (&str), FALSE); + return nm_str_buf_finalize (&str, NULL); } /*****************************************************************************/ @@ -5967,7 +5977,7 @@ nm_setting_ip_config_class_init (NMSettingIPConfigClass *klass) * * Flags for the DHCP hostname and FQDN. * - * Currently this property only includes flags to control the FQDN flags + * Currently, this property only includes flags to control the FQDN flags * set in the DHCP FQDN option. Supported FQDN flags are * %NM_DHCP_HOSTNAME_FLAG_FQDN_SERV_UPDATE, * %NM_DHCP_HOSTNAME_FLAG_FQDN_ENCODED and |