diff options
author | Ji Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2010-03-04 21:59:51 +0000 |
---|---|---|
committer | Ji Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2010-03-04 21:59:51 +0000 |
commit | 93e050bbf5e9b307a88af9e2df4115239bc9dedb (patch) | |
tree | ac61eae750661f09e1402208a206e47ce7380449 /src/fragmentation/fragmentation.c | |
parent | 9c0e92ec7637b081d35582adfe775f8be7b1f2a3 (diff) | |
download | gnunet-93e050bbf5e9b307a88af9e2df4115239bc9dedb.tar.gz gnunet-93e050bbf5e9b307a88af9e2df4115239bc9dedb.zip |
pending fragmentation
Diffstat (limited to 'src/fragmentation/fragmentation.c')
-rw-r--r-- | src/fragmentation/fragmentation.c | 101 |
1 files changed, 96 insertions, 5 deletions
diff --git a/src/fragmentation/fragmentation.c b/src/fragmentation/fragmentation.c index 9550663c7..b52a4b9ab 100644 --- a/src/fragmentation/fragmentation.c +++ b/src/fragmentation/fragmentation.c | |||
@@ -33,7 +33,8 @@ | |||
33 | 33 | ||
34 | #include "platform.h" | 34 | #include "platform.h" |
35 | #include "gnunet_fragmentation_lib.h" | 35 | #include "gnunet_fragmentation_lib.h" |
36 | 36 | #include "gnunet_protocols.h" | |
37 | #include "gnunet_util_lib.h" | ||
37 | /** | 38 | /** |
38 | * Message fragment. This header is followed | 39 | * Message fragment. This header is followed |
39 | * by the actual data of the fragment. | 40 | * by the actual data of the fragment. |
@@ -53,6 +54,20 @@ struct Fragment | |||
53 | */ | 54 | */ |
54 | uint64_t id GNUNET_PACKED; | 55 | uint64_t id GNUNET_PACKED; |
55 | 56 | ||
57 | size_t mtu; | ||
58 | uint32_t totalNum; | ||
59 | |||
60 | }; | ||
61 | |||
62 | struct GNUNET_FRAGEMENT_Ctxbuffer{ | ||
63 | uint64_t id; | ||
64 | uint16_t size; | ||
65 | char * buff; | ||
66 | int counter; | ||
67 | struct GNUNET_TIME_Absolute receivedTime; | ||
68 | struct GNUNET_PeerIdentity *peerID; | ||
69 | struct GNUNET_FRAGEMENT_Ctxbuffer *next; | ||
70 | int * num; | ||
56 | }; | 71 | }; |
57 | 72 | ||
58 | 73 | ||
@@ -61,6 +76,8 @@ struct Fragment | |||
61 | */ | 76 | */ |
62 | struct GNUNET_FRAGMENT_Context | 77 | struct GNUNET_FRAGMENT_Context |
63 | { | 78 | { |
79 | uint32_t maxNum; | ||
80 | struct GNUNET_FRAGEMENT_Ctxbuffer *buffer; | ||
64 | }; | 81 | }; |
65 | 82 | ||
66 | 83 | ||
@@ -78,7 +95,36 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg, | |||
78 | GNUNET_FRAGMENT_MessageProcessor proc, | 95 | GNUNET_FRAGMENT_MessageProcessor proc, |
79 | void *proc_cls) | 96 | void *proc_cls) |
80 | { | 97 | { |
81 | GNUNET_assert (0); | 98 | uint32_t id = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 256); |
99 | size_t size = sizeof(struct Fragment); | ||
100 | if(msg->size > mtu){ | ||
101 | uint16_t lastSize = (msg->size) % (mtu-size); | ||
102 | int num = ceil(msg->size / mtu - size); | ||
103 | int i; | ||
104 | for(i = 0; i<num; i++){ | ||
105 | struct Fragment *frag = (struct Fragment *)GNUNET_malloc(size); | ||
106 | frag->header.type = htons(GNUNET_MESSAGE_TYPE_FRAGMENT); | ||
107 | frag->id = htonl(id); | ||
108 | frag->off = htons(mtu*i); | ||
109 | frag->mtu = htons(mtu); | ||
110 | if(lastSize!=0){ | ||
111 | frag->totalNum = htons(num+1); | ||
112 | } | ||
113 | else{ | ||
114 | frag->totalNum = htons(num); | ||
115 | } | ||
116 | if(i!=num-1){ | ||
117 | frag->header.size = htons(mtu - size); | ||
118 | memcpy((char*)&frag[1], (char *)&msg[1]+frag->off, mtu - size); | ||
119 | } | ||
120 | else{ | ||
121 | frag->header.size = htons(lastSize); | ||
122 | memcpy((char*)&frag[1], (char *)&msg[1]+frag->off, lastSize); | ||
123 | } | ||
124 | proc(proc_cls, &frag->header); | ||
125 | free(frag); | ||
126 | } | ||
127 | } | ||
82 | } | 128 | } |
83 | 129 | ||
84 | 130 | ||
@@ -95,7 +141,10 @@ GNUNET_FRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats, | |||
95 | GNUNET_FRAGMENT_MessageProcessor proc, | 141 | GNUNET_FRAGMENT_MessageProcessor proc, |
96 | void *proc_cls) | 142 | void *proc_cls) |
97 | { | 143 | { |
98 | return NULL; | 144 | struct GNUNET_FRAGMENT_Context *ctx = (struct GNUNET_FRAGMENT_Context*)GNUNET_malloc(sizeof(struct GNUNET_FRAGMENT_Context)); |
145 | ctx->maxNum = 100; | ||
146 | ctx->buffer = NULL; | ||
147 | return ctx; | ||
99 | } | 148 | } |
100 | 149 | ||
101 | 150 | ||
@@ -105,7 +154,13 @@ GNUNET_FRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats, | |||
105 | void | 154 | void |
106 | GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *ctx) | 155 | GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *ctx) |
107 | { | 156 | { |
108 | GNUNET_assert (0); | 157 | struct GNUNET_FRAGEMENT_Ctxbuffer *buffer; |
158 | for(buffer = ctx->buffer; buffer!=NULL; buffer = buffer->next){ | ||
159 | GNUNET_free(buffer->num); | ||
160 | GNUNET_free(buffer); | ||
161 | } | ||
162 | GNUNET_free(ctx); | ||
163 | GNUNET_assert (0); | ||
109 | } | 164 | } |
110 | 165 | ||
111 | 166 | ||
@@ -121,7 +176,43 @@ GNUNET_FRAGMENT_process (struct GNUNET_FRAGMENT_Context *ctx, | |||
121 | const struct GNUNET_PeerIdentity *sender, | 176 | const struct GNUNET_PeerIdentity *sender, |
122 | const struct GNUNET_MessageHeader *msg) | 177 | const struct GNUNET_MessageHeader *msg) |
123 | { | 178 | { |
124 | GNUNET_assert (0); | 179 | uint16_t type = ntohs(msg->type); |
180 | int exited = 0, received = 0; | ||
181 | if(type!=GNUNET_MESSAGE_TYPE_FRAGMENT){ | ||
182 | return; | ||
183 | } | ||
184 | struct Fragment *frag = (struct Fragment *)msg; | ||
185 | struct GNUNET_FRAGEMENT_Ctxbuffer* buffer; | ||
186 | for(buffer = ctx->buffer; buffer!= NULL; buffer = buffer->next){ | ||
187 | if(ctx->buffer->counter == ntohs(frag->totalNum)){return;} | ||
188 | if(buffer->id == ntohl(frag->id)&&(buffer->peerID==sender)){ | ||
189 | exited = 1; | ||
190 | int i; | ||
191 | for(i = 0; i<ntohs(frag->totalNum); i++){ | ||
192 | if(buffer->num[i]==ntohs(frag->off)/ntohs(frag->mtu)){ | ||
193 | received = 1; | ||
194 | break; | ||
195 | } | ||
196 | } | ||
197 | if(!received){ | ||
198 | buffer->num[buffer->counter++]==ntohs(frag->off)/ntohs(frag->mtu); | ||
199 | } | ||
200 | buffer->receivedTime = GNUNET_TIME_absolute_get (); | ||
201 | uint16_t size = ntohs(frag->header.size); | ||
202 | memcpy(&buffer->buff[ntohs(frag->off)], &frag[1], size); | ||
203 | break; | ||
204 | } | ||
205 | } | ||
206 | if(!exited){ | ||
207 | buffer = (struct GNUNET_FRAGEMENT_Ctxbuffer* )GNUNET_malloc(sizeof(struct GNUNET_FRAGEMENT_Ctxbuffer)); | ||
208 | buffer->num = (int*)GNUNET_malloc(ntohs(frag->totalNum)*sizeof(int)); | ||
209 | buffer->num[buffer->counter++]==ntohs(frag->off)/ntohs(frag->mtu); | ||
210 | memcpy(buffer->peerID,sender,sizeof(struct GNUNET_PeerIdentity)); | ||
211 | buffer->receivedTime = GNUNET_TIME_absolute_get (); | ||
212 | uint16_t size = ntohs(frag->header.size); | ||
213 | memcpy(&buffer->buff[ntohs(frag->off)], &frag[1], size); | ||
214 | } | ||
215 | |||
125 | } | 216 | } |
126 | 217 | ||
127 | 218 | ||