From 39589dd2116957e3265c8bd55cf1b9633b1e0b1a Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 11 Sep 2011 09:54:37 +0000 Subject: [PATCH] allow cancellation of certain transport API operations --- src/include/gnunet_transport_service.h | 38 ++++++++++++-- src/transport/transport_api_address_lookup.c | 49 ++++++++++++------- .../transport_api_peer_address_lookup.c | 43 ++++++++++------ 3 files changed, 93 insertions(+), 37 deletions(-) diff --git a/src/include/gnunet_transport_service.h b/src/include/gnunet_transport_service.h index 8435271f9..4f868e526 100644 --- a/src/include/gnunet_transport_service.h +++ b/src/include/gnunet_transport_service.h @@ -629,7 +629,6 @@ GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh); * @param hello the hello message * @param cont continuation to call when HELLO has been sent * @param cls closure for continuation - * */ void GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle, @@ -637,6 +636,12 @@ GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle, GNUNET_SCHEDULER_Task cont, void *cls); +/** + * Handle to cancel a pending address lookup. + */ +struct GNUNET_TRANSPORT_AddressLookupContext; + + /** * Convert a binary address into a human readable address. * @@ -649,8 +654,9 @@ GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle, * @param timeout how long is the lookup allowed to take at most * @param aluc function to call with the results * @param aluc_cls closure for aluc + * @return handle to cancel the operation, NULL on error */ -void +struct GNUNET_TRANSPORT_AddressLookupContext * GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *address, size_t addressLen, int numeric, const char *nameTrans, @@ -659,6 +665,21 @@ GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, void *aluc_cls); +/** + * Cancel request for address conversion. + * + * @param alc handle for the request to cancel + */ +void +GNUNET_TRANSPORT_address_lookup_cancel (struct GNUNET_TRANSPORT_AddressLookupContext *alc); + + +/** + * Handle to cancel a pending address lookup. + */ +struct GNUNET_TRANSPORT_AddressLookupContext; + + /** * Return all the known addresses for a peer. FIXME: document better! * FIXME: use better name! @@ -669,7 +690,7 @@ GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, * @param peer_address_callback function to call with the results * @param peer_address_callback_cls closure for peer_address_callback */ -void +struct GNUNET_TRANSPORT_PeerAddressLookupContext * GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_PeerIdentity *peer, @@ -679,9 +700,18 @@ GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle void *peer_address_callback_cls); +/** + * Cancel request for peer lookup. + * + * @param alc handle for the request to cancel + */ +void +GNUNET_TRANSPORT_peer_address_lookup_cancel (struct GNUNET_TRANSPORT_PeerAddressLookupContext *alc); + + /** * Return all the known addresses. FIXME: document better! - * FIXME: use better name! + * FIXME: use better name! FIXME: extend API to allow cancellation! * * @param cfg configuration to use * @param timeout how long is the lookup allowed to take at most diff --git a/src/transport/transport_api_address_lookup.c b/src/transport/transport_api_address_lookup.c index 1cbafdab0..a760f4775 100644 --- a/src/transport/transport_api_address_lookup.c +++ b/src/transport/transport_api_address_lookup.c @@ -30,7 +30,7 @@ /** * Context for the address lookup. */ -struct AddressLookupCtx +struct GNUNET_TRANSPORT_AddressLookupContext { /** * Function to call with the human-readable address. @@ -57,14 +57,14 @@ struct AddressLookupCtx /** * Function called with responses from the service. * - * @param cls our 'struct AddressLookupCtx*' + * @param cls our 'struct GNUNET_TRANSPORT_AddressLookupContext*' * @param msg NULL on timeout or error, otherwise presumably a * message with the human-readable address */ static void address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) { - struct AddressLookupCtx *alucb = cls; + struct GNUNET_TRANSPORT_AddressLookupContext *alucb = cls; const char *address; uint16_t size; @@ -115,8 +115,9 @@ address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) * @param timeout how long is the lookup allowed to take at most * @param aluc function to call with the results * @param aluc_cls closure for aluc + * @return handle to cancel the operation, NULL on error */ -void +struct GNUNET_TRANSPORT_AddressLookupContext * GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *address, size_t addressLen, int numeric, const char *nameTrans, @@ -127,7 +128,7 @@ GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, size_t slen; size_t len; struct AddressLookupMessage *msg; - struct AddressLookupCtx *aluCB; + struct GNUNET_TRANSPORT_AddressLookupContext *alc; struct GNUNET_CLIENT_Connection *client; char *addrbuf; @@ -136,15 +137,11 @@ GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) { GNUNET_break (0); - aluc (aluc_cls, NULL); - return; + return NULL; } client = GNUNET_CLIENT_connect ("transport", cfg); - if (client == NULL) - { - aluc (aluc_cls, NULL); - return; - } + if (client == NULL) + return NULL; msg = GNUNET_malloc (len); msg->header.size = htons (len); msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP); @@ -154,17 +151,33 @@ GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, addrbuf = (char *) &msg[1]; memcpy (addrbuf, address, addressLen); memcpy (&addrbuf[addressLen], nameTrans, slen); - aluCB = GNUNET_malloc (sizeof (struct AddressLookupCtx)); - aluCB->cb = aluc; - aluCB->cb_cls = aluc_cls; - aluCB->timeout = GNUNET_TIME_relative_to_absolute (timeout); - aluCB->client = client; + alc = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_AddressLookupContext)); + alc->cb = aluc; + alc->cb_cls = aluc_cls; + alc->timeout = GNUNET_TIME_relative_to_absolute (timeout); + alc->client = client; GNUNET_assert (GNUNET_OK == GNUNET_CLIENT_transmit_and_get_response (client, &msg->header, timeout, GNUNET_YES, &address_response_processor, - aluCB)); + alc)); GNUNET_free (msg); + return alc; } + +/** + * Cancel request for address conversion. + * + * @param alc handle for the request to cancel + */ +void +GNUNET_TRANSPORT_address_lookup_cancel (struct GNUNET_TRANSPORT_AddressLookupContext *alc) +{ + GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO); + GNUNET_free (alc); +} + + + /* end of transport_api_address_lookup.c */ diff --git a/src/transport/transport_api_peer_address_lookup.c b/src/transport/transport_api_peer_address_lookup.c index c84874b4b..83ed074a3 100644 --- a/src/transport/transport_api_peer_address_lookup.c +++ b/src/transport/transport_api_peer_address_lookup.c @@ -42,7 +42,7 @@ /** * Context for the address lookup. */ -struct AddressLookupCtx +struct GNUNET_TRANSPORT_PeerAddressLookupContext { /** * Function to call with the human-readable address. @@ -69,7 +69,7 @@ struct AddressLookupCtx /** * Function called with responses from the service. * - * @param cls our 'struct AddressLookupCtx*' + * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*' * @param msg NULL on timeout or error, otherwise presumably a * message with the human-readable address */ @@ -77,7 +77,7 @@ static void peer_address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) { - struct AddressLookupCtx *alucb = cls; + struct GNUNET_TRANSPORT_PeerAddressLookupContext *alucb = cls; const char *address; uint16_t size; @@ -124,8 +124,9 @@ peer_address_response_processor (void *cls, * @param timeout how long is the lookup allowed to take at most * @param peer_address_callback function to call with the results * @param peer_address_callback_cls closure for peer_address_callback + * @return handle to cancel the operation, NULL on error */ -void +struct GNUNET_TRANSPORT_PeerAddressLookupContext * GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_PeerIdentity *peer, @@ -135,29 +136,41 @@ GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle void *peer_address_callback_cls) { struct PeerAddressLookupMessage msg; - struct AddressLookupCtx *peer_address_lookup_cb; + struct GNUNET_TRANSPORT_PeerAddressLookupContext *alc; struct GNUNET_CLIENT_Connection *client; client = GNUNET_CLIENT_connect ("transport", cfg); if (client == NULL) - { - peer_address_callback (peer_address_callback_cls, NULL); - return; - } + return NULL; msg.header.size = htons (sizeof (struct PeerAddressLookupMessage)); msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP); msg.timeout = GNUNET_TIME_relative_hton (timeout); memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity)); - peer_address_lookup_cb = GNUNET_malloc (sizeof (struct AddressLookupCtx)); - peer_address_lookup_cb->cb = peer_address_callback; - peer_address_lookup_cb->cb_cls = peer_address_callback_cls; - peer_address_lookup_cb->timeout = GNUNET_TIME_relative_to_absolute (timeout); - peer_address_lookup_cb->client = client; + alc = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext)); + alc->cb = peer_address_callback; + alc->cb_cls = peer_address_callback_cls; + alc->timeout = GNUNET_TIME_relative_to_absolute (timeout); + alc->client = client; GNUNET_assert (GNUNET_OK == GNUNET_CLIENT_transmit_and_get_response (client, &msg.header, timeout, GNUNET_YES, &peer_address_response_processor, - peer_address_lookup_cb)); + alc)); + return alc; } + +/** + * Cancel request for address conversion. + * + * @param alc handle for the request to cancel + */ +void +GNUNET_TRANSPORT_peer_address_lookup_cancel (struct GNUNET_TRANSPORT_PeerAddressLookupContext *alc) +{ + GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO); + GNUNET_free (alc); +} + + /* end of transport_api_peer_address_lookup.c */ -- 2.25.1