From 84e15afe4933ee2ca62e3e111ca261c3a986c157 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 21 Apr 2010 20:19:43 +0000 Subject: [PATCH] activating new peerinfo API --- TODO | 10 +- src/core/gnunet-service-core.c | 22 +- src/hostlist/hostlist-server.c | 20 +- src/include/gnunet_peerinfo_service.h | 84 +----- src/include/gnunet_protocols.h | 5 - src/peerinfo-tool/gnunet-peerinfo.c | 17 +- src/peerinfo/gnunet-service-peerinfo.c | 40 --- src/peerinfo/peerinfo.h | 25 -- src/peerinfo/peerinfo_api.c | 344 ++--------------------- src/peerinfo/test_peerinfo_api.c | 30 +- src/transport/gnunet-service-transport.c | 33 ++- 11 files changed, 133 insertions(+), 497 deletions(-) diff --git a/TODO b/TODO index b5b5176b2..dcc13d8e9 100644 --- a/TODO +++ b/TODO @@ -1,9 +1,4 @@ 0.9.0pre1: -* PEERINFO: [CG] - - finish implementing NEW API (cancel, timeout, submit-during-receive prevention) - - test new API - - transition existing code to new API - - remove support for old API * FS: [CG] - support recursive download even if filename is NULL and we hence do not generate files on disk (use temp_filename) @@ -175,6 +170,11 @@ Optimizations: * DATASTORE (?): - check for duplicates on insertion (currently, same content is frequently stored again [seen with KBLOCKS and SBLOCKS]!) +* PEERINFO: + - merge multiple HELLOs of the same peer in the transmission queue + (theoretically reduces overhead; bounds message queue size) + - merge multiple iteration requests over "all" peers in the queue + (theoretically reduces overhead; bounds messgae queue size) Minor features: * TCP: diff --git a/src/core/gnunet-service-core.c b/src/core/gnunet-service-core.c index bad6f05d3..aa4d4afb1 100644 --- a/src/core/gnunet-service-core.c +++ b/src/core/gnunet-service-core.c @@ -655,6 +655,11 @@ static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key; */ struct GNUNET_SCHEDULER_Handle *sched; +/** + * Handle to peerinfo service. + */ +static struct GNUNET_PEERINFO_Handle *peerinfo; + /** * Our configuration. */ @@ -2491,8 +2496,7 @@ send_key (struct Neighbour *n) GNUNET_i2s (&n->peer)); #endif GNUNET_assert (n->pitr == NULL); - n->pitr = GNUNET_PEERINFO_iterate (cfg, - sched, + n->pitr = GNUNET_PEERINFO_iterate (peerinfo, &n->peer, 0, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20), @@ -2923,8 +2927,7 @@ handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m) /* lookup n's public key, then try again */ GNUNET_assert (n->skm == NULL); n->skm = m_cpy; - n->pitr = GNUNET_PEERINFO_iterate (cfg, - sched, + n->pitr = GNUNET_PEERINFO_iterate (peerinfo, &n->peer, 0, GNUNET_TIME_UNIT_MINUTES, @@ -3737,6 +3740,8 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_CRYPTO_rsa_key_free (my_private_key); if (stats != NULL) GNUNET_STATISTICS_destroy (stats, GNUNET_NO); + if (peerinfo != NULL) + GNUNET_PEERINFO_disconnect (peerinfo); } @@ -3781,12 +3786,21 @@ run (void *cls, GNUNET_SCHEDULER_shutdown (s); return; } + peerinfo = GNUNET_PEERINFO_connect (sched, cfg); + if (NULL == peerinfo) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not access PEERINFO service. Exiting.\n")); + GNUNET_SCHEDULER_shutdown (s); + return; + } my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); GNUNET_free (keyfile); if (my_private_key == NULL) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Core service could not access hostkey. Exiting.\n")); + GNUNET_PEERINFO_disconnect (peerinfo); GNUNET_SCHEDULER_shutdown (s); return; } diff --git a/src/hostlist/hostlist-server.c b/src/hostlist/hostlist-server.c index 4a28cdfc3..fdc3e34d0 100644 --- a/src/hostlist/hostlist-server.c +++ b/src/hostlist/hostlist-server.c @@ -94,6 +94,11 @@ static struct MHD_Response *response; */ static struct GNUNET_PEERINFO_IteratorContext *pitr; +/** + * Handle for accessing peerinfo service. + */ +static struct GNUNET_PEERINFO_Handle *peerinfo; + /** * Context for host processor. */ @@ -280,7 +285,8 @@ update_response (void *cls, response_task = GNUNET_SCHEDULER_NO_TASK; results = GNUNET_malloc(sizeof(struct HostSet)); - pitr = GNUNET_PEERINFO_iterate (cfg, sched, + GNUNET_assert (peerinfo != NULL); + pitr = GNUNET_PEERINFO_iterate (peerinfo, NULL, 0, GNUNET_TIME_UNIT_MINUTES, @@ -596,6 +602,13 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, sched = s; cfg = c; stats = st; + peerinfo = GNUNET_PEERINFO_connect (sched, cfg); + if (peerinfo == NULL) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not access PEERINFO service. Exiting.\n")); + return GNUNET_SYSERR; + } if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg, "HOSTLIST", "HTTPPORT", @@ -724,6 +737,11 @@ GNUNET_HOSTLIST_server_stop () MHD_destroy_response (response); response = NULL; } + if (peerinfo != NULL) + { + GNUNET_PEERINFO_disconnect (peerinfo); + peerinfo = NULL; + } } /* end of hostlist-server.c */ diff --git a/src/include/gnunet_peerinfo_service.h b/src/include/gnunet_peerinfo_service.h index ac88df201..4f689e504 100644 --- a/src/include/gnunet_peerinfo_service.h +++ b/src/include/gnunet_peerinfo_service.h @@ -51,14 +51,15 @@ struct GNUNET_PEERINFO_Handle; /** * Connect to the peerinfo service. * - * @param cfg configuration to use * @param sched scheduler to use + * @param cfg configuration to use * @return NULL on error (configuration related, actual connection * etablishment may happen asynchronously). */ struct GNUNET_PEERINFO_Handle * -GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_SCHEDULER_Handle *sched); +GNUNET_PEERINFO_connect (struct GNUNET_SCHEDULER_Handle *sched, + const struct GNUNET_CONFIGURATION_Handle *cfg); + /** @@ -86,25 +87,10 @@ GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h); * @param hello the verified (!) HELLO message */ void -GNUNET_PEERINFO_add_peer_new (struct GNUNET_PEERINFO_Handle *h, - const struct GNUNET_HELLO_Message *hello); - +GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h, + const struct GNUNET_HELLO_Message *hello); -/** - * Add a host to the persistent list. - * - * @param cfg configuration to use - * @param sched scheduler to use - * @param peer identity of the peer - * @param hello the verified (!) HELLO message - */ -void -GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_SCHEDULER_Handle *sched, - const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_HELLO_Message *hello); - /** * Type of an iterator over the hosts. Note that each * host will be called with each available protocol. @@ -127,40 +113,6 @@ typedef void struct GNUNET_PEERINFO_IteratorContext; -/** - * Call a method for each known matching host and change - * its trust value. The method will be invoked once for - * each host and then finally once with a NULL pointer. After - * that final invocation, the iterator context must no longer - * be used. - * - * Note that the last call can be triggered by timeout or by simply - * being done; however, the trust argument will be set to zero if we - * are done, 1 if we timed out and 2 for fatal error. - * - * @param cfg configuration to use - * @param sched scheduler to use - * @param peer restrict iteration to this peer only (can be NULL) - * @param trust_delta how much to change the trust in all matching peers - * @param timeout how long to wait until timing out - * @param callback the method to call for each peer - * @param callback_cls closure for callback - * @return NULL on error (in this case, 'callback' is never called!), - * otherwise an iterator context - */ -struct GNUNET_PEERINFO_IteratorContext * -GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_SCHEDULER_Handle *sched, - const struct GNUNET_PeerIdentity *peer, - int trust_delta, - struct GNUNET_TIME_Relative timeout, - GNUNET_PEERINFO_Processor callback, - void *callback_cls); - - -struct GNUNET_PEERINFO_NewIteratorContext; - - /** * Call a method for each known matching host and change its trust * value. The callback method will be invoked once for each matching @@ -183,23 +135,14 @@ struct GNUNET_PEERINFO_NewIteratorContext; * @return NULL on error (in this case, 'callback' is never called!), * otherwise an iterator context */ -struct GNUNET_PEERINFO_NewIteratorContext * -GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h, - const struct GNUNET_PeerIdentity *peer, - int trust_delta, - struct GNUNET_TIME_Relative timeout, - GNUNET_PEERINFO_Processor callback, - void *callback_cls); - - +struct GNUNET_PEERINFO_IteratorContext * +GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h, + const struct GNUNET_PeerIdentity *peer, + int trust_delta, + struct GNUNET_TIME_Relative timeout, + GNUNET_PEERINFO_Processor callback, + void *callback_cls); -/** - * Cancel an iteration over peer information. - * - * @param ic context of the iterator to cancel - */ -void -GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic); /** @@ -211,6 +154,7 @@ void GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic); + /** * Handle for notifications about changes to the set of known peers. */ diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h index d992c9fb7..72fcccf94 100644 --- a/src/include/gnunet_protocols.h +++ b/src/include/gnunet_protocols.h @@ -198,11 +198,6 @@ extern "C" */ #define GNUNET_MESSAGE_TYPE_TRANSPORT_PONG 33 -/** - * Request addition of a HELLO - */ -#define GNUNET_MESSAGE_TYPE_PEERINFO_ADD 36 - /** * Request update and listing of a peer. */ diff --git a/src/peerinfo-tool/gnunet-peerinfo.c b/src/peerinfo-tool/gnunet-peerinfo.c index 63c5cc008..5efd7262c 100644 --- a/src/peerinfo-tool/gnunet-peerinfo.c +++ b/src/peerinfo-tool/gnunet-peerinfo.c @@ -39,6 +39,8 @@ static int get_self; static struct GNUNET_SCHEDULER_Handle *sched; +static struct GNUNET_PEERINFO_Handle *peerinfo; + static const struct GNUNET_CONFIGURATION_Handle *cfg; struct PrintContext @@ -166,7 +168,10 @@ print_peer_info (void *cls, struct PrintContext *pc; if (peer == NULL) - return; + { + GNUNET_PEERINFO_disconnect (peerinfo); + return; + } if (be_quiet) { GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc); @@ -212,8 +217,14 @@ run (void *cls, cfg = c; if (get_self != GNUNET_YES) { - (void) GNUNET_PEERINFO_iterate (cfg, - sched, + peerinfo = GNUNET_PEERINFO_connect (sched, cfg); + if (peerinfo == NULL) + { + fprintf (stderr, + _("Could not access PEERINFO service. Exiting.\n")); + return; + } + (void) GNUNET_PEERINFO_iterate (peerinfo, NULL, 0, GNUNET_TIME_relative_multiply diff --git a/src/peerinfo/gnunet-service-peerinfo.c b/src/peerinfo/gnunet-service-peerinfo.c index 1802da959..e200eae2f 100644 --- a/src/peerinfo/gnunet-service-peerinfo.c +++ b/src/peerinfo/gnunet-service-peerinfo.c @@ -29,8 +29,6 @@ * * TODO: * - HostEntries are never 'free'd (add expiration, upper bound?) - * - AddPeer message is obsolete with NEW peerinfo API (remove handler, - * message struct and protocol) */ #include "platform.h" @@ -648,43 +646,6 @@ cron_clean_data_hosts (void *cls, } -/** - * Handle ADD-message. - * - * @param cls closure - * @param client identification of the client - * @param message the actual message - */ -static void -handle_add (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) -{ - const struct PeerAddMessage *pam; - const struct GNUNET_MessageHeader *hello; - uint16_t size; - - size = ntohs (message->size); - if (size < - sizeof (struct PeerAddMessage) + sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } - pam = (const struct PeerAddMessage *) message; - hello = (const struct GNUNET_MessageHeader *) &pam[1]; - if (size != sizeof (struct PeerAddMessage) + ntohs (hello->size)) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } - bind_address (&pam->peer, (const struct GNUNET_HELLO_Message *) hello); - GNUNET_SERVER_receive_done (client, GNUNET_OK); -} - - /** * Handle HELLO-message. * @@ -787,7 +748,6 @@ handle_notify (void *cls, */ static struct GNUNET_SERVER_MessageHandler handlers[] = { {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0}, - {&handle_add, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_ADD, 0}, {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET, sizeof (struct ListPeerMessage)}, {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL, diff --git a/src/peerinfo/peerinfo.h b/src/peerinfo/peerinfo.h index 607aad4db..3e959a6c7 100644 --- a/src/peerinfo/peerinfo.h +++ b/src/peerinfo/peerinfo.h @@ -29,31 +29,6 @@ #define DEBUG_PEERINFO GNUNET_NO -/** - * Add the given peer to the list. This message - * is always followed by a verified HELLO message. - */ -struct PeerAddMessage -{ - - /** - * Type will be GNUNET_MESSAGE_TYPE_PEERINFO_ADD - */ - struct GNUNET_MessageHeader header; - - /** - * Always zero. - */ - uint32_t reserved GNUNET_PACKED; - - /** - * For which peer do we provide a HELLO message here? - */ - struct GNUNET_PeerIdentity peer; - -}; - - /** * Message requesting a listing of all known peers, * possibly modified by the specified trust value diff --git a/src/peerinfo/peerinfo_api.c b/src/peerinfo/peerinfo_api.c index 5392d1696..44af04c92 100644 --- a/src/peerinfo/peerinfo_api.c +++ b/src/peerinfo/peerinfo_api.c @@ -127,14 +127,14 @@ struct GNUNET_PEERINFO_Handle /** * Connect to the peerinfo service. * - * @param cfg configuration to use * @param sched scheduler to use + * @param cfg configuration to use * @return NULL on error (configuration related, actual connection * etablishment may happen asynchronously). */ struct GNUNET_PEERINFO_Handle * -GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_SCHEDULER_Handle *sched) +GNUNET_PEERINFO_connect (struct GNUNET_SCHEDULER_Handle *sched, + const struct GNUNET_CONFIGURATION_Handle *cfg) { struct GNUNET_CLIENT_Connection *client; struct GNUNET_PEERINFO_Handle *ret; @@ -197,7 +197,7 @@ static void reconnect (struct GNUNET_PEERINFO_Handle *h) { GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR); - h->client = GNUNET_CLIENT_connect (h->sched, "client", h->cfg); + h->client = GNUNET_CLIENT_connect (h->sched, "peerinfo", h->cfg); GNUNET_assert (h->client != NULL); } @@ -291,8 +291,8 @@ trigger_transmit (struct GNUNET_PEERINFO_Handle *h) * @param hello the verified (!) HELLO message */ void -GNUNET_PEERINFO_add_peer_new (struct GNUNET_PEERINFO_Handle *h, - const struct GNUNET_HELLO_Message *hello) +GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h, + const struct GNUNET_HELLO_Message *hello) { uint16_t hs = GNUNET_HELLO_size (hello); struct TransmissionQueueEntry *tqe; @@ -321,7 +321,7 @@ GNUNET_PEERINFO_add_peer_new (struct GNUNET_PEERINFO_Handle *h, /** * Context for an iteration request. */ -struct GNUNET_PEERINFO_NewIteratorContext +struct GNUNET_PEERINFO_IteratorContext { /** * Handle to the PEERINFO service. @@ -370,7 +370,7 @@ struct GNUNET_PEERINFO_NewIteratorContext static void peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) { - struct GNUNET_PEERINFO_NewIteratorContext *ic = cls; + struct GNUNET_PEERINFO_IteratorContext *ic = cls; const struct InfoMessage *im; const struct GNUNET_HELLO_Message *hello; uint16_t ms; @@ -461,14 +461,14 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) * We've transmitted the iteration request. Now get ready to process * the results (or handle transmission error). * - * @param cls the 'struct GNUNET_PEERINFO_NewIteratorContext' + * @param cls the 'struct GNUNET_PEERINFO_IteratorContext' * @param transmit_success GNUNET_OK if transmission worked */ static void iterator_start_receive (void *cls, int transmit_success) { - struct GNUNET_PEERINFO_NewIteratorContext *ic = cls; + struct GNUNET_PEERINFO_IteratorContext *ic = cls; if (GNUNET_OK != transmit_success) { @@ -491,14 +491,14 @@ iterator_start_receive (void *cls, /** * Peerinfo iteration request has timed out. * - * @param cls the 'struct GNUNET_PEERINFO_NewIteratorContext*' + * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*' * @param tc scheduler context */ static void signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { - struct GNUNET_PEERINFO_NewIteratorContext *ic = cls; + struct GNUNET_PEERINFO_IteratorContext *ic = cls; ic->timeout_task = GNUNET_SCHEDULER_NO_TASK; ic->callback (ic->callback_cls, NULL, NULL, 1); @@ -535,17 +535,17 @@ signal_timeout (void *cls, * @return NULL on error (in this case, 'callback' is never called!), * otherwise an iterator context */ -struct GNUNET_PEERINFO_NewIteratorContext * -GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h, - const struct GNUNET_PeerIdentity *peer, - int trust_delta, - struct GNUNET_TIME_Relative timeout, - GNUNET_PEERINFO_Processor callback, - void *callback_cls) +struct GNUNET_PEERINFO_IteratorContext * +GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h, + const struct GNUNET_PeerIdentity *peer, + int trust_delta, + struct GNUNET_TIME_Relative timeout, + GNUNET_PEERINFO_Processor callback, + void *callback_cls) { struct ListAllPeersMessage *lapm; struct ListPeerMessage *lpm; - struct GNUNET_PEERINFO_NewIteratorContext *ic; + struct GNUNET_PEERINFO_IteratorContext *ic; struct TransmissionQueueEntry *tqe; #if DEBUG_PEERINFO @@ -573,7 +573,7 @@ GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h, lpm->trust_change = htonl (trust_delta); memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity)); } - ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NewIteratorContext)); + ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext)); ic->h = h; ic->tqe = tqe; ic->callback = callback; @@ -603,7 +603,7 @@ GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h, * @param ic context of the iterator to cancel */ void -GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic) +GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic) { if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK) { @@ -622,304 +622,4 @@ GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *i } - -/* ***************************** OLD API ****************************** */ - - - - -#define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) - - -struct CAFContext -{ - struct GNUNET_CLIENT_Connection *client; - struct GNUNET_MessageHeader *msg; -}; - - -static size_t -copy_and_free (void *cls, size_t size, void *buf) -{ - struct CAFContext *cc = cls; - struct GNUNET_MessageHeader *msg = cc->msg; - uint16_t msize; - - if (buf == NULL) - { -#if DEBUG_PEERINFO - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _ - ("Failed to transmit message of type %u to `%s' service.\n"), - ntohs (msg->type), "peerinfo"); -#endif - GNUNET_free (msg); - GNUNET_CLIENT_disconnect (cc->client, GNUNET_NO); - GNUNET_free (cc); - return 0; - } - msize = ntohs (msg->size); - GNUNET_assert (size >= msize); - memcpy (buf, msg, msize); - GNUNET_free (msg); - GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES); - GNUNET_free (cc); - return msize; -} - - - -/** - * Add a host to the persistent list. - * - * @param cfg configuration to use - * @param sched scheduler to use - * @param peer identity of the peer - * @param hello the verified (!) HELLO message - */ -void -GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_SCHEDULER_Handle *sched, - const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_HELLO_Message *hello) -{ - struct GNUNET_CLIENT_Connection *client; - struct PeerAddMessage *pam; - uint16_t hs; - struct CAFContext *cc; - -#if DEBUG_PEERINFO - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding peer `%s' to peerinfo database\n", - GNUNET_i2s(peer)); -#endif - client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg); - if (client == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Could not connect to `%s' service.\n"), "peerinfo"); - return; - } - hs = GNUNET_HELLO_size (hello); -#if DEBUG_PEERINFO - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Size of `%s' is %u bytes\n", - "HELLO", - (unsigned int) GNUNET_HELLO_size (hello)); -#endif - pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs); - pam->header.size = htons (hs + sizeof (struct PeerAddMessage)); - pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD); - memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity)); - memcpy (&pam[1], hello, hs); - cc = GNUNET_malloc (sizeof (struct CAFContext)); - cc->client = client; - cc->msg = &pam->header; - GNUNET_CLIENT_notify_transmit_ready (client, - ntohs (pam->header.size), - ADD_PEER_TIMEOUT, - GNUNET_NO, - ©_and_free, cc); -} - - -/** - * Context for the info handler. - */ -struct GNUNET_PEERINFO_IteratorContext -{ - - /** - * Our connection to the PEERINFO service. - */ - struct GNUNET_CLIENT_Connection *client; - - /** - * Function to call with information. - */ - GNUNET_PEERINFO_Processor callback; - - /** - * Closure for callback. - */ - void *callback_cls; - - /** - * When should we time out? - */ - struct GNUNET_TIME_Absolute timeout; - -}; - - -/** - * Type of a function to call when we receive a message - * from the service. - * - * @param cls closure - * @param msg message received, NULL on timeout or fatal error - */ -static void -info_handler (void *cls, const struct GNUNET_MessageHeader *msg) -{ - struct GNUNET_PEERINFO_IteratorContext *ic = cls; - const struct InfoMessage *im; - const struct GNUNET_HELLO_Message *hello; - uint16_t ms; - - if (msg == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Failed to receive response from `%s' service.\n"), - "peerinfo"); - ic->callback (ic->callback_cls, NULL, NULL, 1); - GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO); - GNUNET_free (ic); - return; - } - if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END) - { -#if DEBUG_PEERINFO - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received end of list of peers from peerinfo database\n"); -#endif - ic->callback (ic->callback_cls, NULL, NULL, 0); - GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO); - GNUNET_free (ic); - return; - } - ms = ntohs (msg->size); - if ((ms < sizeof (struct InfoMessage)) || - (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO)) - { - GNUNET_break (0); - ic->callback (ic->callback_cls, NULL, NULL, 2); - GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO); - GNUNET_free (ic); - return; - } - im = (const struct InfoMessage *) msg; - hello = NULL; - if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader)) - { - hello = (const struct GNUNET_HELLO_Message *) &im[1]; - if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello)) - { - GNUNET_break (0); - ic->callback (ic->callback_cls, NULL, NULL, 2); - GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO); - GNUNET_free (ic); - return; - } - } -#if DEBUG_PEERINFO - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n", - (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello), - "HELLO", - GNUNET_i2s (&im->peer)); -#endif - ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust)); - GNUNET_CLIENT_receive (ic->client, - &info_handler, - ic, - GNUNET_TIME_absolute_get_remaining (ic->timeout)); -} - - -/** - * Call a method for each known matching host and change - * its trust value. The method will be invoked once for - * each host and then finally once with a NULL pointer. - * Note that the last call can be triggered by timeout or - * by simply being done; however, the trust argument will - * be set to zero if we are done and to 1 if we timed out. - * - * @param cfg configuration to use - * @param sched scheduler to use - * @param peer restrict iteration to this peer only (can be NULL) - * @param trust_delta how much to change the trust in all matching peers - * @param timeout how long to wait until timing out - * @param callback the method to call for each peer - * @param callback_cls closure for callback - * @return NULL on error, otherwise an iterator context - */ -struct GNUNET_PEERINFO_IteratorContext * -GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_SCHEDULER_Handle *sched, - const struct GNUNET_PeerIdentity *peer, - int trust_delta, - struct GNUNET_TIME_Relative timeout, - GNUNET_PEERINFO_Processor callback, - void *callback_cls) -{ - struct GNUNET_CLIENT_Connection *client; - struct ListAllPeersMessage *lapm; - struct ListPeerMessage *lpm; - struct GNUNET_PEERINFO_IteratorContext *ihc; - - client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg); - if (client == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Could not connect to `%s' service.\n"), "peerinfo"); - return NULL; - } -#if DEBUG_PEERINFO - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Requesting list of peers from peerinfo database\n"); -#endif - if (peer == NULL) - { - ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) + - sizeof (struct ListAllPeersMessage)); - lapm = (struct ListAllPeersMessage *) &ihc[1]; - lapm->header.size = htons (sizeof (struct ListAllPeersMessage)); - lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL); - lapm->trust_change = htonl (trust_delta); - } - else - { - ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) + - sizeof (struct ListPeerMessage)); - lpm = (struct ListPeerMessage *) &ihc[1]; - lpm->header.size = htons (sizeof (struct ListPeerMessage)); - lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET); - lpm->trust_change = htonl (trust_delta); - memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity)); - } - ihc->client = client; - ihc->callback = callback; - ihc->callback_cls = callback_cls; - ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout); - if (GNUNET_OK != - GNUNET_CLIENT_transmit_and_get_response (client, - (const struct GNUNET_MessageHeader*) &ihc[1], - timeout, - GNUNET_YES, - &info_handler, - ihc)) - { - GNUNET_break (0); - GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO); - GNUNET_free (ihc); - return NULL; - } - return ihc; -} - - -/** - * Cancel an iteration over peer information. - * - * @param ic context of the iterator to cancel - */ -void -GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic) -{ - GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO); - GNUNET_free (ic); -} - - /* end of peerinfo_api.c */ diff --git a/src/peerinfo/test_peerinfo_api.c b/src/peerinfo/test_peerinfo_api.c index 10ea1fa81..3fa622f50 100644 --- a/src/peerinfo/test_peerinfo_api.c +++ b/src/peerinfo/test_peerinfo_api.c @@ -40,7 +40,7 @@ static struct GNUNET_SCHEDULER_Handle *sched; static const struct GNUNET_CONFIGURATION_Handle *cfg; -static struct GNUNET_PEERINFO_NewIteratorContext *ic; +static struct GNUNET_PEERINFO_IteratorContext *ic; static struct GNUNET_PEERINFO_Handle *h; @@ -93,7 +93,7 @@ add_peer () memset (&pkey, 32, sizeof (pkey)); GNUNET_CRYPTO_hash (&pkey, sizeof (pkey), &pid.hashPubKey); h2 = GNUNET_HELLO_create (&pkey, &address_generator, &agc); - GNUNET_PEERINFO_add_peer_new (h, h2); + GNUNET_PEERINFO_add_peer (h, h2); GNUNET_free (h2); } @@ -116,12 +116,12 @@ process (void *cls, /* try again */ retries++; add_peer (); - ic = GNUNET_PEERINFO_iterate_new (h, - NULL, - 0, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_SECONDS, 15), - &process, cls); + ic = GNUNET_PEERINFO_iterate (h, + NULL, + 0, + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 15), + &process, cls); return; } GNUNET_assert (peer == NULL); @@ -152,14 +152,14 @@ run (void *cls, { sched = s; cfg = c; - h = GNUNET_PEERINFO_connect (cfg, sched); + h = GNUNET_PEERINFO_connect (sched, cfg); add_peer (); - ic = GNUNET_PEERINFO_iterate_new (h, - NULL, - 0, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_SECONDS, 15), - &process, cls); + ic = GNUNET_PEERINFO_iterate (h, + NULL, + 0, + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 15), + &process, cls); } diff --git a/src/transport/gnunet-service-transport.c b/src/transport/gnunet-service-transport.c index ecf68719b..220879b3b 100644 --- a/src/transport/gnunet-service-transport.c +++ b/src/transport/gnunet-service-transport.c @@ -834,6 +834,11 @@ static struct TransportPlugin *plugins; */ static struct GNUNET_SERVER_Handle *server; +/** + * Handle to peerinfo service. + */ +static struct GNUNET_PEERINFO_Handle *peerinfo; + /** * All known neighbours and their HELLOs. */ @@ -1775,7 +1780,7 @@ refresh_hello () GNUNET_free_non_null (our_hello); our_hello = hello; our_hello_version++; - GNUNET_PEERINFO_add_peer (cfg, sched, &my_identity, our_hello); + GNUNET_PEERINFO_add_peer (peerinfo, our_hello); npos = neighbours; while (npos != NULL) { @@ -2567,7 +2572,7 @@ setup_new_neighbour (const struct GNUNET_PeerIdentity *peer, &neighbour_timeout_task, n); if (do_hello) { - n->piter = GNUNET_PEERINFO_iterate (cfg, sched, peer, + n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer, 0, GNUNET_TIME_UNIT_FOREVER_REL, &add_hello_for_peer, n); transmit_to_peer (NULL, NULL, 0, @@ -3245,8 +3250,7 @@ check_pending_validation (void *cls, hello = GNUNET_HELLO_create (&ve->publicKey, &add_validated_address, &avac); - GNUNET_PEERINFO_add_peer (cfg, sched, - &target, + GNUNET_PEERINFO_add_peer (peerinfo, hello); GNUNET_free (hello); } @@ -3596,7 +3600,7 @@ check_hello_validated (void *cls, plain_hello = GNUNET_HELLO_create (&pk, NULL, NULL); - GNUNET_PEERINFO_add_peer (cfg, sched, &target, plain_hello); + GNUNET_PEERINFO_add_peer (peerinfo, plain_hello); GNUNET_free (plain_hello); #if DEBUG_TRANSPORT GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, @@ -3732,8 +3736,7 @@ process_hello (struct TransportPlugin *plugin, chvc); /* finally, check if HELLO was previously validated (continuation will then schedule actual validation) */ - chvc->piter = GNUNET_PEERINFO_iterate (cfg, - sched, + chvc->piter = GNUNET_PEERINFO_iterate (peerinfo, &target, 0, HELLO_VERIFICATION_TIMEOUT, @@ -4788,6 +4791,22 @@ run (void *cls, return; } max_connect_per_transport = (uint32_t) tneigh; + peerinfo = GNUNET_PEERINFO_connect (sched, cfg); + if (peerinfo == NULL) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not access PEERINFO service. Exiting.\n")); + GNUNET_SCHEDULER_shutdown (s); + if (stats != NULL) + { + GNUNET_STATISTICS_destroy (stats, GNUNET_NO); + stats = NULL; + } + GNUNET_CONTAINER_multihashmap_destroy (validation_map); + validation_map = NULL; + GNUNET_free (keyfile); + return; + } my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); GNUNET_free (keyfile); if (my_private_key == NULL) -- 2.25.1