diff options
| author | Michael Biebl <biebl@debian.org> | 2018-06-04 00:08:31 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2018-06-04 00:08:31 +0200 |
| commit | 0dd9df69fdbd475c48a0c8d5b0a1882550fe7321 (patch) | |
| tree | 249cf25643b1fe408e10679bb61613bc6540e894 /shared/nm-utils | |
| parent | 2e94a3b93171ab3fb95bf689aab1664d23988809 (diff) | |
| parent | 04bc9e1cd3544445d883ad29ea108c1645c8e7b7 (diff) | |
Update upstream source from tag 'upstream/1.11.4'
Update to upstream version '1.11.4' with Debian dir d0638aa2e32d5bae4e8daa021b9a66b7c4d6647e
Diffstat (limited to 'shared/nm-utils')
| -rw-r--r-- | shared/nm-utils/nm-enum-utils.c | 136 | ||||
| -rw-r--r-- | shared/nm-utils/nm-glib.h | 2 | ||||
| -rw-r--r-- | shared/nm-utils/nm-hash-utils.c | 16 | ||||
| -rw-r--r-- | shared/nm-utils/nm-hash-utils.h | 10 | ||||
| -rw-r--r-- | shared/nm-utils/nm-macros-internal.h | 11 | ||||
| -rw-r--r-- | shared/nm-utils/nm-random-utils.c | 2 | ||||
| -rw-r--r-- | shared/nm-utils/nm-shared-utils.c | 154 | ||||
| -rw-r--r-- | shared/nm-utils/nm-shared-utils.h | 37 | ||||
| -rw-r--r-- | shared/nm-utils/nm-test-utils.h | 24 | ||||
| -rw-r--r-- | shared/nm-utils/siphash24.c | 204 | ||||
| -rw-r--r-- | shared/nm-utils/siphash24.h | 23 | ||||
| -rw-r--r-- | shared/nm-utils/unaligned.h | 13 |
12 files changed, 306 insertions, 326 deletions
diff --git a/shared/nm-utils/nm-enum-utils.c b/shared/nm-utils/nm-enum-utils.c index b9bc6e88..023f8385 100644 --- a/shared/nm-utils/nm-enum-utils.c +++ b/shared/nm-utils/nm-enum-utils.c @@ -27,6 +27,62 @@ #define IS_FLAGS_SEPARATOR(ch) (NM_IN_SET ((ch), ' ', '\t', ',', '\n', '\r')) +static void +_ASSERT_enum_values_info (GType type, + const NMUtilsEnumValueInfo *value_infos) +{ +#if NM_MORE_ASSERTS > 5 + nm_auto_unref_gtypeclass GTypeClass *klass = NULL; + gs_unref_hashtable GHashTable *ht = NULL; + + klass = g_type_class_ref (type); + + g_assert (G_IS_ENUM_CLASS (klass) || G_IS_FLAGS_CLASS (klass)); + + if (!value_infos) + return; + + ht = g_hash_table_new (g_str_hash, g_str_equal); + + for (; value_infos->nick; value_infos++) { + + g_assert (value_infos->nick[0]); + + /* duplicate nicks make no sense!! */ + g_assert (!g_hash_table_contains (ht, value_infos->nick)); + g_hash_table_add (ht, (gpointer) value_infos->nick); + + if (G_IS_ENUM_CLASS (klass)) { + GEnumValue *enum_value; + + enum_value = g_enum_get_value_by_nick (G_ENUM_CLASS (klass), value_infos->nick); + if (enum_value) { + /* we do allow specifying the same name via @value_infos and @type. + * That might make sense, if @type comes from a library where older versions + * of the library don't yet support the value. In this case, the caller can + * provide the nick via @value_infos, to support the older library version. + * And then, when actually running against a newer library version where + * @type knows the nick, we have this situation. + * + * However, what never is allowed, is to use a name (nick) to re-number + * the value. That is, if both @value_infos and @type contain a particular + * nick, their numeric values must agree as well. + */ + g_assert (enum_value->value == value_infos->value); + } + } else { + GFlagsValue *flags_value; + + flags_value = g_flags_get_value_by_nick (G_FLAGS_CLASS (klass), value_infos->nick); + if (flags_value) { + /* see ENUM case above. */ + g_assert (flags_value->value == (guint) value_infos->value); + } + } + } +#endif +} + static gboolean _is_hex_string (const char *str) { @@ -67,16 +123,18 @@ _nm_utils_enum_to_str_full (GType type, const char *flags_separator, const NMUtilsEnumValueInfo *value_infos) { - nm_auto_unref_gtypeclass GTypeClass *class = NULL; + nm_auto_unref_gtypeclass GTypeClass *klass = NULL; + + _ASSERT_enum_values_info (type, value_infos); if ( flags_separator && ( !flags_separator[0] || NM_STRCHAR_ANY (flags_separator, ch, !IS_FLAGS_SEPARATOR (ch)))) g_return_val_if_reached (NULL); - class = g_type_class_ref (type); + klass = g_type_class_ref (type); - if (G_IS_ENUM_CLASS (class)) { + if (G_IS_ENUM_CLASS (klass)) { GEnumValue *enum_value; for ( ; value_infos && value_infos->nick; value_infos++) { @@ -84,13 +142,13 @@ _nm_utils_enum_to_str_full (GType type, return g_strdup (value_infos->nick); } - enum_value = g_enum_get_value (G_ENUM_CLASS (class), value); + enum_value = g_enum_get_value (G_ENUM_CLASS (klass), value); if ( !enum_value || !_enum_is_valid_enum_nick (enum_value->value_nick)) return g_strdup_printf ("%d", value); else return g_strdup (enum_value->value_nick); - } else if (G_IS_FLAGS_CLASS (class)) { + } else if (G_IS_FLAGS_CLASS (klass)) { GFlagsValue *flags_value; GString *str = g_string_new (""); unsigned uvalue = (unsigned) value; @@ -120,7 +178,7 @@ _nm_utils_enum_to_str_full (GType type, } do { - flags_value = g_flags_get_first_value (G_FLAGS_CLASS (class), uvalue); + flags_value = g_flags_get_first_value (G_FLAGS_CLASS (klass), uvalue); if (str->len) g_string_append (str, flags_separator); if ( !flags_value @@ -159,7 +217,7 @@ _nm_utils_enum_from_str_full (GType type, char **err_token, const NMUtilsEnumValueInfo *value_infos) { - GTypeClass *class; + GTypeClass *klass; gboolean ret = FALSE; int value = 0; gs_free char *str_clone = NULL; @@ -169,13 +227,15 @@ _nm_utils_enum_from_str_full (GType type, g_return_val_if_fail (str, FALSE); + _ASSERT_enum_values_info (type, value_infos); + str_clone = strdup (str); s = nm_str_skip_leading_spaces (str_clone); g_strchomp (s); - class = g_type_class_ref (type); + klass = g_type_class_ref (type); - if (G_IS_ENUM_CLASS (class)) { + if (G_IS_ENUM_CLASS (klass)) { GEnumValue *enum_value; if (s[0]) { @@ -191,21 +251,15 @@ _nm_utils_enum_from_str_full (GType type, value = (int) v64; ret = TRUE; } - } else { - enum_value = g_enum_get_value_by_nick (G_ENUM_CLASS (class), s); - if (enum_value) { - value = enum_value->value; - ret = TRUE; - } else { - nick = _find_value_info (value_infos, s); - if (nick) { - value = nick->value; - ret = TRUE; - } - } + } else if ((nick = _find_value_info (value_infos, s))) { + value = nick->value; + ret = TRUE; + } else if ((enum_value = g_enum_get_value_by_nick (G_ENUM_CLASS (klass), s))) { + value = enum_value->value; + ret = TRUE; } } - } else if (G_IS_FLAGS_CLASS (class)) { + } else if (G_IS_FLAGS_CLASS (klass)) { GFlagsValue *flags_value; unsigned uvalue = 0; @@ -236,19 +290,13 @@ _nm_utils_enum_from_str_full (GType type, break; } uvalue |= (unsigned) v64; - } else { - flags_value = g_flags_get_value_by_nick (G_FLAGS_CLASS (class), s); - if (flags_value) - uvalue |= flags_value->value; - else { - nick = _find_value_info (value_infos, s); - if (nick) - uvalue = (unsigned) nick->value; - else { - ret = FALSE; - break; - } - } + } else if ((nick = _find_value_info (value_infos, s))) + uvalue |= (unsigned) nick->value; + else if ((flags_value = g_flags_get_value_by_nick (G_FLAGS_CLASS (klass), s))) + uvalue |= flags_value->value; + else { + ret = FALSE; + break; } } @@ -261,23 +309,23 @@ _nm_utils_enum_from_str_full (GType type, NM_SET_OUT (err_token, !ret && s[0] ? g_strdup (s) : NULL); NM_SET_OUT (out_value, ret ? value : 0); - g_type_class_unref (class); + g_type_class_unref (klass); return ret; } const char ** _nm_utils_enum_get_values (GType type, gint from, gint to) { - GTypeClass *class; + GTypeClass *klass; GPtrArray *array; gint i; char sbuf[64]; - class = g_type_class_ref (type); + klass = g_type_class_ref (type); array = g_ptr_array_new (); - if (G_IS_ENUM_CLASS (class)) { - GEnumClass *enum_class = G_ENUM_CLASS (class); + if (G_IS_ENUM_CLASS (klass)) { + GEnumClass *enum_class = G_ENUM_CLASS (klass); GEnumValue *enum_value; for (i = 0; i < enum_class->n_values; i++) { @@ -289,8 +337,8 @@ _nm_utils_enum_get_values (GType type, gint from, gint to) g_ptr_array_add (array, (gpointer) g_intern_string (nm_sprintf_buf (sbuf, "%d", enum_value->value))); } } - } else if (G_IS_FLAGS_CLASS (class)) { - GFlagsClass *flags_class = G_FLAGS_CLASS (class); + } else if (G_IS_FLAGS_CLASS (klass)) { + GFlagsClass *flags_class = G_FLAGS_CLASS (klass); GFlagsValue *flags_value; for (i = 0; i < flags_class->n_values; i++) { @@ -303,12 +351,12 @@ _nm_utils_enum_get_values (GType type, gint from, gint to) } } } else { - g_type_class_unref (class); + g_type_class_unref (klass); g_ptr_array_free (array, TRUE); g_return_val_if_reached (NULL); } - g_type_class_unref (class); + g_type_class_unref (klass); g_ptr_array_add (array, NULL); return (const char **) g_ptr_array_free (array, FALSE); diff --git a/shared/nm-utils/nm-glib.h b/shared/nm-utils/nm-glib.h index f1498dc4..010f1820 100644 --- a/shared/nm-utils/nm-glib.h +++ b/shared/nm-utils/nm-glib.h @@ -20,7 +20,6 @@ #ifndef __NM_GLIB_H__ #define __NM_GLIB_H__ - #include <gio/gio.h> #include <string.h> @@ -86,7 +85,6 @@ g_steal_pointer (gpointer pp) (0 ? (*(pp)) : (g_steal_pointer) (pp)) #endif - static inline gboolean _nm_g_strv_contains (const gchar * const *strv, const gchar *str) diff --git a/shared/nm-utils/nm-hash-utils.c b/shared/nm-utils/nm-hash-utils.c index 8d8c21ce..4bc12b7c 100644 --- a/shared/nm-utils/nm-hash-utils.c +++ b/shared/nm-utils/nm-hash-utils.c @@ -28,8 +28,6 @@ #include "nm-shared-utils.h" #include "nm-random-utils.h" -#include "siphash24.c" - /*****************************************************************************/ #define HASH_KEY_SIZE 16u @@ -49,7 +47,7 @@ _get_hash_key_init (void) } g_arr _nm_alignas (guint64); static gsize g_lock; const guint8 *g; - struct siphash siph_state; + CSipHash siph_state; uint64_t h; guint *p; @@ -66,9 +64,9 @@ _get_hash_key_init (void) /* use siphash() of the key-size, to mangle the first guint. Otherwise, * the first guint has only the entropy that nm_utils_random_bytes() * generated for the first 4 bytes and relies on a good random generator. */ - siphash24_init (&siph_state, g_arr.v8); - siphash24_compress (g_arr.v8, sizeof (g_arr.v8), &siph_state); - h = siphash24_finalize (&siph_state); + c_siphash_init (&siph_state, g_arr.v8); + c_siphash_append (&siph_state, g_arr.v8, sizeof (g_arr.v8)); + h = c_siphash_finalize (&siph_state); p = (guint *) g_arr.v8; if (sizeof (guint) < sizeof (h)) *p = *p ^ ((guint) (h & 0xFFFFFFFFu)) ^ ((guint) (h >> 32)); @@ -97,10 +95,10 @@ guint nm_hash_static (guint static_seed) { /* note that we only xor the static_seed with the key. - * We don't use siphash24(), which would mix the bits better. + * We don't use siphash, which would mix the bits better. * Note that this doesn't matter, because static_seed is not * supposed to be a value that you are hashing (for that, use - * full siphash24()). + * full siphash). * Instead, different callers may set a different static_seed * so that nm_hash_str(NULL) != nm_hash_ptr(NULL). * @@ -121,7 +119,7 @@ nm_hash_init (NMHashState *state, guint static_seed) g = _get_hash_key (); memcpy (seed, g, HASH_KEY_SIZE); seed[0] ^= static_seed; - siphash24_init (&state->_state, (const guint8 *) seed); + c_siphash_init (&state->_state, (const guint8 *) seed); } guint diff --git a/shared/nm-utils/nm-hash-utils.h b/shared/nm-utils/nm-hash-utils.h index 3bd3f652..b7742e0f 100644 --- a/shared/nm-utils/nm-hash-utils.h +++ b/shared/nm-utils/nm-hash-utils.h @@ -22,11 +22,11 @@ #ifndef __NM_HASH_UTILS_H__ #define __NM_HASH_UTILS_H__ -#include "siphash24.h" +#include "c-siphash/src/c-siphash.h" #include "nm-macros-internal.h" struct _NMHashState { - struct siphash _state; + CSipHash _state; }; typedef struct _NMHashState NMHashState; @@ -42,7 +42,7 @@ nm_hash_complete (NMHashState *state) nm_assert (state); - h = siphash24_finalize (&state->_state); + h = c_siphash_finalize (&state->_state); /* we don't ever want to return a zero hash. * @@ -57,7 +57,7 @@ nm_hash_update (NMHashState *state, const void *ptr, gsize n) nm_assert (ptr); nm_assert (n > 0); - siphash24_compress (ptr, n, &state->_state); + c_siphash_append (&state->_state, ptr, n); } #define nm_hash_update_val(state, val) \ @@ -168,7 +168,7 @@ nm_hash_update_mem (NMHashState *state, const void *ptr, gsize n) * instead. */ nm_hash_update (state, &n, sizeof (n)); if (n > 0) - siphash24_compress (ptr, n, &state->_state); + c_siphash_append (&state->_state, ptr, n); } static inline void diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index cc7205a4..908b25fd 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -153,6 +153,14 @@ _nm_auto_protect_errno (int *p_saved_errno) } #define NM_AUTO_PROTECT_ERRNO(errsv_saved) nm_auto(_nm_auto_protect_errno) _nm_unused const int errsv_saved = (errno) +static inline void +_nm_auto_unref_gsource (GSource **ptr) +{ + if (*ptr) + g_source_unref (g_steal_pointer (ptr)); +} +#define nm_auto_unref_gsource nm_auto(_nm_auto_unref_gsource) + /*****************************************************************************/ /* http://stackoverflow.com/a/11172679 */ @@ -1291,7 +1299,6 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) #define false 0 #endif - #ifdef _G_BOOLEAN_EXPR /* g_assert() uses G_LIKELY(), which in turn uses _G_BOOLEAN_EXPR(). * As glib's implementation uses a local variable _g_boolean_var_, @@ -1368,4 +1375,6 @@ nm_close (int fd) return r; } +#define NM_PID_T_INVAL ((pid_t) -1) + #endif /* __NM_MACROS_INTERNAL_H__ */ diff --git a/shared/nm-utils/nm-random-utils.c b/shared/nm-utils/nm-random-utils.c index 65986b3d..3e968a8e 100644 --- a/shared/nm-utils/nm-random-utils.c +++ b/shared/nm-utils/nm-random-utils.c @@ -52,7 +52,7 @@ * value. * * Note that if calling getrandom() fails because there is not enough - * entroy (at early boot), the function will read /dev/urandom. + * entropy (at early boot), the function will read /dev/urandom. * Which of course, still has low entropy, and cause kernel to log * a warning. */ diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index 6937065c..d0019c11 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -549,6 +549,108 @@ nm_cmp_int2ptr_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_ /*****************************************************************************/ +const char * +nm_utils_dbus_path_get_last_component (const char *dbus_path) +{ + if (dbus_path) { + dbus_path = strrchr (dbus_path, '/'); + if (dbus_path) + return dbus_path + 1; + } + return NULL; +} + +static gint64 +_dbus_path_component_as_num (const char *p) +{ + gint64 n; + + /* no odd stuff. No leading zeros, only a non-negative, decimal integer. + * + * Otherwise, there would be multiple ways to encode the same number "10" + * and "010". That is just confusing. A number has no leading zeros, + * if it has, it's not a number (as far as we are concerned here). */ + if (p[0] == '0') { + if (p[1] != '\0') + return -1; + else + return 0; + } + if (!(p[0] >= '1' && p[0] <= '9')) + return -1; + if (!NM_STRCHAR_ALL (&p[1], ch, (ch >= '0' && ch <= '9'))) + return -1; + n = _nm_utils_ascii_str_to_int64 (p, 10, 0, G_MAXINT64, -1); + nm_assert (n == -1 || nm_streq0 (p, nm_sprintf_bufa (100, "%"G_GINT64_FORMAT, n))); + return n; +} + +int +nm_utils_dbus_path_cmp (const char *dbus_path_a, const char *dbus_path_b) +{ + const char *l_a, *l_b; + gsize plen; + gint64 n_a, n_b; + + /* compare function for two D-Bus paths. It behaves like + * strcmp(), except, if both paths have the same prefix, + * and both end in a (positive) number, then the paths + * will be sorted by number. */ + + NM_CMP_SELF (dbus_path_a, dbus_path_b); + + /* if one or both paths have no slash (and no last component) + * compare the full paths directly. */ + if ( !(l_a = nm_utils_dbus_path_get_last_component (dbus_path_a)) + || !(l_b = nm_utils_dbus_path_get_last_component (dbus_path_b))) + goto comp_full; + + /* check if both paths have the same prefix (up to the last-component). */ + plen = l_a - dbus_path_a; + if (plen != (l_b - dbus_path_b)) + goto comp_full; + NM_CMP_RETURN (strncmp (dbus_path_a, dbus_path_b, plen)); + + n_a = _dbus_path_component_as_num (l_a); + n_b = _dbus_path_component_as_num (l_b); + if (n_a == -1 && n_b == -1) + goto comp_l; + + /* both components must be convertiable to a number. If they are not, + * (and only one of them is), then we must always strictly sort numeric parts + * after non-numeric components. If we wouldn't, we wouldn't have + * a total order. + * + * An example of a not total ordering would be: + * "8" < "010" (numeric) + * "0x" < "8" (lexical) + * "0x" > "010" (lexical) + * We avoid this, by forcing that a non-numeric entry "0x" always sorts + * before numeric entries. + * + * Additionally, _dbus_path_component_as_num() would also reject "010" as + * not a valid number. + */ + if (n_a == -1) + return -1; + if (n_b == -1) + return 1; + + NM_CMP_DIRECT (n_a, n_b); + nm_assert (nm_streq (dbus_path_a, dbus_path_b)); + return 0; + +comp_full: + NM_CMP_DIRECT_STRCMP0 (dbus_path_a, dbus_path_b); + return 0; +comp_l: + NM_CMP_DIRECT_STRCMP0 (l_a, l_b); + nm_assert (nm_streq (dbus_path_a, dbus_path_b)); + return 0; +} + +/*****************************************************************************/ + /** * nm_utils_strsplit_set: * @str: the string to split. @@ -1269,8 +1371,7 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid) char filename[256]; gs_free gchar *contents = NULL; size_t length; - gs_strfreev gchar **tokens = NULL; - guint num_tokens; + gs_free const char **tokens = NULL; gchar *p; char state = ' '; gint64 ppid = 0; @@ -1290,7 +1391,7 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid) * processes trying to fool us */ p = strrchr (contents, ')'); - if (p == NULL) + if (!p) goto fail; p += 2; /* skip ') ' */ if (p - contents >= (int) length) @@ -1298,11 +1399,9 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid) state = p[0]; - tokens = g_strsplit (p, " ", 0); + tokens = nm_utils_strsplit_set (p, " "); - num_tokens = g_strv_length (tokens); - - if (num_tokens < 20) + if (NM_PTRARRAY_LEN (tokens) < 20) goto fail; if (out_ppid) { @@ -1359,3 +1458,44 @@ _nm_utils_strv_sort (const char **strv, gssize len) nm_strcmp_p_with_data, NULL); } + +/*****************************************************************************/ + +gpointer +_nm_utils_user_data_pack (int nargs, gconstpointer *args) +{ + int i; + gpointer *data; + + nm_assert (nargs > 0); + nm_assert (args); + + data = g_slice_alloc (((gsize) nargs) * sizeof (gconstpointer)); + for (i = 0; i < nargs; i++) + data[i] = (gpointer) args[i]; + return data; +} + +void +_nm_utils_user_data_unpack (gpointer user_data, int nargs, ...) +{ + gpointer *data = user_data; + va_list ap; + int i; + + nm_assert (data); + nm_assert (nargs > 0); + + va_start (ap, nargs); + for (i = 0; i < nargs; i++) { + gpointer *dst; + + dst = va_arg (ap, gpointer *); + nm_assert (dst); + + *dst = data[i]; + } + va_end (ap); + + g_slice_free1 (((gsize) nargs) * sizeof (gconstpointer), user_data); +} diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index 84325bb7..4b081630 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -113,6 +113,9 @@ nm_ip_addr_set (int addr_family, gpointer dst, const NMIPAddr *src) #define NM_CMP_DIRECT_MEMCMP(a, b, size) \ NM_CMP_RETURN (memcmp ((a), (b), (size))) +#define NM_CMP_DIRECT_STRCMP0(a, b) \ + NM_CMP_RETURN (g_strcmp0 ((a), (b))) + #define NM_CMP_DIRECT_IN6ADDR(a, b) \ G_STMT_START { \ const struct in6_addr *const _a = (a); \ @@ -191,6 +194,18 @@ void nm_utils_strbuf_append_str (char **buf, gsize *len, const char *str); const char *nm_strquote (char *buf, gsize buf_len, const char *str); +static inline gboolean +nm_utils_is_separator (const char c) +{ + return NM_IN_SET (c, ' ', '\t'); +} + +/*****************************************************************************/ + +const char *nm_utils_dbus_path_get_last_component (const char *dbus_path); + +int nm_utils_dbus_path_cmp (const char *dbus_path_a, const char *dbus_path_b); + /*****************************************************************************/ const char **nm_utils_strsplit_set (const char *str, const char *delimiters); @@ -440,6 +455,16 @@ nm_g_variant_unref_floating (GVariant *var) /*****************************************************************************/ +static inline int +nm_utf8_collate0 (const char *a, const char *b) +{ + if (!a) + return !b ? 0 : -1; + if (!b) + return 1; + return g_utf8_collate (a, b); +} + int nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data); int nm_cmp_uint32_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data); int nm_cmp_int2ptr_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data); @@ -611,4 +636,16 @@ guint64 nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ /*****************************************************************************/ +gpointer _nm_utils_user_data_pack (int nargs, gconstpointer *args); + +#define nm_utils_user_data_pack(...) \ + _nm_utils_user_data_pack(NM_NARG (__VA_ARGS__), (gconstpointer[]) { __VA_ARGS__ }) + +void _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...); + +#define nm_utils_user_data_unpack(user_data, ...) \ + _nm_utils_user_data_unpack(user_data, NM_NARG (__VA_ARGS__), __VA_ARGS__) + +/*****************************************************************************/ + #endif /* __NM_SHARED_UTILS_H__ */ diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index cc33a1ae..efbe6c9a 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -211,7 +211,6 @@ _nmtst_exit (void) \ nmtst_free (); \ } - static inline gboolean nmtst_initialized (void) { @@ -276,7 +275,6 @@ BREAK_INNER_LOOPS: return (char **) g_array_free (result, FALSE); } - /* free instances allocated by nmtst (especially nmtst_init()) on shutdown * to release memory. After nmtst_free(), the test is uninitialized again. */ static inline void @@ -923,33 +921,26 @@ _nmtst_main_loop_run_timeout (gpointer user_data) { GMainLoop **p_loop = user_data; - g_assert (p_loop); - g_assert (*p_loop); - - g_main_loop_quit (*p_loop); - *p_loop = NULL; - + g_assert (p_loop && *p_loop); + g_main_loop_quit (g_steal_pointer (p_loop)); return G_SOURCE_REMOVE; } static inline gboolean nmtst_main_loop_run (GMainLoop *loop, guint timeout_ms) { - GSource *source = NULL; - guint id = 0; + nm_auto_unref_gsource GSource *source = NULL; GMainLoop *loopx = loop; if (timeout_ms > 0) { source = g_timeout_source_new (timeout_ms); g_source_set_callback (source, _nmtst_main_loop_run_timeout, &loopx, NULL); - id = g_source_attach (source, g_main_loop_get_context (loop)); - g_assert (id); - g_source_unref (source); + g_source_attach (source, g_main_loop_get_context (loop)); } g_main_loop_run (loop); - if (source && loopx) + if (source) g_source_destroy (source); /* if the timeout was reached, return FALSE. */ @@ -1140,7 +1131,7 @@ _nmtst_assert_ip4_address (const char *file, int line, in_addr_t addr, const cha char buf[100]; g_error ("%s:%d: Unexpected IPv4 address: expected %s, got %s", - file, line, str_expected ? str_expected : "0.0.0.0", + file, line, str_expected ?: "0.0.0.0", inet_ntop (AF_INET, &addr, buf, sizeof (buf))); } } @@ -1158,7 +1149,7 @@ _nmtst_assert_ip6_address (const char *file, int line, const struct in6_addr *ad char buf[100]; g_error ("%s:%d: Unexpected IPv6 address: expected %s, got %s", - file, line, str_expected ? str_expected : "::", + file, line, str_expected ?: "::", inet_ntop (AF_INET6, addr, buf, sizeof (buf))); } } @@ -1831,7 +1822,6 @@ nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const cha nmtst_assert_hwaddr_equals (hwaddr1, hwaddr1_len, expected, __FILE__, __LINE__) #endif - #if defined(__NM_SIMPLE_CONNECTION_H__) && defined(__NM_SETTING_CONNECTION_H__) && defined(__NM_KEYFILE_INTERNAL_H__) static inline NMConnection * diff --git a/shared/nm-utils/siphash24.c b/shared/nm-utils/siphash24.c deleted file mode 100644 index 8e59afb2..00000000 --- a/shared/nm-utils/siphash24.c +++ /dev/null @@ -1,204 +0,0 @@ -/* - SipHash reference C implementation - - Written in 2012 by - Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com> - Daniel J. Bernstein <djb@cr.yp.to> - - To the extent possible under law, the author(s) have dedicated all copyright - and related and neighboring rights to this software to the public domain - worldwide. This software is distributed without any warranty. - - You should have received a copy of the CC0 Public Domain Dedication along with - this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. - - (Minimal changes made by Lennart Poettering, to make clean for inclusion in systemd) - (Refactored by Tom Gundersen to split up in several functions and follow systemd - coding style) -*/ - -#include "nm-default.h" - -#define assert(cond) nm_assert (cond) -#define _fallthrough_ _nm_fallthrough - -#include <stdio.h> - -#include "siphash24.h" -#include "unaligned.h" - -static inline uint64_t rotate_left(uint64_t x, uint8_t b) { - assert(b < 64); - - return (x << b) | (x >> (64 - b)); -} - -static inline void sipround(struct siphash *state) { - assert(state); - - state->v0 += state->v1; - state->v1 = rotate_left(state->v1, 13); - state->v1 ^= state->v0; - state->v0 = rotate_left(state->v0, 32); - state->v2 += state->v3; - state->v3 = rotate_left(state->v3, 16); - state->v3 ^= state->v2; - state->v0 += state->v3; - state->v3 = rotate_left(state->v3, 21); - state->v3 ^= state->v0; - state->v2 += state->v1; - state->v1 = rotate_left(state->v1, 17); - state->v1 ^= state->v2; - state->v2 = rotate_left(state->v2, 32); -} - -void siphash24_init(struct siphash *state, const uint8_t k[16]) { - uint64_t k0, k1; - - assert(state); - assert(k); - - k0 = unaligned_read_le64(k); - k1 = unaligned_read_le64(k + 8); - - *state = (struct siphash) { - /* "somepseudorandomlygeneratedbytes" */ - .v0 = 0x736f6d6570736575ULL ^ k0, - .v1 = 0x646f72616e646f6dULL ^ k1, - .v2 = 0x6c7967656e657261ULL ^ k0, - .v3 = 0x7465646279746573ULL ^ k1, - .padding = 0, - .inlen = 0, - }; -} - -void siphash24_compress(const void *_in, size_t inlen, struct siphash *state) { - - const uint8_t *in = _in; - const uint8_t *end = in + inlen; - size_t left = state->inlen & 7; - uint64_t m; - - assert(in); - assert(state); - - /* Update total length */ - state->inlen += inlen; - - /* If padding exists, fill it out */ - if (left > 0) { - for ( ; in < end && left < 8; in ++, left ++) - state->padding |= ((uint64_t) *in) << (left * 8); - - if (in == end && left < 8) - /* We did not have enough input to fill out the padding completely */ - return; - -#ifdef DEBUG - printf("(%3zu) v0 %08x %08x\n", state->inlen, (uint32_t) (state->v0 >> 32), (uint32_t) state->v0); - printf("(%3zu) v1 %08x %08x\n", state->inlen, (uint32_t) (state->v1 >> 32), (uint32_t) state->v1); - printf("(%3zu) v2 %08x %08x\n", state->inlen, (uint32_t) (state->v2 >> 32), (uint32_t) state->v2); - printf("(%3zu) v3 %08x %08x\n", state->inlen, (uint32_t) (state->v3 >> 32), (uint32_t) state->v3); - printf("(%3zu) compress padding %08x %08x\n", state->inlen, (uint32_t) (state->padding >> 32), (uint32_t)state->padding); -#endif - - state->v3 ^= state->padding; - sipround(state); - sipround(state); - state->v0 ^= state->padding; - - state->padding = 0; - } - - end -= (state->inlen % sizeof(uint64_t)); - - for ( ; in < end; in += 8) { - m = unaligned_read_le64(in); -#ifdef DEBUG - printf("(%3zu) v0 %08x %08x\n", state->inlen, (uint32_t) (state->v0 >> 32), (uint32_t) state->v0); - printf("(%3zu) v1 %08x %08x\n", state->inlen, (uint32_t) (state->v1 >> 32), (uint32_t) state->v1); - printf("(%3zu) v2 %08x %08x\n", state->inlen, (uint32_t) (state->v2 >> 32), (uint32_t) state->v2); - printf("(%3zu) v3 %08x %08x\n", state->inlen, (uint32_t) (state->v3 >> 32), (uint32_t) state->v3); - printf("(%3zu) compress %08x %08x\n", state->inlen, (uint32_t) (m >> 32), (uint32_t) m); -#endif - state->v3 ^= m; - sipround(state); - sipround(state); - state->v0 ^= m; - } - - left = state->inlen & 7; - switch (left) { - case 7: - state->padding |= ((uint64_t) in[6]) << 48; - _fallthrough_; - case 6: - state->padding |= ((uint64_t) in[5]) << 40; - _fallthrough_; - case 5: - state->padding |= ((uint64_t) in[4]) << 32; - _fallthrough_; - case 4: - state->padding |= ((uint64_t) in[3]) << 24; - _fallthrough_; - case 3: - state->padding |= ((uint64_t) in[2]) << 16; - _fallthrough_; - case 2: - state->padding |= ((uint64_t) in[1]) << 8; - _fallthrough_; - case 1: - state->padding |= ((uint64_t) in[0]); - _fallthrough_; - case 0: - break; - } -} - -uint64_t siphash24_finalize(struct siphash *state) { - uint64_t b; - - assert(state); - - b = state->padding | (((uint64_t) state->inlen) << 56); - -#ifdef DEBUG - printf("(%3zu) v0 %08x %08x\n", state->inlen, (uint32_t) (state->v0 >> 32), (uint32_t) state->v0); - printf("(%3zu) v1 %08x %08x\n", state->inlen, (uint32_t) (state->v1 >> 32), (uint32_t) state->v1); - printf("(%3zu) v2 %08x %08x\n", state->inlen, (uint32_t) (state->v2 >> 32), (uint32_t) state->v2); - printf("(%3zu) v3 %08x %08x\n", state->inlen, (uint32_t) (state->v3 >> 32), (uint32_t) state->v3); - printf("(%3zu) padding %08x %08x\n", state->inlen, (uint32_t) (state->padding >> 32), (uint32_t) state->padding); -#endif - - state->v3 ^= b; - sipround(state); - sipround(state); - state->v0 ^= b; - -#ifdef DEBUG - printf("(%3zu) v0 %08x %08x\n", state->inlen, (uint32_t) (state->v0 >> 32), (uint32_t) state->v0); - printf("(%3zu) v1 %08x %08x\n", state->inlen, (uint32_t) (state->v1 >> 32), (uint32_t) state->v1); - printf("(%3zu) v2 %08x %08x\n", state->inlen, (uint32_t) (state->v2 >> 32), (uint32_t) state->v2); - printf("(%3zu) v3 %08x %08x\n", state->inlen, (uint32_t) (state->v3 >> 32), (uint32_t) state->v3); -#endif - state->v2 ^= 0xff; - - sipround(state); - sipround(state); - sipround(state); - sipround(state); - - return state->v0 ^ state->v1 ^ state->v2 ^ state->v3; -} - -uint64_t siphash24(const void *in, size_t inlen, const uint8_t k[16]) { - struct siphash state; - - assert(in); - assert(k); - - siphash24_init(&state, k); - siphash24_compress(in, inlen, &state); - - return siphash24_finalize(&state); -} diff --git a/shared/nm-utils/siphash24.h b/shared/nm-utils/siphash24.h deleted file mode 100644 index 54e2420c..00000000 --- a/shared/nm-utils/siphash24.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include <inttypes.h> -#include <stddef.h> -#include <stdint.h> -#include <sys/types.h> - -struct siphash { - uint64_t v0; - uint64_t v1; - uint64_t v2; - uint64_t v3; - uint64_t padding; - size_t inlen; -}; - -void siphash24_init(struct siphash *state, const uint8_t k[16]); -void siphash24_compress(const void *in, size_t inlen, struct siphash *state); -#define siphash24_compress_byte(byte, state) siphash24_compress((const uint8_t[]) { (byte) }, 1, (state)) - -uint64_t siphash24_finalize(struct siphash *state); - -uint64_t siphash24(const void *in, size_t inlen, const uint8_t k[16]); diff --git a/shared/nm-utils/unaligned.h b/shared/nm-utils/unaligned.h index 73302b42..feddaa91 100644 --- a/shared/nm-utils/unaligned.h +++ b/shared/nm-utils/unaligned.h @@ -5,19 +5,6 @@ This file is part of systemd. Copyright 2014 Tom Gundersen - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ #include <endian.h> |