PacketPassInterface.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @file PacketPassInterface.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Interface allowing a packet sender to pass data packets to a packet receiver.
  32. */
  33. #ifndef BADVPN_FLOW_PACKETPASSINTERFACE_H
  34. #define BADVPN_FLOW_PACKETPASSINTERFACE_H
  35. #include <stdint.h>
  36. #include <stddef.h>
  37. #include <misc/debug.h>
  38. #include <base/DebugObject.h>
  39. #include <base/BPending.h>
  40. #define PPI_STATE_NONE 1
  41. #define PPI_STATE_OPERATION_PENDING 2
  42. #define PPI_STATE_BUSY 3
  43. #define PPI_STATE_DONE_PENDING 4
  44. typedef void (*PacketPassInterface_handler_send) (void *user, uint8_t *data, int data_len);
  45. typedef void (*PacketPassInterface_handler_requestcancel) (void *user);
  46. typedef void (*PacketPassInterface_handler_done) (void *user);
  47. typedef struct {
  48. // provider data
  49. int mtu;
  50. PacketPassInterface_handler_send handler_operation;
  51. PacketPassInterface_handler_requestcancel handler_requestcancel;
  52. void *user_provider;
  53. // user data
  54. PacketPassInterface_handler_done handler_done;
  55. void *user_user;
  56. // operation job
  57. BPending job_operation;
  58. uint8_t *job_operation_data;
  59. int job_operation_len;
  60. // requestcancel job
  61. BPending job_requestcancel;
  62. // done job
  63. BPending job_done;
  64. // state
  65. int state;
  66. int cancel_requested;
  67. DebugObject d_obj;
  68. } PacketPassInterface;
  69. static void PacketPassInterface_Init (PacketPassInterface *i, int mtu, PacketPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg);
  70. static void PacketPassInterface_Free (PacketPassInterface *i);
  71. static void PacketPassInterface_EnableCancel (PacketPassInterface *i, PacketPassInterface_handler_requestcancel handler_requestcancel);
  72. static void PacketPassInterface_Done (PacketPassInterface *i);
  73. static int PacketPassInterface_GetMTU (PacketPassInterface *i);
  74. static void PacketPassInterface_Sender_Init (PacketPassInterface *i, PacketPassInterface_handler_done handler_done, void *user);
  75. static void PacketPassInterface_Sender_Send (PacketPassInterface *i, uint8_t *data, int data_len);
  76. static void PacketPassInterface_Sender_RequestCancel (PacketPassInterface *i);
  77. static int PacketPassInterface_HasCancel (PacketPassInterface *i);
  78. void _PacketPassInterface_job_operation (PacketPassInterface *i);
  79. void _PacketPassInterface_job_requestcancel (PacketPassInterface *i);
  80. void _PacketPassInterface_job_done (PacketPassInterface *i);
  81. void PacketPassInterface_Init (PacketPassInterface *i, int mtu, PacketPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg)
  82. {
  83. ASSERT(mtu >= 0)
  84. // init arguments
  85. i->mtu = mtu;
  86. i->handler_operation = handler_operation;
  87. i->handler_requestcancel = NULL;
  88. i->user_provider = user;
  89. // set no user
  90. i->handler_done = NULL;
  91. // init jobs
  92. BPending_Init(&i->job_operation, pg, (BPending_handler)_PacketPassInterface_job_operation, i);
  93. BPending_Init(&i->job_requestcancel, pg, (BPending_handler)_PacketPassInterface_job_requestcancel, i);
  94. BPending_Init(&i->job_done, pg, (BPending_handler)_PacketPassInterface_job_done, i);
  95. // set state
  96. i->state = PPI_STATE_NONE;
  97. DebugObject_Init(&i->d_obj);
  98. }
  99. void PacketPassInterface_Free (PacketPassInterface *i)
  100. {
  101. DebugObject_Free(&i->d_obj);
  102. // free jobs
  103. BPending_Free(&i->job_done);
  104. BPending_Free(&i->job_requestcancel);
  105. BPending_Free(&i->job_operation);
  106. }
  107. void PacketPassInterface_EnableCancel (PacketPassInterface *i, PacketPassInterface_handler_requestcancel handler_requestcancel)
  108. {
  109. ASSERT(!i->handler_requestcancel)
  110. ASSERT(!i->handler_done)
  111. ASSERT(handler_requestcancel)
  112. i->handler_requestcancel = handler_requestcancel;
  113. }
  114. void PacketPassInterface_Done (PacketPassInterface *i)
  115. {
  116. ASSERT(i->state == PPI_STATE_BUSY)
  117. DebugObject_Access(&i->d_obj);
  118. // unset requestcancel job
  119. BPending_Unset(&i->job_requestcancel);
  120. // schedule done
  121. BPending_Set(&i->job_done);
  122. // set state
  123. i->state = PPI_STATE_DONE_PENDING;
  124. }
  125. int PacketPassInterface_GetMTU (PacketPassInterface *i)
  126. {
  127. DebugObject_Access(&i->d_obj);
  128. return i->mtu;
  129. }
  130. void PacketPassInterface_Sender_Init (PacketPassInterface *i, PacketPassInterface_handler_done handler_done, void *user)
  131. {
  132. ASSERT(handler_done)
  133. ASSERT(!i->handler_done)
  134. DebugObject_Access(&i->d_obj);
  135. i->handler_done = handler_done;
  136. i->user_user = user;
  137. }
  138. void PacketPassInterface_Sender_Send (PacketPassInterface *i, uint8_t *data, int data_len)
  139. {
  140. ASSERT(data_len >= 0)
  141. ASSERT(data_len <= i->mtu)
  142. ASSERT(!(data_len > 0) || data)
  143. ASSERT(i->state == PPI_STATE_NONE)
  144. ASSERT(i->handler_done)
  145. DebugObject_Access(&i->d_obj);
  146. // schedule operation
  147. i->job_operation_data = data;
  148. i->job_operation_len = data_len;
  149. BPending_Set(&i->job_operation);
  150. // set state
  151. i->state = PPI_STATE_OPERATION_PENDING;
  152. i->cancel_requested = 0;
  153. }
  154. void PacketPassInterface_Sender_RequestCancel (PacketPassInterface *i)
  155. {
  156. ASSERT(i->state == PPI_STATE_OPERATION_PENDING || i->state == PPI_STATE_BUSY || i->state == PPI_STATE_DONE_PENDING)
  157. ASSERT(i->handler_requestcancel)
  158. DebugObject_Access(&i->d_obj);
  159. // ignore multiple cancel requests
  160. if (i->cancel_requested) {
  161. return;
  162. }
  163. // remember we requested cancel
  164. i->cancel_requested = 1;
  165. if (i->state == PPI_STATE_OPERATION_PENDING) {
  166. // unset operation job
  167. BPending_Unset(&i->job_operation);
  168. // set done job
  169. BPending_Set(&i->job_done);
  170. // set state
  171. i->state = PPI_STATE_DONE_PENDING;
  172. } else if (i->state == PPI_STATE_BUSY) {
  173. // set requestcancel job
  174. BPending_Set(&i->job_requestcancel);
  175. }
  176. }
  177. int PacketPassInterface_HasCancel (PacketPassInterface *i)
  178. {
  179. DebugObject_Access(&i->d_obj);
  180. return !!i->handler_requestcancel;
  181. }
  182. #endif