xvine:fixes
[oweals/gnunet.git] / src / dv / gnunet-service-dv.c
index 1c185197a15eb828e061e11c54f63fee2fda62b0..abade848fe77017d20bfaf0231d0dca918d9f7d5 100644 (file)
@@ -341,7 +341,7 @@ static struct GNUNET_CONTAINER_MultiPeerMap *direct_neighbors;
 /**
  * Hashmap with all routes that we currently support; contains
  * routing information for all peers from distance 2
- * up to distance DEFAULT_FISHEYE_DEPTH.
+ * up to distance #DEFAULT_FISHEYE_DEPTH.
  */
 static struct GNUNET_CONTAINER_MultiPeerMap *all_routes;
 
@@ -637,7 +637,7 @@ core_transmit_notify (void *cls, size_t size, void *buf)
     dn->cth =
       GNUNET_CORE_notify_transmit_ready (core_api,
                                         GNUNET_YES /* cork */,
-                                        0 /* priority */,
+                                        GNUNET_CORE_PRIO_BEST_EFFORT,
                                         GNUNET_TIME_UNIT_FOREVER_REL,
                                         &dn->peer,
                                         msize,
@@ -650,7 +650,7 @@ core_transmit_notify (void *cls, size_t size, void *buf)
  * Forward the given payload to the given target.
  *
  * @param target where to send the message
- * @param distance expected (remaining) distance to the target
+ * @param distance distance to the @a sender
  * @param uid unique ID for the message
  * @param sender original sender of the message
  * @param actual_target ultimate recipient for the message
@@ -705,7 +705,7 @@ forward_payload (struct DirectNeighbor *target,
   if (NULL == target->cth)
     target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
                                                     GNUNET_YES /* cork */,
-                                                    0 /* priority */,
+                                                    GNUNET_CORE_PRIO_BEST_EFFORT,
                                                     GNUNET_TIME_UNIT_FOREVER_REL,
                                                     &target->peer,
                                                     msize,
@@ -1302,7 +1302,10 @@ handle_direct_disconnect (struct DirectNeighbor *neighbor)
  *
  * @param cls closure
  * @param address the address
- * @param active is this address in active use
+ * @param active #GNUNET_YES if this address is actively used
+ *        to maintain a connection to a peer;
+ *        #GNUNET_NO if the address is not actively used;
+ *        #GNUNET_SYSERR if this address is no longer available for ATS
  * @param bandwidth_out assigned outbound bandwidth for the connection
  * @param bandwidth_in assigned inbound bandwidth for the connection
  * @param ats performance data for the address (as far as known)
@@ -1321,8 +1324,17 @@ handle_ats_update (void *cls,
   uint32_t distance;
   enum GNUNET_ATS_Network_Type network = GNUNET_ATS_NET_UNSPECIFIED;
 
-  if (GNUNET_NO == active)
+  if (NULL == address)
+  {
+    /* ATS service temporarily disconnected */
+    return;
+  }
+
+  if (GNUNET_YES != active)
+  {
+    // FIXME: handle disconnect/inactive case too!
     return;
+  }
   distance = get_atsi_distance (ats, ats_count);
   network = get_atsi_network (ats, ats_count);
   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
@@ -1539,6 +1551,7 @@ handle_set_union_result (void *cls,
                         enum GNUNET_SET_Status status)
 {
   struct DirectNeighbor *neighbor = cls;
+  struct DirectNeighbor *dn;
   struct Target *target;
   char *status_str;
 
@@ -1575,9 +1588,7 @@ handle_set_union_result (void *cls,
       GNUNET_break_op (0);
       return;
     }
-    if (GNUNET_YES ==
-        GNUNET_CONTAINER_multipeermap_contains (direct_neighbors,
-                                                &((struct Target *) element->data)->peer))
+    if ( (NULL != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors, &((struct Target *) element->data)->peer))) && (DIRECT_NEIGHBOR_COST == dn->distance) )
     {
       /* this is a direct neighbor of ours, we do not care about routes
          to this peer */
@@ -1775,6 +1786,7 @@ handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
   const struct GNUNET_MessageHeader *payload;
   struct Route *route;
   struct DirectNeighbor *neighbor;
+  struct DirectNeighbor *dn;
   struct Target *target;
   uint32_t distance;
   char me[5];
@@ -1788,34 +1800,47 @@ handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
     return GNUNET_SYSERR;
   }
   rm = (const struct RouteMessage *) message;
+  distance = ntohl (rm->distance);
+  payload = (const struct GNUNET_MessageHeader *) &rm[1];
+  if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
   strncpy (prev, GNUNET_i2s (peer), 4);
   strncpy (me, GNUNET_i2s (&my_identity), 4);
   strncpy (src, GNUNET_i2s (&rm->sender), 4);
   strncpy (dst, GNUNET_i2s (&rm->target), 4);
   prev[4] = me[4] = src[4] = dst[4] = '\0';
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Handling DV message from %s to %s routed by %s to me (%s @ %u)\n",
+             "Handling DV message with %u bytes payload of type %u from %s to %s routed by %s to me (%s @ hop %u)\n",
+              ntohs (message->size) - sizeof (struct RouteMessage),
+              ntohs (payload->type),
               src, dst,
               prev, me,
-              (unsigned int) ntohl (rm->distance));
+              (unsigned int) distance + 1);
 
-  payload = (const struct GNUNET_MessageHeader *) &rm[1];
-  if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
   if (0 == memcmp (&rm->target,
                   &my_identity,
                   sizeof (struct GNUNET_PeerIdentity)))
   {
+    if ((NULL
+        != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
+            &rm->sender))) && (DIRECT_NEIGHBOR_COST == dn->distance))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Discarding DV message, as %s is a direct neighbor\n",
+                  GNUNET_i2s (&rm->sender));
+      GNUNET_STATISTICS_update (stats,
+                                "# messages discarded (direct neighbor)",
+                                1, GNUNET_NO);
+      return GNUNET_OK;
+    }
     /* message is for me, check reverse route! */
     route = GNUNET_CONTAINER_multipeermap_get (all_routes,
                                               &rm->sender);
     if ( (NULL == route) &&
-         (NULL == GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
-                                                     &rm->sender)) &&
-         (ntohl (rm->distance) < DEFAULT_FISHEYE_DEPTH) )
+         (distance < DEFAULT_FISHEYE_DEPTH) )
     {
       /* don't have reverse route yet, learn it! */
       neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
@@ -1827,11 +1852,11 @@ handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
       }
       target = GNUNET_new (struct Target);
       target->peer = rm->sender;
-      target->distance = htonl (ntohl (rm->distance));
+      target->distance = htonl (distance);
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Learning target %s at distance %u from delivery!\n",
+                  "Learning sender %s at distance %u from delivery!\n",
                   GNUNET_i2s (&rm->sender),
-                  1 + ntohl (rm->distance));
+                  (unsigned int) distance + 1);
       if (NULL == neighbor->neighbor_table)
         neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
       if (GNUNET_YES !=
@@ -1842,15 +1867,17 @@ handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
       {
         GNUNET_break_op (0);
         GNUNET_free (target);
+        return GNUNET_SYSERR;
       }
       add_new_route (target, neighbor);
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Delivering %u bytes to myself!\n",
-               ntohs (payload->size));
+               "Delivering %u bytes from %s to myself!\n",
+               ntohs (payload->size),
+                GNUNET_i2s (&rm->sender));
     send_data_to_plugin (payload,
                         &rm->sender,
-                        1 + ntohl (rm->distance));
+                        1 + distance);
     return GNUNET_OK;
   }
   if ( (NULL == GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
@@ -1859,9 +1886,9 @@ handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
                                                    &rm->sender)) )
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Learning target %s at distance %u from forwarding!\n",
+                "Learning sender %s at distance %u from forwarding!\n",
                 GNUNET_i2s (&rm->sender),
-                1 + ntohl (rm->distance));
+                1 + distance);
     neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
                                                   peer);
     if (NULL == neighbor)
@@ -1871,7 +1898,7 @@ handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
     }
     target = GNUNET_new (struct Target);
     target->peer = rm->sender;
-    target->distance = htonl (ntohl (rm->distance));
+    target->distance = htonl (distance);
     if (NULL == neighbor->neighbor_table)
       neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
     if (GNUNET_YES !=
@@ -1903,28 +1930,16 @@ handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
                                 1, GNUNET_NO);
       return GNUNET_OK;
     }
-    distance = DIRECT_NEIGHBOR_COST;
   }
   else
   {
-    if (ntohl (route->target.distance) > ntohl (rm->distance) + 1)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Distance too far, not routing %u bytes!\n",
-                  ntohs (payload->size));
-      GNUNET_STATISTICS_update (stats,
-                                "# messages discarded (target too far)",
-                                1, GNUNET_NO);
-      return GNUNET_OK;
-    }
     neighbor = route->next_hop;
-    distance = ntohl (route->target.distance);
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Forwarding message to %s\n",
              GNUNET_i2s (&neighbor->peer));
   forward_payload (neighbor,
-                  distance,
+                  distance + 1,
                   0,
                   &rm->sender,
                   &rm->target,
@@ -1969,6 +1984,10 @@ handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
   if (NULL == route)
   {
     /* got disconnected */
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "No route to %s, dropping local message of type %u\n",
+                GNUNET_i2s (&msg->target),
+                ntohs (payload->type));
     GNUNET_STATISTICS_update (stats,
                              "# local messages discarded (no route)",
                              1, GNUNET_NO);
@@ -1977,12 +1996,13 @@ handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Forwarding %u bytes to %s\n",
+             "Forwarding %u bytes of type %u to %s\n",
              ntohs (payload->size),
+              ntohs (payload->type),
              GNUNET_i2s (&msg->target));
 
   forward_payload (route->next_hop,
-                  ntohl (route->target.distance),
+                  0 /* first hop, distance is zero */,
                   htonl (msg->uid),
                   &my_identity,
                   &msg->target,