* @brief core service; this is the main API for encrypted P2P
* communications
* @author Christian Grothoff
- *
- * TODO:
- * - implement atsi parsing and passing
*/
#include "platform.h"
#include "gnunet_constants.h"
*/
void *cont_cls;
+ /**
+ * Transmit handle (if one is associated with this ControlMessage), or NULL.
+ */
+ struct GNUNET_CORE_TransmitHandle *th;
};
reconnect_later (struct GNUNET_CORE_Handle *h)
{
struct ControlMessage *cm;
+ struct PeerRecord *pr;
+ while (NULL != (cm = h->pending_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (h->pending_head,
+ h->pending_tail,
+ cm);
+ if (cm->th != NULL)
+ cm->th->cm = NULL;
+ if (cm->cont != NULL)
+ cm->cont (cm->cont_cls, NULL);
+ GNUNET_free (cm);
+ }
if (h->client != NULL)
{
GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
h->client = NULL;
+ h->cth = NULL;
GNUNET_CONTAINER_multihashmap_iterate (h->peers,
&disconnect_and_free_peer_entry,
h);
}
+ while (NULL != (pr = h->ready_peer_head))
+ GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
+ h->ready_peer_tail,
+ pr);
+
+ GNUNET_assert (h->pending_head == NULL);
h->currently_down = GNUNET_YES;
- while (NULL != (cm = h->pending_head))
- {
- GNUNET_CONTAINER_DLL_remove (h->pending_head,
- h->pending_tail,
- cm);
- cm->cont (cm->cont_cls, NULL);
- GNUNET_free (cm);
- }
GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
h->retry_backoff);
const struct GNUNET_SCHEDULER_TaskContext *tc);
-/**
- * Control message was sent, mark it as such.
- *
- * @param cls the 'struct GNUNET_CORE_TransmitHandle*'
- * @param tc scheduler context
- */
-static void
-mark_control_message_sent (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct GNUNET_CORE_TransmitHandle *th = cls;
-
- th->cm = NULL;
-}
-
-
/**
* Send a control message to the peer asking for transmission
* of the message in the given peer record.
pr);
cm = GNUNET_malloc (sizeof (struct ControlMessage) +
sizeof (struct SendMessageRequest));
- cm->cont = &mark_control_message_sent;
- cm->cont_cls = th;
th->cm = cm;
+ cm->th = th;
smr = (struct SendMessageRequest*) &cm[1];
smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
smr->header.size = htons (sizeof (struct SendMessageRequest));
h->pending_tail,
h->pending_tail,
cm);
+#if DEBUG_CORE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding SEND REQUEST for peer `%s' to message queue\n",
+ GNUNET_i2s (&pr->peer));
+#endif
trigger_next_request (h, GNUNET_NO);
}
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct PeerRecord *pr = cls;
+ struct GNUNET_CORE_Handle *h = pr->ch;
struct GNUNET_CORE_TransmitHandle *th;
-
+
pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
th = pr->pending_head;
GNUNET_CONTAINER_DLL_remove (pr->pending_head,
pr->pending_tail,
th);
pr->queue_size--;
+ if ( (pr->prev != NULL) ||
+ (pr->next != NULL) ||
+ (pr == h->ready_peer_head) )
+ {
+ /* the request that was 'approved' by core was
+ canceled before it could be transmitted; remove
+ us from the 'ready' list */
+ GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
+ h->ready_peer_tail,
+ pr);
+ }
#if DEBUG_CORE
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Signalling timeout of request for transmission to CORE service\n");
memcpy (buf, hdr, msize);
GNUNET_CONTAINER_DLL_remove (h->pending_head,
h->pending_tail,
- cm);
+ cm);
+ if (cm->th != NULL)
+ cm->th->cm = NULL;
if (NULL != cm->cont)
GNUNET_SCHEDULER_add_continuation (cm->cont,
cm->cont_cls,
/* now check for 'ready' P2P messages */
if (NULL != (pr = h->ready_peer_head))
{
+ GNUNET_assert (pr->pending_head != NULL);
th = pr->pending_head;
if (size < th->msize + sizeof (struct SendMessage))
{
if ( (GNUNET_YES == h->currently_down) &&
(ignore_currently_down == GNUNET_NO) )
- return;
+ {
+#if DEBUG_CORE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Core connection down, not processing queue\n");
+#endif
+ return;
+ }
if (NULL != h->cth)
- return;
+ {
+#if DEBUG_CORE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Request pending, not processing queue\n");
+#endif
+ return;
+ }
if (h->pending_head != NULL)
msize = ntohs (((struct GNUNET_MessageHeader*) &h->pending_head[1])->size);
- else if (h->ready_peer_head != NULL)
+ else if (h->ready_peer_head != NULL)
msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
else
- return; /* no pending message */
+ {
+#if DEBUG_CORE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Request queue empty, not processing queue\n");
+#endif
+ return; /* no pending message */
+ }
h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
msize,
GNUNET_TIME_UNIT_FOREVER_REL,
int trigger;
uint16_t msize;
uint16_t et;
+ uint32_t ats_count;
if (msg == NULL)
{
}
break;
case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
- if (msize != sizeof (struct ConnectNotifyMessage))
+ if (msize < sizeof (struct ConnectNotifyMessage))
{
GNUNET_break (0);
reconnect_later (h);
return;
}
cnm = (const struct ConnectNotifyMessage *) msg;
+ ats_count = ntohl (cnm->ats_count);
+ if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
+ (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
+ {
+ GNUNET_break (0);
+ reconnect_later (h);
+ return;
+ }
#if DEBUG_CORE
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Received notification about connection from `%s'.\n",
if (NULL != h->connects)
h->connects (h->cls,
&cnm->peer,
- NULL /* FIXME: atsi! */);
+ &cnm->ats);
break;
case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
if (msize != sizeof (struct DisconnectNotifyMessage))
GNUNET_break (0);
break;
}
- if (msize != sizeof (struct PeerStatusNotifyMessage))
+ if (msize < sizeof (struct PeerStatusNotifyMessage))
{
GNUNET_break (0);
reconnect_later (h);
GNUNET_break (0);
return;
}
+ ats_count = ntohl (psnm->ats_count);
+ if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
+ (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
+ {
+ GNUNET_break (0);
+ reconnect_later (h);
+ return;
+ }
#if DEBUG_CORE
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Received notification about status change by `%s'.\n",
psnm->bandwidth_in,
psnm->bandwidth_out,
GNUNET_TIME_absolute_ntoh (psnm->timeout),
- NULL /* FIXME: atsi */);
+ &psnm->ats);
break;
case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
- if (msize <
- sizeof (struct NotifyTrafficMessage) +
- sizeof (struct GNUNET_MessageHeader))
+ if (msize < sizeof (struct NotifyTrafficMessage))
{
GNUNET_break (0);
reconnect_later (h);
GNUNET_break (0);
return;
}
- em = (const struct GNUNET_MessageHeader *) &ntm[1];
+ ats_count = ntohl (ntm->ats_count);
+ if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
+ + sizeof (struct GNUNET_MessageHeader)) ||
+ (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
+ {
+ GNUNET_break (0);
+ reconnect_later (h);
+ return;
+ }
+ em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
#if DEBUG_CORE
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Received message of type %u and size %u from peer `%4s'\n",
return;
}
if ((GNUNET_NO == h->inbound_hdr_only) &&
- (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
+ (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
+ + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
{
GNUNET_break (0);
reconnect_later (h);
}
if (GNUNET_OK !=
h->handlers[hpos].callback (h->cls, &ntm->peer, em,
- NULL /* FIXME: atsi */))
+ &ntm->ats))
{
/* error in processing, do not process other messages! */
break;
}
if (NULL != h->inbound_notify)
h->inbound_notify (h->cls, &ntm->peer, em,
- NULL /* FIXME: atsi */);
+ &ntm->ats);
break;
case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
- if (msize <
- sizeof (struct NotifyTrafficMessage) +
- sizeof (struct GNUNET_MessageHeader))
+ if (msize < sizeof (struct NotifyTrafficMessage))
{
GNUNET_break (0);
reconnect_later (h);
GNUNET_break (0);
return;
}
- em = (const struct GNUNET_MessageHeader *) &ntm[1];
+ ats_count = ntohl (ntm->ats_count);
+ if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
+ + sizeof (struct GNUNET_MessageHeader)) ||
+ (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
+ {
+ GNUNET_break (0);
+ reconnect_later (h);
+ return;
+ }
+ em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
&ntm->peer.hashPubKey);
if (pr == NULL)
GNUNET_i2s (&ntm->peer));
#endif
if ((GNUNET_NO == h->outbound_hdr_only) &&
- (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
+ (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
+ + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
{
GNUNET_break (0);
reconnect_later (h);
break;
}
h->outbound_notify (h->cls, &ntm->peer, em,
- NULL /* FIXME: atsi? */);
+ &ntm->ats);
break;
case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
if (msize != sizeof (struct SendMessageReady))
"Received notification about transmission readiness to `%s'.\n",
GNUNET_i2s (&smr->peer));
#endif
+ if (pr->pending_head == NULL)
+ {
+ /* request must have been cancelled between the original request
+ and the response from core, ignore core's readiness */
+ return;
+ }
+
th = pr->pending_head;
if (ntohs (smr->smr_id) != th->smr_id)
{
/**
* Disconnect from the core service. This function can only
* be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
- * requests have been explicitly cancelled.
+ * requests have been explicitly canceled.
*
* @param handle connection to core to disconnect
*/
GNUNET_CONTAINER_DLL_remove (handle->pending_head,
handle->pending_tail,
cm);
- cm->cont (cm->cont_cls, NULL);
+ if (cm->th != NULL)
+ cm->th->cm = NULL;
+ if (cm->cont != NULL)
+ cm->cont (cm->cont_cls, NULL);
GNUNET_free (cm);
}
GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
/* bound queue size */
if (pr->queue_size == handle->queue_size)
{
- /* find lowest-priority entry */
- minp = pr->pending_head;
- prev = minp->next;
+ /* find lowest-priority entry, but skip the head of the list */
+ minp = pr->pending_head->next;
+ prev = minp;
while (prev != NULL)
{
if (prev->priority < minp->priority)
return NULL;
}
if (priority <= minp->priority)
- return NULL; /* priority too low */
+ {
+#if DEBUG_CORE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Dropping transmission request: priority too low\n");
+#endif
+ return NULL; /* priority too low */
+ }
GNUNET_CONTAINER_DLL_remove (pr->pending_head,
pr->pending_tail,
minp);
th);
pr->queue_size++;
/* was the request queue previously empty? */
+#if DEBUG_CORE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Transmission request added to queue\n");
+#endif
if (pr->pending_head == th)
request_next_transmission (pr);
return th;
GNUNET_CONTAINER_DLL_remove (pr->pending_head,
pr->pending_tail,
th);
+ pr->queue_size--;
if (th->cm != NULL)
{
/* we're currently in the control queue, remove */
(pr == h->ready_peer_head) )
{
/* the request that was 'approved' by core was
- cancelled before it could be transmitted; remove
+ canceled before it could be transmitted; remove
us from the 'ready' list */
GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
h->ready_peer_tail,
ret->cont_cls = cont_cls;
cm->cont = &peer_request_connect_cont;
cm->cont_cls = ret;
+#if DEBUG_CORE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Queueing REQUEST_CONNECT request\n");
+#endif
if (h->pending_head == cm)
trigger_next_request (h, GNUNET_NO);
return ret;
rim->reserve_inbound = htonl (amount);
rim->preference_change = GNUNET_htonll(preference);
rim->peer = *peer;
+#if DEBUG_CORE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Queueing CHANGE PREFERENCE request\n");
+#endif
GNUNET_CONTAINER_DLL_insert (h->pending_head,
h->pending_tail,
cm);
pr->pcic = info;
pr->pcic_cls = info_cls;
+ if (h->pending_head == cm)
+ trigger_next_request (h, GNUNET_NO);
return irc;
}
}
-#if NEW
-/* ********************* GNUNET_CORE_iterate_peers *********************** */
-
-/**
- * Context for 'iterate_peers' helper function.
- */
-struct IterationContext
-{
- /**
- * Callback to call.
- */
- GNUNET_CORE_ConnectEventHandler peer_cb;
-
- /**
- * Closure for 'peer_cb'.
- */
- void *cb_cls;
-};
-
-
-/**
- * Call callback for each peer.
- *
- * @param cls the 'struct IterationContext'
- * @param hc peer identity, not used
- * @param value the 'struct PeerRecord'
- * @return GNUNET_YES (continue iteration)
- */
-static int
-iterate_peers (void *cls,
- const GNUNET_HashCode *hc,
- void *value)
-{
- struct IterationContext *ic = cls;
- struct PeerRecord *pr = value;
-
- ic->peer_cb (ic->cb_cls,
- &pr->peer,
- NULL /* FIXME: pass atsi? */);
- return GNUNET_YES;
-}
-
-
-/**
- * Obtain statistics and/or change preferences for the given peer.
- *
- * @param h handle to core
- * @param peer_cb function to call with the peer information
- * @param cb_cls closure for peer_cb
- * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
- */
-int
-GNUNET_CORE_iterate_peers (struct GNUNET_CORE_Handle *h,
- GNUNET_CORE_ConnectEventHandler peer_cb,
- void *cb_cls)
-{
- struct IterationContext ic;
-
- ic.peer_cb = peer_cb;
- ic.cb_cls = cb_cls;
- GNUNET_CONTAINER_multihashmap_iterate (h->peers,
- &iterate_peers,
- &ic);
- return GNUNET_OK;
-}
-#endif
-
/* end of core_api.c */