- use strings
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh.c
index 419cc19daf49cc54550a4c2b00697d190f701f1d..4cd0aff8e1318c0488fd32f89f5bb5b822ecf028 100644 (file)
@@ -1704,55 +1704,6 @@ announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /******************      GENERAL HELPER FUNCTIONS      ************************/
 /******************************************************************************/
 
-/**
- * Check if one pid is bigger than other, accounting for overflow.
- *
- * @param bigger Argument that should be bigger.
- * @param smaller Argument that should be smaller.
- *
- * @return True if bigger (arg1) has a higher value than smaller (arg 2).
- */
-static int
-is_pid_bigger (uint32_t bigger, uint32_t smaller)
-{
-    return (GNUNET_YES == PID_OVERFLOW(smaller, bigger) ||
-            (bigger > smaller && GNUNET_NO == PID_OVERFLOW(bigger, smaller)));
-}
-
-/**
- * Get the higher ACK value out of two values, taking in account overflow.
- *
- * @param a First ACK value.
- * @param b Second ACK value.
- *
- * @return Highest ACK value from the two.
- */
-static uint32_t
-max_pid (uint32_t a, uint32_t b)
-{
-  if (is_pid_bigger(a, b))
-    return a;
-  return b;
-}
-
-
-/**
- * Get the lower ACK value out of two values, taking in account overflow.
- *
- * @param a First ACK value.
- * @param b Second ACK value.
- *
- * @return Lowest ACK value from the two.
- */
-static uint32_t
-min_pid (uint32_t a, uint32_t b)
-{
-  if (is_pid_bigger(a, b))
-    return b;
-  return a;
-}
-
-
 /**
  * Decrements the reference counter and frees all resources if needed
  *
@@ -1981,7 +1932,8 @@ send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
 
   type = ntohs (payload->type);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
+              GNUNET_MESH_DEBUG_M2S (type));
 
   memcpy (cbuf, msg, sizeof (cbuf));
   switch (htons (msg->type))
@@ -3258,6 +3210,7 @@ tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
   clinfo.fwd_ack = t->fwd_pid + 1;
   clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
   clinfo.fwd_pid = t->fwd_pid;
+  clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
   t->nclients--;
   GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
 }
@@ -3481,6 +3434,31 @@ tunnel_get_neighbor_fc (const struct MeshTunnel *t,
 }
 
 
+/**
+ * Get the Flow Control info of a client.
+ * 
+ * @param t Tunnel on which to look.
+ * @param c Client whose ACK to get.
+ * 
+ * @return ACK value.
+ */
+static struct MeshTunnelClientInfo *
+tunnel_get_client_fc (struct MeshTunnel *t,
+                      struct MeshClient *c)
+{
+  unsigned int i;
+
+  for (i = 0; i < t->nclients; i++)
+  {
+    if (t->clients[i] != c)
+      continue;
+    return &t->clients_fc[i];
+  }
+  GNUNET_assert (0);
+  return NULL; // avoid compiler / coverity complaints
+}
+
+
 /**
  * Iterator to get the appropiate ACK value from all children nodes.
  *
@@ -3540,7 +3518,7 @@ tunnel_get_children_fwd_ack (struct MeshTunnel *t)
   if (0 == ctx.nchildren)
     return -1LL;
 
-  if (GNUNET_YES == t->nobuffer && is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
+  if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
     ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
 
   return (int64_t) ctx.max_child_ack;
@@ -3572,31 +3550,6 @@ tunnel_set_client_fwd_ack (struct MeshTunnel *t,
 }
 
 
-/**
- * Get the ACK value of a client in a particular tunnel.
- * 
- * @param t Tunnel on which to look.
- * @param c Client whose ACK to get.
- * 
- * @return ACK value.
- */
-uint32_t // FIXME fc Is this funcion needed anymore?
-tunnel_get_client_fwd_ack (struct MeshTunnel *t,
-                       struct MeshClient *c)
-{
-  unsigned int i;
-
-  for (i = 0; i < t->nclients; i++)
-  {
-    if (t->clients[i] != c)
-      continue;
-    return t->clients_fc[i].fwd_ack;
-  }
-  GNUNET_break (0);
-  return UINT32_MAX;
-}
-
-
 /**
  * Get the highest ACK value of all clients in a particular tunnel,
  * according to the buffering/speed settings.
@@ -3619,15 +3572,15 @@ tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
   {
     if (-1 == ack ||
         (GNUNET_YES == t->speed_min &&
-         GNUNET_YES == is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
+         GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
         (GNUNET_NO == t->speed_min &&
-         GNUNET_YES == is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
+         GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
     {
       ack = t->clients_fc[i].fwd_ack;
     }
   }
 
-  if (GNUNET_YES == t->nobuffer && is_pid_bigger(ack, t->fwd_pid))
+  if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
     ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
 
   return (uint32_t) ack;
@@ -3665,15 +3618,15 @@ tunnel_get_fwd_ack (struct MeshTunnel *t)
 
   if (GNUNET_YES == t->speed_min)
   {
-    ack = min_pid ((uint32_t) child_ack, ack);
-    ack = min_pid ((uint32_t) client_ack, ack);
+    ack = GMC_min_pid ((uint32_t) child_ack, ack);
+    ack = GMC_min_pid ((uint32_t) client_ack, ack);
   }
   else
   {
-    ack = max_pid ((uint32_t) child_ack, ack);
-    ack = max_pid ((uint32_t) client_ack, ack);
+    ack = GMC_max_pid ((uint32_t) child_ack, ack);
+    ack = GMC_max_pid ((uint32_t) client_ack, ack);
   }
-  if (GNUNET_YES == t->nobuffer && is_pid_bigger(ack, t->fwd_pid))
+  if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
     ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
               count, buffer_free, child_ack, client_ack, ack);
@@ -3825,7 +3778,7 @@ tunnel_send_child_bck_ack (void *cls,
   cinfo = tunnel_get_neighbor_fc (t, &peer);
 
   if (cinfo->bck_ack != cinfo->pid &&
-      GNUNET_NO == is_pid_bigger (cinfo->bck_ack, cinfo->pid))
+      GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
     return;
 
   cinfo->bck_ack++;
@@ -4326,14 +4279,15 @@ send_core_data_multicast (void *cls, size_t size, void *buf)
       mc = (struct GNUNET_MESH_Multicast *) mh;
       mh = (struct GNUNET_MessageHeader *) &mc[1];
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  " multicast, payload type %u\n", ntohs (mh->type));
+                  " multicast, payload type %s\n",
+                  GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   " multicast, payload size %u\n", ntohs (mh->size));
     }
     else
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
-                  ntohs (mh->type));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
+                  GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
     }
   }
 #endif
@@ -5040,8 +4994,8 @@ handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
     return GNUNET_OK;
   }
   msg = (struct GNUNET_MESH_Unicast *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
-              ntohs (msg[1].header.type));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
+              GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
   /* Check tunnel */
   t = tunnel_get (&msg->oid, ntohl (msg->tid));
   if (NULL == t)
@@ -5066,7 +5020,7 @@ handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
   }
   t->skip += (pid - t->fwd_pid) - 1;
   t->fwd_pid = pid;
-  if (is_pid_bigger (pid, t->last_fwd_ack))
+  if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
   {
     GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
     GNUNET_break_op (0);
@@ -5101,7 +5055,7 @@ handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
                                          &tunnel_add_skip,
                                          &neighbor);
-  if (is_pid_bigger (pid, cinfo->fwd_ack))
+  if (GMC_is_pid_bigger (pid, cinfo->fwd_ack))
   {
     GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
     GNUNET_break_op (0);
@@ -5232,8 +5186,8 @@ handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
     return GNUNET_OK;
   }
   msg = (struct GNUNET_MESH_ToOrigin *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
-              ntohs (msg[1].header.type));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
+              GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
   t = tunnel_get (&msg->oid, ntohl (msg->tid));
 
   if (NULL == t)
@@ -5304,8 +5258,8 @@ handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
               GNUNET_i2s (peer));
   msg = (struct GNUNET_MESH_ACK *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
-              ntohs (msg[1].header.type));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
+              GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
   t = tunnel_get (&msg->oid, ntohl (msg->tid));
 
   if (NULL == t)
@@ -6858,6 +6812,16 @@ handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
+  /* PID should be as expected */
+  if (ntohl (data_msg->pid) != t->fwd_pid + 1)
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Unicast PID, expected %u, got %u\n",
+              t->fwd_pid + 1, ntohl (data_msg->pid));
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
 
   /* Ok, everything is correct, send the message
    * (pretend we got it from a mesh peer)
@@ -6872,13 +6836,6 @@ handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
     copy->oid = my_full_id;
     copy->tid = htonl (t->id.tid);
     copy->ttl = htonl (default_ttl);
-    if (ntohl (copy->pid) != t->fwd_pid + 1)
-    {
-      GNUNET_break (0);
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Unicast PID, expected %u, got %u\n",
-                t->fwd_pid + 1, ntohl (copy->pid));
-    }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "  calling generic handler...\n");
     handle_mesh_data_unicast (NULL, &my_full_id, &copy->header, NULL, 0);
@@ -6901,7 +6858,7 @@ handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
                         const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_ToOrigin *data_msg;
-  struct GNUNET_PeerIdentity id;
+  struct MeshTunnelClientInfo *clinfo;
   struct MeshClient *c;
   struct MeshTunnel *t;
   MESH_TunnelNumber tid;
@@ -6950,7 +6907,18 @@ handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  GNUNET_PEER_resolve (t->id.oid, &id);
+
+  /* PID should be as expected */
+  clinfo = tunnel_get_client_fc (t, c);
+  if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "To Origin PID, expected %u, got %u\n",
+                clinfo->bck_pid + 1, ntohl (data_msg->pid));
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
 
   /* Ok, everything is correct, send the message
    * (pretend we got it from a mesh peer)
@@ -6962,7 +6930,7 @@ handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
     /* Work around const limitation */
     copy = (struct GNUNET_MESH_ToOrigin *) buf;
     memcpy (buf, data_msg, size);
-    copy->oid = id;
+    GNUNET_PEER_resolve (t->id.oid, &copy->oid);
     copy->tid = htonl (t->id.tid);
     copy->ttl = htonl (default_ttl);
     GNUNET_assert (ntohl (copy->pid) == (t->bck_pid + 1));
@@ -7030,6 +6998,17 @@ handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
+  /* PID should be as expected */
+  if (ntohl (data_msg->pid) != t->fwd_pid + 1)
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Multicast PID, expected %u, got %u\n",
+              t->fwd_pid + 1, ntohl (data_msg->pid));
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
   {
     char buf[ntohs (message->size)] GNUNET_ALIGN;
     struct GNUNET_MESH_Multicast *copy;