From 5079cd99423a0be5e5f0cfe4a7b59437f7ebf368 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 17 Mar 2019 12:21:00 +0100 Subject: [PATCH] libnm: move parsing VLAN priority mapping to "shared/nm-libnm-core-utils.h" The same code is used by nmcli. Obviously, clients also need to parse string representations. That begs the question whether this should be public API of libnm. Maybe, but don't decide that now, just reuse the code internally via "shared/nm-libnm-core-utils.h". --- libnm-core/nm-setting-vlan.c | 67 ++---------------------------------- shared/nm-libnm-core-utils.c | 54 +++++++++++++++++++++++++++++ shared/nm-libnm-core-utils.h | 23 +++++++++++++ 3 files changed, 79 insertions(+), 65 deletions(-) diff --git a/libnm-core/nm-setting-vlan.c b/libnm-core/nm-setting-vlan.c index 88843e7706..5e34834952 100644 --- a/libnm-core/nm-setting-vlan.c +++ b/libnm-core/nm-setting-vlan.c @@ -25,6 +25,7 @@ #include +#include "nm-libnm-core-utils.h" #include "nm-utils.h" #include "nm-core-types-internal.h" #include "nm-setting-connection.h" @@ -106,70 +107,6 @@ nm_setting_vlan_get_flags (NMSettingVlan *setting) return NM_SETTING_VLAN_GET_PRIVATE (setting)->flags; } -static guint32 -get_max_prio (NMVlanPriorityMap map, gboolean from) -{ - if (map == NM_VLAN_INGRESS_MAP) - return from ? MAX_8021P_PRIO : MAX_SKB_PRIO; - else if (map == NM_VLAN_EGRESS_MAP) - return from ? MAX_SKB_PRIO : MAX_8021P_PRIO; - g_assert_not_reached (); -} - -static gboolean -priority_map_parse_str (NMVlanPriorityMap map_type, - const char *str, - gboolean allow_wildcard_to, - guint32 *out_from, - guint32 *out_to, - gboolean *out_has_wildcard_to) -{ - const char *s2; - gint64 v1, v2; - - nm_assert (str); - - s2 = strchr (str, ':'); - - if (!s2) { - if (!allow_wildcard_to) - return FALSE; - v1 = _nm_utils_ascii_str_to_int64 (str, 10, 0, G_MAXUINT32, -1); - v2 = -1; - } else { - gs_free char *s1_free = NULL; - gsize s1_len = (s2 - str); - - s2 = nm_str_skip_leading_spaces (&s2[1]); - if ( s2[0] == '\0' - || ( s2[0] == '*' - && NM_STRCHAR_ALL (&s2[1], ch, g_ascii_isspace (ch)))) { - if (!allow_wildcard_to) - return FALSE; - v2 = -1; - } else { - v2 = _nm_utils_ascii_str_to_int64 (s2, 10, 0, G_MAXUINT32, -1); - if ( v2 < 0 - || v2 > get_max_prio (map_type, FALSE)) - return FALSE; - } - - v1 = _nm_utils_ascii_str_to_int64 (nm_strndup_a (100, str, s1_len, &s1_free), - 10, 0, G_MAXUINT32, -1); - } - - if ( v1 < 0 - || v1 > get_max_prio (map_type, TRUE)) - return FALSE; - - NM_SET_OUT (out_from, v1); - NM_SET_OUT (out_to, v2 < 0 - ? 0u - : (guint) v2); - NM_SET_OUT (out_has_wildcard_to, v2 < 0); - return TRUE; -} - static NMVlanQosMapping * priority_map_new (guint32 from, guint32 to) { @@ -188,7 +125,7 @@ priority_map_new_from_str (NMVlanPriorityMap map, const char *str) { guint32 from, to; - if (!priority_map_parse_str (map, str, FALSE, &from, &to, NULL)) + if (!nm_utils_vlan_priority_map_parse_str (map, str, FALSE, &from, &to, NULL)) return NULL; return priority_map_new (from, to); } diff --git a/shared/nm-libnm-core-utils.c b/shared/nm-libnm-core-utils.c index 244b318d72..d1e5f75460 100644 --- a/shared/nm-libnm-core-utils.c +++ b/shared/nm-libnm-core-utils.c @@ -20,3 +20,57 @@ #include "nm-libnm-core-utils.h" /*****************************************************************************/ + +gboolean +nm_utils_vlan_priority_map_parse_str (NMVlanPriorityMap map_type, + const char *str, + gboolean allow_wildcard_to, + guint32 *out_from, + guint32 *out_to, + gboolean *out_has_wildcard_to) +{ + const char *s2; + gint64 v1, v2; + + nm_assert (str); + + s2 = strchr (str, ':'); + + if (!s2) { + if (!allow_wildcard_to) + return FALSE; + v1 = _nm_utils_ascii_str_to_int64 (str, 10, 0, G_MAXUINT32, -1); + v2 = -1; + } else { + gs_free char *s1_free = NULL; + gsize s1_len = (s2 - str); + + s2 = nm_str_skip_leading_spaces (&s2[1]); + if ( s2[0] == '\0' + || ( s2[0] == '*' + && NM_STRCHAR_ALL (&s2[1], ch, g_ascii_isspace (ch)))) { + if (!allow_wildcard_to) + return FALSE; + v2 = -1; + } else { + v2 = _nm_utils_ascii_str_to_int64 (s2, 10, 0, G_MAXUINT32, -1); + if ( v2 < 0 + || (guint32) v2 > nm_utils_vlan_priority_map_get_max_prio (map_type, FALSE)) + return FALSE; + } + + v1 = _nm_utils_ascii_str_to_int64 (nm_strndup_a (100, str, s1_len, &s1_free), + 10, 0, G_MAXUINT32, -1); + } + + if ( v1 < 0 + || (guint32) v1 > nm_utils_vlan_priority_map_get_max_prio (map_type, TRUE)) + return FALSE; + + NM_SET_OUT (out_from, v1); + NM_SET_OUT (out_to, v2 < 0 + ? 0u + : (guint) v2); + NM_SET_OUT (out_has_wildcard_to, v2 < 0); + return TRUE; +} diff --git a/shared/nm-libnm-core-utils.h b/shared/nm-libnm-core-utils.h index fcdd425a54..5a846659e6 100644 --- a/shared/nm-libnm-core-utils.h +++ b/shared/nm-libnm-core-utils.h @@ -20,4 +20,27 @@ /****************************************************************************/ +#include "nm-setting-vlan.h" + +static inline guint32 +nm_utils_vlan_priority_map_get_max_prio (NMVlanPriorityMap map, gboolean from) +{ + if (map == NM_VLAN_INGRESS_MAP) { + return from + ? 7u /* MAX_8021P_PRIO */ + : (guint32) G_MAXUINT32 /* MAX_SKB_PRIO */; + } + nm_assert (map == NM_VLAN_EGRESS_MAP); + return from + ? (guint32) G_MAXUINT32 /* MAX_SKB_PRIO */ + : 7u /* MAX_8021P_PRIO */; +} + +gboolean nm_utils_vlan_priority_map_parse_str (NMVlanPriorityMap map_type, + const char *str, + gboolean allow_wildcard_to, + guint32 *out_from, + guint32 *out_to, + gboolean *out_has_wildcard_to); + #endif /* __NM_LIBNM_SHARED_UTILS_H__ */