+ struct Route *route = value;
+
+ if (route->next_hop != neighbor)
+ return GNUNET_YES; /* not affected */
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
+ release_route (route);
+ send_disconnect_to_plugin (&route->target.peer);
+ GNUNET_free (route);
+ return GNUNET_YES;
+}
+
+
+/**
+ * Handle the case that a direct connection to a peer is
+ * disrupted. Remove all routes via that peer and
+ * stop the consensus with it.
+ *
+ * @param neighbor peer that was disconnected (or at least is no
+ * longer at distance 1)
+ */
+static void
+handle_direct_disconnect (struct DirectNeighbor *neighbor)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Culling routes via %s due to direct disconnect\n",
+ GNUNET_i2s (&neighbor->peer));
+ GNUNET_CONTAINER_multipeermap_iterate (all_routes,
+ &cull_routes,
+ neighbor);
+ if (NULL != neighbor->direct_route)
+ {
+ release_route (neighbor->direct_route);
+ GNUNET_free (neighbor->direct_route);
+ neighbor->direct_route = NULL;
+ }
+ if (NULL != neighbor->neighbor_table_consensus)
+ {
+ GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
+ &free_targets,
+ NULL);
+ GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
+ neighbor->neighbor_table_consensus = NULL;
+ }
+ if (NULL != neighbor->neighbor_table)
+ {
+ GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
+ &free_targets,
+ NULL);
+ GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
+ neighbor->neighbor_table = NULL;
+ }
+ if (NULL != neighbor->set_op)
+ {
+ GNUNET_SET_operation_cancel (neighbor->set_op);
+ neighbor->set_op = NULL;
+ }
+ if (NULL != neighbor->my_set)
+ {
+ GNUNET_SET_destroy (neighbor->my_set);
+ neighbor->my_set = NULL;
+ }
+ if (NULL != neighbor->listen_handle)
+ {
+ GNUNET_SET_listen_cancel (neighbor->listen_handle);
+ neighbor->listen_handle = NULL;
+ }
+ if (NULL != neighbor->initiate_task)
+ {
+ GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
+ neighbor->initiate_task = NULL;
+ }
+}
+
+
+/**
+ * Function that is called with QoS information about an address; used
+ * to update our current distance to another peer.
+ *
+ * @param cls closure
+ * @param address the address
+ * @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 prop performance data for the address (as far as known)
+ */
+static void
+handle_ats_update (void *cls,
+ const struct GNUNET_HELLO_Address *address,
+ int active,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+ const struct GNUNET_ATS_Properties *prop)
+{
+ struct DirectNeighbor *neighbor;
+ uint32_t distance;
+ enum GNUNET_ATS_Network_Type network;
+
+ if (NULL == address)
+ {
+ /* ATS service temporarily disconnected */
+ return;
+ }
+
+ if (GNUNET_YES != active)
+ {
+ // FIXME: handle disconnect/inactive case too!
+ return;
+ }
+ distance = prop->distance;
+ network = prop->scope;
+ GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
+ /* check if entry exists */
+ neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
+ &address->peer);
+ if (NULL != neighbor)
+ {
+ neighbor->network = network;
+ if (neighbor->distance == distance)
+ return; /* nothing new to see here, move along */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "ATS says distance to %s is now %u\n",
+ GNUNET_i2s (&address->peer),
+ (unsigned int) distance);
+ if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
+ (DIRECT_NEIGHBOR_COST == distance) )
+ return; /* no change */
+ if (DIRECT_NEIGHBOR_COST == neighbor->distance)
+ {
+ neighbor->distance = distance;
+ GNUNET_STATISTICS_update (stats,
+ "# peers connected (1-hop)",
+ -1, GNUNET_NO);
+ handle_direct_disconnect (neighbor);
+ schedule_refresh_routes ();
+ return;
+ }
+ neighbor->distance = distance;
+ if (DIRECT_NEIGHBOR_COST != neighbor->distance)
+ return;
+ if (GNUNET_YES != neighbor->connected)
+ return;
+ handle_direct_connect (neighbor);
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "ATS says distance to %s is now %u\n",
+ GNUNET_i2s (&address->peer),
+ (unsigned int) distance);
+ neighbor = GNUNET_new (struct DirectNeighbor);
+ neighbor->peer = address->peer;
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
+ &neighbor->peer,
+ neighbor,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ neighbor->connected = GNUNET_NO; /* not yet */
+ neighbor->distance = distance;
+ neighbor->network = network;
+}
+
+
+/**
+ * Check if a target was removed from the set of the other peer; if so,
+ * if we also used it for our route, we need to remove it from our
+ * 'all_routes' set (and later check if an alternative path now exists).
+ *
+ * @param cls the `struct DirectNeighbor`
+ * @param key peer identity for the target
+ * @param value a `struct Target` previously reachable via the given neighbor
+ */
+static int
+check_target_removed (void *cls,
+ const struct GNUNET_PeerIdentity *key,
+ void *value)
+{
+ struct DirectNeighbor *neighbor = cls;
+ struct Target *new_target;
+ struct Route *current_route;
+
+ new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
+ key);
+ current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
+ key);
+ if (NULL != new_target)
+ {
+ /* target was in old set, is in new set */
+ if ( (NULL != current_route) &&
+ (current_route->next_hop == neighbor) &&
+ (current_route->target.distance != new_target->distance) )
+ {
+ /* need to recalculate routes due to distance change */
+ neighbor->target_removed = GNUNET_YES;
+ }
+ return GNUNET_OK;
+ }
+ /* target was revoked, check if it was used */
+ if ( (NULL == current_route) ||
+ (current_route->next_hop != neighbor) )
+ {
+ /* didn't matter, wasn't used */
+ return GNUNET_OK;
+ }
+ /* remove existing route */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Lost route to %s\n",
+ GNUNET_i2s (¤t_route->target.peer));
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
+ send_disconnect_to_plugin (¤t_route->target.peer);
+ release_route (current_route);
+ GNUNET_free (current_route);
+ neighbor->target_removed = GNUNET_YES;
+ return GNUNET_OK;
+}
+
+
+/**
+ * Check if a target was added to the set of the other peer; if it
+ * was added or impoves the existing route, do the needed updates.
+ *
+ * @param cls the `struct DirectNeighbor`
+ * @param key peer identity for the target
+ * @param value a `struct Target` now reachable via the given neighbor
+ */
+static int
+check_target_added (void *cls,
+ const struct GNUNET_PeerIdentity *key,
+ void *value)
+{
+ struct DirectNeighbor *neighbor = cls;
+ struct Target *target = value;
+ struct Route *current_route;