[telepathy-gabble/telepathy-gabble-0.8] Use a hash of the machine ID as default resource

Will Thompson will.thompson at collabora.co.uk
Mon Nov 9 03:32:36 PST 2009


Technically the first 8 bytes may not be pseudo-random and so we might
collide if we just use those rather than (a function of) the full range.
---
 src/connection.c |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/src/connection.c b/src/connection.c
index da87522..ecb48be 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -321,21 +321,26 @@ gabble_connection_constructor (GType type,
 }
 
 static gchar *
-get_machine_id (void)
+dup_default_resource (void)
 {
   /* This is a once-per-process leak. */
-  static char *local_machine_id = NULL;
+  static gchar *default_resource = NULL;
 
-  if (G_UNLIKELY (local_machine_id == NULL))
+  if (G_UNLIKELY (default_resource == NULL))
     {
-      local_machine_id = dbus_get_local_machine_id ();
+      char *local_machine_id = dbus_get_local_machine_id ();
 
       if (local_machine_id == NULL)
         g_error ("Out of memory getting local machine ID");
+
+      default_resource = sha1_hex (local_machine_id, strlen (local_machine_id));
+      /* Let's keep the resource a maneagable length. */
+      default_resource[8] = '\0';
+
+      dbus_free (local_machine_id);
     }
 
-  /* 32 bytes of hex. The first 8 should be unique enough... */
-  return g_strndup (local_machine_id, 8);
+  return g_strdup (default_resource);
 }
 
 static void
@@ -351,7 +356,7 @@ gabble_connection_constructed (GObject *object)
 
   if (priv->resource == NULL)
     {
-      priv->resource = get_machine_id ();
+      priv->resource = dup_default_resource ();
       DEBUG ("defaulted resource to %s", priv->resource);
     }
 
-- 
1.5.6.5



More information about the telepathy-commits mailing list