aboutsummaryrefslogtreecommitdiff
path: root/src/fragmentation
diff options
context:
space:
mode:
authorJi Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96>2010-04-01 14:56:59 +0000
committerJi Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96>2010-04-01 14:56:59 +0000
commit6dac073d8bdd079f31b147ee35757d86fcac0246 (patch)
tree625be9e16ea3a2bf9dee369235ea6ebe792ebc92 /src/fragmentation
parenta6f8b3403ad79d6e7823637e48a5118ca5731f72 (diff)
downloadgnunet-6dac073d8bdd079f31b147ee35757d86fcac0246.tar.gz
gnunet-6dac073d8bdd079f31b147ee35757d86fcac0246.zip
to be continued
Diffstat (limited to 'src/fragmentation')
-rw-r--r--src/fragmentation/fragmentation.c41
-rw-r--r--src/fragmentation/test_frag_ji.c15
2 files changed, 32 insertions, 24 deletions
diff --git a/src/fragmentation/fragmentation.c b/src/fragmentation/fragmentation.c
index c67059f34..74d5a6b4c 100644
--- a/src/fragmentation/fragmentation.c
+++ b/src/fragmentation/fragmentation.c
@@ -47,21 +47,22 @@ struct Fragment
47 /** 47 /**
48 * Fragment offset. 48 * Fragment offset.
49 */ 49 */
50 uint32_t off GNUNET_PACKED; 50 uint16_t off GNUNET_PACKED;
51 51
52 /** 52 /**
53 * "unique" id for the fragment 53 * "unique" id for the fragment
54 */ 54 */
55 uint64_t id GNUNET_PACKED; 55 uint32_t id GNUNET_PACKED;
56
57 uint16_t mtu;
58 uint16_t totalNum;
59 uint16_t totalSize;
56 60
57 size_t mtu;
58 uint32_t totalNum;
59 uint64_t totalSize;
60}; 61};
61 62
62struct GNUNET_FRAGEMENT_Ctxbuffer{ 63struct GNUNET_FRAGEMENT_Ctxbuffer{
63 struct GNUNET_FRAGEMENT_Ctxbuffer *next; 64 struct GNUNET_FRAGEMENT_Ctxbuffer *next;
64 uint64_t id; 65 uint32_t id;
65 uint16_t size; 66 uint16_t size;
66 char * buff; 67 char * buff;
67 int counter; 68 int counter;
@@ -99,6 +100,7 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg,
99{ 100{
100 uint32_t id = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 256); 101 uint32_t id = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 256);
101 size_t size = sizeof(struct Fragment); 102 size_t size = sizeof(struct Fragment);
103
102 if(ntohs(msg->size) > mtu-size){ 104 if(ntohs(msg->size) > mtu-size){
103 uint16_t lastSize; 105 uint16_t lastSize;
104 uint16_t num; 106 uint16_t num;
@@ -115,14 +117,14 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg,
115 struct Fragment *frag; 117 struct Fragment *frag;
116 if(actualNum != num){ 118 if(actualNum != num){
117 if(i!=actualNum-1){ 119 if(i!=actualNum-1){
118 frag = GNUNET_malloc(mtu); 120 frag = (struct Fragment *)GNUNET_malloc(mtu);
119 } 121 }
120 else{ 122 else{
121 frag = GNUNET_malloc(lastSize+size); 123 frag = (struct Fragment *)GNUNET_malloc(lastSize+size);
122 } 124 }
123 } 125 }
124 else{ 126 else{
125 frag = GNUNET_malloc(mtu); 127 frag = (struct Fragment *)GNUNET_malloc(mtu);
126 } 128 }
127 frag->header.type = htons(GNUNET_MESSAGE_TYPE_FRAGMENT); 129 frag->header.type = htons(GNUNET_MESSAGE_TYPE_FRAGMENT);
128 frag->id = htonl(id); 130 frag->id = htonl(id);
@@ -130,19 +132,20 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg,
130 frag->mtu = htons(mtu); 132 frag->mtu = htons(mtu);
131 frag->totalNum = htons(actualNum); 133 frag->totalNum = htons(actualNum);
132 frag->totalSize = msg->size; 134 frag->totalSize = msg->size;
135 char *m = (char *)msg;
133 if(actualNum != num){ 136 if(actualNum != num){
134 if(i!=actualNum-1){ 137 if(i!=actualNum-1){
135 frag->header.size = htons(mtu); 138 frag->header.size = frag->mtu;
136 memcpy(&frag[1], msg + ntohs(frag->off), mtu - size); 139 memcpy(&frag[1], m + (mtu-size)*i, mtu - size);
137 } 140 }
138 else{ 141 else{
139 frag->header.size = htons(lastSize+size); 142 frag->header.size = htons(lastSize+size);
140 memcpy(&frag[1], msg + ntohs(frag->off), lastSize); 143 memcpy(&frag[1], m + (mtu-size)*i, lastSize);
141 } 144 }
142 } 145 }
143 else{ 146 else{
144 frag->header.size = htons(mtu); 147 frag->header.size = frag->mtu;
145 memcpy(&frag[1], msg + ntohs(frag->off), mtu - size); 148 memcpy(&frag[1], m + (mtu-size)*i, mtu - size);
146 } 149 }
147 proc(proc_cls, &frag->header); 150 proc(proc_cls, &frag->header);
148 GNUNET_free(frag); 151 GNUNET_free(frag);
@@ -234,7 +237,7 @@ GNUNET_FRAGMENT_process (struct GNUNET_FRAGMENT_Context *ctx,
234 } 237 }
235 238
236 if(!exist){ 239 if(!exist){
237 buffer = GNUNET_malloc(sizeof(struct GNUNET_FRAGEMENT_Ctxbuffer)); 240 buffer = (struct GNUNET_FRAGEMENT_Ctxbuffer*)GNUNET_malloc(sizeof(struct GNUNET_FRAGEMENT_Ctxbuffer));
238 buffer->num = (int*)GNUNET_malloc(ntohs(frag->totalNum)*sizeof(int)); 241 buffer->num = (int*)GNUNET_malloc(ntohs(frag->totalNum)*sizeof(int));
239 int j; 242 int j;
240 for(j = 0; j<ntohs(frag->totalNum); j++){ 243 for(j = 0; j<ntohs(frag->totalNum); j++){
@@ -243,9 +246,9 @@ GNUNET_FRAGMENT_process (struct GNUNET_FRAGMENT_Context *ctx,
243 buffer->peerID = sender; 246 buffer->peerID = sender;
244 buffer->id = ntohl(frag->id); 247 buffer->id = ntohl(frag->id);
245 buffer->receivedTime = GNUNET_TIME_absolute_get (); 248 buffer->receivedTime = GNUNET_TIME_absolute_get ();
246 uint64_t si = ntohs(frag->totalSize); 249 uint16_t si = ntohs(frag->totalSize);
247 buffer->size = si; 250 buffer->size = si;
248 buffer->buff = (char *)GNUNET_malloc(si); 251 buffer->buff = (char*)GNUNET_malloc(si);
249 buffer->next = ctx->buffer; 252 buffer->next = ctx->buffer;
250 ctx->buffer = buffer; 253 ctx->buffer = buffer;
251 } 254 }
diff --git a/src/fragmentation/test_frag_ji.c b/src/fragmentation/test_frag_ji.c
index 059994f2b..6512ddb03 100644
--- a/src/fragmentation/test_frag_ji.c
+++ b/src/fragmentation/test_frag_ji.c
@@ -18,9 +18,12 @@ void message_proc1(void *cls, const struct GNUNET_MessageHeader * msg){
18 if(ntohs(originalMsg->type) != ntohs(msg->type)){ 18 if(ntohs(originalMsg->type) != ntohs(msg->type)){
19 fprintf(stderr, "the received message has the different type with the sent one!\n"); 19 fprintf(stderr, "the received message has the different type with the sent one!\n");
20 } 20 }
21 if(memcmp(msg+960, originalMsg+960, 68)){ 21 if(memcmp(msg, originalMsg, originalMsg->size)){
22 fprintf(stderr, "the received message is not the sent one!\n"); 22 fprintf(stderr, "the received message is not the sent one!\n");
23 } 23 }
24 else{
25 fprintf(stdout, "You got the right message!\n");
26 }
24 27
25} 28}
26 29
@@ -28,25 +31,27 @@ void message_proc2(void *cls, const struct GNUNET_MessageHeader * msg){
28 printf("enter into message_proc2\n"); 31 printf("enter into message_proc2\n");
29 struct combine * com2 = (struct combine* )cls; 32 struct combine * com2 = (struct combine* )cls;
30 GNUNET_FRAGMENT_process(com2->ctx, com2->sender, msg); 33 GNUNET_FRAGMENT_process(com2->ctx, com2->sender, msg);
34
31} 35}
32 36
33int 37int
34main(int argc, char * argv[]){ 38main(int argc, char * argv[]){
35 39
36 uint16_t mtu = 512; 40 uint32_t mtu = 512;
37 struct GNUNET_FRAGMENT_Context * ctx; 41 struct GNUNET_FRAGMENT_Context * ctx;
38 struct GNUNET_MessageHeader *msg = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader)+2*mtu); 42 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *)GNUNET_malloc(sizeof(struct GNUNET_MessageHeader)+2*mtu);
39 ctx = GNUNET_FRAGMENT_context_create(NULL, message_proc1, msg); 43 ctx = GNUNET_FRAGMENT_context_create(NULL, message_proc1, msg);
40 msg->size = htons(sizeof(struct GNUNET_MessageHeader)+2*mtu); 44 msg->size = htons(sizeof(struct GNUNET_MessageHeader)+2*mtu);
41 msg->type = htons(GNUNET_MESSAGE_TYPE_HELLO); 45 msg->type = htons(GNUNET_MESSAGE_TYPE_HELLO);
42 struct GNUNET_PeerIdentity *sender; 46 struct GNUNET_PeerIdentity *sender;
43 sender = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity)); 47 sender = (struct GNUNET_PeerIdentity *)GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
44 48
45 memset(sender, 9, sizeof(struct GNUNET_PeerIdentity)); 49 memset(sender, 9, sizeof(struct GNUNET_PeerIdentity));
46 50
47 memset(&msg[1], 5, 2*mtu); 51 memset(&msg[1], 5, 2*mtu);
52
48 struct combine *com; 53 struct combine *com;
49 com = GNUNET_malloc(sizeof(struct combine)); 54 com = (struct combine *)GNUNET_malloc(sizeof(struct combine));
50 com->ctx = ctx; 55 com->ctx = ctx;
51 com->sender = sender; 56 com->sender = sender;
52 GNUNET_FRAGMENT_fragment(msg, mtu, message_proc2, com); 57 GNUNET_FRAGMENT_fragment(msg, mtu, message_proc2, com);