[gssdp] Add user-agent cache to GSSDP client



commit 32ad4a5fdf8928c6487d9925755f8da592678632
Author: Jens Georg <mail jensge org>
Date:   Fri Dec 30 18:38:28 2011 +0100

    Add user-agent cache to GSSDP client
    
    https://bugzilla.gnome.org/show_bug.cgi?id=653894

 libgssdp/gssdp-client.c           |   76 +++++++++++++++++++++++++++++++++++++
 libgssdp/gssdp-client.h           |    9 ++++
 libgssdp/gssdp-socket-functions.h |    1 +
 3 files changed, 86 insertions(+), 0 deletions(-)
---
diff --git a/libgssdp/gssdp-client.c b/libgssdp/gssdp-client.c
index e5d0c01..4b8c043 100644
--- a/libgssdp/gssdp-client.c
+++ b/libgssdp/gssdp-client.c
@@ -76,6 +76,7 @@ typedef unsigned long in_addr_t;
 #include "gssdp-socket-source.h"
 #include "gssdp-marshal.h"
 #include "gssdp-protocol.h"
+#include "gssdp-socket-functions.h"
 #ifdef HAVE_PKTINFO
 #include "gssdp-pktinfo-message.h"
 #endif
@@ -125,6 +126,7 @@ typedef struct _GSSDPHeaderField GSSDPHeaderField;
 struct _GSSDPClientPrivate {
         char              *server_id;
 
+        GHashTable        *user_agent_cache;
         guint              socket_ttl;
         guint              msearch_port;
         GSSDPNetworkDevice device;
@@ -181,6 +183,10 @@ gssdp_client_initable_init    (GInitable     *initable,
                                GCancellable  *cancellable,
                                GError       **error);
 
+char *
+arp_lookup                    (GSSDPClient   *client,
+                               const char    *ip_address);
+
 static void
 gssdp_client_init (GSSDPClient *client)
 {
@@ -317,6 +323,11 @@ gssdp_client_initable_init (GInitable                   *initable,
 
         client->priv->initialized = TRUE;
 
+        client->priv->user_agent_cache = g_hash_table_new_full (g_str_hash,
+                                                                g_str_equal,
+                                                                g_free,
+                                                                g_free);
+
         return TRUE;
 }
 
@@ -456,6 +467,9 @@ gssdp_client_finalize (GObject *object)
         g_free (client->priv->device.host_ip);
         g_free (client->priv->device.network);
 
+        if (client->priv->user_agent_cache)
+                g_hash_table_unref (client->priv->user_agent_cache);
+
         G_OBJECT_CLASS (gssdp_client_parent_class)->finalize (object);
 }
 
@@ -817,6 +831,63 @@ gssdp_client_set_network (GSSDPClient *client,
 }
 
 /**
+ * gssdp_client_add_cache_entry:
+ * @client: A #GSSDPClient
+ * @ip_address: The host to add to the cache
+ * @user_agent: User agent ot the host to add
+ **/
+void
+gssdp_client_add_cache_entry (GSSDPClient  *client,
+                               const char   *ip_address,
+                               const char   *user_agent)
+{
+        char *hwaddr;
+
+        g_return_if_fail (client != NULL);
+        g_return_if_fail (ip_address != NULL);
+        g_return_if_fail (user_agent != NULL);
+
+        hwaddr = arp_lookup (client, ip_address);
+
+        if (hwaddr)
+                g_hash_table_insert (client->priv->user_agent_cache,
+                                     hwaddr,
+                                     g_strdup (user_agent));
+}
+
+/**
+ * gssdp_client_guess_user_agent:
+ * @client: A #GSSDPClient
+ * @ip_address: IP address to guess the user-agent for
+ *
+ * Returns: (transfer none): The user-agent cached for this IP, %NULL if none
+ * is cached.
+ **/
+const char *
+gssdp_client_guess_user_agent (GSSDPClient *client,
+                               const char  *ip_address)
+{
+        char *hwaddr;
+
+        g_return_val_if_fail (GSSDP_IS_CLIENT (client), NULL);
+        g_return_val_if_fail (ip_address != NULL, NULL);
+
+        hwaddr = arp_lookup (client, ip_address);
+
+        if (hwaddr) {
+                const char *agent;
+
+                agent =  g_hash_table_lookup (client->priv->user_agent_cache,
+                                              hwaddr);
+                g_free (hwaddr);
+
+                return agent;
+        }
+
+        return NULL;
+}
+
+/**
  * gssdp_client_get_network:
  * @client: A #GSSDPClient
  *
@@ -1841,3 +1912,8 @@ init_network_info (GSSDPClient *client, GError **error)
         return ret;
 }
 
+char *
+arp_lookup (GSSDPClient *client, const char *ip_address)
+{
+        return g_strdup (ip_address);
+}
diff --git a/libgssdp/gssdp-client.h b/libgssdp/gssdp-client.h
index ef51334..03a1866 100644
--- a/libgssdp/gssdp-client.h
+++ b/libgssdp/gssdp-client.h
@@ -120,6 +120,15 @@ gssdp_client_remove_header    (GSSDPClient *client,
 void
 gssdp_client_clear_headers    (GSSDPClient *client);
 
+void
+gssdp_client_add_cache_entry  (GSSDPClient  *client,
+                               const char   *ip_address,
+                               const char   *user_agent);
+
+const char *
+gssdp_client_guess_user_agent (GSSDPClient *client,
+                               const char  *ip_address);
+
 G_END_DECLS
 
 #endif /* __GSSDP_CLIENT_H__ */
diff --git a/libgssdp/gssdp-socket-functions.h b/libgssdp/gssdp-socket-functions.h
index 68109a2..7eaae59 100644
--- a/libgssdp/gssdp-socket-functions.h
+++ b/libgssdp/gssdp-socket-functions.h
@@ -37,4 +37,5 @@ G_GNUC_INTERNAL gboolean
 gssdp_socket_enable_info         (GSocket *socket,
                                   gboolean enable,
                                   GError **error);
+
 #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]