d8280ec47419a50f4bc597f3050f1bcb19f9540d
[oweals/gnunet.git] / src / include / gnunet_fragmentation_lib.h
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 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
53  * created by the fragmentation module.
54  *
55  * @param cls closure
56  * @param msg the message that was created
57  */
58 typedef void (*GNUNET_FRAGMENT_MessageProcessor) (void *cls,
59                                                   const struct GNUNET_MessageHeader *msg);
60
61
62 /**
63  * Create a fragmentation context for the given message.
64  * Fragments the message into fragments of size "mtu" or
65  * less.  Calls 'proc' on each un-acknowledged fragment,
66  * using both the expected 'delay' between messages and
67  * acknowledgements and the given 'tracker' to guide the
68  * frequency of calls to 'proc'.
69  *
70  * @param stats statistics context
71  * @param mtu the maximum message size for each fragment
72  * @param tracker bandwidth tracker to use for flow control (can be NULL)
73  * @param delay expected delay between fragment transmission
74  *              and ACK based on previous messages
75  * @param msg the message to fragment
76  * @param proc function to call for each fragment to transmit
77  * @param proc_cls closure for proc
78  * @return the fragmentation context
79  */
80 struct GNUNET_FRAGMENT_Context *
81 GNUNET_FRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
82                                 uint16_t mtu,
83                                 struct GNUNET_BANDWIDTH_Tracker *tracker,
84                                 struct GNUNET_TIME_Relative delay,
85                                 const struct GNUNET_MessageHeader *msg,
86                                 GNUNET_FRAGMENT_MessageProcessor proc,
87                                 void *proc_cls);
88
89
90 /**
91  * Process an acknowledgement message we got from the other
92  * side (to control re-transmits).
93  *
94  * @param fc fragmentation context
95  * @param msg acknowledgement message we received
96  * @return GNUNET_OK if this ack completes the work of the 'fc'
97  *                   (all fragments have been received);
98  *         GNUNET_NO if more messages are pending
99  *         GNUNET_SYSERR if this ack is not valid for this fc
100  */
101 int GNUNET_FRAGMENT_process_ack (struct GNUNET_FRAGMENT_Context *fc,
102                                  const struct GNUNET_MessageHeader *msg);
103
104
105 /**
106  * Destroy the given fragmentation context (stop calling 'proc', free
107  * resources).
108  *
109  * @param fc fragmentation context
110  * @return average delay between transmission and ACK for the
111  *         last message, FOREVER if the message was not fully transmitted
112  */
113 struct GNUNET_TIME_Relative
114 GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *fc);
115
116
117 /**
118  * Defragmentation context (one per connection).
119  */
120 struct GNUNET_DEFRAGMENT_Context;
121
122
123 /**
124  * Create a defragmentation context.
125  *
126  * @param stats statistics context
127  * @param mtu the maximum message size for each fragment 
128  * @param num_msgs how many fragmented messages
129  *                 to we defragment at most at the same time?
130  * @param cls closure for proc and ackp
131  * @param proc function to call with defragmented messages
132  * @param ackp function to call with acknowledgements (to send
133  *             back to the other side)
134  * @return the defragmentation context
135  */
136 struct GNUNET_DEFRAGMENT_Context *
137 GNUNET_DEFRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
138                                   uint16_t mtu,
139                                   unsigned int num_msgs,
140                                   void *cls,
141                                   GNUNET_FRAGMENT_MessageProcessor proc,
142                                   GNUNET_FRAGMENT_MessageProcessor ackp);
143
144
145 /**
146  * Destroy the given defragmentation context.
147  *
148  * @param dc defragmentation context
149  */
150 void
151 GNUNET_DEFRAGMENT_context_destroy (struct GNUNET_DEFRAGMENT_Context *dc);
152
153
154 /**
155  * We have received a fragment.  Process it.
156  *
157  * @param dc the context
158  * @param msg the message that was received
159  */
160 void
161 GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
162                                     const struct GNUNET_MessageHeader *msg);
163
164
165 #if 0                           /* keep Emacsens' auto-indent happy */
166 {
167 #endif
168 #ifdef __cplusplus
169 }
170 #endif
171
172 /* end of gnunet_fragmentation_lib.h */
173 #endif