/**
* @file arm/arm_api.c
* @brief API for accessing the ARM service
- * @author Christian Grothoff, LRN
+ * @author Christian Grothoff
+ * @author LRN
*/
#include "platform.h"
-#include "gnunet_arm_service.h"
#include "gnunet_util_lib.h"
+#include "gnunet_arm_service.h"
#include "gnunet_protocols.h"
#include "arm.h"
struct ARMControlMessage *control_sent_tail;
/**
- * ID of the reconnect task (if any).
+ * Callback to invoke on connection/disconnection.
*/
- GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+ GNUNET_ARM_ConnectionStatusCallback conn_status;
/**
- * Current delay we use for re-trying to connect to core.
+ * Closure for conn_status.
*/
- struct GNUNET_TIME_Relative retry_backoff;
+ void *conn_status_cls;
/**
- * Callback to invoke on connection/disconnection.
+ * ARM control message for the 'arm_termination_handler'
+ * with the continuation to call once the ARM shutdown is done.
*/
- GNUNET_ARM_ConnectionStatusCallback conn_status;
+ struct ARMControlMessage *thm;
/**
- * Closure for conn_status.
+ * ID of the reconnect task (if any).
*/
- void *conn_status_cls;
+ GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+
+ /**
+ * Current delay we use for re-trying to connect to core.
+ */
+ struct GNUNET_TIME_Relative retry_backoff;
/**
* Counter for request identifiers
struct GNUNET_TIME_Absolute timeout;
/**
- * Flags for passing std descriptors to ARM (when starting ARM).
+ * Task to run when request times out.
*/
- enum GNUNET_OS_InheritStdioFlags std_inheritance;
+ GNUNET_SCHEDULER_TaskIdentifier timeout_task_id;
/**
- * Task to run when request times out.
+ * Flags for passing std descriptors to ARM (when starting ARM).
*/
- GNUNET_SCHEDULER_TaskIdentifier timeout_task_id;
+ enum GNUNET_OS_InheritStdioFlags std_inheritance;
/**
* Type of the request expressed as a message type (start, stop or list).
uint16_t type;
};
-static void
-client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg);
+/**
+ * Connect to arm.
+ *
+ * @param h arm handle
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ */
static int
reconnect_arm (struct GNUNET_ARM_Handle *h);
+
+/**
+ * Check the list of pending requests, send the next
+ * one to the arm.
+ *
+ * @param h arm handle
+ * @param ignore_currently_down transmit message even if not initialized?
+ */
static void
trigger_next_request (struct GNUNET_ARM_Handle *h, int ignore_currently_down);
}
-static void
-clear_pending_messages (struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus result)
-{
- struct ARMControlMessage *cm;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Clearing pending messages\n");
-
- while (NULL != (cm = h->control_pending_head))
- {
- GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
- h->control_pending_tail, cm);
- GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
- GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
- if (NULL != cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, result, NULL, 0);
- GNUNET_free_non_null (cm->msg);
- GNUNET_free (cm);
- }
-}
-
/**
* Close down any existing connection to the ARM service and
* try re-establishing it later.
GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
h->reconnect_task =
GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_arm_task, h);
- /* Don't clear pending messages on disconnection, deliver them later
+ /* Don't clear pending messages on disconnection, deliver them later
clear_pending_messages (h, GNUNET_ARM_REQUEST_DISCONNECTED);
GNUNET_assert (NULL == h->control_pending_head);
*/
h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
if (NULL != h->conn_status)
- h->conn_status (h->conn_status_cls, h, GNUNET_NO);
+ h->conn_status (h->conn_status_cls, GNUNET_NO);
+}
+
+
+/**
+ * Find a control message by its unique ID.
+ *
+ * @param h ARM handle
+ * @param id unique message ID to use for the lookup
+ * @return NULL if not found
+ */
+static struct ARMControlMessage *
+find_cm_by_id (struct GNUNET_ARM_Handle *h, uint64_t id)
+{
+ struct ARMControlMessage *result;
+ for (result = h->control_sent_head; result; result = result->next)
+ if (id == result->msg->request_id)
+ return result;
+ return NULL;
+}
+
+
+/**
+ * Handler for ARM 'termination' reply (failure to receive).
+ *
+ * @param cls our "struct GNUNET_ARM_Handle"
+ * @param msg expected to be NULL
+ */
+static void
+arm_termination_handler (void *cls, const struct GNUNET_MessageHeader *msg)
+{
+ struct GNUNET_ARM_Handle *h = cls;
+ struct ARMControlMessage *cm;
+
+ if (NULL != msg)
+ {
+ GNUNET_break (0);
+ GNUNET_CLIENT_receive (h->client, &arm_termination_handler, h,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+ return;
+ }
+ cm = h->thm;
+ h->thm = NULL;
+ h->currently_down = GNUNET_YES;
+ GNUNET_CLIENT_disconnect (h->client);
+ h->client = NULL;
+ if (NULL != cm->result_cont)
+ cm->result_cont (cm->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK,
+ (const char *) &cm->msg[1],
+ GNUNET_ARM_RESULT_STOPPED);
+ GNUNET_free (cm->msg);
+ GNUNET_free (cm);
+}
+
+
+/**
+ * Handler for ARM replies.
+ *
+ * @param cls our "struct GNUNET_ARM_Handle"
+ * @param msg the message received from the arm service
+ */
+static void
+client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
+{
+ struct GNUNET_ARM_Handle *h = cls;
+ const struct GNUNET_ARM_Message *arm_msg;
+ const struct GNUNET_ARM_ResultMessage *res;
+ const struct GNUNET_ARM_ListResultMessage *lres;
+ struct ARMControlMessage *cm;
+ const char **list;
+ const char *pos;
+ uint64_t id;
+ enum GNUNET_ARM_Result result;
+ uint16_t size_check;
+ uint16_t rcount;
+ uint16_t msize;
+ unsigned char fail;
+
+ list = NULL;
+ rcount = 0;
+ if (NULL == msg)
+ {
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ _("Client was disconnected from arm service, trying to reconnect.\n"));
+ reconnect_arm_later (h);
+ return;
+ }
+ msize = ntohs (msg->size);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Processing message of type %u and size %u from arm service\n",
+ ntohs (msg->type), msize);
+ if (msize < sizeof (struct GNUNET_ARM_Message))
+ {
+ GNUNET_break (0);
+ reconnect_arm_later (h);
+ return;
+ }
+ arm_msg = (const struct GNUNET_ARM_Message *) msg;
+ id = GNUNET_ntohll (arm_msg->request_id);
+ cm = find_cm_by_id (h, id);
+ if (NULL == cm)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Message with unknown id %llu\n", id);
+ return;
+ }
+ fail = GNUNET_NO;
+ switch (ntohs (msg->type))
+ {
+ case GNUNET_MESSAGE_TYPE_ARM_RESULT:
+ if (msize < sizeof (struct GNUNET_ARM_ResultMessage))
+ {
+ GNUNET_assert (0);
+ fail = GNUNET_YES;
+ }
+ break;
+ case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
+ if (msize < sizeof (struct GNUNET_ARM_ListResultMessage))
+ {
+ GNUNET_break (0);
+ fail = GNUNET_YES;
+ break;
+ }
+ size_check = 0;
+ lres = (const struct GNUNET_ARM_ListResultMessage *) msg;
+ rcount = ntohs (lres->count);
+ {
+ unsigned int i;
+
+ list = GNUNET_malloc (sizeof (const char *) * rcount);
+ pos = (const char *)&lres[1];
+ for (i = 0; i < rcount; i++)
+ {
+ const char *end = memchr (pos, 0, msize - size_check);
+ if (NULL == end)
+ {
+ GNUNET_break (0);
+ fail = GNUNET_YES;
+ break;
+ }
+ list[i] = pos;
+ size_check += (end - pos) + 1;
+ pos = end + 1;
+ }
+ if (GNUNET_YES == fail)
+ {
+ GNUNET_free (list);
+ list = NULL;
+ }
+ }
+ break;
+ default:
+ fail = GNUNET_YES;
+ break;
+ }
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
+ GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
+ GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
+ h->control_sent_tail, cm);
+ if (GNUNET_YES == fail)
+ {
+ reconnect_arm_later (h);
+ GNUNET_free (cm->msg);
+ GNUNET_free (cm);
+ return;
+ }
+ if ( (GNUNET_MESSAGE_TYPE_ARM_RESULT == ntohs (msg->type)) &&
+ (0 == strcasecmp ((const char *) &cm->msg[1],
+ "arm")) &&
+ (NULL != (res = (const struct GNUNET_ARM_ResultMessage *) msg)) &&
+ (GNUNET_ARM_RESULT_STOPPING == ntohl (res->result)) )
+ {
+ /* special case: if we are stopping 'gnunet-service-arm', we do not just
+ wait for the result message, but also wait for the service to close
+ the connection (and then we have to close our client handle as well);
+ this is done by installing a different receive handler, waiting for
+ the connection to go down */
+ if (NULL != h->thm)
+ {
+ GNUNET_break (0);
+ cm->result_cont (h->thm->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK,
+ (const char *) &h->thm->msg[1],
+ GNUNET_ARM_RESULT_IS_NOT_KNOWN);
+ GNUNET_free (h->thm->msg);
+ GNUNET_free (h->thm);
+ }
+ h->thm = cm;
+ GNUNET_CLIENT_receive (h->client, &arm_termination_handler, h,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+ return;
+ }
+ GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+ switch (ntohs (msg->type))
+ {
+ case GNUNET_MESSAGE_TYPE_ARM_RESULT:
+ res = (const struct GNUNET_ARM_ResultMessage *) msg;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received response from ARM for service `%s': %u\n",
+ (const char *) &cm->msg[1], ntohs (msg->type));
+ result = (enum GNUNET_ARM_Result) ntohl (res->result);
+ if (NULL != cm->result_cont)
+ cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_SENT_OK,
+ (const char *) &cm->msg[1], result);
+ break;
+ case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
+ if (NULL != cm->list_cont)
+ cm->list_cont (cm->cont_cls, GNUNET_ARM_REQUEST_SENT_OK, rcount,
+ list);
+ GNUNET_free (list);
+ break;
+ }
+ GNUNET_free (cm->msg);
+ GNUNET_free (cm);
}
+
/**
* Transmit the next message to the arm service.
*
* @param cls closure with the 'struct GNUNET_ARM_Handle'
* @param size number of bytes available in buf
* @param buf where the callee should write the message
- * @return number of bytes written to buf
+ * @return number of bytes written to buf
*/
static size_t
transmit_arm_message (void *cls, size_t size, void *buf)
"transmit_arm_message is running with %p buffer of size %lu. ARM is known to be %s\n",
buf, size, h->currently_down ? "unconnected" : "connected");
GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
- h->cth = NULL;
+ h->cth = NULL;
if ((GNUNET_YES == h->currently_down) && (NULL != buf))
{
h->currently_down = GNUNET_NO;
end:
if ((GNUNET_YES == notify_connection) && (NULL != h->conn_status))
- h->conn_status (h->conn_status_cls, h, GNUNET_YES);
+ h->conn_status (h->conn_status_cls, GNUNET_YES);
return msize;
}
}
if (NULL != h->control_pending_head)
msize =
- ntohs (((struct GNUNET_MessageHeader *) &h->
- control_pending_head[1])->size);
+ ntohs (h->control_pending_head->msg->header.size);
else if (GNUNET_NO == ignore_currently_down)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
* Connect to arm.
*
* @param h arm handle
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
*/
static int
reconnect_arm (struct GNUNET_ARM_Handle *h)
LOG (GNUNET_ERROR_TYPE_DEBUG,
"arm_api, GNUNET_CLIENT_connect returned NULL\n");
if (NULL != h->conn_status)
- h->conn_status (h->conn_status_cls, h, GNUNET_SYSERR);
+ h->conn_status (h->conn_status_cls, GNUNET_SYSERR);
return GNUNET_SYSERR;
}
LOG (GNUNET_ERROR_TYPE_DEBUG,
void
GNUNET_ARM_disconnect_and_free (struct GNUNET_ARM_Handle *h)
{
+ struct ARMControlMessage *cm;
+
LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from ARM service\n");
if (NULL != h->cth)
{
GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
h->cth = NULL;
}
- clear_pending_messages (h, GNUNET_ARM_REQUEST_DISCONNECTED);
+ while ((NULL != (cm = h->control_pending_head))
+ || (NULL != (cm = h->control_sent_head)) )
+ {
+ if (NULL != h->control_pending_head)
+ GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
+ h->control_pending_tail, cm);
+ else
+ GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
+ h->control_sent_tail, cm);
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
+ GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
+ if (NULL != cm->result_cont)
+ cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_DISCONNECTED,
+ NULL, 0);
+ /* FIXME: What about list callback? */
+ GNUNET_free_non_null (cm->msg);
+ GNUNET_free (cm);
+ }
if (NULL != h->client)
{
GNUNET_CLIENT_disconnect (h->client);
cm->h->control_sent_tail, cm);
}
if (NULL != cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_TIMEOUT, NULL, 0);
+ cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_TIMEOUT, NULL, 0);
else if (NULL != cm->list_cont)
- cm->list_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_TIMEOUT, 0, NULL);
+ cm->list_cont (cm->cont_cls, GNUNET_ARM_REQUEST_TIMEOUT, 0, NULL);
GNUNET_free_non_null (cm->msg);
GNUNET_free (cm);
}
* it is not, start the ARM process.
*
* @param cls the context for the request that we will report on (struct ARMControlMessage *)
- * @param tc why were we called (reason says if ARM is running)
+ * @param result GNUNET_YES if ARM is running
*/
static void
-arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+arm_service_report (void *cls,
+ int result)
{
struct ARMControlMessage *cm = cls;
struct GNUNET_ARM_Handle *h;
char *lopostfix;
test_is_active = cm->h->service_test_is_active;
-
- /* FIXME: shouldn't we check for GNUNET_SCHEDULER_REASON_SHUTDOWN ? */
if ((GNUNET_YES == test_is_active) &&
- (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)))
+ (GNUNET_YES == result))
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Looks like `%s' is already running.\n",
"gnunet-service-arm");
/* arm is running! */
if (cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
+ cm->result_cont (cm->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK, "arm",
+ GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
}
if (GNUNET_NO == test_is_active)
{
GNUNET_CONFIGURATION_destroy (cm->h->cfg);
GNUNET_free (cm->h);
}
- if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) ||
+ if ((GNUNET_YES == result) ||
(GNUNET_NO == test_is_active))
{
GNUNET_free (cm);
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, "arm", "BINARY");
if (cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_NOT_KNOWN);
+ cm->result_cont (cm->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK, "arm",
+ GNUNET_ARM_RESULT_IS_NOT_KNOWN);
GNUNET_free (cm);
GNUNET_free (loprefix);
GNUNET_free (lopostfix);
if (NULL == proc)
{
if (cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm",
+ cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_SENT_OK, "arm",
GNUNET_ARM_RESULT_START_FAILED);
GNUNET_free (cm);
return;
}
if (cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm",
+ cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_SENT_OK, "arm",
GNUNET_ARM_RESULT_STARTING);
GNUNET_OS_process_destroy (proc);
h = cm->h;
{
GNUNET_break (0);
if (cb != NULL)
- cb (cb_cls, h, GNUNET_ARM_REQUEST_TOO_LONG, NULL, 0);
+ cb (cb_cls, GNUNET_ARM_REQUEST_TOO_LONG, NULL, 0);
return;
}
LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting %s of service `%s'.\n",
memcpy (&msg[1], service_name, slen);
cm->msg = msg;
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Inserting a control message into the queue. Timeout is %llu\n",
- GNUNET_TIME_absolute_get_remaining (cm->timeout).rel_value);
+ "Inserting a control message into the queue. Timeout is %s\n",
+ GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (cm->timeout),
+ GNUNET_NO));
GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
h->control_pending_tail, cm);
cm->timeout_task_id =
*/
void
GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
- const char *service_name, enum GNUNET_OS_InheritStdioFlags std_inheritance,
- struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cont,
- void *cont_cls)
+ const char *service_name,
+ enum GNUNET_OS_InheritStdioFlags std_inheritance,
+ struct GNUNET_TIME_Relative timeout,
+ GNUNET_ARM_ResultCallback cont,
+ void *cont_cls)
{
struct ARMControlMessage *cm;
size_t slen;
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "ARM is already running\n");
if (NULL != cont)
- cont (cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
+ cont (cont_cls, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
}
else if (GNUNET_NO == h->service_test_is_active)
{
*/
LOG (GNUNET_ERROR_TYPE_DEBUG, "Service test is already in progress, we're busy\n");
if (NULL != cont)
- cont (cont_cls, h, GNUNET_ARM_REQUEST_BUSY, NULL, 0);
+ cont (cont_cls, GNUNET_ARM_REQUEST_BUSY, NULL, 0);
}
return;
}
*/
void
GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
- const char *service_name, struct GNUNET_TIME_Relative timeout,
- GNUNET_ARM_ResultCallback cont, void *cont_cls)
+ const char *service_name,
+ struct GNUNET_TIME_Relative timeout,
+ GNUNET_ARM_ResultCallback cont,
+ void *cont_cls)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"Stopping service `%s' within %s\n",
- service_name,
+ service_name,
GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
change_service (h, service_name, timeout, cont, cont_cls,
GNUNET_MESSAGE_TYPE_ARM_STOP);
void
GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
struct GNUNET_TIME_Relative timeout,
- GNUNET_ARM_ServiceListCallback cont,
+ GNUNET_ARM_ServiceListCallback cont,
void *cont_cls)
{
struct ARMControlMessage *cm;
struct GNUNET_ARM_Message *msg;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Requesting LIST from ARM service with timeout: %s\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Requesting LIST from ARM service with timeout: %s\n",
GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
cm = GNUNET_malloc (sizeof (struct ARMControlMessage));
cm->h = h;
}
-static struct ARMControlMessage *
-find_cm_by_id (struct GNUNET_ARM_Handle *h, uint64_t id)
-{
- struct ARMControlMessage *result;
- for (result = h->control_sent_head; result; result = result->next)
- if (id == result->msg->request_id)
- return result;
- return NULL;
-}
-
-
-/**
- * Handler for ARM replies.
- *
- * @param cls our "struct GNUNET_ARM_Handle"
- * @param msg the message received from the arm service
- */
-static void
-client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
-{
- struct GNUNET_ARM_Handle *h = cls;
- const struct GNUNET_ARM_Message *arm_msg;
- const struct GNUNET_ARM_ResultMessage *res;
- const struct GNUNET_ARM_ListResultMessage *lres;
- struct ARMControlMessage *cm;
- const char **list;
- const char *pos;
- uint64_t id;
- enum GNUNET_ARM_Result result;
- uint16_t size_check;
- uint16_t rcount;
- uint16_t msize;
- unsigned char fail;
-
- list = NULL;
- if (NULL == msg)
- {
- LOG (GNUNET_ERROR_TYPE_INFO,
- _("Client was disconnected from arm service, trying to reconnect.\n"));
- reconnect_arm_later (h);
- return;
- }
- msize = ntohs (msg->size);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Processing message of type %u and size %u from arm service\n",
- ntohs (msg->type), msize);
- if (msize < sizeof (struct GNUNET_ARM_Message))
- {
- GNUNET_break (0);
- reconnect_arm_later (h);
- return;
- }
- arm_msg = (const struct GNUNET_ARM_Message *) msg;
- id = GNUNET_ntohll (arm_msg->request_id);
- cm = find_cm_by_id (h, id);
- if (NULL == cm)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Message with unknown id %llu\n", id);
- return;
- }
- fail = GNUNET_NO;
- switch (ntohs (msg->type))
- {
- case GNUNET_MESSAGE_TYPE_ARM_RESULT:
- if (msize < sizeof (struct GNUNET_ARM_ResultMessage))
- {
- GNUNET_assert (0);
- fail = GNUNET_YES;
- }
- break;
- case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
- if (msize < sizeof (struct GNUNET_ARM_ListResultMessage))
- {
- GNUNET_break (0);
- fail = GNUNET_YES;
- break;
- }
- size_check = 0;
- lres = (const struct GNUNET_ARM_ListResultMessage *) msg;
- rcount = ntohs (lres->count);
- {
- unsigned int i;
-
- list = GNUNET_malloc (sizeof (const char *) * rcount);
- pos = (const char *)&lres[1];
- for (i = 0; i < rcount; i++)
- {
- const char *end = memchr (pos, 0, msize - size_check);
- if (NULL == end)
- {
- GNUNET_break (0);
- fail = GNUNET_YES;
- break;
- }
- list[i] = pos;
- size_check += (end - pos) + 1;
- pos = end + 1;
- }
- if (GNUNET_YES == fail)
- {
- GNUNET_free (list);
- list = NULL;
- }
- }
- break;
- default:
- fail = GNUNET_YES;
- break;
- }
- GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
- GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
- GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
- h->control_sent_tail, cm);
- if (GNUNET_YES == fail)
- {
- reconnect_arm_later (h);
- GNUNET_free (cm->msg);
- GNUNET_free (cm);
- return;
- }
- GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
- GNUNET_TIME_UNIT_FOREVER_REL);
- switch (ntohs (msg->type))
- {
- case GNUNET_MESSAGE_TYPE_ARM_RESULT:
- res = (const struct GNUNET_ARM_ResultMessage *) msg;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Received response from ARM for service `%s': %u\n",
- (const char *) &cm->msg[1], ntohs (msg->type));
- result = (enum GNUNET_ARM_Result) ntohl (res->result);
- if (NULL != cm->result_cont)
- cm->result_cont (cm->cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK,
- (const char *) &cm->msg[1], result);
- break;
- case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
- if (NULL != cm->list_cont)
- cm->list_cont (cm->cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, rcount,
- list);
- GNUNET_free (list);
- break;
- }
- GNUNET_free (cm->msg);
- GNUNET_free (cm);
-}
-
/* end of arm_api.c */