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