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".
This commit is contained in:
Thomas Haller 2019-03-17 12:21:00 +01:00
parent d0f1e68b3e
commit 5079cd9942
3 changed files with 79 additions and 65 deletions

View file

@ -25,6 +25,7 @@
#include <stdlib.h>
#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);
}

View file

@ -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;
}

View file

@ -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__ */