* fragmented packet at any given point in time (prevents
* DoS attacks). Fragmented messages that have not been
* completed after a certain amount of time are discarded.
- * @author Christian Grothoff
+ * @author Ji Lu
*/
#include "platform.h"
{
struct Fragment *frag;
if(actualNum != num){
- if(i!=actualNum-1){
- frag = (struct Fragment *)GNUNET_malloc(mtu);
- }
- else{
- frag = (struct Fragment *)GNUNET_malloc(lastSize+size);
- }
- }
+ if(i!=actualNum-1){
+ frag = (struct Fragment *)GNUNET_malloc(mtu);
+ }
+ else{
+ frag = (struct Fragment *)GNUNET_malloc(lastSize+size);
+ }
+ }
else{
frag = (struct Fragment *)GNUNET_malloc(mtu);
}
frag->mtu = htons(mtu);
frag->totalNum = htons(actualNum);
frag->totalSize = msg->size;
- char *m = (char *)msg;
+ char *tmpMsg = (char *)msg;
if(actualNum != num){
if(i!=actualNum-1){
- frag->header.size = frag->mtu;
- memcpy(&frag[1], m + (mtu-size)*i, mtu - size);
+ frag->header.size = htons(mtu);
+ memcpy(&frag[1], tmpMsg + (mtu-size)*i, mtu - size);
}
else{
frag->header.size = htons(lastSize+size);
- memcpy(&frag[1], m + (mtu-size)*i, lastSize);
+ memcpy(&frag[1], tmpMsg + (mtu-size)*i, lastSize);
}
}
else{
- frag->header.size = frag->mtu;
- memcpy(&frag[1], m + (mtu-size)*i, mtu - size);
+ frag->header.size = htons(mtu);
+ memcpy(&frag[1], tmpMsg + (mtu-size)*i, mtu - size);
}
proc(proc_cls, &frag->header);
GNUNET_free(frag);
--- /dev/null
+/*\r
+ This file is part of GNUnet.\r
+ (C) 2009 Christian Grothoff (and other contributing authors)\r
+\r
+ GNUnet is free software; you can redistribute it and/or modify\r
+ it under the terms of the GNU General Public License as published\r
+ by the Free Software Foundation; either version 2, or (at your\r
+ option) any later version.\r
+\r
+ GNUnet is distributed in the hope that it will be useful, but\r
+ WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ General Public License for more details.\r
+\r
+ You should have received a copy of the GNU General Public License\r
+ along with GNUnet; see the file COPYING. If not, write to the\r
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
+ Boston, MA 02111-1307, USA.\r
+*/\r
+\r
+/**\r
+ * @file fragmentation/test_fragmentation_enhanced.c\r
+ * @brief testcase for the fragmentation.c\r
+ * @author Ji Lu\r
+ */\r
+\r
+#include "platform.h"\r
+#include "gnunet_protocols.h"\r
+#include "gnunet_fragmentation_lib.h"\r
+\r
+struct combine{\r
+ struct GNUNET_FRAGMENT_Context* ctx;\r
+ struct GNUNET_PeerIdentity* sender;\r
+};\r
+\r
+void message_proc1(void *cls, const struct GNUNET_MessageHeader * msg){\r
+\r
+ fprintf(stderr, "enter into message_proc1\n");\r
+\r
+ struct GNUNET_MessageHeader * originalMsg = (struct GNUNET_MessageHeader *)cls;\r
+\r
+ if(ntohs(originalMsg->size) != ntohs(msg->size)){\r
+ fprintf(stderr, "the received message has the different size with the sent one!\n");\r
+ }\r
+ if(ntohs(originalMsg->type) != ntohs(msg->type)){\r
+ fprintf(stderr, "the received message has the different type with the sent one!\n");\r
+ }\r
+ if(memcmp(msg, originalMsg, originalMsg->size)){\r
+ fprintf(stderr, "the received message is not the sent one!\n");\r
+ }\r
+ else{\r
+ fprintf(stdout, "You got the right message!\n");\r
+ }\r
+\r
+}\r
+\r
+void message_proc2(void *cls, const struct GNUNET_MessageHeader * msg){\r
+\r
+ printf("enter into message_proc2\n");\r
+\r
+ struct combine * com2 = (struct combine* )cls;\r
+ GNUNET_FRAGMENT_process(com2->ctx, com2->sender, msg);\r
+\r
+}\r
+\r
+int\r
+main(int argc, char * argv[]){\r
+
+ uint32_t mtu = 512;\r
+ struct GNUNET_FRAGMENT_Context * ctx;\r
+ struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *)GNUNET_malloc(sizeof(struct GNUNET_MessageHeader)+2*mtu);\r
+ ctx = GNUNET_FRAGMENT_context_create(NULL, message_proc1, msg);\r
+ msg->size = htons(sizeof(struct GNUNET_MessageHeader)+4*mtu);\r
+ msg->type = htons(GNUNET_MESSAGE_TYPE_HELLO);\r
+ struct GNUNET_PeerIdentity *sender;\r
+ sender = (struct GNUNET_PeerIdentity *)GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));\r
+\r
+ memset(sender, 9, sizeof(struct GNUNET_PeerIdentity));\r
+\r
+ memset(&msg[1], 5, 2*mtu);\r
+\r
+ struct combine *com;\r
+ com = (struct combine *)GNUNET_malloc(sizeof(struct combine));\r
+ com->ctx = ctx;\r
+ com->sender = sender;\r
+ GNUNET_FRAGMENT_fragment(msg, mtu, message_proc2, com);\r
+ GNUNET_free(msg);\r
+ return 0;\r
+}\r