+ for (i = 0; i < atsi_count; i++)
+ if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
+ return (0 == ntohl (atsi->value)) ? DIRECT_NEIGHBOR_COST : ntohl (atsi->value); // FIXME: 0 check should not be required once ATS is fixed!
+ /* If we do not have explicit distance data, assume direct neighbor. */
+ return DIRECT_NEIGHBOR_COST;
+}
+
+
+/**
+ * Multipeermap iterator for freeing routes that go via a particular
+ * neighbor that disconnected and is thus no longer available.
+ *
+ * @param cls the direct neighbor that is now unavailable
+ * @param key key value stored under
+ * @param value a 'struct Route' that may or may not go via neighbor
+ *
+ * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
+ */
+static int
+cull_routes (void *cls,
+ const struct GNUNET_PeerIdentity *key,
+ void *value)
+{
+ struct DirectNeighbor *neighbor = cls;
+ 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_CONTAINER_multipeermap_iterate (all_routes,
+ &cull_routes,
+ neighbor);
+ if (NULL != neighbor->cth)
+ {
+ GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
+ neighbor->cth = NULL;
+ }
+
+ 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 (GNUNET_SCHEDULER_NO_TASK != neighbor->initiate_task)
+ {
+ GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
+ neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+}
+
+
+/**
+ * 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 is this address in active use
+ * @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)
+ * @param ats_count number of performance records in 'ats'
+ */
+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_Information *ats,
+ uint32_t ats_count)
+{
+ struct DirectNeighbor *neighbor;
+ uint32_t distance;
+
+ if (GNUNET_NO == active)
+ return;
+ distance = get_atsi_distance (ats, ats_count);
+ /*
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "ATS says distance to %s is %u\n",
+ GNUNET_i2s (&address->peer),
+ (unsigned int) distance);*/
+ /* check if entry exists */
+ neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
+ &address->peer);
+ if (NULL != neighbor)
+ {
+ 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);
+ GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
+ &refresh_routes,
+ NULL);
+ return;
+ }
+ neighbor->distance = distance;
+ if (DIRECT_NEIGHBOR_COST != neighbor->distance)
+ return;
+ if (GNUNET_YES != neighbor->connected)
+ return;
+ handle_direct_connect (neighbor);
+ return;
+ }
+ neighbor = GNUNET_new (struct DirectNeighbor);
+ neighbor->peer = address->peer;
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
+ &address->peer,
+ neighbor,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ neighbor->connected = GNUNET_NO; /* not yet */
+ neighbor->distance = distance;
+}
+
+
+/**
+ * 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);
+ if (NULL == new_target)
+ {
+ /* target was revoked, check if it was used */
+ current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
+ key);
+ 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);
+ GNUNET_free (current_route);
+ neighbor->target_removed = GNUNET_YES;
+ return GNUNET_OK;
+ }
+ 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;
+
+ /* target was revoked, check if it was used */
+ current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
+ key);
+ if (NULL != current_route)
+ {
+ /* route exists */
+ if (current_route->next_hop == neighbor)
+ {
+ /* we had the same route before, no change */
+ if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
+ {
+ current_route->target.distance = htonl (ntohl (target->distance) + 1);
+ send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
+ }
+ return GNUNET_OK;
+ }
+ if (ntohl (current_route->target.distance) >= ntohl (target->distance) + 1)
+ {
+ /* alternative, shorter route exists, ignore */
+ return GNUNET_OK;
+ }
+ /* new route is better than the existing one, take over! */
+ /* NOTE: minor security issue: malicious peers may advertise
+ very short routes to take over longer paths; as we don't
+ check that the shorter routes actually work, a malicious
+ direct neighbor can use this to DoS our long routes */
+ current_route->next_hop = neighbor;
+ current_route->target.distance = htonl (ntohl (target->distance) + 1);
+ send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
+ return GNUNET_OK;
+ }
+ /* new route */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Discovered new route to %s using %u hops\n",
+ GNUNET_i2s (&target->peer),
+ (unsigned int) (ntohl (target->distance) + 1));
+ current_route = GNUNET_new (struct Route);
+ current_route->next_hop = neighbor;
+ current_route->target.peer = target->peer;
+ current_route->target.distance = htonl (ntohl (target->distance) + 1);
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_put (all_routes,
+ ¤t_route->target.peer,
+ current_route,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ send_connect_to_plugin (¤t_route->target.peer,
+ ntohl (current_route->target.distance));
+ return GNUNET_OK;
+}
+
+
+/**
+ * Callback for set operation results. Called for each element
+ * in the result set.
+ * We have learned a new route from the other peer. Add it to the
+ * route set we're building.
+ *
+ * @param cls the 'struct DirectNeighbor' we're building the consensus with
+ * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
+ * @param status see enum GNUNET_SET_Status
+ */
+static void
+handle_set_union_result (void *cls,
+ const struct GNUNET_SET_Element *element,
+ enum GNUNET_SET_Status status)
+{
+ struct DirectNeighbor *neighbor = cls;
+ struct Target *target;
+ char *status_str;
+
+ switch (status) {
+ case GNUNET_SET_STATUS_OK:
+ status_str = "GNUNET_SET_STATUS_OK";
+ break;
+ case GNUNET_SET_STATUS_TIMEOUT:
+ status_str = "GNUNET_SET_STATUS_TIMEOUT";
+ break;
+ case GNUNET_SET_STATUS_FAILURE:
+ status_str = "GNUNET_SET_STATUS_FAILURE";
+ break;
+ case GNUNET_SET_STATUS_HALF_DONE:
+ status_str = "GNUNET_SET_STATUS_HALF_DONE";
+ break;
+ case GNUNET_SET_STATUS_DONE:
+ status_str = "GNUNET_SET_STATUS_DONE";
+ break;
+ default:
+ status_str = "UNDEFINED";
+ break;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Got SET union result: %s\n",
+ status_str);
+ switch (status)
+ {
+ case GNUNET_SET_STATUS_OK:
+ if (sizeof (struct Target) != element->size)
+ {
+ GNUNET_break_op (0);
+ return;
+ }
+ target = GNUNET_new (struct Target);
+ memcpy (target, element->data, sizeof (struct Target));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received information about peer `%s' with distance %u\n",
+ GNUNET_i2s (&target->peer), ntohl(target->distance));
+ if (NULL == neighbor->neighbor_table_consensus)
+ neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
+ if (GNUNET_YES !=
+ GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
+ &target->peer,
+ target,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+ {
+ GNUNET_break_op (0);
+ GNUNET_free (target);
+ }
+ break;
+ case GNUNET_SET_STATUS_TIMEOUT:
+ case GNUNET_SET_STATUS_FAILURE:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Failed to establish DV union, will try again later\n");
+ neighbor->set_op = 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 (0 < memcmp (&neighbor->peer,
+ &my_identity,
+ sizeof (struct GNUNET_PeerIdentity)))
+ neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
+ &initiate_set_union,
+ neighbor);
+ break;
+ case GNUNET_SET_STATUS_HALF_DONE:
+ break;
+ case GNUNET_SET_STATUS_DONE:
+ /* we got all of our updates; integrate routing table! */
+ neighbor->target_removed = GNUNET_NO;
+ if (NULL == neighbor->neighbor_table_consensus)
+ neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
+ if (NULL != neighbor->neighbor_table)
+ GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
+ &check_target_removed,
+ neighbor);
+ if (GNUNET_YES == neighbor->target_removed)
+ {
+ /* check if we got an alternative for the removed routes */
+ GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
+ &refresh_routes,
+ NULL);
+ }
+ /* add targets that appeared (and check for improved routes) */
+ GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
+ &check_target_added,
+ neighbor);
+ 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;
+ }
+ neighbor->neighbor_table = neighbor->neighbor_table_consensus;
+ neighbor->neighbor_table_consensus = NULL;
+
+ /* operation done, schedule next run! */
+ neighbor->set_op = NULL;
+ if (0 < memcmp (&neighbor->peer,
+ &my_identity,
+ sizeof (struct GNUNET_PeerIdentity)))
+ neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
+ &initiate_set_union,
+ neighbor);
+ break;
+ default:
+ GNUNET_break (0);
+ return;
+ }
+}
+
+
+/**
+ * Start creating a new DV set union construction, our neighbour has
+ * asked for it (callback for listening peer).
+ *
+ * @param cls the 'struct DirectNeighbor' of the peer we're building
+ * a routing consensus with
+ * @param other_peer the other peer
+ * @param context_msg message with application specific information from
+ * the other peer
+ * @param request request from the other peer, use GNUNET_SET_accept
+ * to accept it, otherwise the request will be refused
+ * Note that we don't use a return value here, as it is also
+ * necessary to specify the set we want to do the operation with,
+ * whith sometimes can be derived from the context message.
+ * Also necessary to specify the timeout.
+ */
+static void
+listen_set_union (void *cls,
+ const struct GNUNET_PeerIdentity *other_peer,
+ const struct GNUNET_MessageHeader *context_msg,
+ struct GNUNET_SET_Request *request)
+{
+ struct DirectNeighbor *neighbor = cls;
+
+ if (NULL == request)
+ return; /* why??? */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Starting to create consensus with %s\n",
+ GNUNET_i2s (&neighbor->peer));
+ 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;
+ }
+ neighbor->my_set = GNUNET_SET_create (cfg,
+ GNUNET_SET_OPERATION_UNION);
+ neighbor->set_op = GNUNET_SET_accept (request,
+ GNUNET_SET_RESULT_ADDED,
+ &handle_set_union_result,
+ neighbor);
+ neighbor->consensus_insertion_offset = 0;
+ neighbor->consensus_insertion_distance = 0;
+ neighbor->consensus_elements = 0;
+ build_set (neighbor);
+}
+
+
+/**
+ * Start creating a new DV set union by initiating the connection.
+ *
+ * @param cls the 'struct DirectNeighbor' of the peer we're building
+ * a routing consensus with
+ * @param tc scheduler context
+ */
+static void
+initiate_set_union (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct DirectNeighbor *neighbor = cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Initiating SET union with peer `%s'\n",
+ GNUNET_i2s (&neighbor->peer));
+ neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
+ neighbor->my_set = GNUNET_SET_create (cfg,
+ GNUNET_SET_OPERATION_UNION);
+ neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
+ &neighbor->real_session_id,
+ NULL,
+ 0 /* FIXME: salt */,
+ GNUNET_SET_RESULT_ADDED,
+ &handle_set_union_result,
+ neighbor);
+ neighbor->consensus_insertion_offset = 0;
+ neighbor->consensus_insertion_distance = 0;
+ neighbor->consensus_elements = 0;
+ build_set (neighbor);
+}
+
+
+/**
+ * Core handler for DV data messages. Whatever this message
+ * contains all we really have to do is rip it out of its
+ * DV layering and give it to our pal the DV plugin to report
+ * in with.
+ *
+ * @param cls closure
+ * @param peer peer which sent the message (immediate sender)
+ * @param message the message
+ * @return GNUNET_OK on success, GNUNET_SYSERR if the other peer violated the protocol
+ */
+static int
+handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ const struct RouteMessage *rm;
+ const struct GNUNET_MessageHeader *payload;
+ struct Route *route;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Handling DV message\n");
+ if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ rm = (const struct RouteMessage *) message;
+ payload = (const struct GNUNET_MessageHeader *) &rm[1];
+ if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
+ {