+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);
+}
+
+
+/**
+ * Compose #PeerIterateResponseMessage using the given peer and address.
+ *
+ * @param peer identity of the peer
+ * @param address the address, NULL on disconnect
+ * @return composed message
+ */
+static struct PeerIterateResponseMessage *
+compose_address_iterate_response_message (
+ const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_HELLO_Address *address)
+{
+ struct PeerIterateResponseMessage *msg;
+ size_t size;
+ size_t tlen;
+ size_t alen;
+ char *addr;
+
+ GNUNET_assert (NULL != peer);
+ if (NULL != address)
+ {
+ tlen = strlen (address->transport_name) + 1;
+ alen = address->address_length;
+ }
+ else
+ {
+ tlen = 0;
+ alen = 0;
+ }
+ size = (sizeof(struct PeerIterateResponseMessage) + alen + tlen);
+ msg = GNUNET_malloc (size);
+ msg->header.size = htons (size);
+ msg->header.type =
+ htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
+ msg->reserved = htonl (0);
+ msg->peer = *peer;
+ msg->addrlen = htonl (alen);
+ msg->pluginlen = htonl (tlen);
+
+ if (NULL != address)
+ {
+ msg->local_address_info = htonl ((uint32_t) address->local_info);
+ addr = (char *) &msg[1];
+ GNUNET_memcpy (addr, address->address, alen);
+ GNUNET_memcpy (&addr[alen], address->transport_name, tlen);
+ }
+ return msg;
+}
+
+
+/**
+ * Context for #send_validation_information() and
+ * #send_peer_information().
+ */
+struct IterationContext
+{
+ /**
+ * Context to use for the transmission.
+ */
+ struct TransportClient *tc;
+
+ /**
+ * Which peers do we care about?
+ */
+ struct GNUNET_PeerIdentity id;
+
+ /**
+ * #GNUNET_YES if @e id should be ignored because we want all peers.
+ */
+ int all;
+};
+
+
+/**
+ * Output information of neighbours to the given client.
+ *
+ * @param cls the `struct PeerIterationContext *`
+ * @param peer identity of the neighbour
+ * @param address the address
+ * @param state current state this peer is in
+ * @param state_timeout timeout for the current state of the peer
+ * @param bandwidth_in inbound quota in NBO
+ * @param bandwidth_out outbound quota in NBO
+ */
+static void
+send_peer_information (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 IterationContext *pc = cls;
+ struct GNUNET_MQ_Envelope *env;
+ struct PeerIterateResponseMessage *msg;
+
+ if ((GNUNET_YES != pc->all) && (0 != memcmp (peer, &pc->id, sizeof(pc->id))))
+ return;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Sending information about `%s' using address `%s' in state `%s'\n",
+ GNUNET_i2s (peer),
+ (NULL != address) ? GST_plugins_a2s (address) : "<none>",
+ GNUNET_TRANSPORT_ps2s (state));
+ msg = compose_address_iterate_response_message (peer, address);
+ msg->state = htonl (state);
+ msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
+ env = GNUNET_MQ_msg_copy (&msg->header);
+ GNUNET_free (msg);
+ GNUNET_MQ_send (pc->tc->mq, env);
+}
+
+
+/**
+ * Client asked to obtain information about a specific or all peers
+ * Process the request.
+ *
+ * @param cls the client
+ * @param msg the peer address information request
+ */
+static void
+handle_client_monitor_peers (void *cls, const struct PeerMonitorMessage *msg)
+{
+ struct TransportClient *tc = cls;
+ struct IterationContext pc;
+
+ if (CT_NONE != tc->type)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVICE_client_drop (tc->client);
+ return;
+ }
+ GNUNET_SERVICE_client_disable_continue_warning (tc->client);
+ GNUNET_SERVICE_client_mark_monitor (tc->client);
+
+ /* Send initial list */
+ pc.tc = tc;
+ if (0 == memcmp (&msg->peer, &all_zeros, sizeof(struct GNUNET_PeerIdentity)))
+ {
+ /* iterate over all neighbours */
+ pc.all = GNUNET_YES;
+ pc.id = msg->peer;
+ }
+ else
+ {
+ /* just return one neighbour */
+ pc.all = GNUNET_NO;
+ pc.id = msg->peer;
+ }
+ GST_neighbours_iterate (&send_peer_information, &pc);
+
+ if (GNUNET_YES != ntohl (msg->one_shot))
+ {
+ tc->details.monitor_peer = msg->peer;
+ tc->type = CT_MONITOR;
+ if (0 !=
+ memcmp (&msg->peer, &all_zeros, sizeof(struct GNUNET_PeerIdentity)))
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client %p started monitoring of the peer `%s'\n",
+ tc,
+ GNUNET_i2s (&msg->peer));
+ else
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client %p started monitoring all peers\n",
+ tc);
+ }
+ else
+ {
+ struct GNUNET_MessageHeader *msg;
+ struct GNUNET_MQ_Envelope *env;
+
+ env =
+ GNUNET_MQ_msg (msg,
+ GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE_END);
+ GNUNET_MQ_send (tc->mq, env);
+ }
+}
+
+
+/**
+ * Function called by the plugin with information about the
+ * current sessions managed by the plugin (for monitoring).
+ *
+ * @param cls closure
+ * @param session session handle this information is about,
+ * NULL to indicate that we are "in sync" (initial
+ * iteration complete)
+ * @param info information about the state of the session,
+ * NULL if @a session is also NULL and we are
+ * merely signalling that the initial iteration is over
+ */
+static void
+plugin_session_info_cb (void *cls,
+ struct GNUNET_ATS_Session *session,
+ const struct GNUNET_TRANSPORT_SessionInfo *info)
+{
+ struct GNUNET_MQ_Envelope *env;
+ struct TransportPluginMonitorMessage *msg;
+ struct GNUNET_MessageHeader *sync;
+ size_t size;
+ size_t slen;
+ uint16_t alen;
+ char *name;
+ char *addr;
+
+ if (0 == GNUNET_notification_context_get_size (plugin_nc))
+ {
+ GST_plugins_monitor_subscribe (NULL, NULL);
+ return;
+ }
+ if ((NULL == info) && (NULL == session))
+ {
+ /* end of initial iteration */
+ if (NULL != sync_client)
+ {
+ env =
+ GNUNET_MQ_msg (sync, GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC);
+ GNUNET_MQ_send (sync_client->mq, env);
+ sync_client = NULL;
+ }
+ return;
+ }
+ GNUNET_assert (NULL != info);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Plugin event for peer %s on transport %s\n",
+ GNUNET_i2s (&info->address->peer),
+ info->address->transport_name);
+ slen = strlen (info->address->transport_name) + 1;
+ alen = info->address->address_length;
+ size = sizeof(struct TransportPluginMonitorMessage) + slen + alen;
+ if (size > UINT16_MAX)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ msg = GNUNET_malloc (size);
+ msg->header.size = htons (size);
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT);
+ msg->session_state = htons ((uint16_t) info->state);
+ msg->is_inbound = htons ((int16_t) info->is_inbound);
+ msg->msgs_pending = htonl (info->num_msg_pending);
+ msg->bytes_pending = htonl (info->num_bytes_pending);
+ msg->timeout = GNUNET_TIME_absolute_hton (info->session_timeout);
+ msg->delay = GNUNET_TIME_absolute_hton (info->receive_delay);
+ msg->peer = info->address->peer;
+ msg->session_id = (uint64_t) (intptr_t) session;
+ msg->plugin_name_len = htons (slen);
+ msg->plugin_address_len = htons (alen);
+ name = (char *) &msg[1];
+ GNUNET_memcpy (name, info->address->transport_name, slen);
+ addr = &name[slen];
+ GNUNET_memcpy (addr, info->address->address, alen);
+ if (NULL != sync_client)
+ {
+ struct GNUNET_MQ_Envelope *env;
+
+ env = GNUNET_MQ_msg_copy (&msg->header);
+ GNUNET_MQ_send (sync_client->mq, env);
+ }
+ else
+ {
+ GNUNET_notification_context_broadcast (plugin_nc, &msg->header, GNUNET_NO);
+ }
+ GNUNET_free (msg);
+}
+
+
+/**
+ * Client asked to obtain information about all plugin connections.
+ *
+ * @param cls the client
+ * @param message the peer address information request
+ */
+static void
+handle_client_monitor_plugins (void *cls,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct TransportClient *tc = cls;
+
+ GNUNET_SERVICE_client_mark_monitor (tc->client);
+ GNUNET_SERVICE_client_disable_continue_warning (tc->client);
+ GNUNET_notification_context_add (plugin_nc, tc->mq);
+ GNUNET_assert (NULL == sync_client);
+ sync_client = tc;
+ GST_plugins_monitor_subscribe (&plugin_session_info_cb, NULL);
+}
+
+
+/**
+ * Broadcast the given message to all of our clients.
+ *
+ * @param msg message to broadcast
+ * @param may_drop #GNUNET_YES if the message can be dropped / is payload
+ */
+void
+GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
+{
+ int done;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Asked to broadcast message of type %u with %u bytes\n",
+ (unsigned int) ntohs (msg->type),
+ (unsigned int) ntohs (msg->size));
+ done = GNUNET_NO;
+ for (struct TransportClient *tc = clients_head; NULL != tc; tc = tc->next)
+ {
+ if (CT_NONE == tc->type)
+ continue; /* client not yet ready */
+ if ((GNUNET_YES == may_drop) && (CT_CORE != tc->type))
+ continue; /* skip, this client does not care about payload */
+ unicast (tc, msg, may_drop);
+ done = GNUNET_YES;
+ }
+ if (GNUNET_NO == done)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Message of type %u not delivered, is CORE service up?\n",
+ ntohs (msg->type));
+}
+
+
+/**
+ * Broadcast the new active address to all clients monitoring the peer.
+ *
+ * @param peer peer this update is about (never NULL)
+ * @param address address, NULL on disconnect
+ * @param state the current state of the peer
+ * @param state_timeout the time out for the state
+ */
+void
+GST_clients_broadcast_peer_notification (
+ const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_HELLO_Address *address,
+ enum GNUNET_TRANSPORT_PeerState state,
+ struct GNUNET_TIME_Absolute state_timeout)
+{
+ struct GNUNET_MQ_Envelope *env;
+ struct PeerIterateResponseMessage *msg;
+
+ msg = compose_address_iterate_response_message (peer, address);
+ msg->state = htonl (state);
+ msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
+ for (struct TransportClient *tc = clients_head; NULL != tc; tc = tc->next)
+ {
+ if (CT_MONITOR != tc->type)
+ continue;
+ if ((0 == memcmp (&tc->details.monitor_peer,
+ &all_zeros,
+ sizeof(struct GNUNET_PeerIdentity))) ||
+ (0 == memcmp (&tc->details.monitor_peer,
+ peer,
+ sizeof(struct GNUNET_PeerIdentity))))
+ {
+ env = GNUNET_MQ_msg_copy (&msg->header);
+ GNUNET_MQ_send (tc->mq, env);
+ }
+ }
+ GNUNET_free (msg);
+}
+
+
+/**
+ * Mark the peer as down so we don't call the continuation
+ * context in the future.
+ *
+ * @param cls NULL
+ * @param peer peer that got disconnected
+ * @param value a `struct SendTransmitContinuationContext` to mark
+ * @return #GNUNET_OK (continue to iterate)
+ */
+static int
+mark_peer_down (void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
+{
+ struct SendTransmitContinuationContext *stcc = value;
+
+ stcc->down = GNUNET_YES;
+ return GNUNET_OK;
+}
+
+
+/**
+ * Notify all clients about a disconnect, and cancel
+ * pending SEND_OK messages for this peer.
+ *
+ * @param peer peer that disconnected
+ */
+void
+GST_clients_broadcast_disconnect (const struct GNUNET_PeerIdentity *peer)
+{
+ struct DisconnectInfoMessage disconnect_msg;
+
+ GNUNET_CONTAINER_multipeermap_get_multiple (active_stccs,
+ peer,
+ &mark_peer_down,
+ NULL);
+ disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
+ disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
+ disconnect_msg.reserved = htonl (0);
+ disconnect_msg.peer = *peer;
+ GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
+}