2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file psycstore/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_psyc_util_lib.h"
32 #include "gnunet_psyc_service.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "psyc-util",__VA_ARGS__)
37 struct GNUNET_PSYC_TransmitHandle
40 * Client connection to service.
42 struct GNUNET_CLIENT_MANAGER_Connection *client;
45 * Message currently being received from the client.
47 struct GNUNET_MessageHeader *msg;
50 * Callback to request next modifier from client.
52 GNUNET_PSYC_TransmitNotifyModifier notify_mod;
55 * Closure for the notify callbacks.
60 * Callback to request next data fragment from client.
62 GNUNET_PSYC_TransmitNotifyData notify_data;
65 * Closure for the notify callbacks.
67 void *notify_data_cls;
70 * Modifier of the environment that is currently being transmitted.
72 struct GNUNET_PSYC_Modifier *mod;
77 const char *mod_value;
80 * Number of bytes remaining to be transmitted from the current modifier value.
82 uint32_t mod_value_remaining;
85 * State of the current message being received from client.
87 enum GNUNET_PSYC_MessageState state;
90 * Number of PSYC_TRANSMIT_ACK messages we are still waiting for.
95 * Is transmission paused?
100 * Are we currently transmitting a message?
105 * Notify callback is currently being called.
113 struct GNUNET_PSYC_ReceiveHandle
118 GNUNET_PSYC_MessageCallback message_cb;
121 * Message part callback.
123 GNUNET_PSYC_MessagePartCallback message_part_cb;
126 * Closure for the callbacks.
131 * ID of the message being received from the PSYC service.
136 * Public key of the slave from which a message is being received.
138 struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
141 * State of the currently being received message from the PSYC service.
143 enum GNUNET_PSYC_MessageState state;
146 * Flags for the currently being received message from the PSYC service.
148 enum GNUNET_PSYC_MessageFlags flags;
151 * Expected value size for the modifier being received from the PSYC service.
153 uint32_t mod_value_size_expected;
156 * Actual value size for the modifier being received from the PSYC service.
158 uint32_t mod_value_size;
166 * Create a PSYC message.
169 * PSYC method for the message.
171 * Environment for the message.
173 * Data payload for the message.
177 * @return Message header with size information,
178 * followed by the message parts.
180 struct GNUNET_PSYC_Message *
181 GNUNET_PSYC_message_create (const char *method_name,
182 const struct GNUNET_PSYC_Environment *env,
186 struct GNUNET_PSYC_Modifier *mod = NULL;
187 struct GNUNET_PSYC_MessageMethod *pmeth = NULL;
188 struct GNUNET_PSYC_MessageModifier *pmod = NULL;
189 struct GNUNET_MessageHeader *pmsg = NULL;
190 uint16_t env_size = 0;
193 mod = GNUNET_PSYC_env_head (env);
196 env_size += sizeof (*pmod) + strlen (mod->name) + 1 + mod->value_size;
201 struct GNUNET_PSYC_Message *msg;
202 uint16_t method_name_size = strlen (method_name) + 1;
203 if (method_name_size == 1)
206 uint16_t msg_size = sizeof (*msg) /* header */
207 + sizeof (*pmeth) + method_name_size /* method */
208 + env_size /* modifiers */
209 + ((0 < data_size) ? sizeof (*pmsg) + data_size : 0) /* data */
210 + sizeof (*pmsg); /* end of message */
211 msg = GNUNET_malloc (msg_size);
212 msg->header.size = htons (msg_size);
213 msg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE); /* FIXME */
215 pmeth = (struct GNUNET_PSYC_MessageMethod *) &msg[1];
216 pmeth->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD);
217 pmeth->header.size = htons (sizeof (*pmeth) + method_name_size);
218 memcpy (&pmeth[1], method_name, method_name_size);
220 uint16_t p = sizeof (*msg) + sizeof (*pmeth) + method_name_size;
223 mod = GNUNET_PSYC_env_head (env);
226 uint16_t mod_name_size = strlen (mod->name) + 1;
227 pmod = (struct GNUNET_PSYC_MessageModifier *) ((char *) msg + p);
228 pmod->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER);
229 pmod->header.size = sizeof (*pmod) + mod_name_size + mod->value_size;
230 p += pmod->header.size;
231 pmod->header.size = htons (pmod->header.size);
233 pmod->oper = mod->oper;
234 pmod->name_size = htons (mod_name_size);
235 pmod->value_size = htonl (mod->value_size);
237 memcpy (&pmod[1], mod->name, mod_name_size);
238 if (0 < mod->value_size)
239 memcpy ((char *) &pmod[1] + mod_name_size, mod->value, mod->value_size);
247 pmsg = (struct GNUNET_MessageHeader *) ((char *) msg + p);
248 pmsg->size = sizeof (*pmsg) + data_size;
250 pmsg->size = htons (pmsg->size);
251 pmsg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
252 memcpy (&pmsg[1], data, data_size);
255 pmsg = (struct GNUNET_MessageHeader *) ((char *) msg + p);
256 pmsg->size = htons (sizeof (*pmsg));
257 pmsg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END);
259 GNUNET_assert (p + sizeof (*pmsg) == msg_size);
265 GNUNET_PSYC_log_message (enum GNUNET_ErrorType kind,
266 const struct GNUNET_MessageHeader *msg)
268 uint16_t size = ntohs (msg->size);
269 uint16_t type = ntohs (msg->type);
270 GNUNET_log (kind, "Message of type %d and size %u:\n", type, size);
273 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE:
275 struct GNUNET_PSYC_MessageHeader *pmsg
276 = (struct GNUNET_PSYC_MessageHeader *) msg;
277 GNUNET_log (kind, "\tID: %" PRIu64 "\tflags: %x" PRIu32 "\n",
278 GNUNET_ntohll (pmsg->message_id), ntohl (pmsg->flags));
281 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
283 struct GNUNET_PSYC_MessageMethod *meth
284 = (struct GNUNET_PSYC_MessageMethod *) msg;
287 (int) (size - sizeof (*meth)),
291 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
293 struct GNUNET_PSYC_MessageModifier *mod
294 = (struct GNUNET_PSYC_MessageModifier *) msg;
295 uint16_t name_size = ntohs (mod->name_size);
296 char oper = ' ' < mod->oper ? mod->oper : ' ';
302 (int) (size - sizeof (*mod) - name_size),
303 ((char *) &mod[1]) + name_size);
306 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
307 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
308 GNUNET_log (kind, "\t%.*s\n", size - sizeof (*msg), &msg[1]);
314 /**** Transmitting messages ****/
318 * Create a transmission handle.
320 struct GNUNET_PSYC_TransmitHandle *
321 GNUNET_PSYC_transmit_create (struct GNUNET_CLIENT_MANAGER_Connection *client)
323 struct GNUNET_PSYC_TransmitHandle *tmit = GNUNET_malloc (sizeof (*tmit));
324 tmit->client = client;
330 * Destroy a transmission handle.
333 GNUNET_PSYC_transmit_destroy (struct GNUNET_PSYC_TransmitHandle *tmit)
340 * Queue a message part for transmission.
342 * The message part is added to the current message buffer.
343 * When this buffer is full, it is added to the transmission queue.
346 * Transmission handle.
348 * Message part, or NULL.
350 * Transmit message now, or wait for buffer to fill up?
351 * #GNUNET_YES or #GNUNET_NO.
354 transmit_queue_insert (struct GNUNET_PSYC_TransmitHandle *tmit,
355 const struct GNUNET_MessageHeader *msg,
358 uint16_t size = (NULL != msg) ? ntohs (msg->size) : 0;
360 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "Queueing message part of type %u and size %u (tmit_now: %u)).\n",
362 NULL != msg ? ntohs (msg->type) : 0, size, tmit_now);
364 if (NULL != tmit->msg)
367 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < tmit->msg->size + size)
369 /* End of message or buffer is full, add it to transmission queue
370 * and start with empty buffer */
371 tmit->msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
372 tmit->msg->size = htons (tmit->msg->size);
373 GNUNET_CLIENT_MANAGER_transmit (tmit->client, tmit->msg);
375 tmit->acks_pending++;
379 /* Message fits in current buffer, append */
380 tmit->msg = GNUNET_realloc (tmit->msg, tmit->msg->size + size);
381 memcpy ((char *) tmit->msg + tmit->msg->size, msg, size);
382 tmit->msg->size += size;
386 if (NULL == tmit->msg && NULL != msg)
388 /* Empty buffer, copy over message. */
389 tmit->msg = GNUNET_malloc (sizeof (*tmit->msg) + size);
390 tmit->msg->size = sizeof (*tmit->msg) + size;
391 memcpy (&tmit->msg[1], msg, size);
394 if (NULL != tmit->msg
395 && (GNUNET_YES == tmit_now
396 || (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD
397 < tmit->msg->size + sizeof (struct GNUNET_MessageHeader))))
399 /* End of message or buffer is full, add it to transmission queue. */
400 tmit->msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
401 tmit->msg->size = htons (tmit->msg->size);
402 GNUNET_CLIENT_MANAGER_transmit (tmit->client, tmit->msg);
404 tmit->acks_pending++;
410 * Request data from client to transmit.
412 * @param tmit Transmission handle.
415 transmit_data (struct GNUNET_PSYC_TransmitHandle *tmit)
417 int notify_ret = GNUNET_YES;
418 uint16_t data_size = 0;
419 char data[GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD] = "";
420 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) data;
421 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
423 if (NULL != tmit->notify_data)
425 data_size = GNUNET_PSYC_DATA_MAX_PAYLOAD;
426 tmit->in_notify = GNUNET_YES;
427 notify_ret = tmit->notify_data (tmit->notify_data_cls, &data_size, &msg[1]);
428 tmit->in_notify = GNUNET_NO;
430 LOG (GNUNET_ERROR_TYPE_DEBUG,
431 "transmit_data (ret: %d, size: %u): %.*s\n",
432 notify_ret, data_size, data_size, &msg[1]);
438 /* Transmission paused, nothing to send. */
439 tmit->paused = GNUNET_YES;
445 tmit->state = GNUNET_PSYC_MESSAGE_STATE_END;
449 LOG (GNUNET_ERROR_TYPE_ERROR,
450 "TransmitNotifyData callback returned error when requesting data.\n");
452 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
453 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
454 msg->size = htons (sizeof (*msg));
455 transmit_queue_insert (tmit, msg, GNUNET_YES);
456 tmit->in_transmit = GNUNET_NO;
462 GNUNET_assert (data_size <= GNUNET_PSYC_DATA_MAX_PAYLOAD);
463 msg->size = htons (sizeof (*msg) + data_size);
464 transmit_queue_insert (tmit, msg, !notify_ret);
467 /* End of message. */
468 if (GNUNET_YES == notify_ret)
470 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END);
471 msg->size = htons (sizeof (*msg));
472 transmit_queue_insert (tmit, msg, GNUNET_YES);
473 /* FIXME: wait for ACK before setting in_transmit to no */
474 tmit->in_transmit = GNUNET_NO;
480 * Request a modifier from a client to transmit.
482 * @param tmit Transmission handle.
485 transmit_mod (struct GNUNET_PSYC_TransmitHandle *tmit)
487 uint16_t max_data_size = 0;
488 uint16_t data_size = 0;
489 char data[GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD] = "";
490 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) data;
491 int notify_ret = GNUNET_YES;
495 case GNUNET_PSYC_MESSAGE_STATE_MODIFIER:
497 struct GNUNET_PSYC_MessageModifier *mod
498 = (struct GNUNET_PSYC_MessageModifier *) msg;
499 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER);
500 msg->size = sizeof (struct GNUNET_PSYC_MessageModifier);
502 if (NULL != tmit->notify_mod)
504 max_data_size = GNUNET_PSYC_MODIFIER_MAX_PAYLOAD;
505 data_size = max_data_size;
506 tmit->in_notify = GNUNET_YES;
507 notify_ret = tmit->notify_mod (tmit->notify_mod_cls, &data_size, &mod[1],
508 &mod->oper, &mod->value_size);
509 tmit->in_notify = GNUNET_NO;
512 mod->name_size = strnlen ((char *) &mod[1], data_size) + 1;
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "transmit_mod (ret: %d, size: %u + %u): %.*s\n",
515 notify_ret, mod->name_size, mod->value_size, data_size, &mod[1]);
516 if (mod->name_size < data_size)
518 tmit->mod_value_remaining
519 = mod->value_size - (data_size - mod->name_size);
520 mod->value_size = htonl (mod->value_size);
521 mod->name_size = htons (mod->name_size);
523 else if (0 < data_size)
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got invalid modifier name.\n");
526 notify_ret = GNUNET_SYSERR;
530 case GNUNET_PSYC_MESSAGE_STATE_MOD_CONT:
532 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT);
533 msg->size = sizeof (struct GNUNET_MessageHeader);
535 if (NULL != tmit->notify_mod)
537 max_data_size = GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD;
538 data_size = max_data_size;
539 tmit->in_notify = GNUNET_YES;
540 notify_ret = tmit->notify_mod (tmit->notify_mod_cls,
541 &data_size, &msg[1], NULL, NULL);
542 tmit->in_notify = GNUNET_NO;
544 tmit->mod_value_remaining -= data_size;
545 LOG (GNUNET_ERROR_TYPE_DEBUG,
546 "transmit_mod (ret: %d, size: %u): %.*s\n",
547 notify_ret, data_size, data_size, &msg[1]);
558 { /* Transmission paused, nothing to send. */
559 tmit->paused = GNUNET_YES;
563 = (0 == tmit->mod_value_remaining)
564 ? GNUNET_PSYC_MESSAGE_STATE_MODIFIER
565 : GNUNET_PSYC_MESSAGE_STATE_MOD_CONT;
568 case GNUNET_YES: /* End of modifiers. */
569 GNUNET_assert (0 == tmit->mod_value_remaining);
573 LOG (GNUNET_ERROR_TYPE_ERROR,
574 "TransmitNotifyModifier callback returned with error.\n");
576 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
577 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
578 msg->size = htons (sizeof (*msg));
579 transmit_queue_insert (tmit, msg, GNUNET_YES);
580 tmit->in_transmit = GNUNET_NO;
586 GNUNET_assert (data_size <= max_data_size);
587 msg->size = htons (msg->size + data_size);
588 transmit_queue_insert (tmit, msg, GNUNET_NO);
591 if (GNUNET_YES == notify_ret)
593 tmit->state = GNUNET_PSYC_MESSAGE_STATE_DATA;
594 if (0 == tmit->acks_pending)
595 transmit_data (tmit);
605 transmit_notify_env (void *cls, uint16_t *data_size, void *data, uint8_t *oper,
606 uint32_t *full_value_size)
609 struct GNUNET_PSYC_TransmitHandle *tmit = cls;
610 uint16_t name_size = 0;
611 uint32_t value_size = 0;
612 const char *value = NULL;
616 if (NULL != tmit->mod)
617 tmit->mod = tmit->mod->next;
618 if (NULL == tmit->mod)
619 { /* No more modifiers, continue with data */
624 GNUNET_assert (tmit->mod->value_size < UINT32_MAX);
625 *full_value_size = tmit->mod->value_size;
626 *oper = tmit->mod->oper;
627 name_size = strlen (tmit->mod->name) + 1;
629 if (name_size + tmit->mod->value_size <= *data_size)
631 value_size = tmit->mod->value_size;
632 *data_size = name_size + value_size;
634 else /* full modifier does not fit in data, continuation needed */
636 value_size = *data_size - name_size;
637 tmit->mod_value = tmit->mod->value + value_size;
640 memcpy (data, tmit->mod->name, name_size);
641 memcpy ((char *)data + name_size, tmit->mod->value, value_size);
645 { /* Modifier continuation */
646 GNUNET_assert (NULL != tmit->mod_value && 0 < tmit->mod_value_remaining);
647 value = tmit->mod_value;
648 if (tmit->mod_value_remaining <= *data_size)
650 value_size = tmit->mod_value_remaining;
651 tmit->mod_value = NULL;
655 value_size = *data_size;
656 tmit->mod_value += value_size;
659 if (*data_size < value_size)
661 LOG (GNUNET_ERROR_TYPE_DEBUG,
662 "Value in environment larger than buffer: %u < %zu\n",
663 *data_size, value_size);
668 *data_size = value_size;
669 memcpy (data, value, value_size);
670 return (NULL == tmit->mod_value) ? GNUNET_YES : GNUNET_NO;
676 * Transmit a message.
679 * Transmission handle.
681 * Which method should be invoked.
683 * Environment for the message.
684 * Should stay available until the first call to notify_data.
685 * Can be NULL if there are no modifiers or @a notify_mod is
688 * Function to call to obtain modifiers.
689 * Can be NULL if there are no modifiers or @a env is provided instead.
691 * Function to call to obtain fragments of the data.
693 * Closure for @a notify_mod and @a notify_data.
695 * Flags for the message being transmitted.
697 * @return #GNUNET_OK if the transmission was started.
698 * #GNUNET_SYSERR if another transmission is already going on.
701 GNUNET_PSYC_transmit_message (struct GNUNET_PSYC_TransmitHandle *tmit,
702 const char *method_name,
703 const struct GNUNET_PSYC_Environment *env,
704 GNUNET_PSYC_TransmitNotifyModifier notify_mod,
705 GNUNET_PSYC_TransmitNotifyData notify_data,
709 if (GNUNET_NO != tmit->in_transmit)
710 return GNUNET_SYSERR;
711 tmit->in_transmit = GNUNET_YES;
713 size_t size = strlen (method_name) + 1;
714 struct GNUNET_PSYC_MessageMethod *pmeth;
715 tmit->msg = GNUNET_malloc (sizeof (*tmit->msg) + sizeof (*pmeth) + size);
716 tmit->msg->size = sizeof (*tmit->msg) + sizeof (*pmeth) + size;
718 if (NULL != notify_mod)
720 tmit->notify_mod = notify_mod;
721 tmit->notify_mod_cls = notify_cls;
725 tmit->notify_mod = &transmit_notify_env;
726 tmit->notify_mod_cls = tmit;
729 struct GNUNET_PSYC_Modifier mod = {};
730 mod.next = GNUNET_PSYC_env_head (env);
733 struct GNUNET_PSYC_Modifier *m = tmit->mod;
734 while (NULL != (m = m->next))
736 if (m->oper != GNUNET_PSYC_OP_SET)
737 flags |= GNUNET_PSYC_MASTER_TRANSMIT_STATE_MODIFY;
746 pmeth = (struct GNUNET_PSYC_MessageMethod *) &tmit->msg[1];
747 pmeth->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD);
748 pmeth->header.size = htons (sizeof (*pmeth) + size);
749 pmeth->flags = htonl (flags);
750 memcpy (&pmeth[1], method_name, size);
752 tmit->state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
753 tmit->notify_data = notify_data;
754 tmit->notify_data_cls = notify_cls;
762 * Resume transmission.
764 * @param tmit Transmission handle.
767 GNUNET_PSYC_transmit_resume (struct GNUNET_PSYC_TransmitHandle *tmit)
769 if (GNUNET_YES != tmit->in_transmit || GNUNET_NO != tmit->in_notify)
772 if (0 == tmit->acks_pending)
774 tmit->paused = GNUNET_NO;
775 transmit_data (tmit);
781 * Abort transmission request.
783 * @param tmit Transmission handle.
786 GNUNET_PSYC_transmit_cancel (struct GNUNET_PSYC_TransmitHandle *tmit)
788 if (GNUNET_NO == tmit->in_transmit)
791 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
792 tmit->in_transmit = GNUNET_NO;
793 tmit->paused = GNUNET_NO;
796 struct GNUNET_MessageHeader msg;
797 msg.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
798 msg.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
799 msg.size = htons (sizeof (msg));
800 transmit_queue_insert (tmit, &msg, GNUNET_YES);
805 * Got acknowledgement of a transmitted message part, continue transmission.
807 * @param tmit Transmission handle.
810 GNUNET_PSYC_transmit_got_ack (struct GNUNET_PSYC_TransmitHandle *tmit)
812 if (0 == tmit->acks_pending)
814 LOG (GNUNET_ERROR_TYPE_WARNING, "Ignoring extraneous message ACK\n");
818 tmit->acks_pending--;
822 case GNUNET_PSYC_MESSAGE_STATE_MODIFIER:
823 case GNUNET_PSYC_MESSAGE_STATE_MOD_CONT:
827 case GNUNET_PSYC_MESSAGE_STATE_DATA:
828 transmit_data (tmit);
831 case GNUNET_PSYC_MESSAGE_STATE_END:
832 case GNUNET_PSYC_MESSAGE_STATE_CANCEL:
836 LOG (GNUNET_ERROR_TYPE_DEBUG,
837 "Ignoring message ACK in state %u.\n", tmit->state);
842 /**** Receiving messages ****/
846 * Create handle for receiving messages.
848 struct GNUNET_PSYC_ReceiveHandle *
849 GNUNET_PSYC_receive_create (GNUNET_PSYC_MessageCallback message_cb,
850 GNUNET_PSYC_MessagePartCallback message_part_cb,
853 struct GNUNET_PSYC_ReceiveHandle *recv = GNUNET_malloc (sizeof (*recv));
854 recv->message_cb = message_cb;
855 recv->message_part_cb = message_part_cb;
856 recv->cb_cls = cb_cls;
862 * Destroy handle for receiving messages.
865 GNUNET_PSYC_receive_destroy (struct GNUNET_PSYC_ReceiveHandle *recv)
872 * Reset stored data related to the last received message.
875 GNUNET_PSYC_receive_reset (struct GNUNET_PSYC_ReceiveHandle *recv)
877 recv->state = GNUNET_PSYC_MESSAGE_STATE_START;
879 recv->message_id = 0;
880 recv->mod_value_size = 0;
881 recv->mod_value_size_expected = 0;
886 recv_error (struct GNUNET_PSYC_ReceiveHandle *recv)
888 if (NULL != recv->message_part_cb)
889 recv->message_part_cb (recv->cb_cls, NULL, NULL);
891 if (NULL != recv->message_cb)
892 recv->message_cb (recv->cb_cls, NULL);
894 GNUNET_PSYC_receive_reset (recv);
899 * Handle incoming PSYC message.
901 * @param recv Receive handle.
902 * @param msg The message.
904 * @return #GNUNET_OK on success,
905 * #GNUNET_SYSERR on receive error.
908 GNUNET_PSYC_receive_message (struct GNUNET_PSYC_ReceiveHandle *recv,
909 const struct GNUNET_PSYC_MessageHeader *msg)
911 uint16_t size = ntohs (msg->header.size);
912 uint32_t flags = ntohl (msg->flags);
914 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG,
915 (struct GNUNET_MessageHeader *) msg);
917 if (GNUNET_PSYC_MESSAGE_STATE_START == recv->state)
919 recv->message_id = GNUNET_ntohll (msg->message_id);
921 recv->slave_pub_key = msg->slave_pub_key;
922 recv->mod_value_size = 0;
923 recv->mod_value_size_expected = 0;
925 else if (GNUNET_ntohll (msg->message_id) != recv->message_id)
928 LOG (GNUNET_ERROR_TYPE_WARNING,
929 "Unexpected message ID. Got: %" PRIu64 ", expected: %" PRIu64 "\n",
930 GNUNET_ntohll (msg->message_id), recv->message_id);
933 return GNUNET_SYSERR;
935 else if (flags != recv->flags)
937 LOG (GNUNET_ERROR_TYPE_WARNING,
938 "Unexpected message flags. Got: %lu, expected: %lu\n",
942 return GNUNET_SYSERR;
945 uint16_t pos = 0, psize = 0, ptype, size_eq, size_min;
947 for (pos = 0; sizeof (*msg) + pos < size; pos += psize)
949 const struct GNUNET_MessageHeader *pmsg
950 = (const struct GNUNET_MessageHeader *) ((char *) &msg[1] + pos);
951 psize = ntohs (pmsg->size);
952 ptype = ntohs (pmsg->type);
953 size_eq = size_min = 0;
955 if (psize < sizeof (*pmsg) || sizeof (*msg) + pos + psize > size)
957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
958 "Dropping message of type %u with invalid size %u.\n",
961 return GNUNET_SYSERR;
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "Received message part of type %u and size %u from PSYC.\n",
967 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
971 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
972 size_min = sizeof (struct GNUNET_PSYC_MessageMethod);
974 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
975 size_min = sizeof (struct GNUNET_PSYC_MessageModifier);
977 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
978 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
979 size_min = sizeof (struct GNUNET_MessageHeader);
981 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
982 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
983 size_eq = sizeof (struct GNUNET_MessageHeader);
988 return GNUNET_SYSERR;
991 if (! ((0 < size_eq && psize == size_eq)
992 || (0 < size_min && size_min <= psize)))
996 return GNUNET_SYSERR;
1001 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
1003 struct GNUNET_PSYC_MessageMethod *meth
1004 = (struct GNUNET_PSYC_MessageMethod *) pmsg;
1006 if (GNUNET_PSYC_MESSAGE_STATE_START != recv->state)
1008 LOG (GNUNET_ERROR_TYPE_WARNING,
1009 "Dropping out of order message method (%u).\n",
1011 /* It is normal to receive an incomplete message right after connecting,
1012 * but should not happen later.
1013 * FIXME: add a check for this condition.
1015 GNUNET_break_op (0);
1017 return GNUNET_SYSERR;
1020 if ('\0' != *((char *) meth + psize - 1))
1022 LOG (GNUNET_ERROR_TYPE_WARNING,
1023 "Dropping message with malformed method. "
1024 "Message ID: %" PRIu64 "\n", recv->message_id);
1025 GNUNET_break_op (0);
1027 return GNUNET_SYSERR;
1029 recv->state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
1032 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
1034 if (!(GNUNET_PSYC_MESSAGE_STATE_METHOD == recv->state
1035 || GNUNET_PSYC_MESSAGE_STATE_MODIFIER == recv->state
1036 || GNUNET_PSYC_MESSAGE_STATE_MOD_CONT == recv->state))
1038 LOG (GNUNET_ERROR_TYPE_WARNING,
1039 "Dropping out of order message modifier (%u).\n",
1041 GNUNET_break_op (0);
1043 return GNUNET_SYSERR;
1046 struct GNUNET_PSYC_MessageModifier *mod
1047 = (struct GNUNET_PSYC_MessageModifier *) pmsg;
1049 uint16_t name_size = ntohs (mod->name_size);
1050 recv->mod_value_size_expected = ntohl (mod->value_size);
1051 recv->mod_value_size = psize - sizeof (*mod) - name_size;
1053 if (psize < sizeof (*mod) + name_size
1054 || '\0' != *((char *) &mod[1] + name_size - 1)
1055 || recv->mod_value_size_expected < recv->mod_value_size)
1057 LOG (GNUNET_ERROR_TYPE_WARNING, "Dropping malformed modifier.\n");
1058 GNUNET_break_op (0);
1060 return GNUNET_SYSERR;
1062 recv->state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
1065 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
1067 recv->mod_value_size += psize - sizeof (*pmsg);
1069 if (!(GNUNET_PSYC_MESSAGE_STATE_MODIFIER == recv->state
1070 || GNUNET_PSYC_MESSAGE_STATE_MOD_CONT == recv->state)
1071 || recv->mod_value_size_expected < recv->mod_value_size)
1073 LOG (GNUNET_ERROR_TYPE_WARNING,
1074 "Dropping out of order message modifier continuation "
1075 "!(%u == %u || %u == %u) || %lu < %lu.\n",
1076 GNUNET_PSYC_MESSAGE_STATE_MODIFIER, recv->state,
1077 GNUNET_PSYC_MESSAGE_STATE_MOD_CONT, recv->state,
1078 recv->mod_value_size_expected, recv->mod_value_size);
1079 GNUNET_break_op (0);
1081 return GNUNET_SYSERR;
1085 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
1087 if (recv->state < GNUNET_PSYC_MESSAGE_STATE_METHOD
1088 || recv->mod_value_size_expected != recv->mod_value_size)
1090 LOG (GNUNET_ERROR_TYPE_WARNING,
1091 "Dropping out of order message data fragment "
1092 "(%u < %u || %lu != %lu).\n",
1093 recv->state, GNUNET_PSYC_MESSAGE_STATE_METHOD,
1094 recv->mod_value_size_expected, recv->mod_value_size);
1096 GNUNET_break_op (0);
1098 return GNUNET_SYSERR;
1100 recv->state = GNUNET_PSYC_MESSAGE_STATE_DATA;
1105 if (NULL != recv->message_part_cb)
1106 recv->message_part_cb (recv->cb_cls, msg, pmsg);
1110 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
1111 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
1112 GNUNET_PSYC_receive_reset (recv);
1117 if (NULL != recv->message_cb)
1118 recv->message_cb (recv->cb_cls, msg);
1124 * Check if @a data contains a series of valid message parts.
1126 * @param data_size Size of @a data.
1128 * @param[out] first_ptype Type of first message part.
1129 * @param[out] last_ptype Type of last message part.
1131 * @return Number of message parts found in @a data.
1132 * or GNUNET_SYSERR if the message contains invalid parts.
1135 GNUNET_PSYC_receive_check_parts (uint16_t data_size, const char *data,
1136 uint16_t *first_ptype, uint16_t *last_ptype)
1138 const struct GNUNET_MessageHeader *pmsg;
1139 uint16_t parts = 0, ptype = 0, psize = 0, pos = 0;
1140 if (NULL != first_ptype)
1142 if (NULL != last_ptype)
1145 for (pos = 0; pos < data_size; pos += psize, parts++)
1147 pmsg = (const struct GNUNET_MessageHeader *) (data + pos);
1148 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
1149 psize = ntohs (pmsg->size);
1150 ptype = ntohs (pmsg->type);
1151 if (0 == parts && NULL != first_ptype)
1152 *first_ptype = ptype;
1153 if (NULL != last_ptype
1154 && *last_ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END)
1155 *last_ptype = ptype;
1156 if (psize < sizeof (*pmsg)
1157 || pos + psize > data_size
1158 || ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD
1159 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL < ptype)
1161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1162 "Invalid message part of type %u and size %u.\n",
1164 return GNUNET_SYSERR;
1166 /** @todo FIXME: check message part order */
1172 struct ParseMessageClosure
1174 struct GNUNET_PSYC_Environment *env;
1175 const char **method_name;
1177 uint16_t *data_size;
1178 enum GNUNET_PSYC_MessageState msg_state;
1183 parse_message_part_cb (void *cls,
1184 const struct GNUNET_PSYC_MessageHeader *msg,
1185 const struct GNUNET_MessageHeader *pmsg)
1187 struct ParseMessageClosure *pmc = cls;
1190 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
1194 switch (ntohs (pmsg->type))
1196 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
1198 struct GNUNET_PSYC_MessageMethod *
1199 pmeth = (struct GNUNET_PSYC_MessageMethod *) pmsg;
1200 *pmc->method_name = (const char *) &pmeth[1];
1201 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
1205 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
1207 struct GNUNET_PSYC_MessageModifier *
1208 pmod = (struct GNUNET_PSYC_MessageModifier *) pmsg;
1210 const char *name = (const char *) &pmod[1];
1211 const void *value = name + ntohs (pmod->name_size);
1212 GNUNET_PSYC_env_add (pmc->env, pmod->oper, name, value,
1213 ntohl (pmod->value_size));
1214 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
1218 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
1219 *pmc->data = &pmsg[1];
1220 *pmc->data_size = ntohs (pmsg->size) - sizeof (*pmsg);
1221 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_DATA;
1224 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
1225 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_END;
1229 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
1235 * Parse PSYC message.
1238 * The PSYC message to parse.
1239 * @param[out] method_name
1240 * Pointer to the method name inside @a pmsg.
1242 * The environment for the message with a list of modifiers.
1244 * Pointer to data inside @a msg.
1245 * @param[out] data_size
1246 * Size of @data is written here.
1248 * @return #GNUNET_OK on success,
1249 * #GNUNET_SYSERR on parse error.
1252 GNUNET_PSYC_message_parse (const struct GNUNET_PSYC_MessageHeader *msg,
1253 const char **method_name,
1254 struct GNUNET_PSYC_Environment *env,
1256 uint16_t *data_size)
1258 struct ParseMessageClosure cls;
1260 cls.method_name = method_name;
1262 cls.data_size = data_size;
1264 struct GNUNET_PSYC_ReceiveHandle *
1265 recv = GNUNET_PSYC_receive_create (NULL, parse_message_part_cb, &cls);
1266 int ret = GNUNET_PSYC_receive_message (recv, msg);
1267 GNUNET_PSYC_receive_destroy (recv);
1269 if (GNUNET_OK != ret)
1270 return GNUNET_SYSERR;
1272 return (GNUNET_PSYC_MESSAGE_STATE_END == cls.msg_state)
1279 * Initialize PSYC message header.
1282 GNUNET_PSYC_message_header_init (struct GNUNET_PSYC_MessageHeader *pmsg,
1283 const struct GNUNET_MULTICAST_MessageHeader *mmsg,
1286 uint16_t size = ntohs (mmsg->header.size);
1287 uint16_t psize = sizeof (*pmsg) + size - sizeof (*mmsg);
1289 pmsg->header.size = htons (psize);
1290 pmsg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
1291 pmsg->message_id = mmsg->message_id;
1292 pmsg->fragment_offset = mmsg->fragment_offset;
1293 pmsg->flags = htonl (flags);
1295 memcpy (&pmsg[1], &mmsg[1], size - sizeof (*mmsg));
1300 * Create a new PSYC message header from a multicast message.
1302 struct GNUNET_PSYC_MessageHeader *
1303 GNUNET_PSYC_message_header_create (const struct GNUNET_MULTICAST_MessageHeader *mmsg,
1306 struct GNUNET_PSYC_MessageHeader *pmsg;
1307 uint16_t size = ntohs (mmsg->header.size);
1308 uint16_t psize = sizeof (*pmsg) + size - sizeof (*mmsg);
1310 pmsg = GNUNET_malloc (psize);
1311 GNUNET_PSYC_message_header_init (pmsg, mmsg, flags);
1317 * Create a new PSYC message header from a PSYC message.
1319 struct GNUNET_PSYC_MessageHeader *
1320 GNUNET_PSYC_message_header_create_from_psyc (const struct GNUNET_PSYC_Message *msg)
1322 uint16_t msg_size = ntohs (msg->header.size);
1323 struct GNUNET_PSYC_MessageHeader *
1324 pmsg = GNUNET_malloc (sizeof (*pmsg) + msg_size - sizeof (*msg));
1325 pmsg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
1326 pmsg->header.size = htons (sizeof (*pmsg) + msg_size - sizeof (*msg));
1327 memcpy (&pmsg[1], &msg[1], msg_size - sizeof (*msg));