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_common.c
23 * @brief Common functions for PSYC
24 * @author Gabor X Toth
31 * Check if @a data contains a series of valid message parts.
33 * @param data_size Size of @a data.
36 * @return GNUNET_YES or GNUNET_NO
39 GNUNET_PSYC_check_message_parts (uint16_t data_size, const char *data)
41 const struct GNUNET_MessageHeader *pmsg;
45 for (pos = 0; data_size + pos < data_size; pos += psize)
47 pmsg = (const struct GNUNET_MessageHeader *) (data + pos);
48 psize = ntohs (pmsg->size);
49 if (psize < sizeof (*pmsg) || data_size + pos + psize > data_size)
51 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
52 "Invalid message part of type %u and size %u.",
53 ntohs (pmsg->type), psize);
62 GNUNET_PSYC_log_message (enum GNUNET_ErrorType kind,
63 const struct GNUNET_MessageHeader *msg)
65 uint16_t size = ntohs (msg->size);
66 uint16_t type = ntohs (msg->type);
67 GNUNET_log (kind, "Message of type %d and size %u:\n", type, size);
70 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE:
72 struct GNUNET_PSYC_MessageHeader *pmsg
73 = (struct GNUNET_PSYC_MessageHeader *) msg;
74 GNUNET_log (kind, "\tID: %" PRIu64 "\tflags: %" PRIu32 "\n",
75 GNUNET_ntohll (pmsg->message_id), ntohl (pmsg->flags));
78 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
80 struct GNUNET_PSYC_MessageMethod *meth
81 = (struct GNUNET_PSYC_MessageMethod *) msg;
82 GNUNET_log (kind, "\t%.*s\n", size - sizeof (*meth), &meth[1]);
85 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
87 struct GNUNET_PSYC_MessageModifier *mod
88 = (struct GNUNET_PSYC_MessageModifier *) msg;
89 uint16_t name_size = ntohs (mod->name_size);
90 char oper = ' ' < mod->oper ? mod->oper : ' ';
91 GNUNET_log (kind, "\t%c%.*s\t%.*s\n", oper, name_size, &mod[1],
92 ntohs (mod->value_size), ((char *) &mod[1]) + name_size + 1);
95 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
96 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
97 GNUNET_log (kind, "\t%.*s\n", size - sizeof (*msg), &msg[1]);