struct GCD_search_handle * GCD_search (const struct GNUNET_PeerIdentity *peer_id, GCD_search_callback callback, void *cls) { struct GNUNET_HashCode phash; struct GCD_search_handle *h; LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting DHT GET for peer %s\n", GNUNET_i2s (peer_id)); GNUNET_STATISTICS_update (stats, "# DHT search", 1, GNUNET_NO); memset (&phash, 0, sizeof (phash)); GNUNET_memcpy (&phash, peer_id, sizeof (*peer_id)); h = GNUNET_new (struct GCD_search_handle); h->peer_id = GNUNET_PEER_intern (peer_id); h->callback = callback; h->cls = cls; h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */ GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */ &phash, /* key to search */ dht_replication_level, /* replication level */ GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */ 0, /* xquery bits */ &dht_get_id_handler, h); GNUNET_CONTAINER_multihashmap32_put (get_requests, h->peer_id, h, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); return h; }
/** * Process cadet requests. * * @param cls closure * @param server the initialized server * @param c configuration to use */ static void run (void *cls, struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *c) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n"); stats = GNUNET_STATISTICS_create ("cadet", c); /* Scheduled the task to clean up when shutdown is called */ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL); GNUNET_log (GNUNET_ERROR_TYPE_INFO, "reading key\n"); my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c); GNUNET_assert (NULL != my_private_key); GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_full_id.public_key); myid = GNUNET_PEER_intern (&my_full_id); GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STARTING SERVICE (cadet) for peer [%s]\n", GNUNET_i2s (&my_full_id)); GML_init (server); /* Local clients */ GCH_init (c); /* Hellos */ GCC_init (c); /* Connections */ GCP_init (c); /* Peers */ GCD_init (c); /* DHT */ GCT_init (c, my_private_key); /* Tunnels */ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cadet service running\n"); }
/** * Builds a path from a PeerIdentity array. * * @param peers PeerIdentity array. * @param size Size of the @c peers array. * @param myid ID of local peer, to find @c own_pos. * @param own_pos Output parameter: own position in the path. * * @return Fixed and shortened path. */ struct CadetPeerPath * path_build_from_peer_ids (struct GNUNET_PeerIdentity *peers, unsigned int size, GNUNET_PEER_Id myid, unsigned int *own_pos) { struct CadetPeerPath *path; GNUNET_PEER_Id shortid; unsigned int i; unsigned int j; unsigned int offset; /* Create path */ LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n"); path = path_new (size); *own_pos = 0; offset = 0; for (i = 0; i < size; i++) { LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n", i, GNUNET_i2s (&peers[i])); shortid = GNUNET_PEER_intern (&peers[i]); /* Check for loops / duplicates */ for (j = 0; j < i - offset; j++) { if (path->peers[j] == shortid) { LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j); offset = i - j; LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now %u\n", offset); GNUNET_PEER_change_rc (shortid, -1); } } LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset); path->peers[i - offset] = shortid; if (path->peers[i - offset] == myid) *own_pos = i - offset; } path->length -= offset; if (path->peers[*own_pos] != myid) { /* create path: self not found in path through self */ GNUNET_break_op (0); path_destroy (path); return NULL; } return path; }
static int check () { int i; GNUNET_PEER_Id pid; struct GNUNET_PeerIdentity res; struct GNUNET_PeerIdentity zero; GNUNET_PEER_Id ids[] = { 1, 2, 3 }; GNUNET_assert (0 == GNUNET_PEER_intern (NULL)); /* Insert Peers into PeerEntry table and hashmap */ for (i = 0; i < NUMBER_OF_PEERS; i++) { pid = GNUNET_PEER_intern (&pidArr[i]); if (pid != (i + 1)) { FPRINTF (stderr, "%s", "Unexpected Peer ID returned by intern function\n"); return 1; } } /* Referencing the first 3 peers once again */ for (i = 0; i < 3; i++) { pid = GNUNET_PEER_intern (&pidArr[i]); if (pid != (i + 1)) { FPRINTF (stderr, "%s", "Unexpected Peer ID returned by intern function\n"); return 1; } } /* Dereferencing the first 3 peers once [decrementing their reference count] */ GNUNET_PEER_decrement_rcs (ids, 3); /* re-referencing the first 3 peers using the change_rc function */ for (i = 1; i <= 3; i++) GNUNET_PEER_change_rc (i, 1); /* Removing the second Peer from the PeerEntry hash map */ GNUNET_PEER_change_rc (2, -2); /* convert the pid of the first PeerEntry into that of the third */ GNUNET_PEER_resolve (1, &res); GNUNET_assert (0 == memcmp (&res, &pidArr[0], sizeof (res))); /* * Attempt to convert pid = 0 (which is reserved) * into a peer identity object, the peer identity memory * is expected to be set to zero */ memset (&zero, 0, sizeof (struct GNUNET_PeerIdentity)); GNUNET_log_skip (1, GNUNET_YES); GNUNET_PEER_resolve (0, &res); GNUNET_assert (0 == memcmp (&res, &zero, sizeof (res))); /* Removing peer entries 1 and 3 from table using the list decrement function */ /* If count = 0, nothing should be done whatsoever */ GNUNET_PEER_decrement_rcs (ids, 0); ids[1] = 3; GNUNET_PEER_decrement_rcs (ids, 2); GNUNET_PEER_decrement_rcs (ids, 2); return 0; }