gnunet_fragmentation_lib.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2009, 2011, 2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Christian Grothoff
  18. *
  19. * @file
  20. * Library to help fragment messages
  21. *
  22. * @defgroup fragmentation Fragmentation library
  23. * Library to help fragment messages
  24. * @{
  25. *
  26. * @todo Consider additional flow-control for sending from
  27. * fragmentation based on continuations.
  28. */
  29. #ifndef GNUNET_FRAGMENTATION_LIB_H
  30. #define GNUNET_FRAGMENTATION_LIB_H
  31. #include "gnunet_util_lib.h"
  32. #include "gnunet_bandwidth_lib.h"
  33. #include "gnunet_statistics_service.h"
  34. #ifdef __cplusplus
  35. extern "C"
  36. {
  37. #if 0 /* keep Emacsens' auto-indent happy */
  38. }
  39. #endif
  40. #endif
  41. /**
  42. * Fragmentation context.
  43. */
  44. struct GNUNET_FRAGMENT_Context;
  45. /**
  46. * Function that is called with messages created by the fragmentation
  47. * module. In the case of the 'proc' callback of the
  48. * #GNUNET_FRAGMENT_context_create() function, this function must
  49. * eventually call #GNUNET_FRAGMENT_context_transmission_done().
  50. *
  51. * @param cls closure
  52. * @param msg the message that was created
  53. */
  54. typedef void
  55. (*GNUNET_FRAGMENT_MessageProcessor) (void *cls,
  56. const struct GNUNET_MessageHeader *msg);
  57. /**
  58. * Create a fragmentation context for the given message.
  59. * Fragments the message into fragments of size @a mtu or
  60. * less. Calls @a proc on each un-acknowledged fragment,
  61. * using both the expected @a msg_delay between messages and
  62. * acknowledgements and the given @a tracker to guide the
  63. * frequency of calls to @a proc.
  64. *
  65. * @param stats statistics context
  66. * @param mtu the maximum message size for each fragment
  67. * @param tracker bandwidth tracker to use for flow control (can be NULL)
  68. * @param msg_delay initial delay to insert between fragment transmissions
  69. * based on previous messages
  70. * @param ack_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 msg_delay,
  82. struct GNUNET_TIME_Relative ack_delay,
  83. const struct GNUNET_MessageHeader *msg,
  84. GNUNET_FRAGMENT_MessageProcessor proc,
  85. void *proc_cls);
  86. /**
  87. * Continuation to call from the 'proc' function after the fragment
  88. * has been transmitted (and hence the next fragment can now be
  89. * given to proc).
  90. *
  91. * @param fc fragmentation context
  92. */
  93. void
  94. GNUNET_FRAGMENT_context_transmission_done (struct GNUNET_FRAGMENT_Context *fc);
  95. /**
  96. * Process an acknowledgement message we got from the other
  97. * side (to control re-transmits).
  98. *
  99. * @param fc fragmentation context
  100. * @param msg acknowledgement message we received
  101. * @return #GNUNET_OK if this ack completes the work of the 'fc'
  102. * (all fragments have been received);
  103. * #GNUNET_NO if more messages are pending
  104. * #GNUNET_SYSERR if this ack is not valid for this fc
  105. */
  106. int
  107. GNUNET_FRAGMENT_process_ack (struct GNUNET_FRAGMENT_Context *fc,
  108. const struct GNUNET_MessageHeader *msg);
  109. /**
  110. * Destroy the given fragmentation context (stop calling 'proc', free
  111. * resources).
  112. *
  113. * @param fc fragmentation context
  114. * @param msg_delay where to store average delay between individual message transmissions the
  115. * last message (OUT only)
  116. * @param ack_delay where to store average delay between transmission and ACK for the
  117. * last message, set to FOREVER if the message was not fully transmitted (OUT only)
  118. */
  119. void
  120. GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *fc,
  121. struct GNUNET_TIME_Relative *msg_delay,
  122. struct GNUNET_TIME_Relative *ack_delay);
  123. /**
  124. * Convert an ACK message to a printable format suitable for logging.
  125. *
  126. * @param ack message to print
  127. * @return ack in human-readable format
  128. */
  129. const char *
  130. GNUNET_FRAGMENT_print_ack (const struct GNUNET_MessageHeader *ack);
  131. /**
  132. * Defragmentation context (one per connection).
  133. */
  134. struct GNUNET_DEFRAGMENT_Context;
  135. /**
  136. * Function that is called with acknowledgement messages created by
  137. * the fragmentation module. Acknowledgements are cummulative,
  138. * so it is OK to only transmit the 'latest' ack message for the same
  139. * message ID.
  140. *
  141. * @param cls closure
  142. * @param id unique message ID (modulo collisions)
  143. * @param msg the message that was created
  144. */
  145. typedef void
  146. (*GNUNET_DEFRAGMENT_AckProcessor) (void *cls,
  147. uint32_t id,
  148. const struct GNUNET_MessageHeader *msg);
  149. /**
  150. * Create a defragmentation context.
  151. *
  152. * @param stats statistics context
  153. * @param mtu the maximum message size for each fragment
  154. * @param num_msgs how many fragmented messages
  155. * to we defragment at most at the same time?
  156. * @param cls closure for @a proc and @a ackp
  157. * @param proc function to call with defragmented messages
  158. * @param ackp function to call with acknowledgements (to send
  159. * back to the other side)
  160. * @return the defragmentation context
  161. */
  162. struct GNUNET_DEFRAGMENT_Context *
  163. GNUNET_DEFRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
  164. uint16_t mtu,
  165. unsigned int num_msgs,
  166. void *cls,
  167. GNUNET_FRAGMENT_MessageProcessor proc,
  168. GNUNET_DEFRAGMENT_AckProcessor ackp);
  169. /**
  170. * Destroy the given defragmentation context.
  171. *
  172. * @param dc defragmentation context
  173. */
  174. void
  175. GNUNET_DEFRAGMENT_context_destroy (struct GNUNET_DEFRAGMENT_Context *dc);
  176. /**
  177. * We have received a fragment. Process it.
  178. *
  179. * @param dc the context
  180. * @param msg the message that was received
  181. * @return #GNUNET_OK on success,
  182. * #GNUNET_NO if this was a duplicate,
  183. * #GNUNET_SYSERR on error
  184. */
  185. int
  186. GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
  187. const struct GNUNET_MessageHeader *msg);
  188. #if 0 /* keep Emacsens' auto-indent happy */
  189. {
  190. #endif
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif
  195. /** @} */ /* end of group */