added logging
[oweals/gnunet.git] / src / social / social_api.c
index c08de0356532a1563bbfa53f3d6498fd093e9a66..923ab5e33b3b0c2743aea54f1b22c342eca6e12b 100644 (file)
@@ -657,10 +657,17 @@ check_place_state_result (void *cls,
                           const struct GNUNET_OperationResultMessage *res)
 {
   const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
-  uint16_t mod_size = ntohs (mod->size);
-  uint16_t size = ntohs (res->header.size);
+  if (NULL == mod)
+  {
+    GNUNET_break_op (0);
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "Invalid modifier in state result\n");
+    return GNUNET_SYSERR;
+  }
 
-  if (NULL == mod || size - sizeof (*res) != mod_size)
+  uint16_t size = ntohs (res->header.size);
+  uint16_t mod_size = ntohs (mod->size);
+  if (size - sizeof (*res) != mod_size)
   {
     GNUNET_break_op (0);
     LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -700,7 +707,9 @@ handle_place_state_result (void *cls,
 
     const char *name = (const char *) &pmod[1];
     uint16_t name_size = ntohs (pmod->name_size);
-    if ('\0' != name[name_size - 1])
+    if (0 == name_size
+        || mod_size - sizeof (*pmod) < name_size
+        || '\0' != name[name_size - 1])
     {
       GNUNET_break_op (0);
       LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -809,6 +818,9 @@ handle_host_enter_request (void *cls,
 {
   struct GNUNET_SOCIAL_Host *hst = cls;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "handle_host_enter_request\n");
+
   if (NULL == hst->answer_door_cb)
      return;
 
@@ -884,7 +896,7 @@ handle_guest_enter_decision (void *cls,
   struct GNUNET_SOCIAL_Guest *gst = cls;
 
   struct GNUNET_PSYC_Message *pmsg = NULL;
-  if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
+  if (ntohs (dcsn->header.size) > sizeof (*dcsn))
     pmsg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (dcsn);
 
   if (NULL != gst->entry_dcsn_cb)
@@ -1033,7 +1045,7 @@ place_cleanup (struct GNUNET_SOCIAL_Place *plc)
 {
   struct GNUNET_HashCode place_pub_hash;
   GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "%s place cleanup: %s\n",
               GNUNET_YES == plc->is_host ? "host" : "guest",
               GNUNET_h2s (&place_pub_hash));
@@ -1050,6 +1062,8 @@ place_cleanup (struct GNUNET_SOCIAL_Place *plc)
   }
   if (NULL != plc->mq)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "destroying MQ (place_cleanup)\n");
     GNUNET_MQ_destroy (plc->mq);
     plc->mq = NULL;
   }
@@ -1066,20 +1080,19 @@ place_cleanup (struct GNUNET_SOCIAL_Place *plc)
 
 
 void
-place_disconnect (struct GNUNET_SOCIAL_Place *plc,
-                  GNUNET_ContinuationCallback disconnect_cb,
-                  void *disconnect_cls)
+place_disconnect (struct GNUNET_SOCIAL_Place *plc)
 {
-  plc->disconnect_cb = disconnect_cb;
-  plc->disconnect_cls = disconnect_cls;
-
+  struct GNUNET_HashCode place_pub_hash;
+  GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "place_disconnect, plc = %s\n",
+              GNUNET_h2s (&place_pub_hash));
   if (NULL != plc->mq)
   {
-    struct GNUNET_MQ_Envelope *last = GNUNET_MQ_get_last_envelope (plc->mq);
-    if (NULL != last)
+    struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (plc->mq);
+    if (NULL != env)
     {
-      GNUNET_MQ_notify_sent (last,
-                             (GNUNET_MQ_NotifyCallback) place_cleanup, plc);
+      GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) place_cleanup, plc);
     }
     else
     {
@@ -1096,6 +1109,8 @@ place_disconnect (struct GNUNET_SOCIAL_Place *plc,
 void
 place_leave (struct GNUNET_SOCIAL_Place *plc)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "social_api: place_leave\n");
   struct GNUNET_MessageHeader *msg;
   struct GNUNET_MQ_Envelope *
     env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
@@ -1132,16 +1147,18 @@ host_disconnected (void *cls, enum GNUNET_MQ_Error error)
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Host client disconnected (%d), re-connecting\n",
        (int) error);
-  if (NULL != plc->mq)
-  {
-    GNUNET_MQ_destroy (plc->mq);
-    plc->mq = NULL;
-  }
   if (NULL != plc->tmit)
   {
     GNUNET_PSYC_transmit_destroy (plc->tmit);
     plc->tmit = NULL;
   }
+  if (NULL != plc->mq)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "destroying MQ (host_disconnected)\n");
+    GNUNET_MQ_destroy (plc->mq);
+    plc->mq = NULL;
+  }
 
   plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
                                                       host_reconnect,
@@ -1155,46 +1172,39 @@ host_connect (struct GNUNET_SOCIAL_Host *hst)
 {
   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
 
-  GNUNET_MQ_hd_fixed_size (host_enter_ack,
-                           GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
-                           struct HostEnterAck);
-
-  GNUNET_MQ_hd_var_size (host_enter_request,
-                         GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
-                         struct GNUNET_PSYC_JoinRequestMessage);
-
-  GNUNET_MQ_hd_var_size (host_message,
-                         GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
-                         struct GNUNET_PSYC_MessageHeader);
-
-  GNUNET_MQ_hd_fixed_size (place_message_ack,
-                           GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
-                           struct GNUNET_MessageHeader);
-
-  GNUNET_MQ_hd_var_size (place_history_result,
-                         GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
-                         struct GNUNET_OperationResultMessage);
-
-  GNUNET_MQ_hd_var_size (place_state_result,
-                         GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
-                         struct GNUNET_OperationResultMessage);
-
-  GNUNET_MQ_hd_var_size (place_result,
-                         GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
-                         struct GNUNET_OperationResultMessage);
-
   struct GNUNET_MQ_MessageHandler handlers[] = {
-    make_host_enter_ack_handler (hst),
-    make_host_enter_request_handler (hst),
-    make_host_message_handler (plc),
-    make_place_message_ack_handler (plc),
-    make_place_history_result_handler (plc),
-    make_place_state_result_handler (plc),
-    make_place_result_handler (plc),
+    GNUNET_MQ_hd_fixed_size (host_enter_ack,
+                             GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
+                             struct HostEnterAck,
+                             hst),
+    GNUNET_MQ_hd_var_size (host_enter_request,
+                           GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
+                           struct GNUNET_PSYC_JoinRequestMessage,
+                           hst),
+    GNUNET_MQ_hd_var_size (host_message,
+                           GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
+                           struct GNUNET_PSYC_MessageHeader,
+                           hst),
+    GNUNET_MQ_hd_fixed_size (place_message_ack,
+                             GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
+                             struct GNUNET_MessageHeader,
+                             plc),
+    GNUNET_MQ_hd_var_size (place_history_result,
+                           GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
+                           struct GNUNET_OperationResultMessage,
+                           plc),
+    GNUNET_MQ_hd_var_size (place_state_result,
+                           GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
+                           struct GNUNET_OperationResultMessage,
+                           plc),
+    GNUNET_MQ_hd_var_size (place_result,
+                           GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
+                           struct GNUNET_OperationResultMessage,
+                           plc),
     GNUNET_MQ_handler_end ()
   };
 
-  plc->mq = GNUNET_CLIENT_connecT (plc->cfg, "social",
+  plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
                                    handlers, host_disconnected, hst);
   GNUNET_assert (NULL != plc->mq);
   plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
@@ -1579,7 +1589,11 @@ GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
                                GNUNET_ContinuationCallback disconnect_cb,
                                void *cls)
 {
-  place_disconnect (&hst->plc, disconnect_cb, cls);
+  struct GNUNET_SOCIAL_Place *plc = &hst->plc; 
+
+  plc->disconnect_cb = disconnect_cb;
+  plc->disconnect_cls = cls;
+  place_disconnect (plc);
 }
 
 
@@ -1606,6 +1620,8 @@ GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
                           GNUNET_ContinuationCallback disconnect_cb,
                           void *cls)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "GNUNET_SOCIAL_host_leave\n");
   GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
                                GNUNET_SOCIAL_ANNOUNCE_NONE);
   place_leave (&hst->plc);
@@ -1641,16 +1657,18 @@ guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Guest client disconnected (%d), re-connecting\n",
        (int) error);
-  if (NULL != plc->mq)
-  {
-    GNUNET_MQ_destroy (plc->mq);
-    plc->mq = NULL;
-  }
   if (NULL != plc->tmit)
   {
     GNUNET_PSYC_transmit_destroy (plc->tmit);
     plc->tmit = NULL;
   }
+  if (NULL != plc->mq)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "destroying MQ (guest_disconnected)\n");
+    GNUNET_MQ_destroy (plc->mq);
+    plc->mq = NULL;
+  }
 
   plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
                                                       guest_reconnect,
@@ -1664,46 +1682,39 @@ guest_connect (struct GNUNET_SOCIAL_Guest *gst)
 {
   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
 
-  GNUNET_MQ_hd_fixed_size (guest_enter_ack,
-                           GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
-                           struct GNUNET_PSYC_CountersResultMessage);
-
-  GNUNET_MQ_hd_var_size (guest_enter_decision,
-                         GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
-                         struct GNUNET_PSYC_JoinDecisionMessage);
-
-  GNUNET_MQ_hd_var_size (place_message,
-                         GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
-                         struct GNUNET_PSYC_MessageHeader);
-
-  GNUNET_MQ_hd_fixed_size (place_message_ack,
-                           GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
-                           struct GNUNET_MessageHeader);
-
-  GNUNET_MQ_hd_var_size (place_history_result,
-                         GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
-                         struct GNUNET_OperationResultMessage);
-
-  GNUNET_MQ_hd_var_size (place_state_result,
-                         GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
-                         struct GNUNET_OperationResultMessage);
-
-  GNUNET_MQ_hd_var_size (place_result,
-                         GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
-                         struct GNUNET_OperationResultMessage);
-
   struct GNUNET_MQ_MessageHandler handlers[] = {
-    make_guest_enter_ack_handler (gst),
-    make_guest_enter_decision_handler (gst),
-    make_place_message_handler (plc),
-    make_place_message_ack_handler (plc),
-    make_place_history_result_handler (plc),
-    make_place_state_result_handler (plc),
-    make_place_result_handler (plc),
+    GNUNET_MQ_hd_fixed_size (guest_enter_ack,
+                             GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
+                             struct GNUNET_PSYC_CountersResultMessage,
+                             gst),
+    GNUNET_MQ_hd_var_size (guest_enter_decision,
+                           GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
+                           struct GNUNET_PSYC_JoinDecisionMessage,
+                           gst),
+    GNUNET_MQ_hd_var_size (place_message,
+                           GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
+                           struct GNUNET_PSYC_MessageHeader,
+                           plc),
+    GNUNET_MQ_hd_fixed_size (place_message_ack,
+                             GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
+                             struct GNUNET_MessageHeader,
+                             plc),
+    GNUNET_MQ_hd_var_size (place_history_result,
+                           GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
+                           struct GNUNET_OperationResultMessage,
+                           plc),
+    GNUNET_MQ_hd_var_size (place_state_result,
+                           GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
+                           struct GNUNET_OperationResultMessage,
+                           plc),
+    GNUNET_MQ_hd_var_size (place_result,
+                           GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
+                           struct GNUNET_OperationResultMessage,
+                           plc),
     GNUNET_MQ_handler_end ()
   };
 
-  plc->mq = GNUNET_CLIENT_connecT (plc->cfg, "social",
+  plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
                                    handlers, guest_disconnected, gst);
   GNUNET_assert (NULL != plc->mq);
   plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
@@ -2034,7 +2045,14 @@ GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
                                 GNUNET_ContinuationCallback disconnect_cb,
                                 void *cls)
 {
-  place_disconnect (&gst->plc, disconnect_cb, cls);
+  struct GNUNET_SOCIAL_Place *plc = &gst->plc;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "GNUNET_SOCIAL_guest_disconnect, gst = %p\n",
+              gst);
+  plc->disconnect_cb = disconnect_cb;
+  plc->disconnect_cls = cls;
+  place_disconnect (plc);
 }
 
 
@@ -2116,7 +2134,7 @@ GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
   GNUNET_assert (NULL != method_prefix);
   struct MsgProcRequest *mpreq;
   uint16_t method_size = strnlen (method_prefix,
-                                  GNUNET_SERVER_MAX_MESSAGE_SIZE
+                                  GNUNET_MAX_MESSAGE_SIZE
                                   - sizeof (*mpreq)) + 1;
   GNUNET_assert ('\0' == method_prefix[method_size - 1]);
 
@@ -2165,7 +2183,7 @@ place_history_replay (struct GNUNET_SOCIAL_Place *plc,
 
   GNUNET_assert (NULL != method_prefix);
   uint16_t method_size = strnlen (method_prefix,
-                                  GNUNET_SERVER_MAX_MESSAGE_SIZE
+                                  GNUNET_MAX_MESSAGE_SIZE
                                   - sizeof (*req)) + 1;
   GNUNET_assert ('\0' == method_prefix[method_size - 1]);
 
@@ -2291,7 +2309,7 @@ place_state_get (struct GNUNET_SOCIAL_Place *plc,
   look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
 
   GNUNET_assert (NULL != name);
-  size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
+  size_t name_size = strnlen (name, GNUNET_MAX_MESSAGE_SIZE
                               - sizeof (*req)) + 1;
   struct GNUNET_MQ_Envelope *
     env = GNUNET_MQ_msg_extra (req, name_size, type);
@@ -2432,7 +2450,7 @@ GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
   size_t relay_size = relay_count * sizeof (*relays);
   size_t payload_size = name_size + password_size + relay_size;
 
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
+  if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
     return GNUNET_SYSERR;
 
   struct GNUNET_MQ_Envelope *
@@ -2512,7 +2530,7 @@ GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
   struct ZoneAddNymRequest *nreq;
 
   size_t name_size = strlen (name) + 1;
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
+  if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
     return GNUNET_SYSERR;
 
   struct GNUNET_MQ_Envelope *
@@ -2579,36 +2597,31 @@ app_disconnected (void *cls, enum GNUNET_MQ_Error error)
 static void
 app_connect (struct GNUNET_SOCIAL_App *app)
 {
-  GNUNET_MQ_hd_var_size (app_ego,
-                         GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
-                         struct AppEgoMessage);
-
-  GNUNET_MQ_hd_fixed_size (app_ego_end,
-                           GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
-                           struct GNUNET_MessageHeader);
-
-  GNUNET_MQ_hd_var_size (app_place,
-                         GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
-                         struct AppPlaceMessage);
-
-  GNUNET_MQ_hd_fixed_size (app_place_end,
-                           GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
-                           struct GNUNET_MessageHeader);
-
-  GNUNET_MQ_hd_var_size (app_result,
-                         GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
-                         struct GNUNET_OperationResultMessage);
-
   struct GNUNET_MQ_MessageHandler handlers[] = {
-    make_app_ego_handler (app),
-    make_app_ego_end_handler (app),
-    make_app_place_handler (app),
-    make_app_place_end_handler (app),
-    make_app_result_handler (app),
+    GNUNET_MQ_hd_var_size (app_ego,
+                           GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
+                           struct AppEgoMessage,
+                           app),
+    GNUNET_MQ_hd_fixed_size (app_ego_end,
+                             GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
+                             struct GNUNET_MessageHeader,
+                             app),
+    GNUNET_MQ_hd_var_size (app_place,
+                           GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
+                           struct AppPlaceMessage,
+                           app),
+    GNUNET_MQ_hd_fixed_size (app_place_end,
+                             GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
+                             struct GNUNET_MessageHeader,
+                             app),
+    GNUNET_MQ_hd_var_size (app_result,
+                           GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
+                           struct GNUNET_OperationResultMessage,
+                           app),
     GNUNET_MQ_handler_end ()
   };
 
-  app->mq = GNUNET_CLIENT_connecT (app->cfg, "social",
+  app->mq = GNUNET_CLIENT_connect (app->cfg, "social",
                                    handlers, app_disconnected, app);
   GNUNET_assert (NULL != app->mq);
   GNUNET_MQ_send_copy (app->mq, app->connect_env);
@@ -2709,11 +2722,10 @@ GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
 
   if (NULL != app->mq)
   {
-    struct GNUNET_MQ_Envelope *last = GNUNET_MQ_get_last_envelope (app->mq);
-    if (NULL != last)
+    struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
+    if (NULL != env)
     {
-      GNUNET_MQ_notify_sent (last,
-                             (GNUNET_MQ_NotifyCallback) app_cleanup, app);
+      GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) app_cleanup, app);
     }
     else
     {
@@ -2748,6 +2760,7 @@ GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
   struct GNUNET_MQ_Envelope *
     env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
   dreq->place_pub_key = plc->pub_key;
+  dreq->ego_pub_key = plc->ego_pub_key;
 
   GNUNET_MQ_send (app->mq, env);
 }