From 0c3e3371bcc2dad9c4f14561068a1752f01b1756 Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Tue, 29 Nov 2011 15:43:11 +0000 Subject: - latest changes for refactoring: iterate sends disassembled hello-address --- src/transport/gnunet-service-transport_clients.c | 46 +++-- src/transport/gnunet-transport.c | 37 +++-- src/transport/transport.h | 20 ++- src/transport/transport_api_address_lookup.c | 203 +++++++++-------------- src/transport/transport_api_address_to_string.c | 10 +- 5 files changed, 148 insertions(+), 168 deletions(-) (limited to 'src/transport') diff --git a/src/transport/gnunet-service-transport_clients.c b/src/transport/gnunet-service-transport_clients.c index d60514565..7c50218e0 100644 --- a/src/transport/gnunet-service-transport_clients.c +++ b/src/transport/gnunet-service-transport_clients.c @@ -616,12 +616,12 @@ transmit_address_to_client (void *cls, const char *buf) if (NULL == buf) { GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); return; } GNUNET_SERVER_transmit_context_append_data (tc, buf, strlen (buf) + 1, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY); } @@ -641,12 +641,12 @@ transmit_binary_to_client (void *cls, void *buf, size_t size) if (NULL == buf) { GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); return; } GNUNET_SERVER_transmit_context_append_data (tc, buf, size, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE); } @@ -658,7 +658,7 @@ transmit_binary_to_client (void *cls, void *buf, size_t size) * @param message the resolution request */ static void -clients_handle_address_lookup (void *cls, struct GNUNET_SERVER_Client *client, +clients_handle_address_to_string (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { const struct AddressLookupMessage *alum; @@ -702,7 +702,7 @@ clients_handle_address_lookup (void *cls, struct GNUNET_SERVER_Client *client, if (NULL == papi) { GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY); GNUNET_SERVER_transmit_context_run (tc, rtimeout); return; } @@ -771,7 +771,7 @@ clients_handle_peer_address_lookup (void *cls, GST_validation_get_addresses (&peer_address_lookup->peer, &send_address_to_client, tc); GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } @@ -793,17 +793,26 @@ output_addresses (void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_SERVER_TransmitContext *tc = cls; struct AddressIterateResponseMessage *msg; size_t size; - size_t slen; + size_t tlen; + size_t alen; + char * addr; - slen = strlen (address->transport_name) + 1; - size = (sizeof (struct AddressIterateResponseMessage) + slen); + tlen = strlen (address->transport_name) + 1; + alen = address->address_length; + + size = (sizeof (struct AddressIterateResponseMessage) + alen + tlen); msg = GNUNET_malloc (size); - memcpy (&msg->peer, peer, sizeof (struct GNUNET_PeerIdentity)); - memcpy (&msg[0], address->transport_name, slen); - msg->addrlen = ntohs (address->address_length); - msg->pluginlen = ntohs (slen); - // FIXME: what about 'address->address'!? + msg->addrlen = htonl (alen); + msg->pluginlen = htonl (tlen); + msg->peer = *peer; + + addr = (char *) &msg[1]; + memcpy(addr,address->address, alen); + memcpy(&addr[alen], address->transport_name, tlen); + transmit_binary_to_client (tc, msg, size); + + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "output_addresses: peer `%s' transport %s address %s %s\n",GNUNET_i2s(&msg->peer), &addr[alen], GNUNET_a2s((struct sockaddr *) addr, alen), GNUNET_a2s((struct sockaddr *) address->address, address->address_length)); GNUNET_free (msg); } @@ -824,9 +833,10 @@ clients_handle_address_iterate (void *cls, struct GNUNET_SERVER_Client *client, GNUNET_SERVER_disable_receive_done_warning (client); tc = GNUNET_SERVER_transmit_context_create (client); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "clients_handle_address_iterate: \n"); GST_neighbours_iterate (&output_addresses, tc); GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } @@ -849,8 +859,8 @@ GST_clients_start (struct GNUNET_SERVER_Handle *server) {&clients_handle_request_connect, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof (struct TransportRequestConnectMessage)}, - {&clients_handle_address_lookup, NULL, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP, 0}, + {&clients_handle_address_to_string, NULL, + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0}, {&clients_handle_peer_address_lookup, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP, sizeof (struct PeerAddressLookupMessage)}, diff --git a/src/transport/gnunet-transport.c b/src/transport/gnunet-transport.c index 1dc85a903..58d20e16d 100644 --- a/src/transport/gnunet-transport.c +++ b/src/transport/gnunet-transport.c @@ -150,6 +150,7 @@ struct TestContext }; +struct GNUNET_CONFIGURATION_Handle * cfg; /** * Display the result of the test. @@ -432,6 +433,11 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, traffic_received += ntohs (message->size); } +void process_string (void *cls, + const char *address) +{ + fprintf (stdout, _("process_string\n")); +} /** * Function to call with a human-readable format of an address @@ -445,23 +451,23 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, static void process_address (void *cls, const struct GNUNET_HELLO_Address *address) { - if ((address->transport_name != NULL) || - ((address->address != NULL) && (address->address_length > 0))) + if (address == NULL) { - /* Call GNUNET_TRANSPORT_address_to_string to convert to human readable */ - //GNUNET_TRANSPORT_address_to_string(cfg, address, GNUNET_NO) - -#if 0 - fprintf (stdout, _("Peer `%s' plugin: `%s' address `%s'\n"), - (peer != NULL) ? GNUNET_i2s (peer) : "", - (transport != NULL) ? transport : "", ((addr != NULL) && - (addrlen > 0) && - (transport != - NULL)) ? - "how do i resolve the name without transport service?" : - ""); -#endif + return; } + + fprintf (stdout, _("Peer `%s'\n"), + GNUNET_i2s (&address->peer)); + + /* Resolve address to string */ + /* + GNUNET_TRANSPORT_address_to_string (cfg, + address, + GNUNET_NO, + GNUNET_TIME_UNIT_MINUTES, + &process_string, + NULL); + */ } @@ -477,6 +483,7 @@ static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { + cfg = cfg; if (test_configuration) { do_test_configuration (cfg); diff --git a/src/transport/transport.h b/src/transport/transport.h index 66f40cd06..97620a9b6 100644 --- a/src/transport/transport.h +++ b/src/transport/transport.h @@ -360,11 +360,24 @@ struct AddressIterateMessage * timeout to give up. FIXME: remove in the future */ struct GNUNET_TIME_AbsoluteNBO timeout; + + /** + * The identity of the peer to look up. + */ + struct GNUNET_PeerIdentity peer; + + /** + * One shot call or continous replies? + */ + uint32_t one_shot; + }; /** - * Message from the library to the transport service - * asking for human readable addresses known for a peer. + * Message from the transport service to the library + * containing binary addresses known for a peer. + * Memory layout: + * [AddressIterateResponseMessage][address[addrlen]][transportname[pluginlen]] */ struct AddressIterateResponseMessage { @@ -378,7 +391,7 @@ struct AddressIterateResponseMessage */ uint32_t reserved; - /** + /** * Peer identity */ struct GNUNET_PeerIdentity peer; @@ -392,6 +405,7 @@ struct AddressIterateResponseMessage * length of the plugin name */ uint32_t pluginlen GNUNET_PACKED; + }; diff --git a/src/transport/transport_api_address_lookup.c b/src/transport/transport_api_address_lookup.c index 385abd423..5904de5e2 100644 --- a/src/transport/transport_api_address_lookup.c +++ b/src/transport/transport_api_address_lookup.c @@ -65,7 +65,6 @@ struct GNUNET_TRANSPORT_PeerAddressLookupContext struct GNUNET_TIME_Absolute timeout; }; - /** * Function called with responses from the service. * @@ -77,46 +76,61 @@ static void peer_address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) { - struct GNUNET_TRANSPORT_PeerAddressLookupContext *alucb = cls; + struct GNUNET_TRANSPORT_PeerAddressLookupContext *pal_ctx = cls; + struct AddressIterateResponseMessage *air_msg = (struct AddressIterateResponseMessage *) &msg[1]; const struct GNUNET_HELLO_Address *address; uint16_t size; - - if (msg == NULL) + if (air_msg == NULL) { - alucb->cb (alucb->cb_cls, NULL); - GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); - GNUNET_free (alucb); + pal_ctx->cb (pal_ctx->cb_cls, NULL); + GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO); + GNUNET_free (pal_ctx); return; } - GNUNET_break (ntohs (msg->type) == - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); size = ntohs (msg->size); + GNUNET_break (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE); if (size == sizeof (struct GNUNET_MessageHeader)) { /* done! */ - alucb->cb (alucb->cb_cls, NULL ); - GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); - GNUNET_free (alucb); + pal_ctx->cb (pal_ctx->cb_cls, NULL ); + GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO); + GNUNET_free (pal_ctx); return; } - address = (const struct GNUNET_HELLO_Address *) &msg[1]; -#if 0 - if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0') + + size_t tlen = ntohl(air_msg->pluginlen); + size_t alen = ntohl(air_msg->addrlen); + + if (size != sizeof (struct GNUNET_MessageHeader) + sizeof (struct AddressIterateResponseMessage) + tlen + alen) + { + GNUNET_break_op (0); + pal_ctx->cb (pal_ctx->cb_cls, NULL ); + GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO); + GNUNET_free (pal_ctx); + return; + } + + char * addr = (char *) &air_msg[1]; + char * transport_name = &addr[alen]; + + if (transport_name[tlen] != '\0') { - /* invalid reply */ - GNUNET_break (0); - alucb->cb (alucb->cb_cls, NULL ); - GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); - GNUNET_free (alucb); + GNUNET_break_op (0); + pal_ctx->cb (pal_ctx->cb_cls, NULL ); + GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO); + GNUNET_free (pal_ctx); return; } -#endif + + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "peer %s transport_name: %s\n",GNUNET_i2s(&air_msg->peer), transport_name); + + address = GNUNET_HELLO_address_allocate(&air_msg->peer, transport_name, addr, alen); + /* expect more replies */ - GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb, - GNUNET_TIME_absolute_get_remaining (alucb->timeout)); + GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor, pal_ctx, + GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout)); - /* REFACTOR FIX THIS */ - alucb->cb (alucb->cb_cls, address ); + pal_ctx->cb (pal_ctx->cb_cls, address); } @@ -132,35 +146,45 @@ peer_address_response_processor (void *cls, */ struct GNUNET_TRANSPORT_PeerAddressLookupContext * GNUNET_TRANSPORT_peer_get_active_addresses (const struct GNUNET_CONFIGURATION_Handle *cfg, - const struct GNUNET_PeerIdentity *peer, - int one_shot, - struct GNUNET_TIME_Relative timeout, - GNUNET_TRANSPORT_AddressLookUpCallback peer_address_callback, - void *peer_address_callback_cls) + const struct GNUNET_PeerIdentity *peer, + int one_shot, + struct GNUNET_TIME_Relative timeout, + GNUNET_TRANSPORT_AddressLookUpCallback peer_address_callback, + void *peer_address_callback_cls) { - struct PeerAddressLookupMessage msg; - struct GNUNET_TRANSPORT_PeerAddressLookupContext *alc; + struct GNUNET_TRANSPORT_PeerAddressLookupContext *pal_ctx; + struct AddressIterateMessage msg; struct GNUNET_CLIENT_Connection *client; + struct GNUNET_TIME_Absolute abs_timeout; client = GNUNET_CLIENT_connect ("transport", cfg); if (client == NULL) + { + peer_address_callback (peer_address_callback_cls, NULL); return NULL; - msg.header.size = htons (sizeof (struct PeerAddressLookupMessage)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP); - msg.reserved = htonl (0); - msg.timeout = GNUNET_TIME_relative_hton (timeout); - memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity)); - 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; + } + + abs_timeout = GNUNET_TIME_relative_to_absolute (timeout); + + msg.header.size = htons (sizeof (struct AddressIterateMessage)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE); + msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout); + if (peer == NULL) + memset (&msg.peer, 0 , sizeof (struct GNUNET_PeerIdentity)); + else + memcpy (&msg.peer, peer , sizeof (struct GNUNET_PeerIdentity)); + + pal_ctx = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext)); + pal_ctx->cb = peer_address_callback; + pal_ctx->cb_cls = peer_address_callback_cls; + pal_ctx->timeout = abs_timeout; + pal_ctx->client = client; GNUNET_assert (GNUNET_OK == GNUNET_CLIENT_transmit_and_get_response (client, &msg.header, timeout, GNUNET_YES, &peer_address_response_processor, - alc)); - return alc; + pal_ctx)); + return pal_ctx; } @@ -179,64 +203,7 @@ GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct } /** - * Function called with responses from the service. - * - * @param cls our 'struct AddressLookupCtx*' - * @param msg NULL on timeout or error, otherwise presumably a - * message with the human-readable peer and address - */ -static void -peer_address_iteration_response_processor (void *cls, - const struct GNUNET_MessageHeader *msg) -{ - struct GNUNET_TRANSPORT_PeerAddressLookupContext *alucb = cls; - struct AddressIterateResponseMessage *arm; - struct GNUNET_HELLO_Address * address; - uint16_t size; - - if (msg == NULL) - { - alucb->cb (alucb->cb_cls, NULL); - GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); - GNUNET_free (alucb); - return; - } - - GNUNET_break (ntohs (msg->type) == - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); - size = ntohs (msg->size); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %u size %u\n", - ntohs (msg->type), size); - if (size == sizeof (struct GNUNET_MessageHeader)) - { - /* done! */ - alucb->cb (alucb->cb_cls, NULL); - GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); - GNUNET_free (alucb); - return; - } - if (size < sizeof (struct AddressIterateResponseMessage)) - { - /* invalid reply */ - GNUNET_break (0); - alucb->cb (alucb->cb_cls, NULL); - GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); - GNUNET_free (alucb); - return; - } - - arm = (struct AddressIterateResponseMessage *) &msg[1]; - address = (struct GNUNET_HELLO_Address *) &arm[1]; - - /* expect more replies */ - GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb, - GNUNET_TIME_absolute_get_remaining (alucb->timeout)); - alucb->cb (alucb->cb_cls, address); -} - - -/** - * Return all the known addresses for a peer. + * Return all the known addresses for all peers. * * @param cfg configuration to use * @param timeout how long is the lookup allowed to take at most @@ -250,32 +217,12 @@ GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, peer_address_callback, void *peer_address_callback_cls) { - struct AddressIterateMessage msg; - struct GNUNET_TIME_Absolute abs_timeout; - struct GNUNET_TRANSPORT_PeerAddressLookupContext *peer_address_lookup_cb; - struct GNUNET_CLIENT_Connection *client; - - client = GNUNET_CLIENT_connect ("transport", cfg); - if (client == NULL) - { - peer_address_callback (peer_address_callback_cls, NULL); - return; - } - abs_timeout = GNUNET_TIME_relative_to_absolute (timeout); - - msg.header.size = htons (sizeof (struct AddressIterateMessage)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE); - msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout); - peer_address_lookup_cb = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext)); - peer_address_lookup_cb->cb = peer_address_callback; - peer_address_lookup_cb->cb_cls = peer_address_callback_cls; - peer_address_lookup_cb->timeout = abs_timeout; - peer_address_lookup_cb->client = client; - GNUNET_assert (GNUNET_OK == - GNUNET_CLIENT_transmit_and_get_response (client, &msg.header, - timeout, GNUNET_YES, - &peer_address_iteration_response_processor, - peer_address_lookup_cb)); + GNUNET_TRANSPORT_peer_get_active_addresses (cfg, + NULL, + GNUNET_YES, + timeout, + peer_address_callback, + peer_address_callback_cls); } /* end of transport_api_peer_address_lookup.c */ diff --git a/src/transport/transport_api_address_to_string.c b/src/transport/transport_api_address_to_string.c index 259c77152..2e5a34ce2 100644 --- a/src/transport/transport_api_address_to_string.c +++ b/src/transport/transport_api_address_to_string.c @@ -76,7 +76,7 @@ address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) return; } GNUNET_break (ntohs (msg->type) == - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE); size = ntohs (msg->size); if (size == sizeof (struct GNUNET_MessageHeader)) { @@ -108,10 +108,8 @@ address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) * * @param cfg configuration to use * @param address address to convert (binary format) - * @param addressLen number of bytes in address * @param numeric should (IP) addresses be displayed in numeric form * (otherwise do reverse DNS lookup) - * @param nameTrans name of the transport to which the address belongs * @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 @@ -132,6 +130,7 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cf struct GNUNET_CLIENT_Connection *client; char *addrbuf; + GNUNET_assert (address != NULL); alen = GNUNET_HELLO_address_get_size (address); len = sizeof (struct AddressLookupMessage) + alen; if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) @@ -139,12 +138,15 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cf GNUNET_break (0); return NULL; } + client = GNUNET_CLIENT_connect ("transport", cfg); if (client == NULL) return NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "GNUNET_TRANSPORT_address_to_string\n"); msg = GNUNET_malloc (len); msg->header.size = htons (len); - msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP); + msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING); msg->numeric_only = htonl (numeric); msg->timeout = GNUNET_TIME_relative_hton (timeout); msg->addrlen = htonl (alen); -- cgit v1.2.3