to be continued
authorJi Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96>
Thu, 1 Apr 2010 14:56:59 +0000 (14:56 +0000)
committerJi Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96>
Thu, 1 Apr 2010 14:56:59 +0000 (14:56 +0000)
src/fragmentation/fragmentation.c
src/fragmentation/test_frag_ji.c

index c67059f344080a84324b6e729bd743983ef0d177..74d5a6b4c04b292243ea93202b7a7d899e97a980 100644 (file)
@@ -47,21 +47,22 @@ struct Fragment
        /**
         * Fragment offset.
         */
-       uint32_t off GNUNET_PACKED;
+       uint16_t off GNUNET_PACKED;
 
        /**
-        * "unique" id for the fragment
-        */
-       uint64_t id GNUNET_PACKED;
+                        * "unique" id for the fragment
+                        */
+       uint32_t id GNUNET_PACKED;
+
+       uint16_t mtu;
+       uint16_t totalNum;
+       uint16_t totalSize;
 
-       size_t mtu;
-       uint32_t totalNum;
-       uint64_t totalSize;
 };
 
 struct GNUNET_FRAGEMENT_Ctxbuffer{
        struct GNUNET_FRAGEMENT_Ctxbuffer *next;
-       uint64_t id;
+       uint32_t id;
        uint16_t size;
        char * buff;
        int counter;
@@ -99,6 +100,7 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg,
 {
        uint32_t id = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 256);
        size_t size = sizeof(struct Fragment);
+
        if(ntohs(msg->size) > mtu-size){
                uint16_t lastSize;
                uint16_t num;
@@ -115,14 +117,14 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg,
                        struct Fragment *frag;
                        if(actualNum != num){
                                                        if(i!=actualNum-1){
-                                                               frag = GNUNET_malloc(mtu);
+                                                               frag = (struct Fragment *)GNUNET_malloc(mtu);
                                                        }
                                                        else{
-                                                           frag = GNUNET_malloc(lastSize+size);
+                                                           frag = (struct Fragment *)GNUNET_malloc(lastSize+size);
                                                                }
                                                        }
                        else{
-                                       frag = GNUNET_malloc(mtu);
+                                       frag = (struct Fragment *)GNUNET_malloc(mtu);
                                }
                        frag->header.type = htons(GNUNET_MESSAGE_TYPE_FRAGMENT);
                        frag->id = htonl(id);
@@ -130,19 +132,20 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg,
                        frag->mtu = htons(mtu);
                        frag->totalNum = htons(actualNum);
                        frag->totalSize = msg->size;
+                       char *m =  (char *)msg;
                        if(actualNum != num){
                                if(i!=actualNum-1){
-                                       frag->header.size = htons(mtu);
-                                       memcpy(&frag[1], msg + ntohs(frag->off), mtu - size);
+                                       frag->header.size = frag->mtu;
+                                       memcpy(&frag[1], m + (mtu-size)*i, mtu - size);
                                }
                                else{
                                        frag->header.size = htons(lastSize+size);
-                                       memcpy(&frag[1], msg + ntohs(frag->off), lastSize);
+                                       memcpy(&frag[1], m + (mtu-size)*i, lastSize);
                                }
                        }
                        else{
-                               frag->header.size = htons(mtu);
-                               memcpy(&frag[1], msg + ntohs(frag->off), mtu - size);
+                               frag->header.size = frag->mtu;
+                               memcpy(&frag[1], m + (mtu-size)*i, mtu - size);
                        }
                        proc(proc_cls, &frag->header);
                        GNUNET_free(frag);
@@ -234,7 +237,7 @@ GNUNET_FRAGMENT_process (struct GNUNET_FRAGMENT_Context *ctx,
        }
 
        if(!exist){
-               buffer = GNUNET_malloc(sizeof(struct GNUNET_FRAGEMENT_Ctxbuffer));
+               buffer = (struct GNUNET_FRAGEMENT_Ctxbuffer*)GNUNET_malloc(sizeof(struct GNUNET_FRAGEMENT_Ctxbuffer));
                buffer->num = (int*)GNUNET_malloc(ntohs(frag->totalNum)*sizeof(int));
                int j;
                for(j = 0; j<ntohs(frag->totalNum); j++){
@@ -243,9 +246,9 @@ GNUNET_FRAGMENT_process (struct GNUNET_FRAGMENT_Context *ctx,
                buffer->peerID = sender;
                buffer->id = ntohl(frag->id);
                buffer->receivedTime = GNUNET_TIME_absolute_get ();
-               uint64_t si = ntohs(frag->totalSize);
+               uint16_t si = ntohs(frag->totalSize);
                buffer->size = si;
-               buffer->buff = (char *)GNUNET_malloc(si);
+               buffer->buff = (char*)GNUNET_malloc(si);
                buffer->next = ctx->buffer;
                ctx->buffer = buffer;
        }
index 059994f2b44957f045586cddd413140fabbc5687..6512ddb034624352de1cff48268642bd430d2398 100644 (file)
@@ -18,9 +18,12 @@ void message_proc1(void *cls, const struct GNUNET_MessageHeader * msg){
        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+960, originalMsg+960, 68)){\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
@@ -28,25 +31,27 @@ void message_proc2(void *cls, const struct GNUNET_MessageHeader * msg){
        printf("enter into message_proc2\n");\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
        
-       uint16_t mtu = 512;\r
+       uint32_t mtu = 512;\r
        struct GNUNET_FRAGMENT_Context * ctx;\r
-       struct GNUNET_MessageHeader *msg = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader)+2*mtu);\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)+2*mtu);\r
        msg->type = htons(GNUNET_MESSAGE_TYPE_HELLO);\r
        struct GNUNET_PeerIdentity *sender;\r
-       sender = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));\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 = GNUNET_malloc(sizeof(struct combine));\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