- fix coverity
[oweals/gnunet.git] / src / cadet / cadet_api.c
index 353dd13c9f33329dbb5a129f39b1aa0dac096ce3..960f4788c2321a3730f46d35cc704125cd925b10 100644 (file)
@@ -1,20 +1,22 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011 GNUnet e.V.
+
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
      by the Free Software Foundation; either version 3, or (at your
      option) any later version.
+
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      General Public License for more details.
+
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
      Boston, MA 02110-1301, USA.
 */
-
 /**
  * @file cadet/cadet_api.c
  * @brief cadet api: client implementation of new cadet service
@@ -513,10 +515,9 @@ destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
  * Notify client that the transmission has timed out
  *
  * @param cls closure
- * @param tc task context
  */
 static void
-timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+timeout_transmission (void *cls)
 {
   struct GNUNET_CADET_TransmitHandle *th = cls;
   struct GNUNET_CADET_Handle *cadet = th->channel->cadet;
@@ -594,12 +595,12 @@ send_ack (struct GNUNET_CADET_Channel *ch)
 
 /**
  * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
+ * reconnection.
+ *
  * @param cls closure (cadet handle)
- * @param tc task context
  */
 static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+reconnect_cbk (void *cls);
 
 
 /**
@@ -696,17 +697,15 @@ do_reconnect (struct GNUNET_CADET_Handle *h)
 /**
  * Reconnect callback: tries to reconnect again after a failer previous
  * reconnecttion
+ *
  * @param cls closure (cadet handle)
- * @param tc task context
  */
 static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+reconnect_cbk (void *cls)
 {
   struct GNUNET_CADET_Handle *h = cls;
 
   h->reconnect_task = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   do_reconnect (h);
 }
 
@@ -717,7 +716,7 @@ reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *
  * @param h handle to the cadet
  *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
+ * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
  */
 static void
 reconnect (struct GNUNET_CADET_Handle *h)
@@ -811,8 +810,8 @@ process_channel_destroy (struct GNUNET_CADET_Handle *h,
   struct GNUNET_CADET_Channel *ch;
   CADET_ChannelNumber chid;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
   chid = ntohl (msg->channel_id);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X Destroy from service\n", chid);
   ch = retrieve_channel (h, chid);
 
   if (NULL == ch)
@@ -820,7 +819,6 @@ process_channel_destroy (struct GNUNET_CADET_Handle *h,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
   destroy_channel (ch, GNUNET_YES);
 }
 
@@ -1058,11 +1056,17 @@ static void
 process_get_peer (struct GNUNET_CADET_Handle *h,
                   const struct GNUNET_MessageHeader *message)
 {
-  struct GNUNET_CADET_LocalInfoTunnel *msg;
+  struct GNUNET_CADET_LocalInfoPeer *msg;
+  struct GNUNET_PeerIdentity *id;
+  unsigned int epaths;
+  unsigned int paths;
+  unsigned int path_length;
+  unsigned int i;
+  int neighbor;
   size_t esize;
   size_t msize;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Info Peer messasge received\n");
   if (NULL == h->info_cb.peer_cb)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
@@ -1070,27 +1074,60 @@ process_get_peer (struct GNUNET_CADET_Handle *h,
   }
 
   /* Verify message sanity */
-  msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
-  msize = ntohs (message->size);
-  esize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
-  if (esize > msize)
+  msg = (struct GNUNET_CADET_LocalInfoPeer *) message;
+  esize = ntohs (message->size);
+  msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
+  if (esize < msize)
   {
     GNUNET_break_op (0);
     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
     goto clean_cls;
   }
-//   esize += ch_n * sizeof (CADET_ChannelNumber);
-//   esize += c_n * sizeof (struct GNUNET_CADET_Hash);
+  epaths = (unsigned int) ntohs (msg->paths);
+  paths = 0;
+  path_length = 0;
+  neighbor = GNUNET_NO;
+  id = (struct GNUNET_PeerIdentity *) &msg[1];
+  for (i = 0; msize < esize; i++)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
+    msize += sizeof (struct GNUNET_PeerIdentity);
+    path_length++;
+    if (0 == memcmp (&id[i], &msg->destination,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+      if (1 == path_length)
+        neighbor = GNUNET_YES;
+      path_length = 0;
+      paths++;
+    }
+  }
   if (msize != esize)
   {
     GNUNET_break_op (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "m:%u, e: %u\n", msize, esize);
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "m:%u, e: %u\n",
+                (unsigned int) msize,
+                (unsigned int) esize);
+    h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
+    goto clean_cls;
+  }
+  if (paths != epaths)
+  {
+    GNUNET_break_op (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
     goto clean_cls;
   }
 
   /* Call Callback with tunnel info. */
-  h->info_cb.peer_cb (h->info_cls, &msg->destination, 0, 0, 0, NULL);
+  id = (struct GNUNET_PeerIdentity *) &msg[1];
+  h->info_cb.peer_cb (h->info_cls,
+                      &msg->destination,
+                      (int) ntohs (msg->tunnel),
+                      neighbor,
+                      paths,
+                      id);
 
   clean_cls:
   h->info_cb.peer_cb = NULL;
@@ -1178,11 +1215,17 @@ process_get_tunnel (struct GNUNET_CADET_Handle *h,
   if (msize != esize)
   {
     GNUNET_break_op (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
-                msize, esize, ch_n, c_n);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
-                sizeof (struct GNUNET_CADET_LocalInfoTunnel),
-                sizeof (CADET_ChannelNumber), sizeof (struct GNUNET_HashCode));
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "m:%u, e: %u (%u ch, %u conn)\n",
+                (unsigned int) msize,
+                (unsigned int) esize,
+                ch_n,
+                c_n);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "%u (%u ch, %u conn)\n",
+                (unsigned int) sizeof (struct GNUNET_CADET_LocalInfoTunnel),
+                (unsigned int) sizeof (CADET_ChannelNumber),
+                (unsigned int) sizeof (struct GNUNET_HashCode));
     h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
     goto clean_cls;
   }
@@ -1549,8 +1592,8 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
         break;
       default:
         GNUNET_break (0);
-        LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
-             ntohs(msg->type));
+        LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
+             GC_m2s (ntohs(msg->type)));
     }
 
     GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
@@ -1648,6 +1691,8 @@ GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
       {
         /* applications should cancel before destroying channel */
         GNUNET_break (0);
+        LOG (GNUNET_ERROR_TYPE_WARNING,
+             "Channel destroyed without cancelling transmission requests\n");
         th->notify (th->notify_cls, 0, NULL);
       }
       GNUNET_CADET_notify_transmit_ready_cancel (th);
@@ -2134,4 +2179,3 @@ GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
                                       NULL); /* no handler cls */
   return mq;
 }
-