aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-06-23 09:00:41 +0000
committerChristian Grothoff <christian@grothoff.org>2010-06-23 09:00:41 +0000
commit0b5c7f1be64ac0d947d98151c5a74b3afe8353e8 (patch)
tree03c975e5b6519ec839c50794f86cea81fc069cba /src/core
parent45e99c8ffe2b2f86caf02b1cc5cefca280fd3b53 (diff)
downloadgnunet-0b5c7f1be64ac0d947d98151c5a74b3afe8353e8.tar.gz
gnunet-0b5c7f1be64ac0d947d98151c5a74b3afe8353e8.zip
use mst API
Diffstat (limited to 'src/core')
-rw-r--r--src/core/gnunet-service-core.c108
1 files changed, 23 insertions, 85 deletions
diff --git a/src/core/gnunet-service-core.c b/src/core/gnunet-service-core.c
index ce7e33a45..a181ae5bb 100644
--- a/src/core/gnunet-service-core.c
+++ b/src/core/gnunet-service-core.c
@@ -664,6 +664,11 @@ struct GNUNET_SCHEDULER_Handle *sched;
664static struct GNUNET_PEERINFO_Handle *peerinfo; 664static struct GNUNET_PEERINFO_Handle *peerinfo;
665 665
666/** 666/**
667 * Our message stream tokenizer (for encrypted payload).
668 */
669static struct GNUNET_SERVER_MessageStreamTokenizer *mst;
670
671/**
667 * Our configuration. 672 * Our configuration.
668 */ 673 */
669const struct GNUNET_CONFIGURATION_Handle *cfg; 674const struct GNUNET_CONFIGURATION_Handle *cfg;
@@ -3133,14 +3138,17 @@ send_p2p_message_to_client (struct Neighbour *sender,
3133/** 3138/**
3134 * Deliver P2P message to interested clients. 3139 * Deliver P2P message to interested clients.
3135 * 3140 *
3136 * @param sender who sent us the message? 3141 * @param cls always NULL
3142 * @param client who sent us the message (struct Neighbour)
3137 * @param m the message 3143 * @param m the message
3138 * @param msize size of the message (including header)
3139 */ 3144 */
3140static void 3145static void
3141deliver_message (struct Neighbour *sender, 3146deliver_message (void *cls,
3142 const struct GNUNET_MessageHeader *m, size_t msize) 3147 void *client,
3148 const struct GNUNET_MessageHeader *m)
3143{ 3149{
3150 struct Neighbour *sender = client;
3151 size_t msize = ntohs (m->size);
3144 char buf[256]; 3152 char buf[256];
3145 struct Client *cpos; 3153 struct Client *cpos;
3146 uint16_t type; 3154 uint16_t type;
@@ -3209,86 +3217,6 @@ deliver_message (struct Neighbour *sender,
3209 3217
3210 3218
3211/** 3219/**
3212 * Align P2P message and then deliver to interested clients.
3213 *
3214 * @param sender who sent us the message?
3215 * @param buffer unaligned (!) buffer containing message
3216 * @param msize size of the message (including header)
3217 */
3218static void
3219align_and_deliver (struct Neighbour *sender, const char *buffer, size_t msize)
3220{
3221 char abuf[msize];
3222
3223 /* TODO: call to statistics? */
3224 memcpy (abuf, buffer, msize);
3225 deliver_message (sender, (const struct GNUNET_MessageHeader *) abuf, msize);
3226}
3227
3228
3229/**
3230 * Deliver P2P messages to interested clients.
3231 *
3232 * @param sender who sent us the message?
3233 * @param buffer buffer containing messages, can be modified
3234 * @param buffer_size size of the buffer (overall)
3235 * @param offset offset where messages in the buffer start
3236 */
3237static void
3238deliver_messages (struct Neighbour *sender,
3239 const char *buffer, size_t buffer_size, size_t offset)
3240{
3241 struct GNUNET_MessageHeader *mhp;
3242 struct GNUNET_MessageHeader mh;
3243 uint16_t msize;
3244 int need_align;
3245
3246 while (offset + sizeof (struct GNUNET_MessageHeader) <= buffer_size)
3247 {
3248 if (0 != offset % sizeof (uint16_t))
3249 {
3250 /* outch, need to copy to access header */
3251 memcpy (&mh, &buffer[offset], sizeof (struct GNUNET_MessageHeader));
3252 mhp = &mh;
3253 }
3254 else
3255 {
3256 /* can access header directly */
3257 mhp = (struct GNUNET_MessageHeader *) &buffer[offset];
3258 }
3259 msize = ntohs (mhp->size);
3260 if (msize + offset > buffer_size)
3261 {
3262 /* malformed message, header says it is larger than what
3263 would fit into the overall buffer */
3264 GNUNET_break_op (0);
3265 break;
3266 }
3267#if HAVE_UNALIGNED_64_ACCESS
3268 need_align = (0 != offset % 4) ? GNUNET_YES : GNUNET_NO;
3269#else
3270 need_align = (0 != offset % 8) ? GNUNET_YES : GNUNET_NO;
3271#endif
3272#if DEBUG_CORE
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3274 "Delivering %u bytes of message at offset %u/%u to clients.\n",
3275 (unsigned int) msize,
3276 (unsigned int) offset,
3277 (unsigned int) buffer_size);
3278#endif
3279
3280 if (GNUNET_YES == need_align)
3281 align_and_deliver (sender, &buffer[offset], msize);
3282 else
3283 deliver_message (sender,
3284 (const struct GNUNET_MessageHeader *)
3285 &buffer[offset], msize);
3286 offset += msize;
3287 }
3288}
3289
3290
3291/**
3292 * We received an encrypted message. Decrypt, validate and 3220 * We received an encrypted message. Decrypt, validate and
3293 * pass on to the appropriate clients. 3221 * pass on to the appropriate clients.
3294 */ 3222 */
@@ -3435,7 +3363,12 @@ handle_encrypted_message (struct Neighbour *n,
3435 gettext_noop ("# bytes of payload decrypted"), 3363 gettext_noop ("# bytes of payload decrypted"),
3436 size - sizeof (struct EncryptedMessage), 3364 size - sizeof (struct EncryptedMessage),
3437 GNUNET_NO); 3365 GNUNET_NO);
3438 deliver_messages (n, buf, size, sizeof (struct EncryptedMessage)); 3366 if (GNUNET_OK != GNUNET_SERVER_mst_receive (mst,
3367 n,
3368 &buf[sizeof (struct EncryptedMessage)],
3369 size - sizeof (struct EncryptedMessage),
3370 GNUNET_YES, GNUNET_NO))
3371 GNUNET_break_op (0);
3439} 3372}
3440 3373
3441 3374
@@ -3797,6 +3730,8 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3797 GNUNET_STATISTICS_destroy (stats, GNUNET_NO); 3730 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
3798 if (peerinfo != NULL) 3731 if (peerinfo != NULL)
3799 GNUNET_PEERINFO_disconnect (peerinfo); 3732 GNUNET_PEERINFO_disconnect (peerinfo);
3733 if (mst != NULL)
3734 GNUNET_SERVER_mst_destroy (mst);
3800} 3735}
3801 3736
3802 3737
@@ -3889,6 +3824,9 @@ run (void *cls,
3889 &handle_transport_notify_disconnect); 3824 &handle_transport_notify_disconnect);
3890 GNUNET_assert (NULL != transport); 3825 GNUNET_assert (NULL != transport);
3891 stats = GNUNET_STATISTICS_create (sched, "core", cfg); 3826 stats = GNUNET_STATISTICS_create (sched, "core", cfg);
3827 mst = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE,
3828 &deliver_message,
3829 NULL);
3892 GNUNET_SCHEDULER_add_delayed (sched, 3830 GNUNET_SCHEDULER_add_delayed (sched,
3893 GNUNET_TIME_UNIT_FOREVER_REL, 3831 GNUNET_TIME_UNIT_FOREVER_REL,
3894 &cleaning_task, NULL); 3832 &cleaning_task, NULL);