PacketPassInterface.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #ifndef NDEBUG
  72. static int PacketPassInterface_InClient (PacketPassInterface *i);
  73. static int PacketPassInterface_InDone (PacketPassInterface *i);
  74. #endif
  75. void _PacketPassInterface_job_operation (PacketPassInterface *i);
  76. void _PacketPassInterface_job_done (PacketPassInterface *i);
  77. void PacketPassInterface_Init (PacketPassInterface *i, int mtu, PacketPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg)
  78. {
  79. ASSERT(mtu >= 0)
  80. // init arguments
  81. i->mtu = mtu;
  82. i->handler_operation = handler_operation;
  83. i->handler_cancel = NULL;
  84. i->user_provider = user;
  85. // set no user
  86. i->handler_done = NULL;
  87. // init jobs
  88. BPending_Init(&i->job_operation, pg, (BPending_handler)_PacketPassInterface_job_operation, i);
  89. BPending_Init(&i->job_done, pg, (BPending_handler)_PacketPassInterface_job_done, i);
  90. DebugObject_Init(&i->d_obj);
  91. #ifndef NDEBUG
  92. DebugIn_Init(&i->d_in_operation);
  93. DebugIn_Init(&i->d_in_cancel);
  94. DebugIn_Init(&i->d_in_done);
  95. DEAD_INIT(i->d_dead);
  96. i->d_user_busy = 0;
  97. #endif
  98. }
  99. void PacketPassInterface_Free (PacketPassInterface *i)
  100. {
  101. #ifndef NDEBUG
  102. DEAD_KILL(i->d_dead);
  103. #endif
  104. DebugObject_Free(&i->d_obj);
  105. // free jobs
  106. BPending_Free(&i->job_done);
  107. BPending_Free(&i->job_operation);
  108. }
  109. void PacketPassInterface_EnableCancel (PacketPassInterface *i, PacketPassInterface_handler_cancel handler_cancel)
  110. {
  111. ASSERT(!i->handler_cancel)
  112. ASSERT(!i->handler_done)
  113. ASSERT(handler_cancel)
  114. i->handler_cancel = handler_cancel;
  115. }
  116. void PacketPassInterface_Done (PacketPassInterface *i)
  117. {
  118. ASSERT(i->d_user_busy)
  119. ASSERT(i->buf_len >= 0)
  120. ASSERT(i->buf_len <= i->mtu)
  121. ASSERT(i->handler_done)
  122. ASSERT(!BPending_IsSet(&i->job_operation))
  123. DebugObject_Access(&i->d_obj);
  124. #ifndef NDEBUG
  125. DebugIn_AmOut(&i->d_in_cancel);
  126. DebugIn_AmOut(&i->d_in_done);
  127. #endif
  128. BPending_Set(&i->job_done);
  129. }
  130. int PacketPassInterface_GetMTU (PacketPassInterface *i)
  131. {
  132. DebugObject_Access(&i->d_obj);
  133. return i->mtu;
  134. }
  135. void PacketPassInterface_Sender_Init (PacketPassInterface *i, PacketPassInterface_handler_done handler_done, void *user)
  136. {
  137. ASSERT(handler_done)
  138. ASSERT(!i->handler_done)
  139. DebugObject_Access(&i->d_obj);
  140. i->handler_done = handler_done;
  141. i->user_user = user;
  142. }
  143. void PacketPassInterface_Sender_Send (PacketPassInterface *i, uint8_t *data, int data_len)
  144. {
  145. ASSERT(data_len >= 0)
  146. ASSERT(data_len <= i->mtu)
  147. ASSERT(!(data_len > 0) || data)
  148. ASSERT(!i->d_user_busy)
  149. ASSERT(i->handler_done)
  150. DebugObject_Access(&i->d_obj);
  151. #ifndef NDEBUG
  152. DebugIn_AmOut(&i->d_in_operation);
  153. DebugIn_AmOut(&i->d_in_cancel);
  154. #endif
  155. i->buf = data;
  156. i->buf_len = data_len;
  157. #ifndef NDEBUG
  158. i->d_user_busy = 1;
  159. #endif
  160. BPending_Set(&i->job_operation);
  161. }
  162. void PacketPassInterface_Sender_Cancel (PacketPassInterface *i)
  163. {
  164. ASSERT(i->d_user_busy)
  165. ASSERT(i->buf_len >= 0)
  166. ASSERT(i->buf_len <= i->mtu)
  167. ASSERT(i->handler_cancel)
  168. ASSERT(i->handler_done)
  169. DebugObject_Access(&i->d_obj);
  170. #ifndef NDEBUG
  171. DebugIn_AmOut(&i->d_in_operation);
  172. DebugIn_AmOut(&i->d_in_cancel);
  173. #endif
  174. BPending_Unset(&i->job_operation);
  175. BPending_Unset(&i->job_done);
  176. #ifndef NDEBUG
  177. i->d_user_busy = 0;
  178. #endif
  179. if (!BPending_IsSet(&i->job_operation) && !BPending_IsSet(&i->job_done)) {
  180. #ifndef NDEBUG
  181. DebugIn_GoIn(&i->d_in_cancel);
  182. DEAD_ENTER(i->d_dead)
  183. #endif
  184. i->handler_cancel(i->user_provider);
  185. #ifndef NDEBUG
  186. ASSERT(!DEAD_KILLED)
  187. DEAD_LEAVE(i->d_dead);
  188. DebugIn_GoOut(&i->d_in_cancel);
  189. #endif
  190. }
  191. }
  192. int PacketPassInterface_HasCancel (PacketPassInterface *i)
  193. {
  194. DebugObject_Access(&i->d_obj);
  195. return !!i->handler_cancel;
  196. }
  197. #ifndef NDEBUG
  198. int PacketPassInterface_InClient (PacketPassInterface *i)
  199. {
  200. DebugObject_Access(&i->d_obj);
  201. return DebugIn_In(&i->d_in_operation);
  202. }
  203. int PacketPassInterface_InDone (PacketPassInterface *i)
  204. {
  205. DebugObject_Access(&i->d_obj);
  206. return DebugIn_In(&i->d_in_done);
  207. }
  208. #endif
  209. #endif