diff options
author | Ji Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2010-04-01 15:16:00 +0000 |
---|---|---|
committer | Ji Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2010-04-01 15:16:00 +0000 |
commit | 36bfd342bf5703f0f482e0ea0c6adf0682f3d014 (patch) | |
tree | 7154c685d65979674fe1b242e86026f694f1dc9b /src/fragmentation | |
parent | ebb09b65cddeee11ea8a6e3a95eb328bc0f7711c (diff) | |
download | gnunet-36bfd342bf5703f0f482e0ea0c6adf0682f3d014.tar.gz gnunet-36bfd342bf5703f0f482e0ea0c6adf0682f3d014.zip |
final version from ji lu
Diffstat (limited to 'src/fragmentation')
-rw-r--r-- | src/fragmentation/fragmentation.c | 28 | ||||
-rw-r--r-- | src/fragmentation/test_fragmentation_enhanced.c | 89 |
2 files changed, 103 insertions, 14 deletions
diff --git a/src/fragmentation/fragmentation.c b/src/fragmentation/fragmentation.c index c3739a3e9..c06715b99 100644 --- a/src/fragmentation/fragmentation.c +++ b/src/fragmentation/fragmentation.c | |||
@@ -28,7 +28,7 @@ | |||
28 | * fragmented packet at any given point in time (prevents | 28 | * fragmented packet at any given point in time (prevents |
29 | * DoS attacks). Fragmented messages that have not been | 29 | * DoS attacks). Fragmented messages that have not been |
30 | * completed after a certain amount of time are discarded. | 30 | * completed after a certain amount of time are discarded. |
31 | * @author Christian Grothoff | 31 | * @author Ji Lu |
32 | */ | 32 | */ |
33 | 33 | ||
34 | #include "platform.h" | 34 | #include "platform.h" |
@@ -115,13 +115,13 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg, | |||
115 | { | 115 | { |
116 | struct Fragment *frag; | 116 | struct Fragment *frag; |
117 | if(actualNum != num){ | 117 | if(actualNum != num){ |
118 | if(i!=actualNum-1){ | 118 | if(i!=actualNum-1){ |
119 | frag = (struct Fragment *)GNUNET_malloc(mtu); | 119 | frag = (struct Fragment *)GNUNET_malloc(mtu); |
120 | } | 120 | } |
121 | else{ | 121 | else{ |
122 | frag = (struct Fragment *)GNUNET_malloc(lastSize+size); | 122 | frag = (struct Fragment *)GNUNET_malloc(lastSize+size); |
123 | } | 123 | } |
124 | } | 124 | } |
125 | else{ | 125 | else{ |
126 | frag = (struct Fragment *)GNUNET_malloc(mtu); | 126 | frag = (struct Fragment *)GNUNET_malloc(mtu); |
127 | } | 127 | } |
@@ -131,20 +131,20 @@ GNUNET_FRAGMENT_fragment (const struct GNUNET_MessageHeader *msg, | |||
131 | frag->mtu = htons(mtu); | 131 | frag->mtu = htons(mtu); |
132 | frag->totalNum = htons(actualNum); | 132 | frag->totalNum = htons(actualNum); |
133 | frag->totalSize = msg->size; | 133 | frag->totalSize = msg->size; |
134 | char *m = (char *)msg; | 134 | char *tmpMsg = (char *)msg; |
135 | if(actualNum != num){ | 135 | if(actualNum != num){ |
136 | if(i!=actualNum-1){ | 136 | if(i!=actualNum-1){ |
137 | frag->header.size = frag->mtu; | 137 | frag->header.size = htons(mtu); |
138 | memcpy(&frag[1], m + (mtu-size)*i, mtu - size); | 138 | memcpy(&frag[1], tmpMsg + (mtu-size)*i, mtu - size); |
139 | } | 139 | } |
140 | else{ | 140 | else{ |
141 | frag->header.size = htons(lastSize+size); | 141 | frag->header.size = htons(lastSize+size); |
142 | memcpy(&frag[1], m + (mtu-size)*i, lastSize); | 142 | memcpy(&frag[1], tmpMsg + (mtu-size)*i, lastSize); |
143 | } | 143 | } |
144 | } | 144 | } |
145 | else{ | 145 | else{ |
146 | frag->header.size = frag->mtu; | 146 | frag->header.size = htons(mtu); |
147 | memcpy(&frag[1], m + (mtu-size)*i, mtu - size); | 147 | memcpy(&frag[1], tmpMsg + (mtu-size)*i, mtu - size); |
148 | } | 148 | } |
149 | proc(proc_cls, &frag->header); | 149 | proc(proc_cls, &frag->header); |
150 | GNUNET_free(frag); | 150 | GNUNET_free(frag); |
diff --git a/src/fragmentation/test_fragmentation_enhanced.c b/src/fragmentation/test_fragmentation_enhanced.c new file mode 100644 index 000000000..b763e2cad --- /dev/null +++ b/src/fragmentation/test_fragmentation_enhanced.c | |||
@@ -0,0 +1,89 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file fragmentation/test_fragmentation_enhanced.c | ||
23 | * @brief testcase for the fragmentation.c | ||
24 | * @author Ji Lu | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_protocols.h" | ||
29 | #include "gnunet_fragmentation_lib.h" | ||
30 | |||
31 | struct combine{ | ||
32 | struct GNUNET_FRAGMENT_Context* ctx; | ||
33 | struct GNUNET_PeerIdentity* sender; | ||
34 | }; | ||
35 | |||
36 | void message_proc1(void *cls, const struct GNUNET_MessageHeader * msg){ | ||
37 | |||
38 | fprintf(stderr, "enter into message_proc1\n"); | ||
39 | |||
40 | struct GNUNET_MessageHeader * originalMsg = (struct GNUNET_MessageHeader *)cls; | ||
41 | |||
42 | if(ntohs(originalMsg->size) != ntohs(msg->size)){ | ||
43 | fprintf(stderr, "the received message has the different size with the sent one!\n"); | ||
44 | } | ||
45 | if(ntohs(originalMsg->type) != ntohs(msg->type)){ | ||
46 | fprintf(stderr, "the received message has the different type with the sent one!\n"); | ||
47 | } | ||
48 | if(memcmp(msg, originalMsg, originalMsg->size)){ | ||
49 | fprintf(stderr, "the received message is not the sent one!\n"); | ||
50 | } | ||
51 | else{ | ||
52 | fprintf(stdout, "You got the right message!\n"); | ||
53 | } | ||
54 | |||
55 | } | ||
56 | |||
57 | void message_proc2(void *cls, const struct GNUNET_MessageHeader * msg){ | ||
58 | |||
59 | printf("enter into message_proc2\n"); | ||
60 | |||
61 | struct combine * com2 = (struct combine* )cls; | ||
62 | GNUNET_FRAGMENT_process(com2->ctx, com2->sender, msg); | ||
63 | |||
64 | } | ||
65 | |||
66 | int | ||
67 | main(int argc, char * argv[]){ | ||
68 | |||
69 | uint32_t mtu = 512; | ||
70 | struct GNUNET_FRAGMENT_Context * ctx; | ||
71 | struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *)GNUNET_malloc(sizeof(struct GNUNET_MessageHeader)+2*mtu); | ||
72 | ctx = GNUNET_FRAGMENT_context_create(NULL, message_proc1, msg); | ||
73 | msg->size = htons(sizeof(struct GNUNET_MessageHeader)+4*mtu); | ||
74 | msg->type = htons(GNUNET_MESSAGE_TYPE_HELLO); | ||
75 | struct GNUNET_PeerIdentity *sender; | ||
76 | sender = (struct GNUNET_PeerIdentity *)GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity)); | ||
77 | |||
78 | memset(sender, 9, sizeof(struct GNUNET_PeerIdentity)); | ||
79 | |||
80 | memset(&msg[1], 5, 2*mtu); | ||
81 | |||
82 | struct combine *com; | ||
83 | com = (struct combine *)GNUNET_malloc(sizeof(struct combine)); | ||
84 | com->ctx = ctx; | ||
85 | com->sender = sender; | ||
86 | GNUNET_FRAGMENT_fragment(msg, mtu, message_proc2, com); | ||
87 | GNUNET_free(msg); | ||
88 | return 0; | ||
89 | } | ||