* @file transport/gnunet-service-transport_neighbours.c
* @brief neighbour management
* @author Christian Grothoff
- *
- * TODO:
- * - TEST, TEST, TEST...
*/
#include "platform.h"
#include "gnunet_ats_service.h"
*/
#define QUOTA_VIOLATION_DROP_THRESHOLD 10
-/**
- * How often do we send KEEPALIVE messages to each of our neighbours and measure
- * the latency with this neighbour?
- * (idle timeout is 5 minutes or 300 seconds, so with 100s interval we
- * send 3 keepalives in each interval, so 3 messages would need to be
- * lost in a row for a disconnect).
- */
-#define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
-
/**
* How long are we willing to wait for a response from ATS before timing out?
*/
};
+/**
+ * Message a peer sends to another when connected to indicate that a
+ * session is in use and the peer is still alive or to respond to a keep alive.
+ * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
+ * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
+ * When the keep alive response with type is received, transport service
+ * will call the respective plugin to update the session timeout
+ */
+struct SessionKeepAliveMessage
+{
+ /**
+ * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
+ * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * A nonce to identify the session the keep alive is used for
+ */
+ uint32_t nonce GNUNET_PACKED;
+};
+
/**
* Message we send to the other peer to notify him that we intentionally
* are disconnecting (to reduce timeouts). This is just a friendly
* from the map. We should never find a 'struct NeighbourMapEntry'
* in this state in the map. Accessing a 'struct NeighbourMapEntry'
* in this state virtually always means using memory that has been
- * freed (the exception being the cleanup code in 'free_neighbour').
+ * freed (the exception being the cleanup code in #free_neighbour()).
*/
S_DISCONNECT_FINISHED
};
*/
int ats_active;
+ /**
+ * The current nonce sent in the last keep alive messages
+ */
+ uint32_t keep_alive_nonce;
};
}
+/**
+ * Convert state to human-readable string.
+ *
+ * @param state the state value
+ * @return corresponding string
+ */
static const char *
-print_state (int state)
+print_state (enum State state)
{
-
switch (state)
{
case S_NOT_CONNECTED:
* Release its resources and give appropriate notifications
* to ATS and other subsystems.
*
- * @param na address we are done with; 'na' itself must NOT be 'free'd, only the contents!
+ * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
*/
static void
free_address (struct NeighbourAddress *na)
}
na->ats_active = GNUNET_NO;
+ na->keep_alive_nonce = 0;
if (NULL != na->address)
{
GNUNET_HELLO_address_free (na->address);
int is_active)
{
struct GNUNET_TRANSPORT_PluginFunctions *papi;
+
if (NULL == (papi = GST_plugins_find (address->transport_name)))
{
GNUNET_break (0);
na->bandwidth_out = bandwidth_out;
na->session = session;
na->ats_active = is_active;
+ na->keep_alive_nonce = 0;
if (GNUNET_YES == is_active)
{
/* Telling ATS about new session */
* Free a neighbour map entry.
*
* @param n entry to free
- * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
- * GNUNET_YES to keep all sessions
+ * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
+ * #GNUNET_YES to keep all sessions
*/
static void
-free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
+free_neighbour (struct NeighbourMapEntry *n,
+ int keep_sessions)
{
struct MessageQueue *mq;
struct GNUNET_TRANSPORT_PluginFunctions *papi;
if (NULL != n->primary_address.address)
{
- backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address);
+ backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
}
else
backup_primary = NULL;
free_address (&n->primary_address);
free_address (&n->alternative_address);
- /* FIXME-PLUGIN-API: This does not seem to guarantee that all
- transport sessions eventually get killed due to inactivity; they
- MUST have their own timeout logic (but at least TCP doesn't have
- one yet). Are we sure that EVERY 'session' of a plugin is
- actually cleaned up this way!? Note that if we are switching
- between two TCP sessions to the same peer, the existing plugin
- API gives us not even the means to selectively kill only one of
+ /* FIXME: Note that if we are switching between two TCP sessions to
+ the same peer, we might want to selectively kill only one of
them! Killing all sessions like this seems to be very, very
wrong. */
if ((GNUNET_NO == keep_sessions) &&
(NULL != backup_primary) &&
(NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
- papi->disconnect (papi->cls, &n->id);
+ papi->disconnect_peer (papi->cls, &n->id);
GNUNET_free_non_null (backup_primary);
GNUNET_free (n);
}
+
/**
* Transmit a message using the current session of the given
* neighbour.
*
* @param n entry for the recipient
* @param msgbuf buffer to transmit
- * @param msgbuf_size number of bytes in buffer
+ * @param msgbuf_size number of bytes in @a msgbuf buffer
* @param priority transmission priority
* @param timeout transmission timeout
+ * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
+ * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
* @param cont continuation to call when finished (can be NULL)
- * @param cont_cls closure for cont
+ * @param cont_cls closure for @a cont
+ * @return timeout (copy of @a timeout or a calculated one if
+ * @a use_keepalive_timeout is #GNUNET_YES.
*/
-static void
+static struct GNUNET_TIME_Relative
send_with_session (struct NeighbourMapEntry *n,
const char *msgbuf, size_t msgbuf_size,
uint32_t priority,
struct GNUNET_TIME_Relative timeout,
+ unsigned int use_keepalive_timeout,
GNUNET_TRANSPORT_TransmitContinuation cont,
void *cont_cls)
{
struct GNUNET_TRANSPORT_PluginFunctions *papi;
+ struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
GNUNET_assert (n->primary_address.session != NULL);
if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
n->primary_address.session,
msgbuf, msgbuf_size,
priority,
- timeout,
+ (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
+ GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
+ papi->query_keepalive_factor (papi->cls))),
cont, cont_cls)))) &&
(NULL != cont))
cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
GST_neighbours_notify_data_sent (&n->id,
n->primary_address.address, n->primary_address.session, msgbuf_size);
GNUNET_break (NULL != papi);
+ return result;
}
* activities (keep alive, send next message, disconnect if idle, finish
* clean up after disconnect).
*
- * @param cls the 'struct NeighbourMapEntry' for which we are running
+ * @param cls the `struct NeighbourMapEntry` for which we are running
* @param tc scheduler context (unused)
*/
static void
&disconnect_msg.purpose,
&disconnect_msg.signature));
- send_with_session (n,
- (const char *) &disconnect_msg, sizeof (disconnect_msg),
- UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
- &send_disconnect_cont, NULL);
+ (void) send_with_session (n,
+ (const char *) &disconnect_msg, sizeof (disconnect_msg),
+ UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_NO, &send_disconnect_cont, NULL);
GNUNET_STATISTICS_update (GST_stats,
gettext_noop
("# DISCONNECT messages sent"), 1,
static void
disconnect_neighbour (struct NeighbourMapEntry *n)
{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnecting from peer %s in state %s\n",
+ GNUNET_i2s (&n->id),
+ print_state (n->state));
/* depending on state, notify neighbour and/or upper layers of this peer
about disconnect */
switch (n->state)
/**
* We're done with our transmission attempt, continue processing.
*
- * @param cls the 'struct MessageQueue' of the message
+ * @param cls the `struct MessageQueue` of the message
* @param receiver intended receiver
* @param success whether it worked or not
* @param size_payload bytes payload sent
}
if (bytes_in_send_queue < mq->message_buf_size)
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
- bytes_in_send_queue, mq->message_buf_size,
- (GNUNET_OK == success) ? "OK" : "FAIL",
- size_payload, physical);
- GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
+ bytes_in_send_queue, mq->message_buf_size,
+ (GNUNET_OK == success) ? "OK" : "FAIL",
+ size_payload, physical);
+ GNUNET_break (0);
}
1, GNUNET_NO);
GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
n->is_active = mq;
- transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0); /* timeout */
+ transmit_send_continuation (mq, &n->id,
+ GNUNET_SYSERR,
+ mq->message_buf_size, 0); /* timeout */
}
if (NULL == mq)
return; /* no more messages */
GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
n->is_active = mq;
- send_with_session (n,
- mq->message_buf, mq->message_buf_size,
- 0 /* priority */, timeout,
- &transmit_send_continuation, mq);
+ (void) send_with_session (n,
+ mq->message_buf, mq->message_buf_size,
+ 0 /* priority */, timeout, GNUNET_NO,
+ &transmit_send_continuation, mq);
}
static void
send_keepalive (struct NeighbourMapEntry *n)
{
- struct GNUNET_MessageHeader m;
+ struct SessionKeepAliveMessage m;
+ struct GNUNET_TIME_Relative timeout;
+ uint32_t nonce;
GNUNET_assert ((S_CONNECTED == n->state) ||
(S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
(S_CONNECTED_SWITCHING_CONNECT_SENT));
if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
return; /* no keepalive needed at this time */
- m.size = htons (sizeof (struct GNUNET_MessageHeader));
- m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
- send_with_session (n,
- (const void *) &m, sizeof (m),
- UINT32_MAX /* priority */,
- KEEPALIVE_FREQUENCY,
- NULL, NULL);
+
+ nonce = 0; /* 0 indicates 'not set' */
+ while (0 == nonce)
+ nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Sending keep alive to peer `%s' with nonce %u\n",
+ GNUNET_i2s (&n->id), nonce);
+
+ m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
+ m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
+ m.nonce = htonl (nonce);
+
+ timeout = send_with_session (n,
+ (const void *) &m, sizeof (m),
+ UINT32_MAX /* priority */,
+ GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
+ NULL, NULL);
GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
GNUNET_NO);
+ n->primary_address.keep_alive_nonce = nonce;
n->expect_latency_response = GNUNET_YES;
n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
- n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
+ n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
+
}
* we received a KEEPALIVE (or equivalent); send a response.
*
* @param neighbour neighbour to keep alive (by sending keep alive response)
+ * @param m the keep alive message containing the nonce to respond to
*/
void
-GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
+GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
+ const struct GNUNET_MessageHeader *m)
{
struct NeighbourMapEntry *n;
- struct GNUNET_MessageHeader m;
+ const struct SessionKeepAliveMessage *msg_in;
+ struct SessionKeepAliveMessage msg;
+
+ if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
+ return;
+ msg_in = (struct SessionKeepAliveMessage *) m;
if (NULL == (n = lookup_neighbour (neighbour)))
{
GNUNET_STATISTICS_update (GST_stats,
1, GNUNET_NO);
return;
}
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received keep alive request from peer `%s' with nonce %u\n",
+ GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
+
/* send reply to allow neighbour to measure latency */
- m.size = htons (sizeof (struct GNUNET_MessageHeader));
- m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
- send_with_session(n,
- (const void *) &m, sizeof (m),
- UINT32_MAX /* priority */,
- KEEPALIVE_FREQUENCY,
- NULL, NULL);
+ msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
+ msg.nonce = msg_in->nonce;
+ (void) send_with_session(n,
+ (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
+ UINT32_MAX /* priority */,
+ GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
+ NULL, NULL);
}
* plus calculated latency) to ATS.
*
* @param neighbour neighbour to keep alive
+ * @param m the message containing the keep alive response
*/
void
-GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
+GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
+ const struct GNUNET_MessageHeader *m)
{
struct NeighbourMapEntry *n;
+ const struct SessionKeepAliveMessage *msg;
+ struct GNUNET_TRANSPORT_PluginFunctions *papi;
uint32_t latency;
struct GNUNET_ATS_Information ats;
+ if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
+ return;
+
+ msg = (const struct SessionKeepAliveMessage *) m;
if (NULL == (n = lookup_neighbour (neighbour)))
{
GNUNET_STATISTICS_update (GST_stats,
1, GNUNET_NO);
return;
}
+ if (NULL == n->primary_address.address)
+ {
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop
+ ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
+ 1, GNUNET_NO);
+ return;
+ }
+ if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
+ {
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop
+ ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
+ 1, GNUNET_NO);
+ return;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received keep alive response from peer `%s' for session %p\n",
+ GNUNET_i2s (&n->id), n->primary_address.session);
+
+ }
+
+ /* Update session timeout here */
+ if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Updating session for peer `%s' for session %p\n",
+ GNUNET_i2s (&n->id), n->primary_address.session);
+ papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
+ }
+ else
+ {
+ GNUNET_break (0);
+ }
+
+ n->primary_address.keep_alive_nonce = 0;
n->expect_latency_response = GNUNET_NO;
n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
else
latency = n->latency.rel_value_us;
ats.value = htonl (latency);
- GST_ats_update_metrics (&n->id,
- n->primary_address.address,
- n->primary_address.session,
- &ats, 1);
+ GST_ats_update_metrics (&n->id, n->primary_address.address,
+ n->primary_address.session, &ats, 1);
}
*
* @param sender sender of the message
* @param size size of the message
- * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
- * GNUNET_NO if the neighbour is not connected or violates the quota,
- * GNUNET_SYSERR if the connection is not fully up yet
+ * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
+ * #GNUNET_NO if the neighbour is not connected or violates the quota,
+ * #GNUNET_SYSERR if the connection is not fully up yet
* @return how long to wait before reading more from this sender
*/
struct GNUNET_TIME_Relative
GNUNET_break (0);
return;
}
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop
+ ("# SESSION_CONNECT messages sent"),
+ 1, GNUNET_NO);
na->connect_timestamp = GNUNET_TIME_absolute_get ();
connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
connect_msg.reserved = htonl (0);
connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
- (void) papi->send (papi->cls,
- na->session,
- (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
- UINT_MAX,
- GNUNET_TIME_UNIT_FOREVER_REL,
- NULL, NULL);
+ if (-1 ==
+ papi->send (papi->cls,
+ na->session,
+ (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
+ UINT_MAX,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ NULL, NULL))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Failed to transmit CONNECT message via plugin to %s\n"),
+ GST_plugins_a2s (na->address));
+ }
GST_neighbours_notify_data_sent (&na->address->peer,
- na->address, na->session, sizeof (struct SessionConnectMessage));
+ na->address,
+ na->session,
+ sizeof (struct SessionConnectMessage));
}
GNUNET_break (0);
return;
}
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop
+ ("# CONNECT_ACK messages sent"),
+ 1, GNUNET_NO);
connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
connect_msg.reserved = htonl (0);
*
* @param a1 first address to compare
* @param a2 other address to compare
- * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
+ * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
*/
static int
address_matches (const struct NeighbourAddress *a1,
if (NULL == neighbours)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Asked to connect to peer `%s' during shutdown\n",
- GNUNET_i2s (target));
- return; /* during shutdown, do nothing */
- }
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Asked to connect to peer `%s'\n",
- GNUNET_i2s (target));
- if (0 == memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
- {
- /* refuse to connect to myself */
- /* FIXME: can this happen? Is this not an API violation? */
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Refusing to try to connect to myself.\n");
- return;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Asked to connect to peer `%s' during shutdown\n",
+ GNUNET_i2s (target));
+ return; /* during shutdown, do nothing */
}
n = lookup_neighbour (target);
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "Asked to connect to peer `%s' (state: %s)\n",
+ GNUNET_i2s (target),
+ (NULL != n) ? print_state(n->state) : "NEW PEER");
if (NULL != n)
{
switch (n->state)
case S_CONNECT_RECV_BLACKLIST:
case S_CONNECT_RECV_ACK:
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Ignoring request to try to connect to `%s', already trying!\n",
+ "Ignoring request to try to connect to `%s', already trying!\n",
GNUNET_i2s (target));
return; /* already trying */
case S_CONNECTED:
case S_CONNECTED_SWITCHING_BLACKLIST:
case S_CONNECTED_SWITCHING_CONNECT_SENT:
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Ignoring request to try to connect, already connected to `%s'!\n",
+ "Ignoring request to try to connect, already connected to `%s'!\n",
GNUNET_i2s (target));
return; /* already connected */
case S_DISCONNECT:
*
* @param cls closure with the 'struct BlackListCheckContext'
* @param peer peer this check affects
- * @param result GNUNET_OK if the address is allowed
+ * @param result #GNUNET_OK if the address is allowed
*/
static void
handle_test_blacklist_cont (void *cls,
struct NeighbourMapEntry *n;
bcc->bc = NULL;
+ GNUNET_CONTAINER_DLL_remove (bc_head,
+ bc_tail,
+ bcc);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Connection to new address of peer `%s' based on blacklist is `%s'\n",
GNUNET_i2s (peer),
(GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
+ if (GNUNET_OK == result)
+ GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
+ else
+ {
+ /* Blacklist disagreed on connecting to a peer with this address
+ * Destroy address because we are not allowed to use it
+ */
+ if (NULL != bcc->na.session)
+ GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
+ GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
+ }
if (NULL == (n = lookup_neighbour (peer)))
goto cleanup; /* nobody left to care about new address */
switch (n->state)
free_neighbour (n, GNUNET_NO);
break;
case S_INIT_ATS:
- /* still waiting on ATS suggestion */
+ /* waiting on ATS suggestion; still, pass address to ATS as a
+ possibility */
break;
case S_INIT_BLACKLIST:
/* check if the address the blacklist was fine with matches
}
else
{
- // FIXME: should also possibly destroy session with plugin!?
- GNUNET_ATS_address_destroyed (GST_ats,
- bcc->na.address,
- NULL);
free_address (&n->primary_address);
n->state = S_INIT_ATS;
n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
- // FIXME: do we need to ask ATS again for suggestions?
- n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
}
break;
case S_CONNECT_SENT:
}
break;
case S_CONNECT_RECV_BLACKLIST_INBOUND:
- if (GNUNET_OK == result)
- GST_ats_add_address (bcc->na.address, bcc->na.session);
-
n->state = S_CONNECT_RECV_ATS;
n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
GNUNET_ATS_reset_backoff (GST_ats, peer);
n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
break;
case S_CONNECT_RECV_ATS:
- /* still waiting on ATS suggestion, don't care about blacklist */
+ /* waiting on ATS suggestion, don't care about blacklist */
break;
case S_CONNECT_RECV_BLACKLIST:
if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Blacklist result ignored, as it is not for our primary address\n");
break; /* result for an address we currently don't care about */
+ }
if (GNUNET_OK == result)
{
n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
}
else
{
- // FIXME: should also possibly destroy session with plugin!?
- GNUNET_ATS_address_destroyed (GST_ats,
- bcc->na.address,
- NULL);
+ struct GNUNET_TRANSPORT_PluginFunctions *plugin;
+
+ plugin = GST_plugins_find (bcc->na.address->transport_name);
+ if ( (NULL != plugin) &&
+ (NULL != bcc->na.session) )
+ {
+ plugin->disconnect_session (plugin->cls,
+ bcc->na.session);
+ break;
+ }
+ GNUNET_break (NULL != plugin);
free_address (&n->primary_address);
n->state = S_INIT_ATS;
n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
- // FIXME: do we need to ask ATS again for suggestions?
GNUNET_ATS_reset_backoff (GST_ats, peer);
- n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
}
break;
case S_CONNECT_RECV_ACK:
break; /* result for an address we currently don't care about */
if (GNUNET_OK == result)
{
+ n->state = S_RECONNECT_SENT;
send_session_connect (&n->primary_address);
n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
- n->state = S_RECONNECT_SENT;
}
else
{
- GNUNET_ATS_address_destroyed (GST_ats,
- bcc->na.address,
- NULL);
n->state = S_RECONNECT_ATS;
n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
- // FIXME: do we need to ask ATS again for suggestions?
- n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
}
break;
case S_RECONNECT_SENT:
}
else
{
- GNUNET_ATS_address_destroyed (GST_ats,
- bcc->na.address,
- NULL);
- free_address (&n->alternative_address);
n->state = S_CONNECTED;
+ free_address (&n->alternative_address);
}
break;
case S_CONNECTED_SWITCHING_CONNECT_SENT:
}
cleanup:
GNUNET_HELLO_address_free (bcc->na.address);
- GNUNET_CONTAINER_DLL_remove (bc_head,
- bc_tail,
- bcc);
GNUNET_free (bcc);
}
struct BlackListCheckContext *bcc;
struct GST_BlacklistCheck *bc;
- bcc = GNUNET_malloc (sizeof (struct BlackListCheckContext));
+ bcc = GNUNET_new (struct BlackListCheckContext);
bcc->na.address = GNUNET_HELLO_address_copy (address);
bcc->na.session = session;
bcc->na.connect_timestamp = ts;
* @param address address of the other peer, NULL if other peer
* connected to us
* @param session session to use (or NULL)
+ * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
*/
-void
+int
GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_HELLO_Address *address,
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Received CONNECT message from peer `%s'\n",
GNUNET_i2s (peer));
-
if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
{
GNUNET_break_op (0);
- return;
+ return GNUNET_SYSERR;
}
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop
+ ("# CONNECT messages received"),
+ 1, GNUNET_NO);
if (NULL == neighbours)
- return; /* we're shutting down */
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
+ GNUNET_i2s (peer));
+ return GNUNET_OK; /* we're shutting down */
+ }
scm = (const struct SessionConnectMessage *) message;
GNUNET_break_op (0 == ntohl (scm->reserved));
ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
"Unhandled state `%s'\n",
print_state (n->state));
GNUNET_break (0);
- free_neighbour (n, GNUNET_NO);
- break;
+ return GNUNET_SYSERR;
}
+ return GNUNET_OK;
}
struct Session *session,
const struct GNUNET_ATS_Information *ats,
uint32_t ats_count,
- struct GNUNET_BANDWIDTH_Value32NBO
- bandwidth_in,
- struct GNUNET_BANDWIDTH_Value32NBO
- bandwidth_out)
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
{
struct NeighbourMapEntry *n;
struct GNUNET_TRANSPORT_PluginFunctions *papi;
return;
}
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "ATS tells us to switch to address '%s' session %p for "
- "peer `%s' in state %s (quota in/out %u %u )\n",
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "ATS tells us to switch to address '%s/%s' session %p for "
+ "peer `%s' in state %s/%d (quota in/out %u %u )\n",
(address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
+ address->transport_name,
session,
GNUNET_i2s (peer),
print_state (n->state),
+ n->send_connect_ack,
ntohl (bandwidth_in.value__),
ntohl (bandwidth_out.value__));
address and check blacklist again */
set_address (&n->primary_address,
address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
+ n->state = S_CONNECT_RECV_BLACKLIST;
n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
check_blacklist (&n->id,
n->connect_ack_timestamp,
}
/* ATS asks us to switch a life connection; see if we can get
a CONNECT_ACK on it before we actually do this! */
+ n->state = S_CONNECTED_SWITCHING_BLACKLIST;
set_address (&n->alternative_address,
address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
- n->state = S_CONNECTED_SWITCHING_BLACKLIST;
check_blacklist (&n->id,
GNUNET_TIME_absolute_get (),
address, session);
break;
case S_RECONNECT_ATS:
+ n->state = S_RECONNECT_BLACKLIST;
set_address (&n->primary_address,
address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
- n->state = S_RECONNECT_BLACKLIST;
n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
check_blacklist (&n->id,
n->connect_ack_timestamp,
case S_RECONNECT_SENT:
/* ATS asks us to switch while we were trying to reconnect; switch to new
address and check blacklist again */
+ n->state = S_RECONNECT_BLACKLIST;
set_address (&n->primary_address,
address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
- n->state = S_RECONNECT_BLACKLIST;
n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
check_blacklist (&n->id,
n->connect_ack_timestamp,
if (n->primary_address.session == session)
{
/* ATS switches back to still-active session */
- free_address (&n->alternative_address);
n->state = S_CONNECTED;
+ free_address (&n->alternative_address);
break;
}
/* ATS asks us to switch a life connection, update blacklist check */
break;
}
/* ATS asks us to switch a life connection, update blacklist check */
+ n->state = S_CONNECTED_SWITCHING_BLACKLIST;
set_address (&n->alternative_address,
address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
- n->state = S_CONNECTED_SWITCHING_BLACKLIST;
check_blacklist (&n->id,
GNUNET_TIME_absolute_get (),
address, session);
}
+
void
GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_HELLO_Address *address,
- struct Session *session,
- const struct GNUNET_MessageHeader *message)
+ const struct GNUNET_HELLO_Address *address,
+ struct Session *session,
+ const struct GNUNET_MessageHeader *message)
{
struct NeighbourMapEntry *n;
+
n = lookup_neighbour (peer);
if (NULL == n)
- {
- return;
- }
+ return;
n->util_total_bytes_recv += ntohs(message->size);
}
+
void
GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_HELLO_Address *address,
- struct Session *session,
- const struct GNUNET_MessageHeader *message)
+ const struct GNUNET_HELLO_Address *address,
+ struct Session *session,
+ const struct GNUNET_MessageHeader *message)
{
struct NeighbourMapEntry *n;
n = lookup_neighbour (peer);
if (NULL == n)
- {
- return;
- }
+ return;
n->util_payload_bytes_recv += ntohs(message->size);
}
void
GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_HELLO_Address *address,
- struct Session *session,
- size_t size)
+ const struct GNUNET_HELLO_Address *address,
+ struct Session *session,
+ size_t size)
{
struct NeighbourMapEntry *n;
n = lookup_neighbour (peer);
n->util_total_bytes_sent += size;
}
+
void
GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
- size_t size)
+ size_t size)
{
struct NeighbourMapEntry *n;
n = lookup_neighbour (peer);
if (NULL == n)
- {
- return;
- }
+ return;
n->util_payload_bytes_sent += size;
}
case S_INIT_ATS:
if (0 == delay.rel_value_us)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Connection to `%s' timed out waiting for ATS to provide address\n",
GNUNET_i2s (&n->id));
n->state = S_DISCONNECT_FINISHED;
case S_CONNECT_SENT:
if (0 == delay.rel_value_us)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
GNUNET_i2s (&n->id));
+ /* We could not send to this address, delete address and session */
+ if (NULL != n->primary_address.session)
+ GNUNET_ATS_address_destroyed (GST_ats,
+ n->primary_address.address, n->primary_address.session);
+ GNUNET_ATS_address_destroyed (GST_ats,
+ n->primary_address.address, NULL);
disconnect_neighbour (n);
return;
}
msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
(void) send_with_session(n,
(const char *) &msg, sizeof (struct GNUNET_MessageHeader),
- UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
+ UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
NULL, NULL);
}
* @param address address of the other peer, NULL if other peer
* connected to us
* @param session session to use (or NULL)
+ * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
*/
-void
+int
GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_HELLO_Address *address,
if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
{
GNUNET_break_op (0);
- return;
+ return GNUNET_SYSERR;
}
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop
+ ("# CONNECT_ACK messages received"),
+ 1, GNUNET_NO);
scm = (const struct SessionConnectMessage *) message;
GNUNET_break_op (ntohl (scm->reserved) == 0);
if (NULL == (n = lookup_neighbour (peer)))
gettext_noop
("# unexpected CONNECT_ACK messages (no peer)"),
1, GNUNET_NO);
- return;
+ return GNUNET_SYSERR;
}
ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
switch (n->state)
case S_NOT_CONNECTED:
GNUNET_break (0);
free_neighbour (n, GNUNET_NO);
- return;
+ return GNUNET_SYSERR;
case S_INIT_ATS:
case S_INIT_BLACKLIST:
GNUNET_STATISTICS_update (GST_stats,
break;
case S_CONNECT_SENT:
if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
- break; /* ACK does not match our original CONNECT message */
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
+ return GNUNET_OK;
+ }
n->state = S_CONNECTED;
n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
GNUNET_STATISTICS_set (GST_stats,
connect_notify_cb (callback_cls, &n->id,
n->primary_address.bandwidth_in,
n->primary_address.bandwidth_out);
- /* Tell ATS that the outbound session we created to send CONNECT was successfull */
- GST_ats_add_address (n->primary_address.address, n->primary_address.session);
+ /* Tell ATS that the outbound session we created to send CONNECT was successful */
+ GST_ats_add_address (n->primary_address.address,
+ n->primary_address.session,
+ NULL, 0);
set_address (&n->primary_address,
n->primary_address.address,
n->primary_address.session,
n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
- GST_ats_add_address (n->alternative_address.address, n->alternative_address.session);
+ GST_ats_add_address (n->alternative_address.address,
+ n->alternative_address.session,
+ NULL, 0);
set_address (&n->primary_address,
n->alternative_address.address,
n->alternative_address.session,
gettext_noop
("# unexpected CONNECT_ACK messages (disconnecting)"),
1, GNUNET_NO);
- break;
+ return GNUNET_SYSERR;
case S_DISCONNECT_FINISHED:
GNUNET_assert (0);
break;
"Unhandled state `%s'\n",
print_state (n->state));
GNUNET_break (0);
- break;
+ return GNUNET_SYSERR;
}
+ return GNUNET_OK;
}
*
* @param peer identity of the peer where the session died
* @param session session that is gone
- * @return GNUNET_YES if this was a session used, GNUNET_NO if
+ * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
* this session was not in use
*/
int
bcc_next = bcc->next;
if (bcc->na.session == session)
{
- GST_blacklist_test_cancel (bcc->bc);
+ if (NULL != bcc->bc)
+ GST_blacklist_test_cancel (bcc->bc);
GNUNET_HELLO_address_free (bcc->na.address);
GNUNET_CONTAINER_DLL_remove (bc_head,
bc_tail,
{
if (session == n->alternative_address.session)
{
- free_address (&n->alternative_address);
if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
(S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
n->state = S_CONNECTED;
else
GNUNET_break (0);
+ free_address (&n->alternative_address);
}
return GNUNET_NO; /* doesn't affect us further */
}
free_neighbour (n, GNUNET_NO);
return GNUNET_YES;
case S_CONNECTED:
- free_address (&n->primary_address);
n->state = S_RECONNECT_ATS;
+ free_address (&n->primary_address);
n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
/* FIXME: is this ATS call needed? */
n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
case S_CONNECTED_SWITCHING_BLACKLIST:
/* primary went down while we were checking secondary against
blacklist, adopt secondary as primary */
+ n->state = S_RECONNECT_BLACKLIST;
free_address (&n->primary_address);
n->primary_address = n->alternative_address;
memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
- n->state = S_RECONNECT_BLACKLIST;
break;
case S_CONNECTED_SWITCHING_CONNECT_SENT:
/* primary went down while we were waiting for CONNECT_ACK on secondary;
secondary as primary */
+ n->state = S_RECONNECT_SENT;
free_address (&n->primary_address);
n->primary_address = n->alternative_address;
memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
- n->state = S_RECONNECT_SENT;
break;
case S_DISCONNECT:
free_address (&n->primary_address);
* @param address address of the other peer, NULL if other peer
* connected to us
* @param session session to use (or NULL)
+ * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
*/
-void
+int
GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_HELLO_Address *address,
if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
{
GNUNET_break_op (0);
- return;
+ return GNUNET_SYSERR;
}
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop
+ ("# SESSION_ACK messages received"),
+ 1, GNUNET_NO);
if (NULL == (n = lookup_neighbour (peer)))
- return;
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
/* check if we are in a plausible state for having sent
a CONNECT_ACK. If not, return, otherwise break */
if ( ( (S_CONNECT_RECV_ACK != n->state) &&
(S_CONNECT_SENT != n->state) ) ||
(2 != n->send_connect_ack) )
{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
+ GNUNET_i2s (peer),
+ print_state (n->state),
+ n->send_connect_ack);
GNUNET_STATISTICS_update (GST_stats,
- gettext_noop ("# unexpected SESSION ACK messages"), 1,
+ gettext_noop ("# unexpected SESSION_ACK messages"), 1,
GNUNET_NO);
- return;
+ return GNUNET_OK;
}
n->state = S_CONNECTED;
n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
n->primary_address.bandwidth_in,
n->primary_address.bandwidth_out);
- GST_ats_add_address (n->primary_address.address, n->primary_address.session);
+ GST_ats_add_address (n->primary_address.address,
+ n->primary_address.session,
+ NULL, 0);
set_address (&n->primary_address,
n->primary_address.address,
n->primary_address.session,
n->primary_address.bandwidth_in,
n->primary_address.bandwidth_out,
GNUNET_YES);
+ return GNUNET_OK;
}
GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
if (0 != ntohl (quota.value__))
return;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
- GNUNET_i2s (&n->id), "SET_QUOTA");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnecting peer `%4s' due to SET_QUOTA\n",
+ GNUNET_i2s (&n->id));
if (GNUNET_YES == test_connected (n))
GNUNET_STATISTICS_update (GST_stats,
gettext_noop ("# disconnects due to quota of 0"),
* @param msg the disconnect message
*/
void
-GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
- *peer,
- const struct GNUNET_MessageHeader
- *msg)
+GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *msg)
{
struct NeighbourMapEntry *n;
const struct SessionDisconnectMessage *sdm;
GNUNET_NO);
return;
}
+ GNUNET_STATISTICS_update (GST_stats,
+ gettext_noop
+ ("# DISCONNECT messages received"),
+ 1, GNUNET_NO);
sdm = (const struct SessionDisconnectMessage *) msg;
if (NULL == (n = lookup_neighbour (peer)))
return; /* gone already */
gettext_noop
("# other peer asked to disconnect from us"), 1,
GNUNET_NO);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnecting by request from peer %s\n",
+ GNUNET_i2s (peer));
disconnect_neighbour (n);
}
gettext_noop
("# disconnected from peer upon explicit request"), 1,
GNUNET_NO);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Forced disconnect from peer %s\n",
+ GNUNET_i2s (target));
disconnect_neighbour (n);
}