PacketPassInterface.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @file PacketPassInterface.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 allowing a packet sender to pass data packets to a packet receiver.
  25. */
  26. #ifndef BADVPN_FLOW_PACKETPASSINTERFACE_H
  27. #define BADVPN_FLOW_PACKETPASSINTERFACE_H
  28. #include <stdint.h>
  29. #include <stddef.h>
  30. #include <misc/dead.h>
  31. #include <misc/debug.h>
  32. #include <misc/debugin.h>
  33. #include <system/DebugObject.h>
  34. #include <system/BPending.h>
  35. typedef void (*PacketPassInterface_handler_send) (void *user, uint8_t *data, int data_len);
  36. typedef void (*PacketPassInterface_handler_cancel) (void *user);
  37. typedef void (*PacketPassInterface_handler_done) (void *user);
  38. typedef struct {
  39. // provider data
  40. int mtu;
  41. PacketPassInterface_handler_send handler_operation;
  42. PacketPassInterface_handler_cancel handler_cancel;
  43. void *user_provider;
  44. // user data
  45. PacketPassInterface_handler_done handler_done;
  46. void *user_user;
  47. // jobs
  48. BPending job_operation;
  49. BPending job_done;
  50. // packet supplied by user
  51. uint8_t *buf;
  52. int buf_len;
  53. DebugObject d_obj;
  54. #ifndef NDEBUG
  55. DebugIn d_in_operation;
  56. DebugIn d_in_cancel;
  57. DebugIn d_in_done;
  58. dead_t d_dead;
  59. int d_user_busy;
  60. #endif
  61. } PacketPassInterface;
  62. static void PacketPassInterface_Init (PacketPassInterface *i, int mtu, PacketPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg);
  63. static void PacketPassInterface_Free (PacketPassInterface *i);
  64. static void PacketPassInterface_EnableCancel (PacketPassInterface *i, PacketPassInterface_handler_cancel handler_cancel);
  65. static void PacketPassInterface_Done (PacketPassInterface *i);
  66. static int PacketPassInterface_GetMTU (PacketPassInterface *i);
  67. static void PacketPassInterface_Sender_Init (PacketPassInterface *i, PacketPassInterface_handler_done handler_done, void *user);
  68. static void PacketPassInterface_Sender_Send (PacketPassInterface *i, uint8_t *data, int data_len);
  69. static void PacketPassInterface_Sender_Cancel (PacketPassInterface *i);
  70. static int PacketPassInterface_HasCancel (PacketPassInterface *i);
  71. void _PacketPassInterface_job_operation (PacketPassInterface *i);
  72. void _PacketPassInterface_job_done (PacketPassInterface *i);
  73. void PacketPassInterface_Init (PacketPassInterface *i, int mtu, PacketPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg)
  74. {
  75. ASSERT(mtu >= 0)
  76. // init arguments
  77. i->mtu = mtu;
  78. i->handler_operation = handler_operation;
  79. i->handler_cancel = NULL;
  80. i->user_provider = user;
  81. // set no user
  82. i->handler_done = NULL;
  83. // init jobs
  84. BPending_Init(&i->job_operation, pg, (BPending_handler)_PacketPassInterface_job_operation, i);
  85. BPending_Init(&i->job_done, pg, (BPending_handler)_PacketPassInterface_job_done, i);
  86. DebugObject_Init(&i->d_obj);
  87. #ifndef NDEBUG
  88. DebugIn_Init(&i->d_in_operation);
  89. DebugIn_Init(&i->d_in_cancel);
  90. DebugIn_Init(&i->d_in_done);
  91. DEAD_INIT(i->d_dead);
  92. i->d_user_busy = 0;
  93. #endif
  94. }
  95. void PacketPassInterface_Free (PacketPassInterface *i)
  96. {
  97. #ifndef NDEBUG
  98. DEAD_KILL(i->d_dead);
  99. #endif
  100. DebugObject_Free(&i->d_obj);
  101. // free jobs
  102. BPending_Free(&i->job_done);
  103. BPending_Free(&i->job_operation);
  104. }
  105. void PacketPassInterface_EnableCancel (PacketPassInterface *i, PacketPassInterface_handler_cancel handler_cancel)
  106. {
  107. ASSERT(!i->handler_cancel)
  108. ASSERT(!i->handler_done)
  109. ASSERT(handler_cancel)
  110. i->handler_cancel = handler_cancel;
  111. }
  112. void PacketPassInterface_Done (PacketPassInterface *i)
  113. {
  114. ASSERT(i->d_user_busy)
  115. ASSERT(i->buf_len >= 0)
  116. ASSERT(i->buf_len <= i->mtu)
  117. ASSERT(i->handler_done)
  118. ASSERT(!BPending_IsSet(&i->job_operation))
  119. DebugObject_Access(&i->d_obj);
  120. #ifndef NDEBUG
  121. DebugIn_AmOut(&i->d_in_cancel);
  122. DebugIn_AmOut(&i->d_in_done);
  123. #endif
  124. BPending_Set(&i->job_done);
  125. }
  126. int PacketPassInterface_GetMTU (PacketPassInterface *i)
  127. {
  128. DebugObject_Access(&i->d_obj);
  129. return i->mtu;
  130. }
  131. void PacketPassInterface_Sender_Init (PacketPassInterface *i, PacketPassInterface_handler_done handler_done, void *user)
  132. {
  133. ASSERT(handler_done)
  134. ASSERT(!i->handler_done)
  135. DebugObject_Access(&i->d_obj);
  136. i->handler_done = handler_done;
  137. i->user_user = user;
  138. }
  139. void PacketPassInterface_Sender_Send (PacketPassInterface *i, uint8_t *data, int data_len)
  140. {
  141. ASSERT(data_len >= 0)
  142. ASSERT(data_len <= i->mtu)
  143. ASSERT(!(data_len > 0) || data)
  144. ASSERT(!i->d_user_busy)
  145. ASSERT(i->handler_done)
  146. DebugObject_Access(&i->d_obj);
  147. #ifndef NDEBUG
  148. DebugIn_AmOut(&i->d_in_operation);
  149. DebugIn_AmOut(&i->d_in_cancel);
  150. #endif
  151. i->buf = data;
  152. i->buf_len = data_len;
  153. #ifndef NDEBUG
  154. i->d_user_busy = 1;
  155. #endif
  156. BPending_Set(&i->job_operation);
  157. }
  158. void PacketPassInterface_Sender_Cancel (PacketPassInterface *i)
  159. {
  160. ASSERT(i->d_user_busy)
  161. ASSERT(i->buf_len >= 0)
  162. ASSERT(i->buf_len <= i->mtu)
  163. ASSERT(i->handler_cancel)
  164. ASSERT(i->handler_done)
  165. DebugObject_Access(&i->d_obj);
  166. #ifndef NDEBUG
  167. DebugIn_AmOut(&i->d_in_operation);
  168. DebugIn_AmOut(&i->d_in_cancel);
  169. #endif
  170. BPending_Unset(&i->job_operation);
  171. BPending_Unset(&i->job_done);
  172. #ifndef NDEBUG
  173. i->d_user_busy = 0;
  174. #endif
  175. if (!BPending_IsSet(&i->job_operation) && !BPending_IsSet(&i->job_done)) {
  176. #ifndef NDEBUG
  177. DebugIn_GoIn(&i->d_in_cancel);
  178. DEAD_ENTER(i->d_dead)
  179. #endif
  180. i->handler_cancel(i->user_provider);
  181. #ifndef NDEBUG
  182. ASSERT(!DEAD_KILLED)
  183. DEAD_LEAVE(i->d_dead);
  184. DebugIn_GoOut(&i->d_in_cancel);
  185. #endif
  186. }
  187. }
  188. int PacketPassInterface_HasCancel (PacketPassInterface *i)
  189. {
  190. DebugObject_Access(&i->d_obj);
  191. return !!i->handler_cancel;
  192. }
  193. #endif