};
+/**
+ * Client monitoring changes of active addresses of our neighbours.
+ */
+struct MonitoringClient
+{
+ /**
+ * This is a doubly-linked list.
+ */
+ struct MonitoringClient *next;
+
+ /**
+ * This is a doubly-linked list.
+ */
+ struct MonitoringClient *prev;
+
+ /**
+ * Handle to the client.
+ */
+ struct GNUNET_SERVER_Client *client;
+
+ /**
+ * Peer identity to monitor the addresses of.
+ * Zero to monitor all neighrours.
+ */
+ struct GNUNET_PeerIdentity peer;
+
+};
+
+
/**
* Head of linked list of all clients to this service.
*/
*/
static struct TransportClient *clients_tail;
+/**
+ * Head of linked list of monitoring clients.
+ */
+static struct MonitoringClient *monitoring_clients_head;
+
+/**
+ * Tail of linked list of monitoring clients.
+ */
+static struct MonitoringClient *monitoring_clients_tail;
+
+/**
+ * Notification context, to send updates on changes to active addresses
+ * of our neighbours.
+ */
+struct GNUNET_SERVER_NotificationContext *nc = NULL;
+
+
/**
* Find the internal handle associated with the given client handle
*
}
+/**
+ * Find the handle to the monitoring client associated with the given
+ * client handle
+ *
+ * @param client server's client handle to look up
+ * @return handle to the monitoring client
+ */
+static struct MonitoringClient *
+lookup_monitoring_client (struct GNUNET_SERVER_Client *client)
+{
+ struct MonitoringClient *mc;
+
+ mc = monitoring_clients_head;
+ while (mc != NULL)
+ {
+ if (mc->client == client)
+ return mc;
+ mc = mc->next;
+ }
+ return NULL;
+}
+
+
+/**
+ * Setup a new monitoring client using the given server client handle and
+ * the peer identity.
+ *
+ * @param client server's client handle to create our internal handle for
+ * @param peer identity of the peer to monitor the addresses of,
+ * zero to monitor all neighrours.
+ * @return handle to the new monitoring client
+ */
+static struct MonitoringClient *
+setup_monitoring_client (struct GNUNET_SERVER_Client *client,
+ struct GNUNET_PeerIdentity *peer)
+{
+ struct MonitoringClient *mc;
+
+ GNUNET_assert (lookup_monitoring_client (client) == NULL);
+ mc = GNUNET_malloc (sizeof (struct MonitoringClient));
+ mc->client = client;
+ mc->peer = *peer;
+ GNUNET_CONTAINER_DLL_insert (monitoring_clients_head,
+ monitoring_clients_tail,
+ mc);
+ GNUNET_SERVER_notification_context_add (nc, client);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client %X started monitoring of the peer `%s'\n",
+ mc, GNUNET_i2s (peer));
+ return mc;
+}
+
+
/**
* Function called to notify a client about the socket being ready to
* queue more data. "buf" will be NULL and "size" zero if the socket
client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
{
struct TransportClient *tc;
+ struct MonitoringClient *mc;
struct ClientMessageQueueEntry *mqe;
if (client == NULL)
return;
+ mc = lookup_monitoring_client (client);
+ if (mc != NULL)
+ {
+ GNUNET_CONTAINER_DLL_remove (monitoring_clients_head,
+ monitoring_clients_tail,
+ mc);
+ GNUNET_free (mc);
+ }
tc = lookup_client (client);
if (tc == NULL)
return;
}
+/**
+ * Compose AddressIterateResponseMessage using the given peer and address.
+ *
+ * @param peer identity of the peer
+ * @param address the address, NULL on disconnect
+ * @return composed message
+ */
+static struct AddressIterateResponseMessage *
+compose_address_iterate_response_message (const struct GNUNET_PeerIdentity
+ *peer,
+ const struct GNUNET_HELLO_Address
+ *address)
+{
+ struct AddressIterateResponseMessage *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 = alen = 0;
+ size = (sizeof (struct AddressIterateResponseMessage) + alen + tlen);
+ msg = GNUNET_malloc (size);
+ msg->header.size = htons (size);
+ msg->header.type =
+ htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
+ msg->reserved = htonl (0);
+ msg->peer = *peer;
+ msg->addrlen = htonl (alen);
+ msg->pluginlen = htonl (tlen);
+ if (NULL != address)
+ {
+ addr = (char *) &msg[1];
+ memcpy (addr, address->address, alen);
+ memcpy (&addr[alen], address->transport_name, tlen);
+ }
+ return msg;
+}
+
+
/**
* Output the active address of connected neighbours to the given client.
*
{
struct GNUNET_SERVER_TransmitContext *tc = cls;
struct AddressIterateResponseMessage *msg;
- size_t size;
- size_t tlen;
- size_t alen;
- char *addr;
- tlen = strlen (address->transport_name) + 1;
- alen = address->address_length;
- size = (sizeof (struct AddressIterateResponseMessage) + alen + tlen);
- {
- char buf[size];
-
- msg = (struct AddressIterateResponseMessage *) buf;
- msg->reserved = htonl (0);
- msg->peer = *peer;
- msg->addrlen = htonl (alen);
- msg->pluginlen = htonl (tlen);
- addr = (char *) &msg[1];
- memcpy (addr, address->address, alen);
- memcpy (&addr[alen], address->transport_name, tlen);
- GNUNET_SERVER_transmit_context_append_data (tc,
- &buf[sizeof
- (struct
- GNUNET_MessageHeader)],
- size -
- sizeof (struct
- GNUNET_MessageHeader),
- GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
- }
+ msg = compose_address_iterate_response_message (peer, address);
+ GNUNET_SERVER_transmit_context_append_message (tc, &msg->header);
+ GNUNET_free (msg);
}
struct GNUNET_SERVER_TransmitContext *tc;
struct AddressIterateMessage *msg;
struct GNUNET_HELLO_Address *address;
+ struct MonitoringClient *mc;
if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE)
{
return;
}
msg = (struct AddressIterateMessage *) message;
- if (GNUNET_YES != ntohl (msg->one_shot))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Address monitoring not implemented\n");
- GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
- return;
- }
GNUNET_SERVER_disable_receive_done_warning (client);
tc = GNUNET_SERVER_transmit_context_create (client);
if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
if (address != NULL)
output_address (tc, &msg->peer, NULL, 0, address);
}
+ if (GNUNET_YES != ntohl (msg->one_shot))
+ {
+ mc = lookup_monitoring_client (client);
+ if (mc != NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
+ "ServerClient %X tried to start monitoring twice (MonitoringClient %X)\n",
+ client, mc);
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ setup_monitoring_client (client, &msg->peer);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+ }
GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
- GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
+ GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
}
sizeof (struct BlacklistMessage)},
{NULL, NULL, 0, 0}
};
+ nc = GNUNET_SERVER_notification_context_create (server, 0);
GNUNET_SERVER_add_handlers (server, handlers);
GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
NULL);
void
GST_clients_stop ()
{
- /* nothing to do */
+ if (NULL != nc)
+ {
+ GNUNET_SERVER_notification_context_destroy (nc);
+ nc = NULL;
+ }
}
}
+/**
+ * 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
+ */
+void
+GST_clients_broadcast_address_notification (const struct GNUNET_PeerIdentity
+ *peer,
+ const struct GNUNET_HELLO_Address
+ *address)
+{
+ struct AddressIterateResponseMessage *msg;
+ struct MonitoringClient *mc;
+ static struct GNUNET_PeerIdentity all_zeros;
+
+ msg = compose_address_iterate_response_message (peer, address);
+ mc = monitoring_clients_head;
+ while (mc != NULL)
+ {
+ if ((0 == memcmp (&mc->peer, &all_zeros,
+ sizeof (struct GNUNET_PeerIdentity))) ||
+ (0 == memcmp (&mc->peer, peer,
+ sizeof (struct GNUNET_PeerIdentity))))
+ {
+ GNUNET_SERVER_notification_context_unicast (nc, mc->client,
+ &msg->header, GNUNET_NO);
+ }
+
+ mc = mc->next;
+ }
+ GNUNET_free (msg);
+}
+
+
/* end of file gnunet-service-transport_clients.c */
static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
/**
- * Closure for connect_notify_cb and disconnect_notify_cb
+ * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
*/
static void *callback_cls;
*/
static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
+/**
+ * Function to call when we changed an active address of a neighbour.
+ */
+static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
+
/**
* counter for connected neighbours
*/
static int
change (struct NeighbourMapEntry *n, int state, int line)
{
+ int previous_state;
/* allowed transitions */
int allowed = GNUNET_NO;
+ previous_state = n->state;
+
switch (n->state)
{
case S_NOT_CONNECTED:
GNUNET_assert (0);
}
-
+ if (NULL != address_change_cb)
+ {
+ if (n->state == S_CONNECTED)
+ address_change_cb (callback_cls, &n->id, n->address);
+ else if (previous_state == S_CONNECTED)
+ address_change_cb (callback_cls, &n->id, NULL);
+ }
return GNUNET_OK;
}
* @param cls closure for callbacks
* @param connect_cb function to call if we connect to a peer
* @param disconnect_cb function to call if we disconnect from a peer
+ * @param address_cb function to call if we change an active address
+ * of a neighbour
*/
void
-GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
- GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
+GST_neighbours_start (void *cls,
+ GNUNET_TRANSPORT_NotifyConnect connect_cb,
+ GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
+ GNUNET_TRANSPORT_PeerIterateCallback address_cb)
{
callback_cls = cls;
connect_notify_cb = connect_cb;
disconnect_notify_cb = disconnect_cb;
+ address_change_cb = address_cb;
neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
}
callback_cls = NULL;
connect_notify_cb = NULL;
disconnect_notify_cb = NULL;
+ address_change_cb = NULL;
}
struct ContinutionContext
GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
n->address_state = UNUSED;
}
-
}
/* set new address */
GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
&neighbour_timeout_task, n);
+ if (NULL != address_change_cb && n->state == S_CONNECTED)
+ address_change_cb (callback_cls, &n->id, n->address);
+
#if TEST_NEW_CODE
/* Obtain an session for this address from plugin */
struct GNUNET_TRANSPORT_PluginFunctions *papi;
return;
}
- if ((size <
- sizeof (struct GNUNET_MessageHeader) +
- sizeof (struct AddressIterateResponseMessage)) ||
+ if ((size < sizeof (struct AddressIterateResponseMessage)) ||
(ntohs (msg->type) !=
GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE))
{
return;
}
- addr = (const char *) &air_msg[1];
- transport_name = &addr[alen];
-
- if (transport_name[tlen - 1] != '\0')
+ if (alen == 0 && tlen == 0)
{
- GNUNET_break_op (0);
- pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
- GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
- return;
+ pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, NULL);
+ }
+ else
+ {
+ addr = (const char *) &air_msg[1];
+ transport_name = &addr[alen];
+
+ if (transport_name[tlen - 1] != '\0')
+ {
+ GNUNET_break_op (0);
+ pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
+ GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
+ return;
+ }
+
+ /* notify client */
+ address =
+ GNUNET_HELLO_address_allocate (&air_msg->peer, transport_name, addr,
+ alen);
+ pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address);
+ GNUNET_HELLO_address_free (address);
}
/* expect more replies */
GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor,
pal_ctx,
GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
-
- /* notify client */
- address =
- GNUNET_HELLO_address_allocate (&air_msg->peer, transport_name, addr,
- alen);
- pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address);
- GNUNET_HELLO_address_free (address);
}
* @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
* @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
* GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly cancelled)
- * @param timeout how long is the lookup allowed to take at most
+ * @param timeout how long is the lookup allowed to take at most (irrelevant if one_shot is set to GNUNET_NO)
* @param peer_address_callback function to call with the results
* @param peer_address_callback_cls closure for peer_address_callback
*/
struct GNUNET_CLIENT_Connection *client;
struct GNUNET_TIME_Absolute abs_timeout;
- if (GNUNET_YES != one_shot)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Address monitoring not implemented\n");
- return NULL;
- }
client = GNUNET_CLIENT_connect ("transport", cfg);
if (client == NULL)
return NULL;
+ if (GNUNET_YES != one_shot)
+ timeout = GNUNET_TIME_UNIT_FOREVER_REL;
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);