diff options
Diffstat (limited to 'src/fragmentation/fragmentation_new.c')
-rw-r--r-- | src/fragmentation/fragmentation_new.c | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/src/fragmentation/fragmentation_new.c b/src/fragmentation/fragmentation_new.c new file mode 100644 index 000000000..a95afc4a4 --- /dev/null +++ b/src/fragmentation/fragmentation_new.c | |||
@@ -0,0 +1,195 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2009, 2011 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 3, 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 | * @file src/fragmentation/fragmentation_new.c | ||
22 | * @brief library to help fragment messages | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | |||
26 | #include "platform.h" | ||
27 | #include "gnunet_fragmentation_lib.h" | ||
28 | #include "fragmentation.h" | ||
29 | |||
30 | /** | ||
31 | * Fragmentation context. | ||
32 | */ | ||
33 | struct GNUNET_FRAGMENT_Context | ||
34 | { | ||
35 | /** | ||
36 | * Statistics to use. | ||
37 | */ | ||
38 | struct GNUNET_STATISTICS_Handle *stats; | ||
39 | |||
40 | /** | ||
41 | * Tracker for flow control. | ||
42 | */ | ||
43 | struct GNUNET_BANDWIDTH_Tracker *tracker; | ||
44 | |||
45 | /** | ||
46 | * Current expected delay for ACKs. | ||
47 | */ | ||
48 | struct GNUNET_TIME_Relative delay; | ||
49 | |||
50 | /** | ||
51 | * Message to fragment (allocated at the end of this struct). | ||
52 | */ | ||
53 | const struct GNUNET_MessageHeader *msg; | ||
54 | |||
55 | /** | ||
56 | * Function to call for transmissions. | ||
57 | */ | ||
58 | GNUNET_FRAGMENT_MessageProcessor proc; | ||
59 | |||
60 | /** | ||
61 | * Closure for 'proc'. | ||
62 | */ | ||
63 | void *proc_cls; | ||
64 | |||
65 | /** | ||
66 | * Bitfield, set to 1 for each unacknowledged fragment. | ||
67 | */ | ||
68 | uint64_t acks; | ||
69 | |||
70 | /** | ||
71 | * Task performing work for the fragmenter. | ||
72 | */ | ||
73 | GNUNET_SCHEDULER_TaskIdentifier task; | ||
74 | |||
75 | /** | ||
76 | * Target fragment size. | ||
77 | */ | ||
78 | uint16_t mtu; | ||
79 | |||
80 | }; | ||
81 | |||
82 | |||
83 | /** | ||
84 | * Transmit the next fragment to the other peer. | ||
85 | * | ||
86 | * @param cls the 'struct GNUNET_FRAGMENT_Context' | ||
87 | * @param tc scheduler context | ||
88 | */ | ||
89 | static void | ||
90 | transmit_next (void *cls, | ||
91 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
92 | { | ||
93 | struct GNUNET_FRAGMENT_Context *fc = cls; | ||
94 | |||
95 | fc->task = GNUNET_SCHEDULER_NO_TASK; | ||
96 | } | ||
97 | |||
98 | |||
99 | /** | ||
100 | * Create a fragmentation context for the given message. | ||
101 | * Fragments the message into fragments of size "mtu" or | ||
102 | * less. Calls 'proc' on each un-acknowledged fragment, | ||
103 | * using both the expected 'delay' between messages and | ||
104 | * acknowledgements and the given 'tracker' to guide the | ||
105 | * frequency of calls to 'proc'. | ||
106 | * | ||
107 | * @param stats statistics context | ||
108 | * @param mtu the maximum message size for each fragment | ||
109 | * @param tracker bandwidth tracker to use for flow control (can be NULL) | ||
110 | * @param delay expected delay between fragment transmission | ||
111 | * and ACK based on previous messages | ||
112 | * @param msg the message to fragment | ||
113 | * @param proc function to call for each fragment to transmit | ||
114 | * @param proc_cls closure for proc | ||
115 | * @return the fragmentation context | ||
116 | */ | ||
117 | struct GNUNET_FRAGMENT_Context * | ||
118 | GNUNET_FRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats, | ||
119 | uint16_t mtu, | ||
120 | struct GNUNET_BANDWIDTH_Tracker *tracker, | ||
121 | struct GNUNET_TIME_Relative delay, | ||
122 | const struct GNUNET_MessageHeader *msg, | ||
123 | GNUNET_FRAGMENT_MessageProcessor proc, | ||
124 | void *proc_cls) | ||
125 | { | ||
126 | struct GNUNET_FRAGMENT_Context *fc; | ||
127 | size_t size; | ||
128 | uint64_t bits; | ||
129 | |||
130 | GNUNET_assert (mtu >= 1024 + sizeof (struct FragmentHeader)); | ||
131 | size = ntohs (msg->size); | ||
132 | GNUNET_assert (size > mtu); | ||
133 | fc = GNUNET_malloc (sizeof (struct GNUNET_FRAGMENT_Context) + size); | ||
134 | fc->stats = stats; | ||
135 | fc->mtu = mtu; | ||
136 | fc->tracker = tracker; | ||
137 | fc->delay = delay; | ||
138 | fc->msg = (const struct GNUNET_MessageHeader*)&fc[1]; | ||
139 | fc->proc = proc; | ||
140 | fc->proc_cls = proc_cls; | ||
141 | memcpy (&fc[1], msg, size); | ||
142 | bits = (size + mtu - 1) / (mtu - sizeof (struct FragmentHeader)); | ||
143 | GNUNET_assert (bits <= 64); | ||
144 | if (bits == 64) | ||
145 | fc->acks = UINT64_MAX; /* set all 64 bit */ | ||
146 | else | ||
147 | fc->acks = (1 << bits) - 1; /* set lowest 'bits' bit */ | ||
148 | fc->task = GNUNET_SCHEDULER_add_delayed (GNUNET_BANDWIDTH_tracker_get_delay (tracker, mtu), | ||
149 | &transmit_next, | ||
150 | fc); | ||
151 | return fc; | ||
152 | } | ||
153 | |||
154 | |||
155 | /** | ||
156 | * Process an acknowledgement message we got from the other | ||
157 | * side (to control re-transmits). | ||
158 | * | ||
159 | * @param fc fragmentation context | ||
160 | * @param msg acknowledgement message we received | ||
161 | * @return GNUNET_OK if this ack completes the work of the 'fc' | ||
162 | * (all fragments have been received); | ||
163 | * GNUNET_NO if more messages are pending | ||
164 | * GNUNET_SYSERR if this ack is not valid for this fc | ||
165 | */ | ||
166 | int | ||
167 | GNUNET_FRAGMENT_process_ack (struct GNUNET_FRAGMENT_Context *fc, | ||
168 | const struct GNUNET_MessageHeader *msg) | ||
169 | { | ||
170 | return GNUNET_SYSERR; | ||
171 | } | ||
172 | |||
173 | |||
174 | /** | ||
175 | * Destroy the given fragmentation context (stop calling 'proc', free | ||
176 | * resources). | ||
177 | * | ||
178 | * @param fc fragmentation context | ||
179 | * @return average delay between transmission and ACK for the | ||
180 | * last message, FOREVER if the message was not fully transmitted | ||
181 | */ | ||
182 | struct GNUNET_TIME_Relative | ||
183 | GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *fc) | ||
184 | { | ||
185 | struct GNUNET_TIME_Relative ret; | ||
186 | |||
187 | if (fc->task != GNUNET_SCHEDULER_NO_TASK) | ||
188 | GNUNET_SCHEDULER_cancel (fc->task); | ||
189 | ret = fc->delay; | ||
190 | GNUNET_free (fc); | ||
191 | return ret; | ||
192 | } | ||
193 | |||
194 | /* end of fragmentation_new.c */ | ||
195 | |||