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);
272 "Message of type %d and size %u:\n",
277 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE:
279 const struct GNUNET_PSYC_MessageHeader *pmsg
280 = (const struct GNUNET_PSYC_MessageHeader *) msg;
282 "\tID: %" PRIu64 "\tflags: %x" PRIu32 "\n",
283 GNUNET_ntohll (pmsg->message_id),
284 ntohl (pmsg->flags));
287 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
289 const struct GNUNET_PSYC_MessageMethod *meth
290 = (const struct GNUNET_PSYC_MessageMethod *) msg;
293 (int) (size - sizeof (*meth)),
294 (const char *) &meth[1]);
297 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
299 const struct GNUNET_PSYC_MessageModifier *mod
300 = (const struct GNUNET_PSYC_MessageModifier *) msg;
301 uint16_t name_size = ntohs (mod->name_size);
302 char oper = ' ' < mod->oper ? mod->oper : ' ';
307 (const char *) &mod[1],
308 (int) (size - sizeof (*mod) - name_size),
309 ((const char *) &mod[1]) + name_size);
312 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
313 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
316 (int) (size - sizeof (*msg)),
317 (const char *) &msg[1]);
323 /**** Transmitting messages ****/
327 * Create a transmission handle.
329 struct GNUNET_PSYC_TransmitHandle *
330 GNUNET_PSYC_transmit_create (struct GNUNET_CLIENT_MANAGER_Connection *client)
332 struct GNUNET_PSYC_TransmitHandle *tmit = GNUNET_new (struct GNUNET_PSYC_TransmitHandle);
334 tmit->client = client;
340 * Destroy a transmission handle.
343 GNUNET_PSYC_transmit_destroy (struct GNUNET_PSYC_TransmitHandle *tmit)
350 * Queue a message part for transmission.
352 * The message part is added to the current message buffer.
353 * When this buffer is full, it is added to the transmission queue.
356 * Transmission handle.
358 * Message part, or NULL.
360 * Transmit message now, or wait for buffer to fill up?
361 * #GNUNET_YES or #GNUNET_NO.
364 transmit_queue_insert (struct GNUNET_PSYC_TransmitHandle *tmit,
365 const struct GNUNET_MessageHeader *msg,
368 uint16_t size = (NULL != msg) ? ntohs (msg->size) : 0;
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "Queueing message part of type %u and size %u (tmit_now: %u)).\n",
372 NULL != msg ? ntohs (msg->type) : 0, size, tmit_now);
374 if (NULL != tmit->msg)
377 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < tmit->msg->size + size)
379 /* End of message or buffer is full, add it to transmission queue
380 * and start with empty buffer */
381 tmit->msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
382 tmit->msg->size = htons (tmit->msg->size);
383 GNUNET_CLIENT_MANAGER_transmit (tmit->client, tmit->msg);
385 tmit->acks_pending++;
389 /* Message fits in current buffer, append */
390 tmit->msg = GNUNET_realloc (tmit->msg, tmit->msg->size + size);
391 memcpy ((char *) tmit->msg + tmit->msg->size, msg, size);
392 tmit->msg->size += size;
396 if (NULL == tmit->msg && NULL != msg)
398 /* Empty buffer, copy over message. */
399 tmit->msg = GNUNET_malloc (sizeof (*tmit->msg) + size);
400 tmit->msg->size = sizeof (*tmit->msg) + size;
401 memcpy (&tmit->msg[1], msg, size);
404 if (NULL != tmit->msg
405 && (GNUNET_YES == tmit_now
406 || (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD
407 < tmit->msg->size + sizeof (struct GNUNET_MessageHeader))))
409 /* End of message or buffer is full, add it to transmission queue. */
410 tmit->msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
411 tmit->msg->size = htons (tmit->msg->size);
412 GNUNET_CLIENT_MANAGER_transmit (tmit->client, tmit->msg);
414 tmit->acks_pending++;
420 * Request data from client to transmit.
422 * @param tmit Transmission handle.
425 transmit_data (struct GNUNET_PSYC_TransmitHandle *tmit)
427 int notify_ret = GNUNET_YES;
428 uint16_t data_size = 0;
429 char data[GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD] = "";
430 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) data;
431 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
433 if (NULL != tmit->notify_data)
435 data_size = GNUNET_PSYC_DATA_MAX_PAYLOAD;
436 tmit->in_notify = GNUNET_YES;
437 notify_ret = tmit->notify_data (tmit->notify_data_cls, &data_size, &msg[1]);
438 tmit->in_notify = GNUNET_NO;
440 LOG (GNUNET_ERROR_TYPE_DEBUG,
441 "transmit_data (ret: %d, size: %u): %.*s\n",
442 notify_ret, data_size, data_size, &msg[1]);
448 /* Transmission paused, nothing to send. */
449 tmit->paused = GNUNET_YES;
455 tmit->state = GNUNET_PSYC_MESSAGE_STATE_END;
459 LOG (GNUNET_ERROR_TYPE_ERROR,
460 "TransmitNotifyData callback returned error when requesting data.\n");
462 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
463 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
464 msg->size = htons (sizeof (*msg));
465 transmit_queue_insert (tmit, msg, GNUNET_YES);
466 tmit->in_transmit = GNUNET_NO;
472 GNUNET_assert (data_size <= GNUNET_PSYC_DATA_MAX_PAYLOAD);
473 msg->size = htons (sizeof (*msg) + data_size);
474 transmit_queue_insert (tmit, msg, !notify_ret);
477 /* End of message. */
478 if (GNUNET_YES == notify_ret)
480 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END);
481 msg->size = htons (sizeof (*msg));
482 transmit_queue_insert (tmit, msg, GNUNET_YES);
483 /* FIXME: wait for ACK before setting in_transmit to no */
484 tmit->in_transmit = GNUNET_NO;
490 * Request a modifier from a client to transmit.
492 * @param tmit Transmission handle.
495 transmit_mod (struct GNUNET_PSYC_TransmitHandle *tmit)
497 uint16_t max_data_size = 0;
498 uint16_t data_size = 0;
499 char data[GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD] = "";
500 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) data;
501 int notify_ret = GNUNET_YES;
505 case GNUNET_PSYC_MESSAGE_STATE_MODIFIER:
507 struct GNUNET_PSYC_MessageModifier *mod
508 = (struct GNUNET_PSYC_MessageModifier *) msg;
509 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER);
510 msg->size = sizeof (struct GNUNET_PSYC_MessageModifier);
512 if (NULL != tmit->notify_mod)
514 max_data_size = GNUNET_PSYC_MODIFIER_MAX_PAYLOAD;
515 data_size = max_data_size;
516 tmit->in_notify = GNUNET_YES;
517 notify_ret = tmit->notify_mod (tmit->notify_mod_cls, &data_size, &mod[1],
518 &mod->oper, &mod->value_size);
519 tmit->in_notify = GNUNET_NO;
522 mod->name_size = strnlen ((char *) &mod[1], data_size) + 1;
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 "transmit_mod (ret: %d, size: %u + %u): %.*s\n",
525 notify_ret, mod->name_size, mod->value_size, data_size, &mod[1]);
526 if (mod->name_size < data_size)
528 tmit->mod_value_remaining
529 = mod->value_size - (data_size - mod->name_size);
530 mod->value_size = htonl (mod->value_size);
531 mod->name_size = htons (mod->name_size);
533 else if (0 < data_size)
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got invalid modifier name.\n");
536 notify_ret = GNUNET_SYSERR;
540 case GNUNET_PSYC_MESSAGE_STATE_MOD_CONT:
542 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT);
543 msg->size = sizeof (struct GNUNET_MessageHeader);
545 if (NULL != tmit->notify_mod)
547 max_data_size = GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD;
548 data_size = max_data_size;
549 tmit->in_notify = GNUNET_YES;
550 notify_ret = tmit->notify_mod (tmit->notify_mod_cls,
551 &data_size, &msg[1], NULL, NULL);
552 tmit->in_notify = GNUNET_NO;
554 tmit->mod_value_remaining -= data_size;
555 LOG (GNUNET_ERROR_TYPE_DEBUG,
556 "transmit_mod (ret: %d, size: %u): %.*s\n",
557 notify_ret, data_size, data_size, &msg[1]);
568 { /* Transmission paused, nothing to send. */
569 tmit->paused = GNUNET_YES;
573 = (0 == tmit->mod_value_remaining)
574 ? GNUNET_PSYC_MESSAGE_STATE_MODIFIER
575 : GNUNET_PSYC_MESSAGE_STATE_MOD_CONT;
578 case GNUNET_YES: /* End of modifiers. */
579 GNUNET_assert (0 == tmit->mod_value_remaining);
583 LOG (GNUNET_ERROR_TYPE_ERROR,
584 "TransmitNotifyModifier callback returned with error.\n");
586 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
587 msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
588 msg->size = htons (sizeof (*msg));
589 transmit_queue_insert (tmit, msg, GNUNET_YES);
590 tmit->in_transmit = GNUNET_NO;
596 GNUNET_assert (data_size <= max_data_size);
597 msg->size = htons (msg->size + data_size);
598 transmit_queue_insert (tmit, msg, GNUNET_NO);
601 if (GNUNET_YES == notify_ret)
603 tmit->state = GNUNET_PSYC_MESSAGE_STATE_DATA;
604 if (0 == tmit->acks_pending)
605 transmit_data (tmit);
615 transmit_notify_env (void *cls, uint16_t *data_size, void *data, uint8_t *oper,
616 uint32_t *full_value_size)
619 struct GNUNET_PSYC_TransmitHandle *tmit = cls;
620 uint16_t name_size = 0;
621 uint32_t value_size = 0;
622 const char *value = NULL;
626 if (NULL != tmit->mod)
627 tmit->mod = tmit->mod->next;
628 if (NULL == tmit->mod)
629 { /* No more modifiers, continue with data */
634 GNUNET_assert (tmit->mod->value_size < UINT32_MAX);
635 *full_value_size = tmit->mod->value_size;
636 *oper = tmit->mod->oper;
637 name_size = strlen (tmit->mod->name) + 1;
639 if (name_size + tmit->mod->value_size <= *data_size)
641 value_size = tmit->mod->value_size;
642 *data_size = name_size + value_size;
644 else /* full modifier does not fit in data, continuation needed */
646 value_size = *data_size - name_size;
647 tmit->mod_value = tmit->mod->value + value_size;
650 memcpy (data, tmit->mod->name, name_size);
651 memcpy ((char *)data + name_size, tmit->mod->value, value_size);
655 { /* Modifier continuation */
656 GNUNET_assert (NULL != tmit->mod_value && 0 < tmit->mod_value_remaining);
657 value = tmit->mod_value;
658 if (tmit->mod_value_remaining <= *data_size)
660 value_size = tmit->mod_value_remaining;
661 tmit->mod_value = NULL;
665 value_size = *data_size;
666 tmit->mod_value += value_size;
669 if (*data_size < value_size)
671 LOG (GNUNET_ERROR_TYPE_DEBUG,
672 "Value in environment larger than buffer: %u < %zu\n",
673 *data_size, value_size);
678 *data_size = value_size;
679 memcpy (data, value, value_size);
680 return (NULL == tmit->mod_value) ? GNUNET_YES : GNUNET_NO;
686 * Transmit a message.
689 * Transmission handle.
691 * Which method should be invoked.
693 * Environment for the message.
694 * Should stay available until the first call to notify_data.
695 * Can be NULL if there are no modifiers or @a notify_mod is
698 * Function to call to obtain modifiers.
699 * Can be NULL if there are no modifiers or @a env is provided instead.
701 * Function to call to obtain fragments of the data.
703 * Closure for @a notify_mod and @a notify_data.
705 * Flags for the message being transmitted.
707 * @return #GNUNET_OK if the transmission was started.
708 * #GNUNET_SYSERR if another transmission is already going on.
711 GNUNET_PSYC_transmit_message (struct GNUNET_PSYC_TransmitHandle *tmit,
712 const char *method_name,
713 const struct GNUNET_PSYC_Environment *env,
714 GNUNET_PSYC_TransmitNotifyModifier notify_mod,
715 GNUNET_PSYC_TransmitNotifyData notify_data,
719 if (GNUNET_NO != tmit->in_transmit)
720 return GNUNET_SYSERR;
721 tmit->in_transmit = GNUNET_YES;
723 size_t size = strlen (method_name) + 1;
724 struct GNUNET_PSYC_MessageMethod *pmeth;
725 tmit->msg = GNUNET_malloc (sizeof (*tmit->msg) + sizeof (*pmeth) + size);
726 tmit->msg->size = sizeof (*tmit->msg) + sizeof (*pmeth) + size;
728 if (NULL != notify_mod)
730 tmit->notify_mod = notify_mod;
731 tmit->notify_mod_cls = notify_cls;
735 tmit->notify_mod = &transmit_notify_env;
736 tmit->notify_mod_cls = tmit;
739 struct GNUNET_PSYC_Modifier mod = {};
740 mod.next = GNUNET_PSYC_env_head (env);
743 struct GNUNET_PSYC_Modifier *m = tmit->mod;
744 while (NULL != (m = m->next))
746 if (m->oper != GNUNET_PSYC_OP_SET)
747 flags |= GNUNET_PSYC_MASTER_TRANSMIT_STATE_MODIFY;
756 pmeth = (struct GNUNET_PSYC_MessageMethod *) &tmit->msg[1];
757 pmeth->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD);
758 pmeth->header.size = htons (sizeof (*pmeth) + size);
759 pmeth->flags = htonl (flags);
760 memcpy (&pmeth[1], method_name, size);
762 tmit->state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
763 tmit->notify_data = notify_data;
764 tmit->notify_data_cls = notify_cls;
772 * Resume transmission.
774 * @param tmit Transmission handle.
777 GNUNET_PSYC_transmit_resume (struct GNUNET_PSYC_TransmitHandle *tmit)
779 if (GNUNET_YES != tmit->in_transmit || GNUNET_NO != tmit->in_notify)
782 if (0 == tmit->acks_pending)
784 tmit->paused = GNUNET_NO;
785 transmit_data (tmit);
791 * Abort transmission request.
793 * @param tmit Transmission handle.
796 GNUNET_PSYC_transmit_cancel (struct GNUNET_PSYC_TransmitHandle *tmit)
798 if (GNUNET_NO == tmit->in_transmit)
801 tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
802 tmit->in_transmit = GNUNET_NO;
803 tmit->paused = GNUNET_NO;
806 struct GNUNET_MessageHeader msg;
807 msg.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
808 msg.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
809 msg.size = htons (sizeof (msg));
810 transmit_queue_insert (tmit, &msg, GNUNET_YES);
815 * Got acknowledgement of a transmitted message part, continue transmission.
817 * @param tmit Transmission handle.
820 GNUNET_PSYC_transmit_got_ack (struct GNUNET_PSYC_TransmitHandle *tmit)
822 if (0 == tmit->acks_pending)
824 LOG (GNUNET_ERROR_TYPE_WARNING, "Ignoring extraneous message ACK\n");
828 tmit->acks_pending--;
832 case GNUNET_PSYC_MESSAGE_STATE_MODIFIER:
833 case GNUNET_PSYC_MESSAGE_STATE_MOD_CONT:
837 case GNUNET_PSYC_MESSAGE_STATE_DATA:
838 transmit_data (tmit);
841 case GNUNET_PSYC_MESSAGE_STATE_END:
842 case GNUNET_PSYC_MESSAGE_STATE_CANCEL:
846 LOG (GNUNET_ERROR_TYPE_DEBUG,
847 "Ignoring message ACK in state %u.\n", tmit->state);
852 /**** Receiving messages ****/
856 * Create handle for receiving messages.
858 struct GNUNET_PSYC_ReceiveHandle *
859 GNUNET_PSYC_receive_create (GNUNET_PSYC_MessageCallback message_cb,
860 GNUNET_PSYC_MessagePartCallback message_part_cb,
863 struct GNUNET_PSYC_ReceiveHandle *recv = GNUNET_malloc (sizeof (*recv));
864 recv->message_cb = message_cb;
865 recv->message_part_cb = message_part_cb;
866 recv->cb_cls = cb_cls;
872 * Destroy handle for receiving messages.
875 GNUNET_PSYC_receive_destroy (struct GNUNET_PSYC_ReceiveHandle *recv)
882 * Reset stored data related to the last received message.
885 GNUNET_PSYC_receive_reset (struct GNUNET_PSYC_ReceiveHandle *recv)
887 recv->state = GNUNET_PSYC_MESSAGE_STATE_START;
889 recv->message_id = 0;
890 recv->mod_value_size = 0;
891 recv->mod_value_size_expected = 0;
896 recv_error (struct GNUNET_PSYC_ReceiveHandle *recv)
898 if (NULL != recv->message_part_cb)
899 recv->message_part_cb (recv->cb_cls, NULL, NULL);
901 if (NULL != recv->message_cb)
902 recv->message_cb (recv->cb_cls, NULL);
904 GNUNET_PSYC_receive_reset (recv);
909 * Handle incoming PSYC message.
911 * @param recv Receive handle.
912 * @param msg The message.
914 * @return #GNUNET_OK on success,
915 * #GNUNET_SYSERR on receive error.
918 GNUNET_PSYC_receive_message (struct GNUNET_PSYC_ReceiveHandle *recv,
919 const struct GNUNET_PSYC_MessageHeader *msg)
921 uint16_t size = ntohs (msg->header.size);
922 uint32_t flags = ntohl (msg->flags);
924 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG,
925 (struct GNUNET_MessageHeader *) msg);
927 if (GNUNET_PSYC_MESSAGE_STATE_START == recv->state)
929 recv->message_id = GNUNET_ntohll (msg->message_id);
931 recv->slave_pub_key = msg->slave_pub_key;
932 recv->mod_value_size = 0;
933 recv->mod_value_size_expected = 0;
935 else if (GNUNET_ntohll (msg->message_id) != recv->message_id)
938 LOG (GNUNET_ERROR_TYPE_WARNING,
939 "Unexpected message ID. Got: %" PRIu64 ", expected: %" PRIu64 "\n",
940 GNUNET_ntohll (msg->message_id), recv->message_id);
943 return GNUNET_SYSERR;
945 else if (flags != recv->flags)
947 LOG (GNUNET_ERROR_TYPE_WARNING,
948 "Unexpected message flags. Got: %lu, expected: %lu\n",
952 return GNUNET_SYSERR;
955 uint16_t pos = 0, psize = 0, ptype, size_eq, size_min;
957 for (pos = 0; sizeof (*msg) + pos < size; pos += psize)
959 const struct GNUNET_MessageHeader *pmsg
960 = (const struct GNUNET_MessageHeader *) ((char *) &msg[1] + pos);
961 psize = ntohs (pmsg->size);
962 ptype = ntohs (pmsg->type);
963 size_eq = size_min = 0;
965 if (psize < sizeof (*pmsg) || sizeof (*msg) + pos + psize > size)
967 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
968 "Dropping message of type %u with invalid size %u.\n",
971 return GNUNET_SYSERR;
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
975 "Received message part of type %u and size %u from PSYC.\n",
977 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
981 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
982 size_min = sizeof (struct GNUNET_PSYC_MessageMethod);
984 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
985 size_min = sizeof (struct GNUNET_PSYC_MessageModifier);
987 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
988 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
989 size_min = sizeof (struct GNUNET_MessageHeader);
991 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
992 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
993 size_eq = sizeof (struct GNUNET_MessageHeader);
998 return GNUNET_SYSERR;
1001 if (! ((0 < size_eq && psize == size_eq)
1002 || (0 < size_min && size_min <= psize)))
1004 GNUNET_break_op (0);
1006 return GNUNET_SYSERR;
1011 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
1013 struct GNUNET_PSYC_MessageMethod *meth
1014 = (struct GNUNET_PSYC_MessageMethod *) pmsg;
1016 if (GNUNET_PSYC_MESSAGE_STATE_START != recv->state)
1018 LOG (GNUNET_ERROR_TYPE_WARNING,
1019 "Dropping out of order message method (%u).\n",
1021 /* It is normal to receive an incomplete message right after connecting,
1022 * but should not happen later.
1023 * FIXME: add a check for this condition.
1025 GNUNET_break_op (0);
1027 return GNUNET_SYSERR;
1030 if ('\0' != *((char *) meth + psize - 1))
1032 LOG (GNUNET_ERROR_TYPE_WARNING,
1033 "Dropping message with malformed method. "
1034 "Message ID: %" PRIu64 "\n", recv->message_id);
1035 GNUNET_break_op (0);
1037 return GNUNET_SYSERR;
1039 recv->state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
1042 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
1044 if (!(GNUNET_PSYC_MESSAGE_STATE_METHOD == recv->state
1045 || GNUNET_PSYC_MESSAGE_STATE_MODIFIER == recv->state
1046 || GNUNET_PSYC_MESSAGE_STATE_MOD_CONT == recv->state))
1048 LOG (GNUNET_ERROR_TYPE_WARNING,
1049 "Dropping out of order message modifier (%u).\n",
1051 GNUNET_break_op (0);
1053 return GNUNET_SYSERR;
1056 struct GNUNET_PSYC_MessageModifier *mod
1057 = (struct GNUNET_PSYC_MessageModifier *) pmsg;
1059 uint16_t name_size = ntohs (mod->name_size);
1060 recv->mod_value_size_expected = ntohl (mod->value_size);
1061 recv->mod_value_size = psize - sizeof (*mod) - name_size;
1063 if (psize < sizeof (*mod) + name_size
1064 || '\0' != *((char *) &mod[1] + name_size - 1)
1065 || recv->mod_value_size_expected < recv->mod_value_size)
1067 LOG (GNUNET_ERROR_TYPE_WARNING, "Dropping malformed modifier.\n");
1068 GNUNET_break_op (0);
1070 return GNUNET_SYSERR;
1072 recv->state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
1075 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
1077 recv->mod_value_size += psize - sizeof (*pmsg);
1079 if (!(GNUNET_PSYC_MESSAGE_STATE_MODIFIER == recv->state
1080 || GNUNET_PSYC_MESSAGE_STATE_MOD_CONT == recv->state)
1081 || recv->mod_value_size_expected < recv->mod_value_size)
1083 LOG (GNUNET_ERROR_TYPE_WARNING,
1084 "Dropping out of order message modifier continuation "
1085 "!(%u == %u || %u == %u) || %lu < %lu.\n",
1086 GNUNET_PSYC_MESSAGE_STATE_MODIFIER, recv->state,
1087 GNUNET_PSYC_MESSAGE_STATE_MOD_CONT, recv->state,
1088 recv->mod_value_size_expected, recv->mod_value_size);
1089 GNUNET_break_op (0);
1091 return GNUNET_SYSERR;
1095 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
1097 if (recv->state < GNUNET_PSYC_MESSAGE_STATE_METHOD
1098 || recv->mod_value_size_expected != recv->mod_value_size)
1100 LOG (GNUNET_ERROR_TYPE_WARNING,
1101 "Dropping out of order message data fragment "
1102 "(%u < %u || %lu != %lu).\n",
1103 recv->state, GNUNET_PSYC_MESSAGE_STATE_METHOD,
1104 recv->mod_value_size_expected, recv->mod_value_size);
1106 GNUNET_break_op (0);
1108 return GNUNET_SYSERR;
1110 recv->state = GNUNET_PSYC_MESSAGE_STATE_DATA;
1115 if (NULL != recv->message_part_cb)
1116 recv->message_part_cb (recv->cb_cls, msg, pmsg);
1120 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
1121 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
1122 GNUNET_PSYC_receive_reset (recv);
1127 if (NULL != recv->message_cb)
1128 recv->message_cb (recv->cb_cls, msg);
1134 * Check if @a data contains a series of valid message parts.
1136 * @param data_size Size of @a data.
1138 * @param[out] first_ptype Type of first message part.
1139 * @param[out] last_ptype Type of last message part.
1141 * @return Number of message parts found in @a data.
1142 * or GNUNET_SYSERR if the message contains invalid parts.
1145 GNUNET_PSYC_receive_check_parts (uint16_t data_size, const char *data,
1146 uint16_t *first_ptype, uint16_t *last_ptype)
1148 const struct GNUNET_MessageHeader *pmsg;
1149 uint16_t parts = 0, ptype = 0, psize = 0, pos = 0;
1150 if (NULL != first_ptype)
1152 if (NULL != last_ptype)
1155 for (pos = 0; pos < data_size; pos += psize, parts++)
1157 pmsg = (const struct GNUNET_MessageHeader *) (data + pos);
1158 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
1159 psize = ntohs (pmsg->size);
1160 ptype = ntohs (pmsg->type);
1161 if (0 == parts && NULL != first_ptype)
1162 *first_ptype = ptype;
1163 if (NULL != last_ptype
1164 && *last_ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END)
1165 *last_ptype = ptype;
1166 if (psize < sizeof (*pmsg)
1167 || pos + psize > data_size
1168 || ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD
1169 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL < ptype)
1171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1172 "Invalid message part of type %u and size %u.\n",
1174 return GNUNET_SYSERR;
1176 /** @todo FIXME: check message part order */
1182 struct ParseMessageClosure
1184 struct GNUNET_PSYC_Environment *env;
1185 const char **method_name;
1187 uint16_t *data_size;
1188 enum GNUNET_PSYC_MessageState msg_state;
1193 parse_message_part_cb (void *cls,
1194 const struct GNUNET_PSYC_MessageHeader *msg,
1195 const struct GNUNET_MessageHeader *pmsg)
1197 struct ParseMessageClosure *pmc = cls;
1200 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
1204 switch (ntohs (pmsg->type))
1206 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
1208 struct GNUNET_PSYC_MessageMethod *
1209 pmeth = (struct GNUNET_PSYC_MessageMethod *) pmsg;
1210 *pmc->method_name = (const char *) &pmeth[1];
1211 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
1215 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
1217 struct GNUNET_PSYC_MessageModifier *
1218 pmod = (struct GNUNET_PSYC_MessageModifier *) pmsg;
1220 const char *name = (const char *) &pmod[1];
1221 const void *value = name + ntohs (pmod->name_size);
1222 GNUNET_PSYC_env_add (pmc->env, pmod->oper, name, value,
1223 ntohl (pmod->value_size));
1224 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
1228 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
1229 *pmc->data = &pmsg[1];
1230 *pmc->data_size = ntohs (pmsg->size) - sizeof (*pmsg);
1231 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_DATA;
1234 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
1235 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_END;
1239 pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
1245 * Parse PSYC message.
1248 * The PSYC message to parse.
1249 * @param[out] method_name
1250 * Pointer to the method name inside @a pmsg.
1252 * The environment for the message with a list of modifiers.
1254 * Pointer to data inside @a msg.
1255 * @param[out] data_size
1256 * Size of @data is written here.
1258 * @return #GNUNET_OK on success,
1259 * #GNUNET_SYSERR on parse error.
1262 GNUNET_PSYC_message_parse (const struct GNUNET_PSYC_MessageHeader *msg,
1263 const char **method_name,
1264 struct GNUNET_PSYC_Environment *env,
1266 uint16_t *data_size)
1268 struct ParseMessageClosure cls;
1270 cls.method_name = method_name;
1272 cls.data_size = data_size;
1274 struct GNUNET_PSYC_ReceiveHandle *
1275 recv = GNUNET_PSYC_receive_create (NULL, parse_message_part_cb, &cls);
1276 int ret = GNUNET_PSYC_receive_message (recv, msg);
1277 GNUNET_PSYC_receive_destroy (recv);
1279 if (GNUNET_OK != ret)
1280 return GNUNET_SYSERR;
1282 return (GNUNET_PSYC_MESSAGE_STATE_END == cls.msg_state)
1289 * Initialize PSYC message header.
1292 GNUNET_PSYC_message_header_init (struct GNUNET_PSYC_MessageHeader *pmsg,
1293 const struct GNUNET_MULTICAST_MessageHeader *mmsg,
1296 uint16_t size = ntohs (mmsg->header.size);
1297 uint16_t psize = sizeof (*pmsg) + size - sizeof (*mmsg);
1299 pmsg->header.size = htons (psize);
1300 pmsg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
1301 pmsg->message_id = mmsg->message_id;
1302 pmsg->fragment_offset = mmsg->fragment_offset;
1303 pmsg->flags = htonl (flags);
1305 memcpy (&pmsg[1], &mmsg[1], size - sizeof (*mmsg));
1310 * Create a new PSYC message header from a multicast message.
1312 struct GNUNET_PSYC_MessageHeader *
1313 GNUNET_PSYC_message_header_create (const struct GNUNET_MULTICAST_MessageHeader *mmsg,
1316 struct GNUNET_PSYC_MessageHeader *pmsg;
1317 uint16_t size = ntohs (mmsg->header.size);
1318 uint16_t psize = sizeof (*pmsg) + size - sizeof (*mmsg);
1320 pmsg = GNUNET_malloc (psize);
1321 GNUNET_PSYC_message_header_init (pmsg, mmsg, flags);
1327 * Create a new PSYC message header from a PSYC message.
1329 struct GNUNET_PSYC_MessageHeader *
1330 GNUNET_PSYC_message_header_create_from_psyc (const struct GNUNET_PSYC_Message *msg)
1332 uint16_t msg_size = ntohs (msg->header.size);
1333 struct GNUNET_PSYC_MessageHeader *
1334 pmsg = GNUNET_malloc (sizeof (*pmsg) + msg_size - sizeof (*msg));
1335 pmsg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
1336 pmsg->header.size = htons (sizeof (*pmsg) + msg_size - sizeof (*msg));
1337 memcpy (&pmsg[1], &msg[1], msg_size - sizeof (*msg));