adding extended proxy support for http(s) client
[oweals/gnunet.git] / src / psyc / gnunet-service-psyc.c
index 4cdb490a1378c95bffd7d1c71f88819635272f98..fb3aa65f7cda91a09d19f949b3d8d069228676ac 100644 (file)
@@ -63,6 +63,7 @@ static struct GNUNET_PSYCSTORE_Handle *store;
  */
 static struct GNUNET_CONTAINER_MultiHashMap *clients;
 
+
 /**
  * Message in the transmission queue.
  */
@@ -71,10 +72,18 @@ struct TransmitMessage
   struct TransmitMessage *prev;
   struct TransmitMessage *next;
 
+  /**
+   * Buffer with message to be transmitted.
+   */
   char *buf;
-  uint16_t size;
+
   /**
-   * enum MessageState
+   * Size of @a buf
+   */
+  uint16_t size
+;
+  /**
+   * @see enum MessageState
    */
   uint8_t state;
 };
@@ -134,8 +143,21 @@ struct Channel
    */
   struct GNUNET_CONTAINER_MultiHashMap *recv_msgs;
 
+  /**
+   * FIXME
+   */
   GNUNET_SCHEDULER_TaskIdentifier tmit_task;
 
+  /**
+   * Public key of the channel.
+   */
+  struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
+
+  /**
+   * Hash of @a pub_key.
+   */
+  struct GNUNET_HashCode pub_key_hash;
+
   /**
    * Expected value size for the modifier being received from the PSYC service.
    */
@@ -147,34 +169,55 @@ struct Channel
   uint32_t tmit_mod_value_size;
 
   /**
-   * enum MessageState
+   * @see enum MessageState
    */
   uint8_t tmit_state;
 
+  /**
+   * FIXME
+   */
   uint8_t in_transmit;
+
+  /**
+   * Is this a channel master (#GNUNET_YES), or slave (#GNUNET_NO)?
+   */
   uint8_t is_master;
 
   /**
-   * Ready to receive messages from client.
+   * Ready to receive messages from client? #GNUNET_YES or #GNUNET_NO
    */
   uint8_t ready;
 
   /**
-   * Client disconnected.
+   * Is the client disconnected? #GNUNET_YES or #GNUNET_NO
    */
   uint8_t disconnected;
 };
 
+
 /**
  * Client context for a channel master.
  */
 struct Master
 {
+  /**
+   * Channel struct common for Master and Slave
+   */
   struct Channel channel;
+
+  /**
+   * Private key of the channel.
+   */
   struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
-  struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
 
+  /**
+   * Handle for the multicast origin.
+   */
   struct GNUNET_MULTICAST_Origin *origin;
+
+  /**
+   * Transmit handle for multicast.
+   */
   struct GNUNET_MULTICAST_OriginMessageHandle *tmit_handle;
 
   /**
@@ -200,8 +243,6 @@ struct Master
    * @see enum GNUNET_PSYC_Policy
    */
   uint32_t policy;
-
-  struct GNUNET_HashCode pub_key_hash;
 };
 
 
@@ -210,24 +251,55 @@ struct Master
  */
 struct Slave
 {
+  /**
+   * Channel struct common for Master and Slave
+   */
   struct Channel channel;
+
+  /**
+   * Private key of the slave.
+   */
   struct GNUNET_CRYPTO_EddsaPrivateKey slave_key;
-  struct GNUNET_CRYPTO_EddsaPublicKey chan_key;
 
+  /**
+   * Handle for the multicast member.
+   */
   struct GNUNET_MULTICAST_Member *member;
+
+  /**
+   * Transmit handle for multicast.
+   */
   struct GNUNET_MULTICAST_MemberRequestHandle *tmit_handle;
 
+  /**
+   * Peer identity of the origin.
+   */
   struct GNUNET_PeerIdentity origin;
 
+  /**
+   * Number of items in @a relays.
+   */
   uint32_t relay_count;
+
+  /**
+   * Relays that multicast can use to connect.
+   */
   struct GNUNET_PeerIdentity *relays;
 
+  /**
+   * Join request to be transmitted to the master on join.
+   */
   struct GNUNET_MessageHeader *join_req;
 
+  /**
+   * Maximum message ID for this channel.
+   */
   uint64_t max_message_id;
-  uint64_t max_request_id;
 
-  struct GNUNET_HashCode chan_key_hash;
+  /**
+   * Maximum request ID for this channel.
+   */
+  uint64_t max_request_id;
 };
 
 
@@ -267,7 +339,7 @@ client_cleanup (struct Channel *ch)
     struct Master *mst = (struct Master *) ch;
     if (NULL != mst->origin)
       GNUNET_MULTICAST_origin_stop (mst->origin);
-    GNUNET_CONTAINER_multihashmap_remove (clients, &mst->pub_key_hash, mst);
+    GNUNET_CONTAINER_multihashmap_remove (clients, &ch->pub_key_hash, mst);
   }
   else
   {
@@ -323,8 +395,11 @@ client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
 }
 
 
+/**
+ * Master receives a join request from a slave.
+ */
 static void
-join_cb (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *member_key,
+join_cb (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
          const struct GNUNET_MessageHeader *join_req,
          struct GNUNET_MULTICAST_JoinHandle *jh)
 {
@@ -334,7 +409,7 @@ join_cb (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *member_key,
 
 static void
 membership_test_cb (void *cls,
-                    const struct GNUNET_CRYPTO_EddsaPublicKey *member_key,
+                    const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
                     uint64_t message_id, uint64_t group_generation,
                     struct GNUNET_MULTICAST_MembershipTestHandle *mth)
 {
@@ -344,7 +419,7 @@ membership_test_cb (void *cls,
 
 static void
 replay_fragment_cb (void *cls,
-                    const struct GNUNET_CRYPTO_EddsaPublicKey *member_key,
+                    const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
                     uint64_t fragment_id, uint64_t flags,
                     struct GNUNET_MULTICAST_ReplayHandle *rh)
 
@@ -354,7 +429,7 @@ replay_fragment_cb (void *cls,
 
 static void
 replay_message_cb (void *cls,
-                   const struct GNUNET_CRYPTO_EddsaPublicKey *member_key,
+                   const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
                    uint64_t message_id,
                    uint64_t fragment_offset,
                    uint64_t flags,
@@ -439,7 +514,6 @@ hash_key_from_hll (struct GNUNET_HashCode *key, uint64_t n)
 
 static void
 fragment_cache_insert (struct Channel *ch,
-                       const struct GNUNET_HashCode *chan_key_hash,
                        const struct GNUNET_HashCode *msg_id,
                        struct FragmentCache *frag_cache,
                        const struct GNUNET_MULTICAST_MessageHeader *mmsg,
@@ -447,7 +521,8 @@ fragment_cache_insert (struct Channel *ch,
 {
   uint16_t size = ntohs (mmsg->header.size);
   struct GNUNET_CONTAINER_MultiHashMap
-    *chan_msgs = GNUNET_CONTAINER_multihashmap_get (recv_cache, chan_key_hash);
+    *chan_msgs = GNUNET_CONTAINER_multihashmap_get (recv_cache,
+                                                    &ch->pub_key_hash);
 
   if (NULL == frag_cache)
   {
@@ -465,7 +540,7 @@ fragment_cache_insert (struct Channel *ch,
     if (NULL == chan_msgs)
     {
       chan_msgs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
-      GNUNET_CONTAINER_multihashmap_put (recv_cache, chan_key_hash, chan_msgs,
+      GNUNET_CONTAINER_multihashmap_put (recv_cache, &ch->pub_key_hash, chan_msgs,
                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
     }
   }
@@ -512,7 +587,6 @@ fragment_cache_insert (struct Channel *ch,
 
 static void
 fragment_cache_clear (struct Channel *ch,
-                      const struct GNUNET_HashCode *chan_key_hash,
                       const struct GNUNET_HashCode *msg_id,
                       struct FragmentCache *frag_cache,
                       uint8_t send_to_client)
@@ -521,7 +595,8 @@ fragment_cache_clear (struct Channel *ch,
               "%p Clearing message fragment cache.\n", ch);
 
   struct GNUNET_CONTAINER_MultiHashMap
-    *chan_msgs = GNUNET_CONTAINER_multihashmap_get (recv_cache, chan_key_hash);
+    *chan_msgs = GNUNET_CONTAINER_multihashmap_get (recv_cache,
+                                                    &ch->pub_key_hash);
   GNUNET_assert (NULL != chan_msgs);
   struct GNUNET_HashCode *frag_id;
 
@@ -561,11 +636,9 @@ fragment_cache_clear (struct Channel *ch,
  * Store it using PSYCstore and send it to the client of the channel.
  */
 static void
-message_cb (struct Channel *ch,
-            const struct GNUNET_CRYPTO_EddsaPublicKey *chan_key,
-            const struct GNUNET_HashCode *chan_key_hash,
-            const struct GNUNET_MessageHeader *msg)
+message_cb (void *cls, const struct GNUNET_MessageHeader *msg)
 {
+  struct Channel *ch = cls;
   uint16_t type = ntohs (msg->type);
   uint16_t size = ntohs (msg->size);
 
@@ -577,14 +650,14 @@ message_cb (struct Channel *ch,
   {
   case GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE:
   {
-    GNUNET_PSYCSTORE_fragment_store (store, chan_key,
+    GNUNET_PSYCSTORE_fragment_store (store, &ch->pub_key,
                                      (const struct
                                       GNUNET_MULTICAST_MessageHeader *) msg,
                                      0, NULL, NULL);
 
 #if TODO
     /* FIXME: apply modifiers to state in PSYCstore */
-    GNUNET_PSYCSTORE_state_modify (store, chan_key,
+    GNUNET_PSYCSTORE_state_modify (store, &ch->pub_key,
                                    GNUNET_ntohll (mmsg->message_id),
                                    meth->mod_count, mods,
                                    rcb, rcb_cls);
@@ -628,7 +701,7 @@ message_cb (struct Channel *ch,
       {
         if (GNUNET_ntohll (mmsg->fragment_offset) == frag_cache->header_size)
         { /* first data fragment after the header, send cached fragments */
-          fragment_cache_clear (ch, chan_key_hash, &msg_id, frag_cache, GNUNET_YES);
+          fragment_cache_clear (ch, &msg_id, frag_cache, GNUNET_YES);
           message_to_client (ch, mmsg);
           break;
         }
@@ -642,13 +715,13 @@ message_cb (struct Channel *ch,
     case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
     case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
       /* not all modifiers arrived yet, cache fragment */
-      fragment_cache_insert (ch, chan_key_hash, &msg_id, frag_cache, mmsg, ptype);
+      fragment_cache_insert (ch, &msg_id, frag_cache, mmsg, ptype);
       break;
 
     case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
       if (NULL != frag_cache)
       { /* fragments not yet sent to client, remove from cache */
-        fragment_cache_clear (ch, chan_key_hash, &msg_id, frag_cache, GNUNET_NO);
+        fragment_cache_clear (ch, &msg_id, frag_cache, GNUNET_NO);
       }
       else
       {
@@ -666,48 +739,16 @@ message_cb (struct Channel *ch,
 }
 
 
-/**
- * Incoming message fragment from multicast for a master.
- */
-static void
-master_message_cb (void *cls, const struct GNUNET_MessageHeader *msg)
-{
-  struct Master *mst = cls;
-  GNUNET_assert (NULL != mst);
-
-  struct GNUNET_CRYPTO_EddsaPublicKey *chan_key = &mst->pub_key;
-  struct GNUNET_HashCode *chan_key_hash = &mst->pub_key_hash;
-
-  message_cb (&mst->channel, chan_key, chan_key_hash, msg);
-}
-
-
-/**
- * Incoming message fragment from multicast for a slave.
- */
-static void
-slave_message_cb (void *cls, const struct GNUNET_MessageHeader *msg)
-{
-  struct Slave *slv = cls;
-  GNUNET_assert (NULL != slv);
-
-  struct GNUNET_CRYPTO_EddsaPublicKey *chan_key = &slv->chan_key;
-  struct GNUNET_HashCode *chan_key_hash = &slv->chan_key_hash;
-
-  message_cb (&slv->channel, chan_key, chan_key_hash, msg);
-}
-
-
 /**
  * Incoming request fragment from multicast for a master.
  *
  * @param cls          Master.
- * @param member_key   Sending member's public key.
+ * @param slave_key    Sending slave's public key.
  * @param msg          The message.
  * @param flags                Request flags.
  */
 static void
-request_cb (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *member_key,
+request_cb (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
             const struct GNUNET_MessageHeader *msg,
             enum GNUNET_MULTICAST_MessageFlags flags)
 {
@@ -791,7 +832,7 @@ master_counters_cb (void *cls, int result, uint64_t max_fragment_id,
                                        max_fragment_id + 1,
                                        join_cb, membership_test_cb,
                                        replay_fragment_cb, replay_message_cb,
-                                       request_cb, master_message_cb, ch);
+                                       request_cb, message_cb, ch);
     ch->ready = GNUNET_YES;
   }
   GNUNET_SERVER_notification_context_add (nc, ch->client);
@@ -821,13 +862,13 @@ slave_counters_cb (void *cls, int result, uint64_t max_fragment_id,
   {
     slv->max_message_id = max_message_id;
     slv->member
-      = GNUNET_MULTICAST_member_join (cfg, &slv->chan_key, &slv->slave_key,
+      = GNUNET_MULTICAST_member_join (cfg, &ch->pub_key, &slv->slave_key,
                                       &slv->origin,
                                       slv->relay_count, slv->relays,
                                       slv->join_req, join_cb,
                                       membership_test_cb,
                                       replay_fragment_cb, replay_message_cb,
-                                      slave_message_cb, ch);
+                                      message_cb, ch);
     ch->ready = GNUNET_YES;
   }
 
@@ -848,21 +889,21 @@ handle_master_start (void *cls, struct GNUNET_SERVER_Client *client,
   const struct MasterStartRequest *req
     = (const struct MasterStartRequest *) msg;
   struct Master *mst = GNUNET_new (struct Master);
-  mst->channel.client = client;
-  mst->channel.is_master = GNUNET_YES;
+  struct Channel *ch = &mst->channel;
+  ch->client = client;
+  ch->is_master = GNUNET_YES;
   mst->policy = ntohl (req->policy);
   mst->priv_key = req->channel_key;
-  GNUNET_CRYPTO_eddsa_key_get_public (&mst->priv_key, &mst->pub_key);
-  GNUNET_CRYPTO_hash (&mst->pub_key, sizeof (mst->pub_key), &mst->pub_key_hash);
+  GNUNET_CRYPTO_eddsa_key_get_public (&mst->priv_key, &ch->pub_key);
+  GNUNET_CRYPTO_hash (&ch->pub_key, sizeof (ch->pub_key), &ch->pub_key_hash);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "%p Master connected to channel %s.\n",
-              mst, GNUNET_h2s (&mst->pub_key_hash));
+              mst, GNUNET_h2s (&ch->pub_key_hash));
 
-  GNUNET_PSYCSTORE_counters_get (store, &mst->pub_key,
-                                 master_counters_cb, mst);
+  GNUNET_PSYCSTORE_counters_get (store, &ch->pub_key, master_counters_cb, mst);
 
   GNUNET_SERVER_client_set_user_context (client, &mst->channel);
-  GNUNET_CONTAINER_multihashmap_put (clients, &mst->pub_key_hash, mst,
+  GNUNET_CONTAINER_multihashmap_put (clients, &ch->pub_key_hash, mst,
                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -878,12 +919,13 @@ handle_slave_join (void *cls, struct GNUNET_SERVER_Client *client,
   const struct SlaveJoinRequest *req
     = (const struct SlaveJoinRequest *) msg;
   struct Slave *slv = GNUNET_new (struct Slave);
+  struct Channel *ch = &slv->channel;
   slv->channel.client = client;
   slv->channel.is_master = GNUNET_NO;
   slv->slave_key = req->slave_key;
-  slv->chan_key = req->channel_key;
-  GNUNET_CRYPTO_hash (&slv->chan_key, sizeof (slv->chan_key),
-                      &slv->chan_key_hash);
+  ch->pub_key = req->channel_key;
+  GNUNET_CRYPTO_hash (&ch->pub_key, sizeof (ch->pub_key),
+                      &ch->pub_key_hash);
   slv->origin = req->origin;
   slv->relay_count = ntohl (req->relay_count);
   if (0 < slv->relay_count)
@@ -899,10 +941,9 @@ handle_slave_join (void *cls, struct GNUNET_SERVER_Client *client,
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "%p Slave connected to channel %s.\n",
-              slv, GNUNET_h2s (&slv->chan_key_hash));
+              slv, GNUNET_h2s (&ch->pub_key_hash));
 
-  GNUNET_PSYCSTORE_counters_get (store, &slv->chan_key,
-                                 slave_counters_cb, slv);
+  GNUNET_PSYCSTORE_counters_get (store, &ch->pub_key, slave_counters_cb, slv);
 
   GNUNET_SERVER_client_set_user_context (client, &slv->channel);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -1158,6 +1199,61 @@ handle_psyc_message (void *cls, struct GNUNET_SERVER_Client *client,
 };
 
 
+/**
+ * Client requests to add a slave to the membership database.
+ */
+static void
+handle_slave_add (void *cls, struct GNUNET_SERVER_Client *client,
+                  const struct GNUNET_MessageHeader *msg)
+{
+
+}
+
+
+/**
+ * Client requests to remove a slave from the membership database.
+ */
+static void
+handle_slave_remove (void *cls, struct GNUNET_SERVER_Client *client,
+                     const struct GNUNET_MessageHeader *msg)
+{
+
+}
+
+
+/**
+ * Client requests channel history from PSYCstore.
+ */
+static void
+handle_story_request (void *cls, struct GNUNET_SERVER_Client *client,
+                      const struct GNUNET_MessageHeader *msg)
+{
+
+}
+
+
+/**
+ * Client requests best matching state variable from PSYCstore.
+ */
+static void
+handle_state_get (void *cls, struct GNUNET_SERVER_Client *client,
+                  const struct GNUNET_MessageHeader *msg)
+{
+
+}
+
+
+/**
+ * Client requests state variables with a given prefix from PSYCstore.
+ */
+static void
+handle_state_get_prefix (void *cls, struct GNUNET_SERVER_Client *client,
+                         const struct GNUNET_MessageHeader *msg)
+{
+
+}
+
+
 /**
  * Initialize the PSYC service.
  *
@@ -1178,6 +1274,21 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
 
     { &handle_psyc_message, NULL,
       GNUNET_MESSAGE_TYPE_PSYC_MESSAGE, 0 },
+
+    { &handle_slave_add, NULL,
+      GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_SLAVE_ADD, 0 },
+
+    { &handle_slave_remove, NULL,
+      GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_SLAVE_RM, 0 },
+
+    { &handle_story_request, NULL,
+      GNUNET_MESSAGE_TYPE_PSYC_STORY_REQUEST, 0 },
+
+    { &handle_state_get, NULL,
+      GNUNET_MESSAGE_TYPE_PSYC_STATE_GET, 0 },
+
+    { &handle_state_get_prefix, NULL,
+      GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX, 0 }
   };
 
   cfg = c;