gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r19979 - in gnunet: . src/include src/util


From: gnunet
Subject: [GNUnet-SVN] r19979 - in gnunet: . src/include src/util
Date: Thu, 23 Feb 2012 17:41:30 +0100

Author: grothoff
Date: 2012-02-23 17:41:30 +0100 (Thu, 23 Feb 2012)
New Revision: 19979

Modified:
   gnunet/AUTHORS
   gnunet/src/include/gnunet_common.h
   gnunet/src/include/gnunet_crypto_lib.h
   gnunet/src/include/gnunet_server_lib.h
   gnunet/src/util/common_logging.c
   gnunet/src/util/crypto_hash.c
   gnunet/src/util/server.c
Log:
TG: attached are the following patches for GNUnet:

- 1: added GNUNET_i2s_full - full variant of GNUNET_i2s
- 2: GNUNET_CRYPTO_hash_from_string2 with additional length parameter,
     useful to prevent an additional strlen call when the caller already knows
     the length
- 3: custom mst callbacks for the server, enables using the server with a
     custom parser
- 4: added GNUNET_SERVER_client_set_finish_pending_write - enables changing the
     server behavior to finish pending writes when closing the connection

Best regards,
 Gabor Adam Toth 


Modified: gnunet/AUTHORS
===================================================================
--- gnunet/AUTHORS      2012-02-23 16:28:06 UTC (rev 19978)
+++ gnunet/AUTHORS      2012-02-23 16:41:30 UTC (rev 19979)
@@ -31,6 +31,7 @@
 Eric Haumant
 Eric Noack <address@hidden>
 Felix von Leitner [ diet libc snprintf for win32 ]
+Gabor Adam Toth <address@hidden>
 Gerd Knorr <address@hidden>
 Glenn McGrath <address@hidden>
 Hendrik Pagenhardt <address@hidden>

Modified: gnunet/src/include/gnunet_common.h
===================================================================
--- gnunet/src/include/gnunet_common.h  2012-02-23 16:28:06 UTC (rev 19978)
+++ gnunet/src/include/gnunet_common.h  2012-02-23 16:41:30 UTC (rev 19979)
@@ -438,6 +438,17 @@
 const char *
 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
 
+/**
+ * Convert a peer identity to a string (for printing debug messages).
+ * This is one of the very few calls in the entire API that is
+ * NOT reentrant!
+ *
+ * @param pid the peer identity
+ * @return string form of the pid; will be overwritten by next
+ *         call to GNUNET_i2s.
+ */
+const char *
+GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
 
 /**
  * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string

Modified: gnunet/src/include/gnunet_crypto_lib.h
===================================================================
--- gnunet/src/include/gnunet_crypto_lib.h      2012-02-23 16:28:06 UTC (rev 
19978)
+++ gnunet/src/include/gnunet_crypto_lib.h      2012-02-23 16:41:30 UTC (rev 
19979)
@@ -407,15 +407,28 @@
 
 /**
  * Convert ASCII encoding back to GNUNET_CRYPTO_hash
+ *
  * @param enc the encoding
+ * @param enclen number of characters in 'enc' (without 0-terminator, which 
can be missing)
  * @param result where to store the GNUNET_CRYPTO_hash code
  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
  */
 int
-GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result);
+GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
+                                 GNUNET_HashCode * result);
 
 
 /**
+ * Convert ASCII encoding back to GNUNET_CRYPTO_hash
+ * @param enc the encoding
+ * @param result where to store the GNUNET_CRYPTO_hash code
+ * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
+ */
+#define GNUNET_CRYPTO_hash_from_string(enc, result) \
+  GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
+
+
+/**
  * Compute the distance between 2 hashcodes.
  * The computation must be fast, not involve
  * a.a or a.e (they're used elsewhere), and

Modified: gnunet/src/include/gnunet_server_lib.h
===================================================================
--- gnunet/src/include/gnunet_server_lib.h      2012-02-23 16:28:06 UTC (rev 
19978)
+++ gnunet/src/include/gnunet_server_lib.h      2012-02-23 16:41:30 UTC (rev 
19979)
@@ -240,6 +240,14 @@
 
 
 /**
+ * Set if a client should finish a pending write when disconnecting.
+ */
+void
+GNUNET_SERVER_client_set_finish_pending_write (struct GNUNET_SERVER_Client 
*client,
+                                               int finish);
+
+
+/**
  * Disable the warning the server issues if a message is not acknowledged
  * in a timely fashion.  Use this call if a client is intentionally delayed
  * for a while.  Only applies to the current message.
@@ -636,6 +644,64 @@
 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
 
 
+/**
+ * Signature of a function to create a custom tokenizer.
+ *
+ * @param cls closure from 'GNUNET_SERVER_set_callbacks'
+ * @param client handle to client the tokenzier will be used for
+ * @return handle to custom tokenizer ('mst')
+ */
+typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
+                                                  struct GNUNET_SERVER_Client 
*client);
+
+/**
+ * Signature of a function to destroy a custom tokenizer.
+ *
+ * @param cls closure from 'GNUNET_SERVER_set_callbacks'
+ * @param mst custom tokenizer handle
+ */
+typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
+
+/**
+ * Signature of a function to destroy a custom tokenizer.
+ *
+ * @param cls closure from 'GNUNET_SERVER_set_callbacks'
+ * @param mst custom tokenizer handle
+ * @param client_identity ID of client for which this is a buffer,
+ *        can be NULL (will be passed back to 'cb')
+ * @param buf input data to add
+ * @param size number of bytes in buf
+ * @param purge should any excess bytes in the buffer be discarded
+ *       (i.e. for packet-based services like UDP)
+ * @param one_shot only call callback once, keep rest of message in buffer
+ * @return GNUNET_OK if we are done processing (need more data)
+ *         GNUNET_NO if one_shot was set and we have another message ready
+ *         GNUNET_SYSERR if the data stream is corrupt 
+ */
+typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
+                                                 struct GNUNET_SERVER_Client 
*client,
+                                                 const char *buf, size_t size,
+                                                 int purge, int one_shot);
+
+
+/**
+ * Change functions used by the server to tokenize the message stream.
+ * (very rarely used).
+ *
+ * @param server server to modify
+ * @param create new tokenizer initialization function
+ * @param destroy new tokenizer destruction function
+ * @param receive new tokenizer receive function
+ * @param cls closure for 'create', 'receive', 'destroy' 
+ */
+void
+GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
+                             GNUNET_SERVER_MstCreateCallback create,
+                             GNUNET_SERVER_MstDestroyCallback destroy,
+                             GNUNET_SERVER_MstReceiveCallback receive,
+                             void *cls);
+
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif

Modified: gnunet/src/util/common_logging.c
===================================================================
--- gnunet/src/util/common_logging.c    2012-02-23 16:28:06 UTC (rev 19978)
+++ gnunet/src/util/common_logging.c    2012-02-23 16:41:30 UTC (rev 19979)
@@ -948,8 +948,25 @@
   return (const char *) ret.encoding;
 }
 
+/**
+ * Convert a peer identity to a string (for printing debug messages).
+ * This is one of the very few calls in the entire API that is
+ * NOT reentrant!
+ *
+ * @param pid the peer identity
+ * @return string form of the pid; will be overwritten by next
+ *         call to GNUNET_i2s.
+ */
+const char *
+GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
+{
+  static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
 
+  GNUNET_CRYPTO_hash_to_enc (&pid->hashPubKey, &ret);
+  return (const char *) ret.encoding;
+}
 
+
 /**
  * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
  * (for printing debug messages).  This is one of the very few calls

Modified: gnunet/src/util/crypto_hash.c
===================================================================
--- gnunet/src/util/crypto_hash.c       2012-02-23 16:28:06 UTC (rev 19978)
+++ gnunet/src/util/crypto_hash.c       2012-02-23 16:41:30 UTC (rev 19979)
@@ -322,7 +322,8 @@
  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
  */
 int
-GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
+GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
+                                GNUNET_HashCode * result)
 {
   unsigned int rpos;
   unsigned int wpos;
@@ -330,7 +331,7 @@
   unsigned int vbit;
   int ret;
 
-  if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
+  if (enclen != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
     return GNUNET_SYSERR;
 
   vbit = 2;                     /* padding! */

Modified: gnunet/src/util/server.c
===================================================================
--- gnunet/src/util/server.c    2012-02-23 16:28:06 UTC (rev 19978)
+++ gnunet/src/util/server.c    2012-02-23 16:41:30 UTC (rev 19979)
@@ -140,6 +140,10 @@
    */
   int clients_ignore_shutdown;
 
+  GNUNET_SERVER_MstCreateCallback mst_create;
+  GNUNET_SERVER_MstDestroyCallback mst_destroy;
+  GNUNET_SERVER_MstReceiveCallback mst_receive;
+  void *mst_cls;
 };
 
 
@@ -157,7 +161,7 @@
   /**
    * Processing of incoming data.
    */
-  struct GNUNET_SERVER_MessageStreamTokenizer *mst;
+  void *mst;
 
   /**
    * Server that this client belongs to.
@@ -243,6 +247,11 @@
   int receive_pending;
 
   /**
+   * Finish pending write when disconnecting?
+   */
+  int finish_pending_write;
+
+  /**
    * Persist the file handle for this client no matter what happens,
    * force the OS to close once the process actually dies.  Should only
    * be used in special cases!
@@ -597,6 +606,20 @@
 }
 
 
+void
+GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
+                             GNUNET_SERVER_MstCreateCallback create,
+                             GNUNET_SERVER_MstDestroyCallback destroy,
+                             GNUNET_SERVER_MstReceiveCallback receive,
+                             void *cls)
+{
+  server->mst_create = create;
+  server->mst_destroy = destroy;
+  server->mst_receive = receive;
+  server->mst_cls = cls;
+}
+
+
 /**
  * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
  *
@@ -776,9 +799,14 @@
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Server processes additional messages instantly.\n");
 #endif
-    ret =
-        GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
-                                   GNUNET_YES);
+    if (client->server->mst_receive != NULL)
+      ret =
+          client->server->mst_receive (client->server->mst_cls, client->mst,
+                                       client, NULL, 0, GNUNET_NO, GNUNET_YES);
+    else
+      ret =
+          GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
+                                     GNUNET_YES);
   }
 #if DEBUG_SERVER
   LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -857,9 +885,16 @@
 #endif
   GNUNET_SERVER_client_keep (client);
   client->last_activity = now;
-  ret =
-      GNUNET_SERVER_mst_receive (client->mst, client, buf, available, 
GNUNET_NO,
-                                 GNUNET_YES);
+
+  if (server->mst_receive != NULL)
+    ret =
+        client->server->mst_receive (client->server->mst_cls, client->mst,
+                                     client, buf, available, GNUNET_NO, 
GNUNET_YES);
+  else
+    ret =
+        GNUNET_SERVER_mst_receive (client->mst, client, buf, available, 
GNUNET_NO,
+                                   GNUNET_YES);
+
   process_mst (client, ret);
 }
 
@@ -966,6 +1001,14 @@
   client->receive_pending = GNUNET_YES;
   client->callback = NULL;
   client->callback_cls = NULL;
+
+  if (server->mst_create != NULL)
+    client->mst =
+        server->mst_create (server->mst_cls, client);
+  else
+    client->mst =
+        GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
+
   GNUNET_CONNECTION_receive (client->connection,
                              GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
                              client->idle_timeout, &process_incoming, client);
@@ -989,6 +1032,14 @@
 }
 
 
+void
+GNUNET_SERVER_client_set_finish_pending_write (struct GNUNET_SERVER_Client 
*client,
+                                               int finish)
+{
+  client->finish_pending_write = finish;
+}
+
+
 /**
  * Notify the server that the given client handle should
  * be kept (keeps the connection up if possible, increments
@@ -1137,10 +1188,9 @@
   }
 
   rc = client->reference_count;
-  if (client->server != NULL)
+  if (client->shutdown_now != GNUNET_YES)
   {
     server = client->server;
-    client->server = NULL;
     client->shutdown_now = GNUNET_YES;
     prev = NULL;
     pos = server->clients;
@@ -1190,8 +1240,13 @@
 
   if (client->persist == GNUNET_YES)
     GNUNET_CONNECTION_persist_ (client->connection);
-  GNUNET_CONNECTION_destroy (client->connection, GNUNET_NO);
-  GNUNET_SERVER_mst_destroy (client->mst);
+  GNUNET_CONNECTION_destroy (client->connection, client->finish_pending_write);
+
+  if (client->server->mst_destroy != NULL)
+    client->server->mst_destroy (client->server->mst_cls, client->mst);
+  else
+    GNUNET_SERVER_mst_destroy (client->mst);
+
   GNUNET_free (client);
 }
 
@@ -1326,7 +1381,7 @@
 #endif
     return;
   }
-  if (client->server == NULL)
+  if ((client->server == NULL) || (GNUNET_YES == client->shutdown_now))
   {
     GNUNET_SERVER_client_disconnect (client);
     return;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]