+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_hello_lib.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet_transport_service.h"
+#include "gnunet_peerinfo_service.h"
+#include "gnunet_ats_service.h"
+#include "gnunet-service-transport.h"
+#include "gnunet-service-transport_ats.h"
+#include "gnunet-service-transport_hello.h"
+#include "gnunet-service-transport_neighbours.h"
+#include "gnunet-service-transport_plugins.h"
+#include "gnunet-service-transport_validation.h"
+#include "gnunet-service-transport_manipulation.h"
+#include "transport.h"
+
+/**
+ * Size of the blacklist hash map.
+ */
+#define TRANSPORT_BLACKLIST_HT_SIZE 64
+
+/**
+ * How many messages can we have pending for a given client process
+ * before we start to drop incoming messages? We typically should
+ * have only one client and so this would be the primary buffer for
+ * messages, so the number should be chosen rather generously.
+ *
+ * The expectation here is that most of the time the queue is large
+ * enough so that a drop is virtually never required. Note that
+ * this value must be about as large as 'TOTAL_MSGS' in the
+ * 'test_transport_api_reliability.c', otherwise that testcase may
+ * fail.
+ */
+#define MAX_PENDING (128 * 1024)
+
+
+/**
+ * Information we need for an asynchronous session kill.
+ */
+struct GNUNET_ATS_SessionKiller
+{
+ /**
+ * Kept in a DLL.
+ */
+ struct GNUNET_ATS_SessionKiller *next;
+
+ /**
+ * Kept in a DLL.
+ */
+ struct GNUNET_ATS_SessionKiller *prev;
+
+ /**
+ * Session to kill.
+ */
+ struct GNUNET_ATS_Session *session;
+
+ /**
+ * Plugin for the session.
+ */
+ struct GNUNET_TRANSPORT_PluginFunctions *plugin;
+
+ /**
+ * The kill task.
+ */
+ struct GNUNET_SCHEDULER_Task *task;
+};
+
+
+/**
+ * What type of client is the `struct TransportClient` about?
+ */
+enum ClientType
+{
+ /**
+ * We do not know yet (client is fresh).
+ */
+ CT_NONE = 0,
+
+ /**
+ * Is the CORE service, we need to forward traffic to it.
+ */
+ CT_CORE = 1,
+
+ /**
+ * It is a monitor, forward monitor data.
+ */
+ CT_MONITOR = 2,
+
+ /**
+ * It is a blacklist, query about allowed connections.
+ */
+ CT_BLACKLIST = 3,
+
+ /**
+ * CORE client without any handlers.
+ */
+ CT_CORE_NO_HANDLERS = 4
+};
+
+
+/**
+ * Context we use when performing a blacklist check.
+ */
+struct GST_BlacklistCheck;
+
+/**
+ * Client connected to the transport service.
+ */
+struct TransportClient
+{
+ /**
+ * This is a doubly-linked list.
+ */
+ struct TransportClient *next;
+
+ /**
+ * This is a doubly-linked list.
+ */
+ struct TransportClient *prev;
+
+ /**
+ * Handle to the client.
+ */
+ struct GNUNET_SERVICE_Client *client;
+
+ /**
+ * Message queue to the client.
+ */
+ struct GNUNET_MQ_Handle *mq;
+
+ /**
+ * What type of client is this?
+ */
+ enum ClientType type;
+
+ union
+ {
+ /**
+ * Peer identity to monitor the addresses of.
+ * Zero to monitor all neighbours. Valid if
+ * @e type is CT_MONITOR.
+ */
+ struct GNUNET_PeerIdentity monitor_peer;
+
+ /**
+ * Additional details if @e type is CT_BLACKLIST.
+ */
+ struct
+ {
+ /**
+ * Blacklist check that we're currently performing (or NULL
+ * if we're performing one that has been cancelled).
+ */
+ struct GST_BlacklistCheck *bc;
+
+ /**
+ * Set to #GNUNET_YES if we're currently waiting for a reply.
+ */
+ int waiting_for_reply;
+
+ /**
+ * #GNUNET_YES if we have to call receive_done for this client
+ */
+ int call_receive_done;
+ } blacklist;
+ } details;
+};
+
+
+/**
+ * Context we use when performing a blacklist check.
+ */
+struct GST_BlacklistCheck
+{
+ /**
+ * This is a linked list.
+ */
+ struct GST_BlacklistCheck *next;
+
+ /**
+ * This is a linked list.
+ */
+ struct GST_BlacklistCheck *prev;
+
+ /**
+ * Peer being checked.
+ */
+ struct GNUNET_PeerIdentity peer;
+
+ /**
+ * Continuation to call with the result.
+ */
+ GST_BlacklistTestContinuation cont;
+
+ /**
+ * Closure for @e cont.
+ */
+ void *cont_cls;
+
+ /**
+ * Address for #GST_blacklist_abort_matching(), can be NULL.
+ */
+ struct GNUNET_HELLO_Address *address;
+
+ /**
+ * Session for #GST_blacklist_abort_matching(), can be NULL.
+ */
+ struct GNUNET_ATS_Session *session;
+
+ /**
+ * Our current position in the blacklisters list.
+ */
+ struct TransportClient *bl_pos;
+
+ /**
+ * Current task performing the check.
+ */
+ struct GNUNET_SCHEDULER_Task *task;
+};
+
+
+/**
+ * Context for address to string operations
+ */
+struct AddressToStringContext
+{
+ /**
+ * This is a doubly-linked list.
+ */
+ struct AddressToStringContext *next;
+
+ /**
+ * This is a doubly-linked list.
+ */
+ struct AddressToStringContext *prev;
+
+ /**
+ * Client that made the request.
+ */
+ struct TransportClient *tc;
+};
+
+
+/**
+ * Closure for #handle_send_transmit_continuation()
+ */
+struct SendTransmitContinuationContext
+{
+ /**
+ * Client that made the request.
+ */
+ struct TransportClient *tc;
+
+ /**
+ * Peer that was the target.
+ */
+ struct GNUNET_PeerIdentity target;
+
+ /**
+ * At what time did we receive the message?
+ */
+ struct GNUNET_TIME_Absolute send_time;
+
+ /**
+ * Unique ID, for logging.
+ */
+ unsigned long long uuid;
+
+ /**
+ * Set to #GNUNET_YES if the connection for @e target goes
+ * down and we thus must no longer send the
+ * #GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK message.
+ */
+ int down;
+};
+
+
+/**
+ * Head of linked list of all clients to this service.
+ */
+static struct TransportClient *clients_head;
+
+/**
+ * Tail of linked list of all clients to this service.
+ */
+static struct TransportClient *clients_tail;
+
+/**
+ * Map of peer identities to active send transmit continuation
+ * contexts. Used to flag contexts as 'dead' when a connection goes
+ * down. Values are of type `struct SendTransmitContinuationContext
+ * *`.
+ */
+static struct GNUNET_CONTAINER_MultiPeerMap *active_stccs;
+
+/**
+ * Head of linked list of all pending address iterations
+ */
+static struct AddressToStringContext *a2s_head;
+
+/**
+ * Tail of linked list of all pending address iterations
+ */
+static struct AddressToStringContext *a2s_tail;
+
+/**
+ * Head of DLL of active blacklisting queries.
+ */
+static struct GST_BlacklistCheck *bc_head;
+
+/**
+ * Tail of DLL of active blacklisting queries.
+ */
+static struct GST_BlacklistCheck *bc_tail;
+
+/**
+ * Hashmap of blacklisted peers. Values are of type 'char *' (transport names),
+ * can be NULL if we have no static blacklist.
+ */
+static struct GNUNET_CONTAINER_MultiPeerMap *blacklist;
+
+/**
+ * Notification context, to send updates on changes to active plugin
+ * connections.
+ */
+static struct GNUNET_NotificationContext *plugin_nc;
+
+/**
+ * Plugin monitoring client we are currently syncing, NULL if all
+ * monitoring clients are in sync.
+ */
+static struct TransportClient *sync_client;
+
+/**
+ * Peer identity that is all zeros, used as a way to indicate
+ * "all peers". Used for comparissons.
+ */
+static struct GNUNET_PeerIdentity all_zeros;
+
+/**
+ * Statistics handle.
+ */
+struct GNUNET_STATISTICS_Handle *GST_stats;
+
+/**
+ * Configuration handle.
+ */
+const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
+
+/**
+ * Configuration handle.
+ */
+struct GNUNET_PeerIdentity GST_my_identity;
+
+/**
+ * Handle to peerinfo service.
+ */
+struct GNUNET_PEERINFO_Handle *GST_peerinfo;
+
+/**
+ * Our private key.
+ */
+struct GNUNET_CRYPTO_EddsaPrivateKey *GST_my_private_key;
+
+/**
+ * ATS scheduling handle.
+ */
+struct GNUNET_ATS_SchedulingHandle *GST_ats;
+
+/**
+ * ATS connectivity handle.
+ */
+struct GNUNET_ATS_ConnectivityHandle *GST_ats_connect;
+
+/**
+ * Hello address expiration
+ */
+struct GNUNET_TIME_Relative hello_expiration;
+
+/**
+ * Head of DLL of asynchronous tasks to kill sessions.
+ */
+static struct GNUNET_ATS_SessionKiller *sk_head;
+
+/**
+ * Tail of DLL of asynchronous tasks to kill sessions.
+ */
+static struct GNUNET_ATS_SessionKiller *sk_tail;
+
+/**
+ * Interface scanner determines our LAN address range(s).
+ */
+struct GNUNET_NT_InterfaceScanner *GST_is;
+
+/**
+ * Queue the given message for transmission to the given client
+ *
+ * @param tc target of the message
+ * @param msg message to transmit
+ * @param may_drop #GNUNET_YES if the message can be dropped
+ */
+static void
+unicast (struct TransportClient *tc,
+ const struct GNUNET_MessageHeader *msg,
+ int may_drop)
+{
+ struct GNUNET_MQ_Envelope *env;
+
+ if ((GNUNET_MQ_get_length (tc->mq) >= MAX_PENDING) &&
+ (GNUNET_YES == may_drop))
+ {
+ GNUNET_log (
+ GNUNET_ERROR_TYPE_DEBUG,
+ "Dropping message of type %u and size %u, have %u/%u messages pending\n",
+ ntohs (msg->type),
+ ntohs (msg->size),
+ GNUNET_MQ_get_length (tc->mq),
+ MAX_PENDING);
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop (
+ "# messages dropped due to slow client"),
+ 1,
+ GNUNET_NO);
+ return;
+ }
+ env = GNUNET_MQ_msg_copy (msg);
+ GNUNET_MQ_send (tc->mq, env);
+}
+
+
+/**
+ * Called whenever a client connects. Allocates our
+ * data structures associated with that client.
+ *
+ * @param cls closure, NULL
+ * @param client identification of the client
+ * @param mq message queue for the client
+ * @return our `struct TransportClient`
+ */
+static void *
+client_connect_cb (void *cls,
+ struct GNUNET_SERVICE_Client *client,
+ struct GNUNET_MQ_Handle *mq)
+{
+ struct TransportClient *tc;
+
+ tc = GNUNET_new (struct TransportClient);
+ tc->client = client;
+ tc->mq = mq;
+ GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", tc);
+ return tc;
+}
+
+
+/**
+ * Perform next action in the blacklist check.
+ *
+ * @param cls the `struct BlacklistCheck*`
+ */
+static void
+do_blacklist_check (void *cls);
+
+
+/**
+ * Mark the peer as down so we don't call the continuation
+ * context in the future.
+ *
+ * @param cls a `struct TransportClient`
+ * @param peer a peer we are sending to
+ * @param value a `struct SendTransmitContinuationContext` to mark
+ * @return #GNUNET_OK (continue to iterate)
+ */
+static int
+mark_match_down (void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
+{
+ struct TransportClient *tc = cls;
+ struct SendTransmitContinuationContext *stcc = value;
+
+ if (tc == stcc->tc)
+ {
+ stcc->down = GNUNET_YES;
+ stcc->tc = NULL;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Called whenever a client is disconnected. Frees our
+ * resources associated with that client.
+ *
+ * @param cls closure, NULL
+ * @param client identification of the client
+ * @param app_ctx our `struct TransportClient`
+ */
+static void
+client_disconnect_cb (void *cls,
+ struct GNUNET_SERVICE_Client *client,
+ void *app_ctx)
+{
+ struct TransportClient *tc = app_ctx;
+ struct GST_BlacklistCheck *bc;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client %p disconnected, cleaning up.\n",
+ tc);
+ GNUNET_CONTAINER_multipeermap_iterate (active_stccs, &mark_match_down, tc);
+ for (struct AddressToStringContext *cur = a2s_head; NULL != cur;
+ cur = cur->next)
+ {
+ if (cur->tc == tc)
+ cur->tc = NULL;
+ }
+ GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
+ switch (tc->type)
+ {
+ case CT_NONE:
+ break;
+
+ case CT_CORE:
+ break;
+
+ case CT_MONITOR:
+ break;
+
+ case CT_BLACKLIST:
+ for (bc = bc_head; NULL != bc; bc = bc->next)
+ {
+ if (bc->bl_pos != tc)
+ continue;
+ bc->bl_pos = tc->next;
+ if (NULL == bc->task)
+ bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
+ }
+ break;
+
+ case CT_CORE_NO_HANDLERS:
+ break;
+ }
+ GNUNET_free (tc);
+}
+
+
+/**
+ * Function called for each of our connected neighbours. Notify the
+ * client about the existing neighbour.
+ *
+ * @param cls the `struct TransportClient *` to notify
+ * @param peer identity of the neighbour
+ * @param address the address
+ * @param state the current state of the peer
+ * @param state_timeout the time out for the state
+ * @param bandwidth_in inbound bandwidth in NBO
+ * @param bandwidth_out outbound bandwidth in NBO
+ */
+static void
+notify_client_about_neighbour (void *cls,
+ const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_HELLO_Address *address,
+ enum GNUNET_TRANSPORT_PeerState state,
+ struct GNUNET_TIME_Absolute state_timeout,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
+{
+ struct TransportClient *tc = cls;
+ struct ConnectInfoMessage cim;
+
+ if (GNUNET_NO == GST_neighbours_test_connected (peer))
+ return;
+ cim.header.size = htons (sizeof(struct ConnectInfoMessage));
+ cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
+ cim.id = *peer;
+ cim.quota_out = bandwidth_out;
+ unicast (tc, &cim.header, GNUNET_NO);
+}
+
+
+/**
+ * Initialize a normal client. We got a start message from this
+ * client, add it to the list of clients for broadcasting of inbound
+ * messages.
+ *
+ * @param cls the client
+ * @param start the start message that was sent
+ */
+static void
+handle_client_start (void *cls, const struct StartMessage *start)
+{
+ struct TransportClient *tc = cls;
+ const struct GNUNET_MessageHeader *hello;
+ uint32_t options;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p sent START\n", tc);
+ options = ntohl (start->options);
+ if ((0 != (1 & options)) &&
+ (0 != memcmp (&start->self,
+ &GST_my_identity,
+ sizeof(struct GNUNET_PeerIdentity))))
+ {
+ /* client thinks this is a different peer, reject */
+ GNUNET_break (0);
+ GNUNET_SERVICE_client_drop (tc->client);
+ return;
+ }
+ if (CT_NONE != tc->type)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVICE_client_drop (tc->client);
+ return;
+ }
+ if (0 != (2 & options))
+ tc->type = CT_CORE;
+ else
+ tc->type = CT_CORE_NO_HANDLERS;
+ hello = GST_hello_get ();
+ if (NULL != hello)
+ unicast (tc, hello, GNUNET_NO);
+ GST_neighbours_iterate (¬ify_client_about_neighbour, tc);
+ GNUNET_SERVICE_client_continue (tc->client);
+}
+
+
+/**
+ * Client sent us a HELLO. Check the request.
+ *
+ * @param cls the client
+ * @param message the HELLO message
+ */
+static int
+check_client_hello (void *cls, const struct GNUNET_MessageHeader *message)
+{
+ return GNUNET_OK; /* FIXME: check here? */
+}
+
+
+/**
+ * Client sent us a HELLO. Process the request.
+ *
+ * @param cls the client
+ * @param message the HELLO message
+ */
+static void
+handle_client_hello (void *cls, const struct GNUNET_MessageHeader *message)
+{
+ struct TransportClient *tc = cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received HELLO message\n");
+ GST_validation_handle_hello (message);
+ GNUNET_SERVICE_client_continue (tc->client);
+}
+
+
+/**
+ * Function called after the transmission is done. Notify the client that it is
+ * OK to send the next message.
+ *
+ * @param cls closure
+ * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
+ * @param bytes_payload bytes payload sent
+ * @param bytes_on_wire bytes sent on wire
+ */
+static void
+handle_send_transmit_continuation (void *cls,
+ int success,
+ size_t bytes_payload,
+ size_t bytes_on_wire)
+{
+ struct SendTransmitContinuationContext *stcc = cls;
+ struct SendOkMessage send_ok_msg;
+ struct GNUNET_TIME_Relative delay;
+ const struct GNUNET_HELLO_Address *addr;
+
+ delay = GNUNET_TIME_absolute_get_duration (stcc->send_time);
+ addr = GST_neighbour_get_current_address (&stcc->target);
+ if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
+ GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES),
+ (unsigned int) bytes_payload,
+ (unsigned int) bytes_on_wire,
+ GNUNET_i2s (&stcc->target),
+ success,
+ (NULL != addr) ? addr->transport_name : "%");
+ else
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
+ GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES),
+ (unsigned int) bytes_payload,
+ (unsigned int) bytes_on_wire,
+ GNUNET_i2s (&stcc->target),
+ success,
+ (NULL != addr) ? addr->transport_name : "%");
+
+ if (GNUNET_NO == stcc->down)
+ {
+ /* Only send confirmation if we are still connected */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Sending SEND_OK for transmission request %llu\n",
+ stcc->uuid);
+ send_ok_msg.header.size = htons (sizeof(send_ok_msg));
+ send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
+ send_ok_msg.bytes_msg = htonl (bytes_payload);
+ send_ok_msg.bytes_physical = htonl (bytes_on_wire);
+ send_ok_msg.success = htonl (success);
+ send_ok_msg.peer = stcc->target;
+ unicast (stcc->tc, &send_ok_msg.header, GNUNET_NO);
+ }
+ GNUNET_assert (
+ GNUNET_OK ==
+ GNUNET_CONTAINER_multipeermap_remove (active_stccs, &stcc->target, stcc));
+ GNUNET_free (stcc);
+}
+
+
+/**
+ * Client asked for transmission to a peer. Process the request.
+ *
+ * @param cls the client
+ * @param obm the send message that was sent
+ */
+static int
+check_client_send (void *cls, const struct OutboundMessage *obm)
+{
+ uint16_t size;
+ const struct GNUNET_MessageHeader *obmm;
+
+ size = ntohs (obm->header.size) - sizeof(struct OutboundMessage);
+ if (size < sizeof(struct GNUNET_MessageHeader))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ obmm = (const struct GNUNET_MessageHeader *) &obm[1];
+ if (size != ntohs (obmm->size))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Client asked for transmission to a peer. Process the request.
+ *
+ * @param cls the client
+ * @param obm the send message that was sent
+ */
+static void
+handle_client_send (void *cls, const struct OutboundMessage *obm)
+{
+ static unsigned long long uuid_gen;
+ struct TransportClient *tc = cls;
+ const struct GNUNET_MessageHeader *obmm;
+ struct SendTransmitContinuationContext *stcc;
+
+ obmm = (const struct GNUNET_MessageHeader *) &obm[1];
+ if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
+ {
+ /* not connected, not allowed to send; can happen due to asynchronous operations */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Could not send message to peer `%s': not connected\n",
+ GNUNET_i2s (&obm->peer));
+ GNUNET_STATISTICS_update (
+ GST_stats,
+ gettext_noop ("# bytes payload dropped (other peer was not connected)"),
+ ntohs (obmm->size),
+ GNUNET_NO);
+ GNUNET_SERVICE_client_continue (tc->client);
+ return;
+ }
+ GNUNET_log (
+ GNUNET_ERROR_TYPE_DEBUG,
+ "Received SEND request %llu for `%s' and first message of type %u and total size %u\n",
+ uuid_gen,
+ GNUNET_i2s (&obm->peer),
+ ntohs (obmm->type),
+ ntohs (obmm->size));
+ GNUNET_SERVICE_client_continue (tc->client);
+
+ stcc = GNUNET_new (struct SendTransmitContinuationContext);
+ stcc->target = obm->peer;
+ stcc->tc = tc;
+ stcc->send_time = GNUNET_TIME_absolute_get ();
+ stcc->uuid = uuid_gen++;
+ (void) GNUNET_CONTAINER_multipeermap_put (
+ active_stccs,
+ &stcc->target,
+ stcc,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ GST_manipulation_send (&obm->peer,
+ obmm,
+ ntohs (obmm->size),
+ GNUNET_TIME_relative_ntoh (obm->timeout),
+ &handle_send_transmit_continuation,
+ stcc);
+}
+
+
+/**
+ * Take the given address and append it to the set of results sent back to
+ * the client. This function may be called serveral times for a single
+ * conversion. The last invocation will be with a @a address of
+ * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
+ * errors, the callback might be called first with @a address NULL and
+ * @a res being #GNUNET_SYSERR. In that case, there will still be a
+ * subsequent call later with @a address NULL and @a res #GNUNET_OK.
+ *
+ * @param cls the `struct AddressToStringContext`
+ * @param buf text to transmit (contains the human-readable address, or NULL)
+ * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on error,
+ * never #GNUNET_NO
+ */
+static void
+transmit_address_to_client (void *cls, const char *buf, int res)
+{
+ struct AddressToStringContext *actx = cls;
+ struct GNUNET_MQ_Envelope *env;
+ struct AddressToStringResultMessage *atsm;
+ size_t slen;
+
+ GNUNET_assert ((GNUNET_OK == res) || (GNUNET_SYSERR == res));
+ if (NULL == actx->tc)
+ return;
+ if (NULL == buf)
+ {
+ env = GNUNET_MQ_msg (atsm,
+ GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
+ if (GNUNET_OK == res)
+ {
+ /* this was the last call, transmit */
+ atsm->res = htonl (GNUNET_OK);
+ atsm->addr_len = htonl (0);
+ GNUNET_MQ_send (actx->tc->mq, env);
+ GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, actx);
+ GNUNET_free (actx);
+ return;
+ }
+ if (GNUNET_SYSERR == res)
+ {
+ /* address conversion failed, but there will be more callbacks */
+ atsm->res = htonl (GNUNET_SYSERR);
+ atsm->addr_len = htonl (0);
+ GNUNET_MQ_send (actx->tc->mq, env);
+ return;
+ }
+ }
+ GNUNET_assert (GNUNET_OK == res);
+ /* succesful conversion, append*/
+ slen = strlen (buf) + 1;
+ env =
+ GNUNET_MQ_msg_extra (atsm,
+ slen,
+ GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
+ atsm->res = htonl (GNUNET_YES);
+ atsm->addr_len = htonl (slen);
+ GNUNET_memcpy (&atsm[1], buf, slen);
+ GNUNET_MQ_send (actx->tc->mq, env);
+}
+
+
+/**
+ * Client asked to resolve an address. Check the request.
+ *
+ * @param cls the client
+ * @param alum the resolution request
+ * @return #GNUNET_OK if @a alum is well-formed
+ */
+static int
+check_client_address_to_string (void *cls,
+ const struct AddressLookupMessage *alum)
+{
+ const char *plugin_name;
+ const char *address;
+ uint32_t address_len;
+ uint16_t size;
+
+ size = ntohs (alum->header.size);
+ address_len = ntohs (alum->addrlen);
+ if (size <= sizeof(struct AddressLookupMessage) + address_len)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ address = (const char *) &alum[1];
+ plugin_name = (const char *) &address[address_len];
+ if ('\0' != plugin_name[size - sizeof(struct AddressLookupMessage)
+ - address_len - 1])
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Client asked to resolve an address. Process the request.
+ *
+ * @param cls the client
+ * @param alum the resolution request
+ */
+static void
+handle_client_address_to_string (void *cls,
+ const struct AddressLookupMessage *alum)
+{
+ struct TransportClient *tc = cls;
+ struct GNUNET_TRANSPORT_PluginFunctions *papi;
+ const char *plugin_name;
+ const char *address;
+ uint32_t address_len;
+ struct AddressToStringContext *actx;
+ struct GNUNET_MQ_Envelope *env;
+ struct AddressToStringResultMessage *atsm;
+ struct GNUNET_TIME_Relative rtimeout;
+ int32_t numeric;
+
+ address_len = ntohs (alum->addrlen);
+ address = (const char *) &alum[1];
+ plugin_name = (const char *) &address[address_len];
+ rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
+ numeric = ntohs (alum->numeric_only);
+ papi = GST_plugins_printer_find (plugin_name);
+ if (NULL == papi)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Failed to find plugin `%s'\n",
+ plugin_name);
+ env = GNUNET_MQ_msg (atsm,
+ GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
+ atsm->res = htonl (GNUNET_SYSERR);
+ atsm->addr_len = htonl (0);
+ GNUNET_MQ_send (tc->mq, env);
+ env = GNUNET_MQ_msg (atsm,
+ GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
+ atsm->res = htonl (GNUNET_OK);
+ atsm->addr_len = htonl (0);
+ GNUNET_MQ_send (tc->mq, env);
+ return;
+ }
+ actx = GNUNET_new (struct AddressToStringContext);
+ actx->tc = tc;
+ GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
+ GNUNET_SERVICE_client_disable_continue_warning (tc->client);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Pretty-printing address of %u bytes using plugin `%s'\n",
+ address_len,
+ plugin_name);
+ papi->address_pretty_printer (papi->cls,
+ plugin_name,
+ address,
+ address_len,
+ numeric,
+ rtimeout,
+ &transmit_address_to_client,
+ actx);
+}