2 * This file is part of GNUnet
3 * (C) 2013 Christian Grothoff (and other contributing authors)
5 * GNUnet is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your
8 * option) any later version.
10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * @file psyc/psyc_util_lib.c
23 * @brief PSYC utilities; receiving/transmitting/logging PSYC messages.
24 * @author Gabor X Toth
30 #include "gnunet_util_lib.h"
31 #include "gnunet_env_lib.h"
32 #include "gnunet_psyc_service.h"
33 #include "gnunet_psyc_util_lib.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "psyc-util",__VA_ARGS__)
38 struct GNUNET_PSYC_TransmitHandle
41 * Client connection to service.
43 struct GNUNET_CLIENT_MANAGER_Connection *client;
46 * Message currently being received from the client.
48 struct GNUNET_MessageHeader *msg;
51 * Callback to request next modifier from client.
53 GNUNET_PSYC_TransmitNotifyModifier notify_mod;
56 * Closure for the notify callbacks.
61 * Callback to request next data fragment from client.
63 GNUNET_PSYC_TransmitNotifyData notify_data;
66 * Closure for the notify callbacks.
68 void *notify_data_cls;
71 * Modifier of the environment that is currently being transmitted.
73 struct GNUNET_ENV_Modifier *mod;
78 const char *mod_value;
81 * Number of bytes remaining to be transmitted from the current modifier value.
83 uint32_t mod_value_remaining;
86 * State of the current message being received from client.
88 enum GNUNET_PSYC_MessageState state;
91 * Number of PSYC_TRANSMIT_ACK messages we are still waiting for.
96 * Is transmission paused?
101 * Are we currently transmitting a message?
108 struct GNUNET_PSYC_ReceiveHandle
113 GNUNET_PSYC_MessageCallback message_cb;
116 * Message part callback.
118 GNUNET_PSYC_MessagePartCallback message_part_cb;
121 * Closure for the callbacks.
126 * ID of the message being received from the PSYC service.
131 * Public key of the slave from which a message is being received.
133 struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
136 * State of the currently being received message from the PSYC service.
138 enum GNUNET_PSYC_MessageState state;
141 * Flags for the currently being received message from the PSYC service.
143 enum GNUNET_PSYC_MessageFlags flags;
146 * Expected value size for the modifier being received from the PSYC service.
148 uint32_t mod_value_size_expected;
151 * Actual value size for the modifier being received from the PSYC service.
153 uint32_t mod_value_size;
161 * Create a PSYC message.
164 * PSYC method for the message.
166 * Environment for the message.
168 * Data payload for the message.
172 * @return Message header with size information,
173 * followed by the message parts.
175 struct GNUNET_PSYC_Message *
176 GNUNET_PSYC_message_create (const char *method_name,
177 const struct GNUNET_ENV_Environment *env,
181 struct GNUNET_ENV_Modifier *mod = NULL;
182 struct GNUNET_PSYC_MessageMethod *pmeth = NULL;
183 struct GNUNET_PSYC_MessageModifier *pmod = NULL;
184 struct GNUNET_MessageHeader *pmsg = NULL;
185 uint16_t env_size = 0;
188 mod = GNUNET_ENV_environment_head (env);
191 env_size += sizeof (*pmod) + strlen (mod->name) + 1 + mod->value_size;
196 struct GNUNET_PSYC_Message *msg;
197 uint16_t method_name_size = strlen (method_name) + 1;
198 if (method_name_size == 1)
201 uint16_t msg_size = sizeof (*msg) /* header */
202 + sizeof (*pmeth) + method_name_size /* method */
203 + env_size /* modifiers */
204 + ((0 < data_size) ? sizeof (*pmsg) + data_size : 0)/* data */
205 + sizeof (*pmsg); /* end of message */
206 msg = GNUNET_malloc (msg_size);
207 msg->header.size = htons (msg_size);
208 msg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE); /* FIXME */
210 pmeth = (struct GNUNET_PSYC_MessageMethod *) &msg[1];
211 pmeth->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD);
212 pmeth->header.size = htons (sizeof (*pmeth) + method_name_size);
213 memcpy (&pmeth[1], method_name, method_name_size);
215 uint16_t p = sizeof (*msg) + sizeof (*pmeth) + method_name_size;
218 mod = GNUNET_ENV_environment_head (env);
221 uint16_t mod_name_size = strlen (mod->name) + 1;
222 pmod = (struct GNUNET_PSYC_MessageModifier *) ((char *) msg + p);
223 pmod->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER);
224 pmod->header.size = sizeof (*pmod) + mod_name_size + mod->value_size;
225 p += pmod->header.size;
226 pmod->header.size = htons (pmod->header.size);
228 memcpy (&pmod[1], mod->name, mod_name_size);
229 if (0 < mod->value_size)
230 memcpy ((char *) &pmod[1] + mod_name_size, mod->value, mod->value_size);
238 pmsg = (struct GNUNET_MessageHeader *) ((char *) msg + p);
239 pmsg->size = sizeof (*pmsg) + data_size;
241 pmsg->size = htons (pmsg->size);
242 pmsg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
243 memcpy (&pmsg[1], data, data_size);
246 pmsg = (struct GNUNET_MessageHeader *) ((char *) msg + p);
247 pmsg->size = htons (sizeof (*pmsg));
248 pmsg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END);
250 GNUNET_assert (p + sizeof (*pmsg) == msg_size);
256 GNUNET_PSYC_log_message (enum GNUNET_ErrorType kind,
257 const struct GNUNET_MessageHeader *msg)
259 uint16_t size = ntohs (msg->size);
260 uint16_t type = ntohs (msg->type);
261 GNUNET_log (kind, "Message of type %d and size %u:\n", type, size);
264 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE:
266 struct GNUNET_PSYC_MessageHeader *pmsg
267 = (struct GNUNET_PSYC_MessageHeader *) msg;
268 GNUNET_log (kind, "\tID: %" PRIu64 "\tflags: %x" PRIu32 "\n",
269 GNUNET_ntohll (pmsg->message_id), ntohl (pmsg->flags));
272 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
274 struct GNUNET_PSYC_MessageMethod *meth
275 = (struct GNUNET_PSYC_MessageMethod *) msg;
276 GNUNET_log (kind, "\t%.*s\n", size - sizeof (*meth), &meth[1]);
279 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
281 struct GNUNET_PSYC_MessageModifier *mod
282 = (struct GNUNET_PSYC_MessageModifier *) msg;
283 uint16_t name_size = ntohs (mod->name_size);
284 char oper = ' ' < mod->oper ? mod->oper : ' ';
285 GNUNET_log (kind, "\t%c%.*s\t%.*s\n", oper, name_size, &mod[1],
286 size - sizeof (*mod) - name_size,
287 ((char *) &mod[1]) + name_size);
290 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
291 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
292 GNUNET_log (kind, "\t%.*s\n", size - sizeof (*msg), &msg[1]);
298 /**** Transmitting messages ****/
302 * Create a transmission handle.
304 struct GNUNET_PSYC_TransmitHandle *
305 GNUNET_PSYC_transmit_create (struct GNUNET_CLIENT_MANAGER_Connection *client)
307 struct GNUNET_PSYC_TransmitHandle *tmit = GNUNET_malloc (sizeof (*tmit));
308 tmit->client = client;
314 * Destroy a transmission handle.
317 GNUNET_PSYC_transmit_destroy (struct GNUNET_PSYC_TransmitHandle *tmit)
324 * Queue a message part for transmission.
326 * The message part is added to the current message buffer.
327 * When this buffer is full, it is added to the transmission queue.
329 * @param tmit Transmission handle.
330 * @param msg Message part, or NULL.
331 * @param end End of message? #GNUNET_YES or #GNUNET_NO.
334 transmit_queue_insert (struct GNUNET_PSYC_TransmitHandle *tmit,
335 const struct GNUNET_MessageHeader *msg,
338 uint16_t size = (NULL != msg) ? ntohs (msg->size) : 0;
340 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "Queueing message part of type %u and size %u (end: %u)).\n",
342 ntohs (msg->type), size, end);
344 if (NULL != tmit->msg)
347 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < tmit->msg->size + size)
349 /* End of message or buffer is full, add it to transmission queue
350 * and start with empty buffer */
351 tmit->msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
352 tmit->msg->size = htons (tmit->msg->size);
353 GNUNET_CLIENT_MANAGER_transmit (tmit->client, tmit->msg);
355 tmit->acks_pending++;
359 /* Message fits in current buffer, append */
360 tmit->msg = GNUNET_realloc (tmit->msg, tmit->msg->size + size);
361 memcpy ((char *) tmit->msg + tmit->msg->size, msg, size);
362 tmit->msg->size += size;
366 if (NULL == tmit->msg && NULL != msg)
368 /* Empty buffer, copy over message. */
369 tmit->msg = GNUNET_malloc (sizeof (*tmit->msg) + size);
370 tmit->msg->size = sizeof (*tmit->msg) + size;
371 memcpy (&tmit->msg[1], msg, size);
374 if (NULL != tmit->msg
375 && (GNUNET_YES == end
376 || (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD
377 < tmit->msg->size + sizeof (struct GNUNET_MessageHeader))))
379 /* End of message or buffer is full, add it to transmission queue. */
380 tmit->msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
381 tmit->msg->size = htons (tmit->msg->size);
382 GNUNET_CLIENT_MANAGER_transmit (tmit->client, tmit->msg);
384 tmit->acks_pending++;
387 if (GNUNET_YES == end)
388 tmit->in_transmit = GNUNET_NO;
393 * Request data from client to transmit.
395 * @param tmit Transmission handle.
398 transmit_data (struct GNUNET_PSYC_TransmitHandle *tmit)
400 uint16_t data_size = GNUNET_PSYC_DATA_MAX_PAYLOAD;
401 char data[GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD] = "";
402 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) data;
403 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
405 int notify_ret = tmit->notify_data (tmit->notify_data_cls, &data_size, &msg[1]);
406 LOG (GNUNET_ERROR_TYPE_DEBUG,
407 "transmit_data (ret: %d, size: %u): %.*s\n",
408 notify_ret, data_size, data_size, &msg[1]);
414 /* Transmission paused, nothing to send. */
415 tmit->paused = GNUNET_YES;
421 tmit->state = GNUNET_PSYC_MESSAGE_STATE_END;
425 LOG (GNUNET_ERROR_TYPE_ERROR,
426 "TransmitNotifyData callback returned error when requesting data.\n");
428 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
429 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
430 msg->size = htons (sizeof (*msg));
431 transmit_queue_insert (tmit, msg, GNUNET_YES);
437 GNUNET_assert (data_size <= GNUNET_PSYC_DATA_MAX_PAYLOAD);
438 msg->size = htons (sizeof (*msg) + data_size);
439 transmit_queue_insert (tmit, msg, !notify_ret);
442 /* End of message. */
443 if (GNUNET_YES == notify_ret)
445 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END);
446 msg->size = htons (sizeof (*msg));
447 transmit_queue_insert (tmit, msg, GNUNET_YES);
453 * Request a modifier from a client to transmit.
455 * @param tmit Transmission handle.
458 transmit_mod (struct GNUNET_PSYC_TransmitHandle *tmit)
460 uint16_t max_data_size, data_size;
461 char data[GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD] = "";
462 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) data;
467 case GNUNET_PSYC_MESSAGE_STATE_MODIFIER:
469 struct GNUNET_PSYC_MessageModifier *mod
470 = (struct GNUNET_PSYC_MessageModifier *) msg;
471 max_data_size = data_size = GNUNET_PSYC_MODIFIER_MAX_PAYLOAD;
472 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER);
473 msg->size = sizeof (struct GNUNET_PSYC_MessageModifier);
474 notify_ret = tmit->notify_mod (tmit->notify_mod_cls, &data_size, &mod[1],
475 &mod->oper, &mod->value_size);
477 mod->name_size = strnlen ((char *) &mod[1], data_size) + 1;
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "transmit_mod (ret: %d, size: %u + %u): %.*s\n",
480 notify_ret, mod->name_size, mod->value_size, data_size, &mod[1]);
481 if (mod->name_size < data_size)
483 tmit->mod_value_remaining
484 = mod->value_size - (data_size - mod->name_size);
485 mod->value_size = htonl (mod->value_size);
486 mod->name_size = htons (mod->name_size);
488 else if (0 < data_size)
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got invalid modifier name.\n");
491 notify_ret = GNUNET_SYSERR;
495 case GNUNET_PSYC_MESSAGE_STATE_MOD_CONT:
497 max_data_size = data_size = GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD;
498 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT);
499 msg->size = sizeof (struct GNUNET_MessageHeader);
500 notify_ret = tmit->notify_mod (tmit->notify_mod_cls,
501 &data_size, &msg[1], NULL, NULL);
502 tmit->mod_value_remaining -= data_size;
503 LOG (GNUNET_ERROR_TYPE_DEBUG,
504 "transmit_mod (ret: %d, size: %u): %.*s\n",
505 notify_ret, data_size, data_size, &msg[1]);
516 { /* Transmission paused, nothing to send. */
517 tmit->paused = GNUNET_YES;
521 = (0 == tmit->mod_value_remaining)
522 ? GNUNET_PSYC_MESSAGE_STATE_MODIFIER
523 : GNUNET_PSYC_MESSAGE_STATE_MOD_CONT;
526 case GNUNET_YES: /* End of modifiers. */
527 GNUNET_assert (0 == tmit->mod_value_remaining);
531 LOG (GNUNET_ERROR_TYPE_ERROR,
532 "TransmitNotifyModifier callback returned with error.\n");
534 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
535 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
536 msg->size = htons (sizeof (*msg));
538 transmit_queue_insert (tmit, msg, GNUNET_YES);
544 GNUNET_assert (data_size <= max_data_size);
545 msg->size = htons (msg->size + data_size);
546 transmit_queue_insert (tmit, msg, GNUNET_NO);
549 if (GNUNET_YES == notify_ret)
551 tmit->state = GNUNET_PSYC_MESSAGE_STATE_DATA;
552 if (0 == tmit->acks_pending)
553 transmit_data (tmit);
563 transmit_notify_env (void *cls, uint16_t *data_size, void *data, uint8_t *oper,
564 uint32_t *full_value_size)
567 struct GNUNET_PSYC_TransmitHandle *tmit = cls;
568 uint16_t name_size = 0;
569 size_t value_size = 0;
570 const char *value = NULL;
574 if (NULL != tmit->mod)
575 tmit->mod = tmit->mod->next;
576 if (NULL == tmit->mod)
577 { /* No more modifiers, continue with data */
582 GNUNET_assert (tmit->mod->value_size < UINT32_MAX);
583 *full_value_size = tmit->mod->value_size;
584 *oper = tmit->mod->oper;
585 name_size = strlen (tmit->mod->name) + 1;
587 if (name_size + tmit->mod->value_size <= *data_size)
589 *data_size = name_size + tmit->mod->value_size;
593 value_size = *data_size - name_size;
594 tmit->mod_value = tmit->mod->value + value_size;
597 memcpy (data, tmit->mod->name, name_size);
598 memcpy ((char *)data + name_size, tmit->mod->value, value_size);
602 { /* Modifier continuation */
603 GNUNET_assert (NULL != tmit->mod_value && 0 < tmit->mod_value_remaining);
604 value = tmit->mod_value;
605 if (tmit->mod_value_remaining <= *data_size)
607 value_size = tmit->mod_value_remaining;
608 tmit->mod_value = NULL;
612 value_size = *data_size;
613 tmit->mod_value += value_size;
616 if (*data_size < value_size)
618 LOG (GNUNET_ERROR_TYPE_DEBUG,
619 "Value in environment larger than buffer: %u < %zu\n",
620 *data_size, value_size);
625 *data_size = value_size;
626 memcpy (data, value, value_size);
627 return (tmit->mod_value = NULL) ? GNUNET_YES : GNUNET_NO;
633 * Transmit a message.
635 * @param tmit Transmission handle.
636 * @param method_name Which method should be invoked.
637 * @param env Environment for the message.
638 * Should stay available until the first call to notify_data.
639 * Can be NULL if there are no modifiers or @a notify_mod is provided instead.
640 * @param notify_mod Function to call to obtain modifiers.
641 * Can be NULL if there are no modifiers or @a env is provided instead.
642 * @param notify_data Function to call to obtain fragments of the data.
643 * @param notify_cls Closure for @a notify_mod and @a notify_data.
644 * @param flags Flags for the message being transmitted.
646 * @return #GNUNET_OK if the transmission was started.
647 * #GNUNET_SYSERR if another transmission is already going on.
650 GNUNET_PSYC_transmit_message (struct GNUNET_PSYC_TransmitHandle *tmit,
651 const char *method_name,
652 const struct GNUNET_ENV_Environment *env,
653 GNUNET_PSYC_TransmitNotifyModifier notify_mod,
654 GNUNET_PSYC_TransmitNotifyData notify_data,
658 if (GNUNET_NO != tmit->in_transmit)
659 return GNUNET_SYSERR;
660 tmit->in_transmit = GNUNET_YES;
662 size_t size = strlen (method_name) + 1;
663 struct GNUNET_PSYC_MessageMethod *pmeth;
664 tmit->msg = GNUNET_malloc (sizeof (*tmit->msg) + sizeof (*pmeth) + size);
665 tmit->msg->size = sizeof (*tmit->msg) + sizeof (*pmeth) + size;
667 pmeth = (struct GNUNET_PSYC_MessageMethod *) &tmit->msg[1];
668 pmeth->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD);
669 pmeth->header.size = htons (sizeof (*pmeth) + size);
670 pmeth->flags = htonl (flags);
671 memcpy (&pmeth[1], method_name, size);
673 tmit->state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
674 tmit->notify_data = notify_data;
675 tmit->notify_data_cls = notify_cls;
677 if (NULL != notify_mod)
679 tmit->notify_mod = notify_mod;
680 tmit->notify_mod_cls = notify_cls;
684 tmit->notify_mod = &transmit_notify_env;
685 tmit->notify_mod_cls = tmit;
688 struct GNUNET_ENV_Modifier mod = {};
689 mod.next = GNUNET_ENV_environment_head (env);
704 * Resume transmission.
706 * @param tmit Transmission handle.
709 GNUNET_PSYC_transmit_resume (struct GNUNET_PSYC_TransmitHandle *tmit)
711 if (0 == tmit->acks_pending)
713 tmit->paused = GNUNET_NO;
714 transmit_data (tmit);
720 * Abort transmission request.
722 * @param tmit Transmission handle.
725 GNUNET_PSYC_transmit_cancel (struct GNUNET_PSYC_TransmitHandle *tmit)
727 if (GNUNET_NO == tmit->in_transmit)
730 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
731 tmit->in_transmit = GNUNET_NO;
732 tmit->paused = GNUNET_NO;
735 struct GNUNET_MessageHeader msg;
736 msg.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
737 msg.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
738 msg.size = htons (sizeof (msg));
739 transmit_queue_insert (tmit, &msg, GNUNET_YES);
744 * Got acknowledgement of a transmitted message part, continue transmission.
746 * @param tmit Transmission handle.
749 GNUNET_PSYC_transmit_got_ack (struct GNUNET_PSYC_TransmitHandle *tmit)
751 if (0 == tmit->acks_pending)
753 LOG (GNUNET_ERROR_TYPE_WARNING, "Ignoring extraneous message ACK\n");
757 tmit->acks_pending--;
761 case GNUNET_PSYC_MESSAGE_STATE_MODIFIER:
762 case GNUNET_PSYC_MESSAGE_STATE_MOD_CONT:
763 if (GNUNET_NO == tmit->paused)
767 case GNUNET_PSYC_MESSAGE_STATE_DATA:
768 if (GNUNET_NO == tmit->paused)
769 transmit_data (tmit);
772 case GNUNET_PSYC_MESSAGE_STATE_END:
773 case GNUNET_PSYC_MESSAGE_STATE_CANCEL:
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "Ignoring message ACK in state %u.\n", tmit->state);
783 /**** Receiving messages ****/
787 * Create handle for receiving messages.
789 struct GNUNET_PSYC_ReceiveHandle *
790 GNUNET_PSYC_receive_create (GNUNET_PSYC_MessageCallback message_cb,
791 GNUNET_PSYC_MessagePartCallback message_part_cb,
794 struct GNUNET_PSYC_ReceiveHandle *recv = GNUNET_malloc (sizeof (*recv));
795 recv->message_cb = message_cb;
796 recv->message_part_cb = message_part_cb;
797 recv->cb_cls = cb_cls;
803 * Destroy handle for receiving messages.
806 GNUNET_PSYC_receive_destroy (struct GNUNET_PSYC_ReceiveHandle *recv)
813 * Reset stored data related to the last received message.
816 GNUNET_PSYC_receive_reset (struct GNUNET_PSYC_ReceiveHandle *recv)
818 recv->state = GNUNET_PSYC_MESSAGE_STATE_START;
820 recv->message_id = 0;
821 recv->mod_value_size = 0;
822 recv->mod_value_size_expected = 0;
827 recv_error (struct GNUNET_PSYC_ReceiveHandle *recv)
829 if (NULL != recv->message_part_cb)
830 recv->message_part_cb (recv->cb_cls, recv->message_id, 0, recv->flags, NULL);
832 if (NULL != recv->message_cb)
833 recv->message_cb (recv->cb_cls, recv->message_id, recv->flags, NULL);
835 GNUNET_PSYC_receive_reset (recv);
840 * Handle incoming PSYC message.
842 * @param recv Receive handle.
843 * @param msg The message.
845 * @return #GNUNET_OK on success,
846 * #GNUNET_SYSERR on receive error.
849 GNUNET_PSYC_receive_message (struct GNUNET_PSYC_ReceiveHandle *recv,
850 const struct GNUNET_PSYC_MessageHeader *msg)
852 uint16_t size = ntohs (msg->header.size);
853 uint32_t flags = ntohl (msg->flags);
856 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG,
857 (struct GNUNET_MessageHeader *) msg);
859 if (GNUNET_PSYC_MESSAGE_STATE_START == recv->state)
861 recv->message_id = GNUNET_ntohll (msg->message_id);
863 recv->slave_key = msg->slave_key;
864 recv->mod_value_size = 0;
865 recv->mod_value_size_expected = 0;
867 else if (GNUNET_ntohll (msg->message_id) != recv->message_id)
870 LOG (GNUNET_ERROR_TYPE_WARNING,
871 "Unexpected message ID. Got: %" PRIu64 ", expected: %" PRIu64 "\n",
872 GNUNET_ntohll (msg->message_id), recv->message_id);
875 return GNUNET_SYSERR;
877 else if (flags != recv->flags)
879 LOG (GNUNET_ERROR_TYPE_WARNING,
880 "Unexpected message flags. Got: %lu, expected: %lu\n",
884 return GNUNET_SYSERR;
886 message_id = recv->message_id;
888 uint16_t pos = 0, psize = 0, ptype, size_eq, size_min;
890 for (pos = 0; sizeof (*msg) + pos < size; pos += psize)
892 const struct GNUNET_MessageHeader *pmsg
893 = (const struct GNUNET_MessageHeader *) ((char *) &msg[1] + pos);
894 psize = ntohs (pmsg->size);
895 ptype = ntohs (pmsg->type);
896 size_eq = size_min = 0;
898 if (psize < sizeof (*pmsg) || sizeof (*msg) + pos + psize > size)
900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
901 "Dropping message of type %u with invalid size %u.\n",
904 return GNUNET_SYSERR;
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "Received message part from PSYC.\n");
909 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
913 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
914 size_min = sizeof (struct GNUNET_PSYC_MessageMethod);
916 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
917 size_min = sizeof (struct GNUNET_PSYC_MessageModifier);
919 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
920 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
921 size_min = sizeof (struct GNUNET_MessageHeader);
923 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
924 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
925 size_eq = sizeof (struct GNUNET_MessageHeader);
930 return GNUNET_SYSERR;
933 if (! ((0 < size_eq && psize == size_eq)
934 || (0 < size_min && size_min <= psize)))
938 return GNUNET_SYSERR;
943 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
945 struct GNUNET_PSYC_MessageMethod *meth
946 = (struct GNUNET_PSYC_MessageMethod *) pmsg;
948 if (GNUNET_PSYC_MESSAGE_STATE_START != recv->state)
950 LOG (GNUNET_ERROR_TYPE_WARNING,
951 "Dropping out of order message method (%u).\n",
953 /* It is normal to receive an incomplete message right after connecting,
954 * but should not happen later.
955 * FIXME: add a check for this condition.
959 return GNUNET_SYSERR;
962 if ('\0' != *((char *) meth + psize - 1))
964 LOG (GNUNET_ERROR_TYPE_WARNING,
965 "Dropping message with malformed method. "
966 "Message ID: %" PRIu64 "\n", recv->message_id);
969 return GNUNET_SYSERR;
971 recv->state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
974 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
976 if (!(GNUNET_PSYC_MESSAGE_STATE_METHOD == recv->state
977 || GNUNET_PSYC_MESSAGE_STATE_MODIFIER == recv->state
978 || GNUNET_PSYC_MESSAGE_STATE_MOD_CONT == recv->state))
980 LOG (GNUNET_ERROR_TYPE_WARNING,
981 "Dropping out of order message modifier (%u).\n",
985 return GNUNET_SYSERR;
988 struct GNUNET_PSYC_MessageModifier *mod
989 = (struct GNUNET_PSYC_MessageModifier *) pmsg;
991 uint16_t name_size = ntohs (mod->name_size);
992 recv->mod_value_size_expected = ntohl (mod->value_size);
993 recv->mod_value_size = psize - sizeof (*mod) - name_size;
995 if (psize < sizeof (*mod) + name_size
996 || '\0' != *((char *) &mod[1] + name_size - 1)
997 || recv->mod_value_size_expected < recv->mod_value_size)
999 LOG (GNUNET_ERROR_TYPE_WARNING, "Dropping malformed modifier.\n");
1000 GNUNET_break_op (0);
1002 return GNUNET_SYSERR;
1004 recv->state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
1007 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
1009 recv->mod_value_size += psize - sizeof (*pmsg);
1011 if (!(GNUNET_PSYC_MESSAGE_STATE_MODIFIER == recv->state
1012 || GNUNET_PSYC_MESSAGE_STATE_MOD_CONT == recv->state)
1013 || recv->mod_value_size_expected < recv->mod_value_size)
1015 LOG (GNUNET_ERROR_TYPE_WARNING,
1016 "Dropping out of order message modifier continuation "
1017 "!(%u == %u || %u == %u) || %lu < %lu.\n",
1018 GNUNET_PSYC_MESSAGE_STATE_MODIFIER, recv->state,
1019 GNUNET_PSYC_MESSAGE_STATE_MOD_CONT, recv->state,
1020 recv->mod_value_size_expected, recv->mod_value_size);
1021 GNUNET_break_op (0);
1023 return GNUNET_SYSERR;
1027 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
1029 if (recv->state < GNUNET_PSYC_MESSAGE_STATE_METHOD
1030 || recv->mod_value_size_expected != recv->mod_value_size)
1032 LOG (GNUNET_ERROR_TYPE_WARNING,
1033 "Dropping out of order message data fragment "
1034 "(%u < %u || %lu != %lu).\n",
1035 recv->state, GNUNET_PSYC_MESSAGE_STATE_METHOD,
1036 recv->mod_value_size_expected, recv->mod_value_size);
1038 GNUNET_break_op (0);
1040 return GNUNET_SYSERR;
1042 recv->state = GNUNET_PSYC_MESSAGE_STATE_DATA;
1047 if (NULL != recv->message_part_cb)
1048 recv->message_part_cb (recv->cb_cls, recv->message_id, 0, // FIXME: data_offset
1053 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
1054 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
1055 GNUNET_PSYC_receive_reset (recv);
1060 if (NULL != recv->message_cb)
1061 recv->message_cb (recv->cb_cls, message_id, flags, msg);
1067 * Check if @a data contains a series of valid message parts.
1069 * @param data_size Size of @a data.
1071 * @param[out] first_ptype Type of first message part.
1072 * @param[out] last_ptype Type of last message part.
1074 * @return Number of message parts found in @a data.
1075 * or GNUNET_SYSERR if the message contains invalid parts.
1078 GNUNET_PSYC_receive_check_parts (uint16_t data_size, const char *data,
1079 uint16_t *first_ptype, uint16_t *last_ptype)
1081 const struct GNUNET_MessageHeader *pmsg;
1082 uint16_t parts = 0, ptype = 0, psize = 0, pos = 0;
1083 if (NULL != first_ptype)
1085 if (NULL != last_ptype)
1088 for (pos = 0; pos < data_size; pos += psize, parts++)
1090 pmsg = (const struct GNUNET_MessageHeader *) (data + pos);
1091 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
1092 psize = ntohs (pmsg->size);
1093 ptype = ntohs (pmsg->type);
1094 if (0 == parts && NULL != first_ptype)
1095 *first_ptype = ptype;
1096 if (NULL != last_ptype
1097 && *last_ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END)
1098 *last_ptype = ptype;
1099 if (psize < sizeof (*pmsg)
1100 || pos + psize > data_size
1101 || ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD
1102 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL < ptype)
1104 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1105 "Invalid message part of type %u and size %u.\n",
1107 return GNUNET_SYSERR;
1109 /* FIXME: check message part order */
1115 struct ParseMessageClosure
1117 struct GNUNET_ENV_Environment *env;
1118 const char **method_name;
1120 uint16_t *data_size;
1121 enum GNUNET_PSYC_MessageState msg_state;
1126 parse_message_part_cb (void *cls, uint64_t message_id, uint64_t data_offset,
1127 uint32_t flags, const struct GNUNET_MessageHeader *msg)
1129 struct ParseMessageClosure *pmc = cls;
1132 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
1136 switch (ntohs (msg->type))
1138 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
1140 struct GNUNET_PSYC_MessageMethod *
1141 pmeth = (struct GNUNET_PSYC_MessageMethod *) msg;
1142 *pmc->method_name = (const char *) &pmeth[1];
1143 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
1147 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
1149 struct GNUNET_PSYC_MessageModifier *
1150 pmod = (struct GNUNET_PSYC_MessageModifier *) msg;
1152 const char *name = (const char *) &pmod[1];
1153 const void *value = name + pmod->name_size;
1154 GNUNET_ENV_environment_add (pmc->env, pmod->oper, name, value,
1156 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
1160 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
1161 *pmc->data = &msg[1];
1162 *pmc->data_size = ntohs (msg->size) - sizeof (*msg);
1163 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_DATA;
1166 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
1167 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_END;
1171 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
1177 * Parse PSYC message.
1180 * The PSYC message to parse.
1181 * @param[out] method_name
1182 * Pointer to the method name inside @a pmsg.
1184 * The environment for the message with a list of modifiers.
1186 * Pointer to data inside @a pmsg.
1187 * @param[out] data_size
1188 * Size of @data is written here.
1190 * @return #GNUNET_OK on success,
1191 * #GNUNET_SYSERR on parse error.
1194 GNUNET_PSYC_message_parse (const struct GNUNET_PSYC_Message *msg,
1195 const char **method_name,
1196 struct GNUNET_ENV_Environment *env,
1198 uint16_t *data_size)
1200 struct ParseMessageClosure cls;
1202 cls.method_name = method_name;
1204 cls.data_size = data_size;
1206 uint16_t msg_size = ntohs (msg->header.size);
1207 struct GNUNET_PSYC_MessageHeader *
1208 pmsg = GNUNET_malloc (sizeof (*pmsg) + msg_size - sizeof (*msg));
1209 memcpy (&pmsg[1], &msg[1], msg_size - sizeof (*msg));
1211 struct GNUNET_PSYC_ReceiveHandle *
1212 recv = GNUNET_PSYC_receive_create (NULL, &parse_message_part_cb, &cls);
1213 GNUNET_PSYC_receive_message (recv, pmsg);
1214 GNUNET_PSYC_receive_destroy (recv);
1217 return (GNUNET_PSYC_MESSAGE_STATE_END == cls.msg_state)