summaryrefslogtreecommitdiff
path: root/libnm-core
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-10-30 13:07:25 +0100
committerThomas Haller <thaller@redhat.com>2018-10-31 09:41:12 +0100
commit88b081fce4d1dd1093dd3413ed7462f6d4fc7962 (patch)
tree98650e7fb382448a430a8cd5c1d3dd9383eb0cfb /libnm-core
parent3648c58bc5f0e5195ad946b8a3e5efe65c89e4c0 (diff)
libnm: expose UUID utils as internal API
We link against libuuid.so, but it was entirely internal to libnm-core. We only exposed UUIDs in string form. Add API to also handle UUIDs in binary form. Note that libuuid already defines a type "uuid_t". However, don't use it and instead use our own typedef NMUuid. Reasons: - uuid.h should be internal to libnm-core (nm-utils.c specifically), and not be used by or exposed it other parts of the code. - uuid_t is a typedef for a guchar[16] array. Typedefs for arrays are confusing, because depending on whether it's an automatic variable or a pointer in a function argument, they behave differently regarding whether to take their address or not and usage of "sizeof()".
Diffstat (limited to 'libnm-core')
-rw-r--r--libnm-core/nm-core-internal.h12
-rw-r--r--libnm-core/nm-utils.c67
2 files changed, 62 insertions, 17 deletions
diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index 22c85cd76..41fb1ed9f 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -283,6 +283,18 @@ gboolean _nm_utils_check_module_file (const char *name,
gpointer user_data,
GError **error);
+/*****************************************************************************/
+
+typedef struct _NMUuid {
+ guchar uuid[16];
+} NMUuid;
+
+NMUuid *_nm_utils_uuid_parse (const char *str,
+ NMUuid *uuid);
+char *_nm_utils_uuid_unparse (const NMUuid *uuid,
+ char *out_str /*[37]*/);
+NMUuid *_nm_utils_uuid_generate_random (NMUuid *out_uuid);
+
#define NM_UTILS_UUID_TYPE_LEGACY 0
#define NM_UTILS_UUID_TYPE_VARIANT3 1
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index eca16a138..23322b104 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -2785,6 +2785,42 @@ _nm_utils_sriov_vf_from_strparts (const char *index, const char *detail, GError
/*****************************************************************************/
+NMUuid *
+_nm_utils_uuid_parse (const char *str,
+ NMUuid *out_uuid)
+{
+ nm_assert (str);
+ nm_assert (out_uuid);
+
+ if (uuid_parse (str, out_uuid->uuid) != 0)
+ return NULL;
+ return out_uuid;
+}
+
+char *
+_nm_utils_uuid_unparse (const NMUuid *uuid,
+ char *out_str /*[37]*/)
+{
+ nm_assert (uuid);
+
+ if (!out_str) {
+ /* for convenience, allow %NULL to indicate that a new
+ * string should be allocated. */
+ out_str = g_malloc (37);
+ }
+ uuid_unparse_lower (uuid->uuid, out_str);
+ return out_str;
+}
+
+NMUuid *
+_nm_utils_uuid_generate_random (NMUuid *out_uuid)
+{
+ nm_assert (out_uuid);
+
+ uuid_generate_random (out_uuid->uuid);
+ return out_uuid;
+}
+
/**
* nm_utils_uuid_generate_buf_:
* @buf: input buffer, must contain at least 37 bytes
@@ -2794,11 +2830,12 @@ _nm_utils_sriov_vf_from_strparts (const char *index, const char *detail, GError
char *
nm_utils_uuid_generate_buf_ (char *buf)
{
- uuid_t uuid;
+ NMUuid uuid;
- uuid_generate_random (uuid);
- uuid_unparse_lower (uuid, buf);
- return buf;
+ nm_assert (buf);
+
+ _nm_utils_uuid_generate_random (&uuid);
+ return _nm_utils_uuid_unparse (&uuid, buf);
}
/**
@@ -2830,8 +2867,7 @@ nm_utils_uuid_generate (void)
char *
nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, gpointer type_args)
{
- uuid_t uuid;
- char *buf;
+ NMUuid uuid;
g_return_val_if_fail (slen == 0 || s, FALSE);
@@ -2847,37 +2883,34 @@ nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, g
0,
(guint8 *) s,
slen,
- (guint8 *) uuid,
+ (guint8 *) &uuid,
sizeof (uuid));
break;
case NM_UTILS_UUID_TYPE_VARIANT3: {
- uuid_t ns_uuid = { 0 };
+ NMUuid ns_uuid = { 0 };
if (type_args) {
/* type_args can be a name space UUID. Interpret it as (char *) */
- if (uuid_parse ((char *) type_args, ns_uuid) != 0)
+ if (!_nm_utils_uuid_parse (type_args, &ns_uuid))
g_return_val_if_reached (NULL);
}
nm_crypto_md5_hash ((guint8 *) s,
slen,
- (guint8 *) ns_uuid,
+ (guint8 *) &ns_uuid,
sizeof (ns_uuid),
- (guint8 *) uuid,
+ (guint8 *) &uuid,
sizeof (uuid));
- uuid[6] = (uuid[6] & 0x0F) | 0x30;
- uuid[8] = (uuid[8] & 0x3F) | 0x80;
+ uuid.uuid[6] = (uuid.uuid[6] & 0x0F) | 0x30;
+ uuid.uuid[8] = (uuid.uuid[8] & 0x3F) | 0x80;
break;
}
default:
g_return_val_if_reached (NULL);
}
- buf = g_malloc (37);
- uuid_unparse_lower (uuid, &buf[0]);
-
- return buf;
+ return _nm_utils_uuid_unparse (&uuid, NULL);
}
/**