enable caching private->public key mapping in memory to improve CPU consumption for...
[oweals/gnunet.git] / src / include / gnunet_fragmentation_lib.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2009, 2011, 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @author Christian Grothoff
22  *
23  * @file
24  * Library to help fragment messages
25  *
26  * @defgroup fragmentation  Fragmentation library
27  * Library to help fragment messages
28  * @{
29  *
30  * @todo Consider additional flow-control for sending from
31  *       fragmentation based on continuations.
32  */
33
34 #ifndef GNUNET_FRAGMENTATION_LIB_H
35 #define GNUNET_FRAGMENTATION_LIB_H
36
37 #include "gnunet_util_lib.h"
38 #include "gnunet_bandwidth_lib.h"
39 #include "gnunet_statistics_service.h"
40
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #if 0                           /* keep Emacsens' auto-indent happy */
45 }
46 #endif
47 #endif
48
49
50 /**
51  * Fragmentation context.
52  */
53 struct GNUNET_FRAGMENT_Context;
54
55
56 /**
57  * Function that is called with messages created by the fragmentation
58  * module.  In the case of the 'proc' callback of the
59  * #GNUNET_FRAGMENT_context_create() function, this function must
60  * eventually call #GNUNET_FRAGMENT_context_transmission_done().
61  *
62  * @param cls closure
63  * @param msg the message that was created
64  */
65 typedef void
66 (*GNUNET_FRAGMENT_MessageProcessor) (void *cls,
67                                      const struct GNUNET_MessageHeader *msg);
68
69
70 /**
71  * Create a fragmentation context for the given message.
72  * Fragments the message into fragments of size @a mtu or
73  * less.  Calls @a proc on each un-acknowledged fragment,
74  * using both the expected @a msg_delay between messages and
75  * acknowledgements and the given @a tracker to guide the
76  * frequency of calls to @a proc.
77  *
78  * @param stats statistics context
79  * @param mtu the maximum message size for each fragment
80  * @param tracker bandwidth tracker to use for flow control (can be NULL)
81  * @param msg_delay initial delay to insert between fragment transmissions
82  *              based on previous messages
83  * @param ack_delay expected delay between fragment transmission
84  *              and ACK based on previous messages
85  * @param msg the message to fragment
86  * @param proc function to call for each fragment to transmit
87  * @param proc_cls closure for proc
88  * @return the fragmentation context
89  */
90 struct GNUNET_FRAGMENT_Context *
91 GNUNET_FRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
92                                 uint16_t mtu,
93                                 struct GNUNET_BANDWIDTH_Tracker *tracker,
94                                 struct GNUNET_TIME_Relative msg_delay,
95                                 struct GNUNET_TIME_Relative ack_delay,
96                                 const struct GNUNET_MessageHeader *msg,
97                                 GNUNET_FRAGMENT_MessageProcessor proc,
98                                 void *proc_cls);
99
100
101 /**
102  * Continuation to call from the 'proc' function after the fragment
103  * has been transmitted (and hence the next fragment can now be
104  * given to proc).
105  *
106  * @param fc fragmentation context
107  */
108 void
109 GNUNET_FRAGMENT_context_transmission_done (struct GNUNET_FRAGMENT_Context *fc);
110
111
112 /**
113  * Process an acknowledgement message we got from the other
114  * side (to control re-transmits).
115  *
116  * @param fc fragmentation context
117  * @param msg acknowledgement message we received
118  * @return #GNUNET_OK if this ack completes the work of the 'fc'
119  *                   (all fragments have been received);
120  *         #GNUNET_NO if more messages are pending
121  *         #GNUNET_SYSERR if this ack is not valid for this fc
122  */
123 int
124 GNUNET_FRAGMENT_process_ack (struct GNUNET_FRAGMENT_Context *fc,
125                              const struct GNUNET_MessageHeader *msg);
126
127
128 /**
129  * Destroy the given fragmentation context (stop calling 'proc', free
130  * resources).
131  *
132  * @param fc fragmentation context
133  * @param msg_delay where to store average delay between individual message transmissions the
134  *         last message (OUT only)
135  * @param ack_delay where to store average delay between transmission and ACK for the
136  *         last message, set to FOREVER if the message was not fully transmitted (OUT only)
137  */
138 void
139 GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *fc,
140                                  struct GNUNET_TIME_Relative *msg_delay,
141                                  struct GNUNET_TIME_Relative *ack_delay);
142
143
144 /**
145  * Convert an ACK message to a printable format suitable for logging.
146  *
147  * @param ack message to print
148  * @return ack in human-readable format
149  */
150 const char *
151 GNUNET_FRAGMENT_print_ack (const struct GNUNET_MessageHeader *ack);
152
153
154 /**
155  * Defragmentation context (one per connection).
156  */
157 struct GNUNET_DEFRAGMENT_Context;
158
159
160 /**
161  * Function that is called with acknowledgement messages created by
162  * the fragmentation module.  Acknowledgements are cummulative,
163  * so it is OK to only transmit the 'latest' ack message for the same
164  * message ID.
165  *
166  * @param cls closure
167  * @param id unique message ID (modulo collisions)
168  * @param msg the message that was created
169  */
170 typedef void
171 (*GNUNET_DEFRAGMENT_AckProcessor) (void *cls,
172                                    uint32_t id,
173                                    const struct GNUNET_MessageHeader *msg);
174
175
176 /**
177  * Create a defragmentation context.
178  *
179  * @param stats statistics context
180  * @param mtu the maximum message size for each fragment
181  * @param num_msgs how many fragmented messages
182  *                 to we defragment at most at the same time?
183  * @param cls closure for @a proc and @a ackp
184  * @param proc function to call with defragmented messages
185  * @param ackp function to call with acknowledgements (to send
186  *             back to the other side)
187  * @return the defragmentation context
188  */
189 struct GNUNET_DEFRAGMENT_Context *
190 GNUNET_DEFRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
191                                   uint16_t mtu,
192                                   unsigned int num_msgs,
193                                   void *cls,
194                                   GNUNET_FRAGMENT_MessageProcessor proc,
195                                   GNUNET_DEFRAGMENT_AckProcessor ackp);
196
197
198 /**
199  * Destroy the given defragmentation context.
200  *
201  * @param dc defragmentation context
202  */
203 void
204 GNUNET_DEFRAGMENT_context_destroy (struct GNUNET_DEFRAGMENT_Context *dc);
205
206
207 /**
208  * We have received a fragment.  Process it.
209  *
210  * @param dc the context
211  * @param msg the message that was received
212  * @return #GNUNET_OK on success,
213  *         #GNUNET_NO if this was a duplicate,
214  *         #GNUNET_SYSERR on error
215  */
216 int
217 GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
218                                     const struct GNUNET_MessageHeader *msg);
219
220
221
222 #if 0                           /* keep Emacsens' auto-indent happy */
223 {
224 #endif
225 #ifdef __cplusplus
226 }
227 #endif
228
229 #endif
230
231 /** @} */  /* end of group */