BestEffortPacketWriteInterface.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @file BestEffortPacketWriteInterface.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Interface which allows a sender to write packets to a buffer provided by the receiver
  25. * in a best-effort fashion.
  26. */
  27. #ifndef BADVPN_FLOW_BESTEFFORTPACKETWRITEINTERFACE
  28. #define BADVPN_FLOW_BESTEFFORTPACKETWRITEINTERFACE
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <misc/debug.h>
  32. #include <misc/dead.h>
  33. #include <system/DebugObject.h>
  34. /**
  35. * Callback function invoked at the receiver when the sender requests a memory location
  36. * for writing a packet.
  37. * The interface was not in writing state.
  38. *
  39. * @param user value given to {@link BestEffortPacketWriteInterface_Init}
  40. * @param data if a memory location was provided, it must be returned here. It must have
  41. * MTU bytes available. It may be set to NULL if MTU is 0.
  42. * @param return 1 - a memory location was provided. The interface will enter writing state.
  43. * 0 - a memory location was not provided
  44. */
  45. typedef int (*BestEffortPacketWriteInterface_handler_startpacket) (void *user, uint8_t **data);
  46. /**
  47. * Callback function invoked at the receiver when the sender has finished writing a packet.
  48. * The interface was in writing state before.
  49. * The interface enters not writing state after this function returns.
  50. *
  51. * @param user value given to {@link BestEffortPacketWriteInterface_Init}
  52. * @param len length of the packet written. Will be >=0 and <=MTU.
  53. */
  54. typedef void (*BestEffortPacketWriteInterface_handler_endpacket) (void *user, int len);
  55. /**
  56. * Interface which allows a sender to write packets to a buffer provided by the receiver
  57. * in a best-effort fashion.
  58. */
  59. typedef struct {
  60. DebugObject d_obj;
  61. int mtu;
  62. BestEffortPacketWriteInterface_handler_startpacket handler_startpacket;
  63. BestEffortPacketWriteInterface_handler_endpacket handler_endpacket;
  64. void *user;
  65. #ifndef NDEBUG
  66. int sending;
  67. int in_call;
  68. dead_t dead;
  69. #endif
  70. } BestEffortPacketWriteInterface;
  71. /**
  72. * Initializes the interface.
  73. * The interface is initialized in not writing state.
  74. *
  75. * @param i the object
  76. * @param mtu maximum packet size. Must be >=0.
  77. * @param handler_startpacket callback function invoked at the receiver when
  78. * the sender wants a memory location for writing a packet
  79. * @param handler_endpacket callback function invoked at the receiver when the sender
  80. * has finished writing a packet
  81. * @param user value passed to receiver callback functions
  82. */
  83. static void BestEffortPacketWriteInterface_Init (
  84. BestEffortPacketWriteInterface *i,
  85. int mtu,
  86. BestEffortPacketWriteInterface_handler_startpacket handler_startpacket,
  87. BestEffortPacketWriteInterface_handler_endpacket handler_endpacket,
  88. void *user
  89. );
  90. /**
  91. * Frees the interface.
  92. *
  93. * @param i the object
  94. */
  95. static void BestEffortPacketWriteInterface_Free (BestEffortPacketWriteInterface *i);
  96. /**
  97. * Requests a memory location for writing a packet to the receiver.
  98. * The interface must be in not writing state.
  99. *
  100. * @param i the object
  101. * @param data if the function returns 1, will be set to the memory location where the
  102. * * packet should be written. May be set to NULL if MTU is 0.
  103. * @param return - 1 a memory location was provided. The interface enters writing state.
  104. * - 0 a memory location was not provided
  105. */
  106. static int BestEffortPacketWriteInterface_Sender_StartPacket (BestEffortPacketWriteInterface *i, uint8_t **data);
  107. /**
  108. * Sumbits a packet written to the memory location provided by the receiver.
  109. * The interface must be in writing state.
  110. * The interface enters not writing state.
  111. *
  112. * @param i the object
  113. * @param len length of the packet written. Must be >=0 and <=MTU.
  114. */
  115. static void BestEffortPacketWriteInterface_Sender_EndPacket (BestEffortPacketWriteInterface *i, int len);
  116. void BestEffortPacketWriteInterface_Init (
  117. BestEffortPacketWriteInterface *i,
  118. int mtu,
  119. BestEffortPacketWriteInterface_handler_startpacket handler_startpacket,
  120. BestEffortPacketWriteInterface_handler_endpacket handler_endpacket,
  121. void *user
  122. )
  123. {
  124. ASSERT(mtu >= 0)
  125. // init arguments
  126. i->mtu = mtu;
  127. i->handler_startpacket = handler_startpacket;
  128. i->handler_endpacket = handler_endpacket;
  129. i->user = user;
  130. // init debug object
  131. DebugObject_Init(&i->d_obj);
  132. // init debugging
  133. #ifndef NDEBUG
  134. i->sending = 0;
  135. i->in_call = 0;
  136. DEAD_INIT(i->dead);
  137. #endif
  138. }
  139. void BestEffortPacketWriteInterface_Free (BestEffortPacketWriteInterface *i)
  140. {
  141. // free debug object
  142. DebugObject_Free(&i->d_obj);
  143. // free debugging
  144. #ifndef NDEBUG
  145. DEAD_KILL(i->dead);
  146. #endif
  147. }
  148. int BestEffortPacketWriteInterface_Sender_StartPacket (BestEffortPacketWriteInterface *i, uint8_t **data)
  149. {
  150. ASSERT(!i->sending)
  151. ASSERT(!i->in_call)
  152. #ifndef NDEBUG
  153. i->in_call = 1;
  154. DEAD_ENTER(i->dead)
  155. #endif
  156. int res = i->handler_startpacket(i->user, data);
  157. #ifndef NDEBUG
  158. if (DEAD_LEAVE(i->dead)) {
  159. return -1;
  160. }
  161. ASSERT(res == 0 || res == 1)
  162. i->in_call = 0;
  163. if (res) {
  164. i->sending = 1;
  165. }
  166. #endif
  167. return res;
  168. }
  169. void BestEffortPacketWriteInterface_Sender_EndPacket (BestEffortPacketWriteInterface *i, int len)
  170. {
  171. ASSERT(len >= 0)
  172. ASSERT(len <= i->mtu)
  173. ASSERT(i->sending)
  174. ASSERT(!i->in_call)
  175. #ifndef NDEBUG
  176. i->in_call = 1;
  177. DEAD_ENTER(i->dead)
  178. #endif
  179. i->handler_endpacket(i->user, len);
  180. #ifndef NDEBUG
  181. if (DEAD_LEAVE(i->dead)) {
  182. return;
  183. }
  184. i->in_call = 0;
  185. i->sending = 0;
  186. #endif
  187. }
  188. #endif