+ release_route (route);
+ i = get_consensus_slot (new_distance);
+ route->set_offset = i;
+ consensi[new_distance].targets[i] = route;
+ route->target.distance = htonl (new_distance);
+}
+
+
+/**
+ * Initialize this neighbors 'my_set' and when done give
+ * it to the pending set operation for execution.
+ *
+ * Add a single element to the set per call:
+ *
+ * If we reached the last element of a consensus element: increase distance
+ *
+ *
+ * @param cls the neighbor for which we are building the set
+ */
+static void
+build_set (void *cls)
+{
+ struct DirectNeighbor *neighbor = cls;
+ struct GNUNET_SET_Element element;
+ struct Target *target;
+ target = NULL;
+ while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
+ (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
+ {
+ /* If we reached the last element of a consensus array element: increase distance and start with next array */
+ neighbor->consensus_insertion_offset = 0;
+ neighbor->consensus_insertion_distance++;
+ /* skip over NULL entries */
+ while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
+ (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
+ (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
+ neighbor->consensus_insertion_offset++;
+ }
+ if (DEFAULT_FISHEYE_DEPTH - 1 == neighbor->consensus_insertion_distance)
+ {
+ /* we have added all elements to the set, run the operation */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Finished building my SET for peer `%s' with %u elements, committing\n",
+ GNUNET_i2s(&neighbor->peer),
+ neighbor->consensus_elements);
+ GNUNET_SET_commit (neighbor->set_op,
+ neighbor->my_set);
+ GNUNET_SET_destroy (neighbor->my_set);
+ neighbor->my_set = NULL;
+ return;
+ }
+
+ target = &consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]->target;
+ element.size = sizeof (struct Target);
+ element.type = htons (0); /* do we need this? */
+ element.data = target;
+
+ /* Find next non-NULL entry */
+ neighbor->consensus_insertion_offset++;
+ /* skip over NULL entries */
+ while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
+ (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
+ (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
+ {
+ neighbor->consensus_insertion_offset++;
+ }
+
+ if ( (0 != memcmp(&target->peer, &my_identity, sizeof (my_identity))) &&
+ (0 != memcmp(&target->peer, &neighbor->peer, sizeof (neighbor->peer))) )
+ {
+ /* Add target if it is not the neighbor or this peer */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding peer `%s' with distance %u to SET\n",
+ GNUNET_i2s (&target->peer),
+ ntohl (target->distance));
+ GNUNET_SET_add_element (neighbor->my_set,
+ &element,
+ &build_set, neighbor);
+ neighbor->consensus_elements++;
+ }
+ else
+ build_set(neighbor);
+}
+
+
+/**
+ * A peer is now connected to us at distance 1. Initiate DV exchange.
+ *
+ * @param neighbor entry for the neighbor at distance 1
+ */
+static void
+handle_direct_connect (struct DirectNeighbor *neighbor)
+{
+ struct Route *route;
+ struct GNUNET_HashCode h1;
+ struct GNUNET_HashCode h2;
+ struct GNUNET_HashCode session_id;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Direct connection to %s established, routing table exchange begins.\n",
+ GNUNET_i2s (&neighbor->peer));
+ GNUNET_STATISTICS_update (stats,
+ "# peers connected (1-hop)",
+ 1, GNUNET_NO);
+ route = GNUNET_CONTAINER_multipeermap_get (all_routes,
+ &neighbor->peer);
+ if (NULL != route)
+ {
+ send_disconnect_to_plugin (&neighbor->peer);
+ release_route (route);
+ GNUNET_free (route);
+ }
+
+ neighbor->direct_route = GNUNET_new (struct Route);
+ neighbor->direct_route->next_hop = neighbor;
+ neighbor->direct_route->target.peer= neighbor->peer;
+ neighbor->direct_route->target.distance = DIRECT_NEIGHBOR_COST;
+ allocate_route (neighbor->direct_route, DIRECT_NEIGHBOR_COST);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding direct route to %s\n",
+ GNUNET_i2s (&neighbor->direct_route->target.peer));
+
+
+ /* construct session ID seed as XOR of both peer's identities */
+ GNUNET_CRYPTO_hash (&my_identity, sizeof (my_identity), &h1);
+ GNUNET_CRYPTO_hash (&neighbor->peer, sizeof (struct GNUNET_PeerIdentity), &h2);
+ GNUNET_CRYPTO_hash_xor (&h1,
+ &h2,
+ &session_id);
+ /* make sure session ID is unique across applications by salting it with 'DV' */
+ GNUNET_CRYPTO_hkdf (&neighbor->real_session_id, sizeof (struct GNUNET_HashCode),
+ GCRY_MD_SHA512, GCRY_MD_SHA256,
+ "DV-SALT", 2,
+ &session_id, sizeof (session_id),
+ NULL, 0);
+ if (0 < memcmp (&neighbor->peer,
+ &my_identity,
+ sizeof (struct GNUNET_PeerIdentity)))
+ {
+ if (NULL != neighbor->listen_handle)
+ {
+ GNUNET_break (0);
+ }
+ else
+ neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
+ neighbor);
+ }
+ else