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