- tmp hack
[oweals/gnunet.git] / src / mesh / mesh2_api.c
index 90a216774ec3df8c251f0caa494db3a441a07a6e..80c1c03d4183b482f17e2f95c625bcc3200d522c 100644 (file)
 
 #define LOG(kind,...) GNUNET_log_from (kind, "mesh2-api",__VA_ARGS__)
 
+/* FIXME: use dynamic values, expose in API */
+#define INITIAL_WINDOW_SIZE 4
+#define ACK_THRESHOLD 2
+/* FIXME END */
+
 #define DEBUG_ACK GNUNET_YES
 
 /******************************************************************************/
@@ -117,6 +122,21 @@ struct GNUNET_MESH_Handle
      */
   const struct GNUNET_MESH_MessageHandler *message_handlers;
 
+  /**
+   * Number of handlers in the handlers array.
+   */
+  unsigned int n_handlers;
+
+  /**
+   * Ports open.
+   */
+  const uint32_t *ports;
+
+  /**
+   * Number of ports.
+   */
+  unsigned int n_ports;
+
     /**
      * Double linked list of the tunnels this client is connected to, head.
      */
@@ -162,11 +182,6 @@ struct GNUNET_MESH_Handle
      */
   MESH_TunnelNumber next_tid;
 
-    /**
-     * Number of handlers in the handlers array.
-     */
-  unsigned int n_handlers;
-
     /**
      * Have we started the task to receive messages from the service
      * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
@@ -264,6 +279,11 @@ struct GNUNET_MESH_Tunnel
      */
   MESH_TunnelNumber tid;
 
+    /**
+     * Port number.
+     */
+  uint32_t port;
+
     /**
      * Other end of the tunnel.
      */
@@ -285,24 +305,24 @@ struct GNUNET_MESH_Tunnel
   int buffering;
 
     /**
-     * Next packet ID to send.
+     * Maximum allowed PID to send (last ACK recevied).
      */
-  uint32_t next_send_pid;
+  uint32_t last_ack_recv;
 
     /**
-     * Maximum allowed PID to send (ACK recevied).
+     * Last PID received from the service.
      */
-  uint32_t max_send_pid;
+  uint32_t last_pid_recv;
 
-    /**
-     * Last pid received from the service.
-     */
-  uint32_t last_recv_pid;
+  /**
+   * Last packet ID sent to the service.
+   */
+  uint32_t last_pid_sent;
 
   /**
-   * Which ACK value have we last sent to the service?
+   * Last ACK value sent to the service: how much are we willing to accept?
    */
-  uint32_t max_recv_pid;
+  uint32_t last_ack_sent;
 };
 
 
@@ -362,13 +382,13 @@ message_ready_size (struct GNUNET_MESH_Handle *h)
     t = th->tunnel;
     if (GNUNET_NO == th_is_payload (th))
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  message internal\n");
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  message internal\n");
       return th->size;
     }
-    if (GNUNET_NO == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
+    if (GNUNET_YES == GMC_is_pid_bigger(t->last_ack_recv, t->last_pid_sent))
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  message payload ok (%u <= %u)\n",
-           t->next_send_pid, t->max_send_pid);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  message payload ok (%u <= %u)\n",
+           t->last_pid_sent, t->last_ack_recv);
       return th->size;
     }
   }
@@ -426,8 +446,10 @@ create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
   {
     t->tid = tid;
   }
-  t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
-  t->last_recv_pid = (uint32_t) -1;
+  t->last_ack_recv = (uint32_t) -1;
+  t->last_pid_recv = (uint32_t) -1;
+  t->last_ack_sent = (uint32_t) -1;
+  t->last_pid_sent = (uint32_t) -1;
   t->buffering = GNUNET_YES;
   return t;
 }
@@ -575,25 +597,25 @@ send_ack (struct GNUNET_MESH_Handle *h, struct GNUNET_MESH_Tunnel *t)
   struct GNUNET_MESH_LocalAck msg;
   uint32_t delta;
 
-  delta = t->max_recv_pid - t->last_recv_pid;
+  delta = t->last_ack_sent - t->last_pid_recv;
   if (delta > ACK_THRESHOLD)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Not sending ACK on tunnel %X: ACK: %u, PID: %u, buffer %u\n",
-         t->tid, t->max_recv_pid, t->last_recv_pid, delta);
+         t->tid, t->last_ack_sent, t->last_pid_recv, delta);
     return;
   }
   if (GNUNET_YES == t->buffering)
-    t->max_recv_pid = t->last_recv_pid + INITIAL_WINDOW_SIZE;
+    t->last_ack_sent = t->last_pid_recv + INITIAL_WINDOW_SIZE;
   else
-    t->max_recv_pid = t->last_recv_pid + 1;
+    t->last_ack_sent = t->last_pid_recv + 1;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Sending ACK on tunnel %X: %u\n",
-       t->tid, t->max_recv_pid);
+       t->tid, t->last_ack_sent);
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
   msg.header.size = htons (sizeof (msg));
   msg.tunnel_id = htonl (t->tid);
-  msg.max_pid = htonl (t->max_recv_pid);
+  msg.max_pid = htonl (t->last_ack_sent);
 
 #if DEBUG_ACK
   t->mesh->acks_sent++;
@@ -628,28 +650,27 @@ send_connect (struct GNUNET_MESH_Handle *h)
   size_t size;
 
   size = sizeof (struct GNUNET_MESH_ClientConnect);
-  size += h->n_handlers * sizeof (uint16_t);
+  size += h->n_ports * sizeof (uint32_t);
   {
     char buf[size] GNUNET_ALIGN;
     struct GNUNET_MESH_ClientConnect *msg;
-    uint16_t *types;
-    uint16_t ntypes;
+    uint32_t *ports;
+    uint16_t i;
 
     /* build connection packet */
     msg = (struct GNUNET_MESH_ClientConnect *) buf;
     msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
     msg->header.size = htons (size);
-    types = (uint16_t *) & msg[1];
-    for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
+    ports = (uint32_t *) &msg[1];
+    for (i = 0; i < h->n_ports; i++)
     {
-      types[ntypes] = htons (h->message_handlers[ntypes].type);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
-           h->message_handlers[ntypes].type);
+      ports[i] = htonl (h->ports[i]);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
+           h->ports[i]);
     }
-    msg->types = htons (ntypes);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Sending %lu bytes long message %d types\n",
-         ntohs (msg->header.size), ntypes);
+         "Sending %lu bytes long message with %u ports\n",
+         ntohs (msg->header.size), h->n_ports);
     send_packet (h, &msg->header, NULL);
   }
 }
@@ -720,9 +741,10 @@ do_reconnect (struct GNUNET_MESH_Handle *h)
        */
       continue;
     }
-    t->next_send_pid = 0;
-    t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
-    t->last_recv_pid = (uint32_t) -1;
+    t->last_ack_sent = (uint32_t) -1;
+    t->last_pid_sent = (uint32_t) -1;
+    t->last_ack_recv = (uint32_t) -1;
+    t->last_pid_recv = (uint32_t) -1;
     tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
     tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
     tmsg.tunnel_id = htonl (t->tid);
@@ -799,16 +821,18 @@ process_tunnel_created (struct GNUNET_MESH_Handle *h,
   if (NULL != h->new_tunnel)
   {
     t = create_tunnel (h, tid);
+    t->last_ack_sent = INITIAL_WINDOW_SIZE - 1;
     t->peer = GNUNET_PEER_intern (&msg->peer);
     GNUNET_PEER_change_rc (t->peer, 1);
     t->mesh = h;
     t->tid = tid;
-    if ((msg->opt & MESH_TUNNEL_OPT_NOBUFFER) != 0)
+    t->port = ntohl (msg->port);
+    if (0 != (msg->opt & MESH_TUNNEL_OPT_NOBUFFER))
       t->buffering = GNUNET_NO;
     else
       t->buffering = GNUNET_YES;
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  created tunnel %p\n", t);
-    t->ctx = h->new_tunnel (h->cls, t, &msg->peer);
+    t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
     LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
   }
   else
@@ -913,16 +937,16 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
     return GNUNET_YES;
   }
   if (GNUNET_YES ==
-      GMC_is_pid_bigger(pid, t->max_recv_pid))
+      GMC_is_pid_bigger(pid, t->last_ack_sent))
   {
     GNUNET_break (0);
     LOG (GNUNET_ERROR_TYPE_WARNING,
-         "  unauthorized message! (%u, max %u)\n",
-         pid, t->max_recv_pid);
+         "  unauthorized message! (%u, ACK %u)\n",
+         pid, t->last_ack_sent);
     // FIXME fc what now? accept? reject?
     return GNUNET_YES;
   }
-  t->last_recv_pid = pid;
+  t->last_pid_recv = pid;
   type = ntohs (payload->type);
   send_ack (h, t);
   for (i = 0; i < h->n_handlers; i++)
@@ -978,8 +1002,8 @@ process_ack (struct GNUNET_MESH_Handle *h,
   }
   ack = ntohl (msg->max_pid);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %X, ack %u!\n", t->tid, ack);
-  if (GNUNET_YES == GMC_is_pid_bigger(ack, t->max_send_pid))
-    t->max_send_pid = ack;
+  if (GNUNET_YES == GMC_is_pid_bigger(ack, t->last_ack_recv))
+    t->last_ack_recv = ack;
   else
     return;
   if (NULL == h->th && 0 < t->packet_size)
@@ -1173,10 +1197,10 @@ send_callback (void *cls, size_t size, void *buf)
   size_t nsize;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
   if ((0 == size) || (NULL == buf))
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL send callback on %p\n", h);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL send callback on %p\n", h);
     reconnect (h);
     h->th = NULL;
     return 0;
@@ -1189,8 +1213,8 @@ send_callback (void *cls, size_t size, void *buf)
     t = th->tunnel;
     if (GNUNET_YES == th_is_payload (th))
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " payload\n");
-      if (GNUNET_YES == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " payload\n");
+      if (GNUNET_NO == GMC_is_pid_bigger(t->last_ack_recv, t->last_pid_sent))
       {
         /* This tunnel is not ready to transmit yet, try next message */
         next = th->next;
@@ -1206,7 +1230,7 @@ send_callback (void *cls, size_t size, void *buf)
         GNUNET_assert (size >= th->size);
         mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
         psize = th->notify (th->notify_cls, size - sizeof (to), mh);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "  to origin, type %s\n",
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "#  to origin, type %s\n",
              GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
         if (psize > 0)
         {
@@ -1215,7 +1239,7 @@ send_callback (void *cls, size_t size, void *buf)
           to.header.size = htons (psize);
           to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
           to.tid = htonl (t->tid);
-          to.pid = htonl (t->next_send_pid);
+          to.pid = htonl (t->last_pid_sent + 1);
           to.ttl = 0;
           memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
           memcpy (cbuf, &to, sizeof (to));
@@ -1230,7 +1254,7 @@ send_callback (void *cls, size_t size, void *buf)
         GNUNET_assert (size >= th->size);
         mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
         psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "  unicast, type %s\n",
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "#  unicast, type %s\n",
              GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
         if (psize > 0)
         {
@@ -1239,18 +1263,19 @@ send_callback (void *cls, size_t size, void *buf)
           uc.header.size = htons (psize);
           uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
           uc.tid = htonl (t->tid);
-          uc.pid = htonl (t->next_send_pid);
+          uc.pid = htonl (t->last_pid_sent + 1);
           uc.ttl = 0;
           memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
+          memcpy (cbuf, &uc, sizeof (uc));
         }
       }
-      t->next_send_pid++;
+      t->last_pid_sent++;
     }
     else
     {
       struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
 
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  mesh traffic, type %s\n",
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  mesh traffic, type %s\n",
            GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
       memcpy (cbuf, &th[1], th->size);
       psize = th->size;
@@ -1265,12 +1290,12 @@ send_callback (void *cls, size_t size, void *buf)
     size -= psize;
     tsize += psize;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  total size: %u\n", tsize);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "#  total size: %u\n", tsize);
   h->th = NULL;
   size = message_ready_size (h);
   if (0 != size)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  next size: %u\n", size);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "#  next size: %u\n", size);
     h->th =
         GNUNET_CLIENT_notify_transmit_ready (h->client, size,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
@@ -1279,18 +1304,18 @@ send_callback (void *cls, size_t size, void *buf)
   else
   {
     if (NULL != h->th_head)
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  can't transmit any more\n");
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  can't transmit any more\n");
     else
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  nothing left to transmit\n");
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  nothing left to transmit\n");
   }
   if (GNUNET_NO == h->in_receive)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " start receiving from service\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
     h->in_receive = GNUNET_YES;
     GNUNET_CLIENT_receive (h->client, &msg_received, h,
                            GNUNET_TIME_UNIT_FOREVER_REL);
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
   return tsize;
 }
 
@@ -1340,7 +1365,8 @@ struct GNUNET_MESH_Handle *
 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
                      GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
                      GNUNET_MESH_TunnelEndHandler cleaner,
-                     const struct GNUNET_MESH_MessageHandler *handlers)
+                     const struct GNUNET_MESH_MessageHandler *handlers,
+                     const uint32_t *ports)
 {
   struct GNUNET_MESH_Handle *h;
 
@@ -1359,6 +1385,7 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
   }
   h->cls = cls;
   h->message_handlers = handlers;
+  h->ports = ports;
   h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
   h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
@@ -1367,6 +1394,9 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
   for (h->n_handlers = 0;
        handlers && handlers[h->n_handlers].type;
        h->n_handlers++) ;
+  for (h->n_ports = 0;
+       ports && ports[h->n_ports];
+       h->n_ports++) ;
   send_connect (h);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
   return h;
@@ -1455,7 +1485,8 @@ GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
 struct GNUNET_MESH_Tunnel *
 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, 
                            void *tunnel_ctx,
-                           const struct GNUNET_PeerIdentity *peer)
+                           const struct GNUNET_PeerIdentity *peer,
+                           uint32_t port)
 {
   struct GNUNET_MESH_Tunnel *t;
   struct GNUNET_MESH_TunnelMessage msg;
@@ -1468,6 +1499,7 @@ GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
   msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
   msg.tunnel_id = htonl (t->tid);
+  msg.port = htonl (port);
   msg.peer = *peer;
   send_packet (h, &msg.header, t);
   return t;
@@ -1531,7 +1563,6 @@ GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer)
 
   h = tunnel->mesh;
   tunnel->buffering = buffer;
-  tunnel->max_send_pid = tunnel->next_send_pid;
 
   if (GNUNET_YES == buffer)
     msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER);
@@ -1578,9 +1609,9 @@ GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
   add_to_queue (tunnel->mesh, th);
   if (NULL != tunnel->mesh->th)
     return th;
-  if (GMC_is_pid_bigger(tunnel->next_send_pid, tunnel->max_send_pid))
+  if (!GMC_is_pid_bigger (tunnel->last_ack_recv, tunnel->last_pid_sent))
     return th;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    call notify tmt rdy\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    call client notify tmt rdy\n");
   tunnel->mesh->th =
       GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
                                            GNUNET_TIME_UNIT_FOREVER_REL,