-handle client disconnects in GNS
authorChristian Grothoff <christian@grothoff.org>
Thu, 15 Aug 2013 11:09:13 +0000 (11:09 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 15 Aug 2013 11:09:13 +0000 (11:09 +0000)
src/gns/gnunet-service-gns.c

index 7a6f8e277592421bbd7fc3abbd06894b4685bc1b..6b4e63914ded40a5a7587639466d6c0fdd1009d0 100644 (file)
@@ -26,8 +26,6 @@
  * TODO:
  * - conversion of private to public records does NOT check if the
  *   records are actually public 
- * - need to watch for client disconnects and abort operations on
- *   disconnect
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
@@ -550,6 +548,7 @@ handle_lookup (void *cls,
   GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
   
   clh = GNUNET_new (struct ClientLookupHandle);
+  GNUNET_SERVER_client_set_user_context (client, clh);
   GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
   clh->client = client;
   clh->request_id = sh_msg->id;
@@ -581,6 +580,29 @@ handle_lookup (void *cls,
 }
 
 
+/**
+ * One of our clients disconnected, clean up after it.
+ *
+ * @param cls NULL
+ * @param client the client that disconnected
+ */
+static void
+notify_client_disconnect (void *cls,
+                         struct GNUNET_SERVER_Client *client)
+{
+  struct ClientLookupHandle *clh;
+
+  if (NULL == client)
+    return;
+  clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
+  if (NULL == clh)
+    return;
+  GNS_resolver_lookup_cancel (clh->lookup);
+  GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
+  GNUNET_free (clh);
+}
+
+
 /**
  * Process GNS requests.
  *
@@ -687,8 +709,10 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   GNS_resolver_init (namestore_handle, dht_handle, 
                     c,
                     max_parallel_bg_queries);
-  
-  /* Schedule periodic put for our records. */  
+  GNUNET_SERVER_disconnect_notify (server,
+                                  &notify_client_disconnect,
+                                  NULL);
+  /* Schedule periodic put for our records. */    
   first_zone_iteration = GNUNET_YES;
   GNUNET_SERVER_add_handlers (server, handlers);
   statistics = GNUNET_STATISTICS_create ("gns", c);