summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-09-10 13:58:43 +0200
committerThomas Haller <thaller@redhat.com>2020-09-11 16:18:34 +0200
commit2fd53eb509cd3d433f094ec0f5648398be9f9bb4 (patch)
treea22c8db88f84812e3015cdefe7a30d614e098873
parent348ab39f6d14ec42ad4f4c6b6f3038cf9d5bf68c (diff)
downloadNetworkManager-2fd53eb509cd3d433f094ec0f5648398be9f9bb4.tar.gz
core: add NMDhcpLease typedef and simple accessor functions
-rw-r--r--src/NetworkManagerUtils.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h
index 7d2b81dd49..5b3a9f9489 100644
--- a/src/NetworkManagerUtils.h
+++ b/src/NetworkManagerUtils.h
@@ -159,4 +159,65 @@ void nm_utils_ip_routes_to_dbus (int addr_family,
GVariant **out_route_data,
GVariant **out_routes);
+/*****************************************************************************/
+
+/* For now, all we track about a DHCP lease is the GHashTable with
+ * the options.
+ *
+ * We don't add a separate type for that, but we also don't want to use
+ * GHashTable directly (because most importantly leases should be immutable
+ * and passing a GHashTable pointer around neither makes it clear that
+ * this is a lease nor that it's immutable.
+ *
+ * Instead, add a simple opaque pointer and accessors that cast to a GHashTable.
+ *
+ * It has no overhead at run time, but gives some rudimentary type safety. */
+
+typedef struct _NMDhcpLease NMDhcpLease;
+
+static inline NMDhcpLease *
+nm_dhcp_lease_new_from_options (GHashTable *options_take)
+{
+ /* a NMDhcpLease is really just a GHashTable. But it's also supposed to be *immutable*.
+ *
+ * Hence, the API here takes over ownership of the reference to @options_take, that
+ * is to emphasize that we acquire ownership of the hash, and it should not be modified
+ * anymore. */
+ return (NMDhcpLease *) options_take;
+}
+
+static inline GHashTable *
+nm_dhcp_lease_get_options (NMDhcpLease *lease)
+{
+ return (GHashTable *) lease;
+}
+
+static inline void
+nm_dhcp_lease_ref (NMDhcpLease *lease)
+{
+ if (lease)
+ g_hash_table_ref ((GHashTable *) lease);
+}
+
+static inline void
+nm_dhcp_lease_unref (NMDhcpLease *lease)
+{
+ if (lease)
+ g_hash_table_unref ((GHashTable *) lease);
+}
+
+static inline const char *
+nm_dhcp_lease_lookup_option (NMDhcpLease *lease,
+ const char *option)
+{
+ nm_assert (option);
+
+ return nm_g_hash_table_lookup ((GHashTable *) lease, option);
+}
+
+NM_AUTO_DEFINE_FCN (NMDhcpLease *, _nm_auto_unref_dhcplease, nm_dhcp_lease_unref);
+#define nm_auto_unref_dhcplease nm_auto (_nm_auto_unref_dhcplease)
+
+/*****************************************************************************/
+
#endif /* __NETWORKMANAGER_UTILS_H__ */