wip
[oweals/gnunet.git] / src / fragmentation / fragmentation.c
index c3739a3e9e71116b3ec76de5435a8e39c9c05939..3ff737ccf42d055735d40a94315aefaa1ce59531 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  *        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"
 #include "gnunet_fragmentation_lib.h"
 #include "gnunet_protocols.h"
 #include "gnunet_util_lib.h"
+
 /**
  * Message fragment.  This header is followed
  * by the actual data of the fragment.
  */
+
 struct Fragment
 {
 
@@ -66,7 +68,7 @@ struct GNUNET_FRAGEMENT_Ctxbuffer{
        char * buff;
        int counter;
        struct GNUNET_TIME_Absolute receivedTime;
-       struct GNUNET_PeerIdentity *peerID;
+       struct GNUNET_PeerIdentity peerID;
        int * num;
 };
 
@@ -115,13 +117,13 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg,
                {
                        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);
                                }
@@ -131,20 +133,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;
+                       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);
@@ -181,10 +183,16 @@ void
 GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *ctx)
 {
        struct GNUNET_FRAGEMENT_Ctxbuffer *buffer;
-       for(buffer = ctx->buffer; buffer!=NULL; buffer = buffer->next){
-               GNUNET_free(buffer->num);
-               GNUNET_free(buffer);
-       }
+       struct GNUNET_FRAGEMENT_Ctxbuffer *temp;
+       buffer = ctx->buffer;
+
+       while (buffer != NULL)
+         {
+            temp = buffer->next;
+            GNUNET_free(buffer->num);
+            GNUNET_free(buffer);
+            buffer = temp;
+          }
        GNUNET_free(ctx);
        GNUNET_assert (0);
 }
@@ -214,7 +222,8 @@ GNUNET_FRAGMENT_process (struct GNUNET_FRAGMENT_Context *ctx,
        buffer = ctx->buffer;
        while (buffer != NULL)
        {
-               if(buffer->id == ntohl(frag->id)&&(buffer->peerID==sender)){
+               if ((buffer->id == ntohl(frag->id))&&(0 == memcmp (&buffer->peerID,
+                             sender, sizeof (struct GNUNET_PeerIdentity)))){
                        exist = 1;
                        break;
                }
@@ -234,18 +243,18 @@ GNUNET_FRAGMENT_process (struct GNUNET_FRAGMENT_Context *ctx,
        }
 
        if(!exist){
-               buffer = (struct GNUNET_FRAGEMENT_Ctxbuffer*)GNUNET_malloc(sizeof(struct GNUNET_FRAGEMENT_Ctxbuffer));
-               buffer->num = (int*)GNUNET_malloc(ntohs(frag->totalNum)*sizeof(int));
+               buffer = GNUNET_malloc(sizeof(struct GNUNET_FRAGEMENT_Ctxbuffer));
+               buffer->num = GNUNET_malloc(ntohs(frag->totalNum)*sizeof(int));
                int j;
                for(j = 0; j<ntohs(frag->totalNum); j++){
                        buffer->num[j] = -10;
                }
-               buffer->peerID = sender;
+               buffer->peerID = *sender;
                buffer->id = ntohl(frag->id);
                buffer->receivedTime = GNUNET_TIME_absolute_get ();
                uint16_t si = ntohs(frag->totalSize);
                buffer->size = si;
-               buffer->buff = (char*)GNUNET_malloc(si);
+               buffer->buff = GNUNET_malloc(si);
                buffer->next = ctx->buffer;
                ctx->buffer = buffer;
        }
@@ -324,7 +333,7 @@ static int stat_discarded;
 /**
  * Hashtable *with* collision management!
  */
-static FC *defragmentationCache[DEFRAG_BUCKET_COUNT];
+static struct FC *defragmentationCache[DEFRAG_BUCKET_COUNT];
 
 /**
  * Lock for the defragmentation cache.