PacketRecvConnector.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @file PacketRecvConnector.c
  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. #include <stddef.h>
  23. #include <misc/debug.h>
  24. #include <flow/PacketRecvConnector.h>
  25. static int output_handler_recv (PacketRecvConnector *o, uint8_t *data, int *data_len)
  26. {
  27. ASSERT(!o->out_have)
  28. ASSERT(!o->input || !o->in_blocking)
  29. // if we have no input, remember output packet
  30. if (!o->input) {
  31. o->out_have = 1;
  32. o->out = data;
  33. return 0;
  34. }
  35. // try to receive the packet
  36. int res;
  37. while (1) {
  38. DEAD_ENTER_N(obj, o->dead)
  39. DEAD_ENTER_N(inp, o->input_dead)
  40. res = PacketRecvInterface_Receiver_Recv(o->input, data, data_len);
  41. DEAD_LEAVE_N(obj, o->dead);
  42. DEAD_LEAVE_N(inp, o->input_dead);
  43. if (DEAD_KILLED_N(obj)) {
  44. return -1;
  45. }
  46. if (DEAD_KILLED_N(inp)) {
  47. if (!o->input) {
  48. // lost input
  49. o->out_have = 1;
  50. o->out = data;
  51. return 0;
  52. }
  53. // got a new input, retry
  54. continue;
  55. }
  56. break;
  57. };
  58. ASSERT(res == 0 || res == 1)
  59. if (res) {
  60. ASSERT(*data_len >= 0)
  61. ASSERT(*data_len <= o->output_mtu)
  62. }
  63. if (!res) {
  64. // input blocking
  65. o->out_have = 1;
  66. o->out = data;
  67. o->in_blocking = 1;
  68. return 0;
  69. }
  70. return 1;
  71. }
  72. static void input_handler_done (PacketRecvConnector *o, int data_len)
  73. {
  74. ASSERT(o->out_have)
  75. ASSERT(o->input)
  76. ASSERT(o->in_blocking)
  77. // have no output packet
  78. o->out_have = 0;
  79. // input not blocking any more
  80. o->in_blocking = 0;
  81. // allow output to receive more packets
  82. PacketRecvInterface_Done(&o->output, data_len);
  83. return;
  84. }
  85. static void job_handler (PacketRecvConnector *o)
  86. {
  87. ASSERT(o->input)
  88. ASSERT(!o->in_blocking)
  89. ASSERT(o->out_have)
  90. // try to receive the packet
  91. int in_len;
  92. DEAD_ENTER_N(obj, o->dead)
  93. DEAD_ENTER_N(inp, o->input_dead)
  94. int res = PacketRecvInterface_Receiver_Recv(o->input, o->out, &in_len);
  95. DEAD_LEAVE_N(obj, o->dead);
  96. DEAD_LEAVE_N(inp, o->input_dead);
  97. if (DEAD_KILLED_N(obj)) {
  98. return;
  99. }
  100. if (DEAD_KILLED_N(inp)) {
  101. // lost current input. Do nothing here.
  102. // If we gained a new one, its own job is responsible for it.
  103. return;
  104. }
  105. ASSERT(res == 0 || res == 1)
  106. if (res) {
  107. ASSERT(in_len >= 0)
  108. ASSERT(in_len <= o->output_mtu)
  109. }
  110. if (!res) {
  111. // input blocking
  112. o->in_blocking = 1;
  113. return;
  114. }
  115. // have no output packet
  116. o->out_have = 0;
  117. // allow output to receive more packets
  118. PacketRecvInterface_Done(&o->output, in_len);
  119. return;
  120. }
  121. void PacketRecvConnector_Init (PacketRecvConnector *o, int mtu, BPendingGroup *pg)
  122. {
  123. ASSERT(mtu >= 0)
  124. // init arguments
  125. o->output_mtu = mtu;
  126. // init dead var
  127. DEAD_INIT(o->dead);
  128. // init output
  129. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o);
  130. // have no output packet
  131. o->out_have = 0;
  132. // have no input
  133. o->input = NULL;
  134. // init continue job
  135. BPending_Init(&o->continue_job, pg, (BPending_handler)job_handler, o);
  136. // init debug object
  137. DebugObject_Init(&o->d_obj);
  138. }
  139. void PacketRecvConnector_Free (PacketRecvConnector *o)
  140. {
  141. // free debug object
  142. DebugObject_Free(&o->d_obj);
  143. // free continue job
  144. BPending_Free(&o->continue_job);
  145. // free input dead var
  146. if (o->input) {
  147. DEAD_KILL(o->input_dead);
  148. }
  149. // free output
  150. PacketRecvInterface_Free(&o->output);
  151. // free dead var
  152. DEAD_KILL(o->dead);
  153. }
  154. PacketRecvInterface * PacketRecvConnector_GetOutput (PacketRecvConnector *o)
  155. {
  156. return &o->output;
  157. }
  158. void PacketRecvConnector_ConnectInput (PacketRecvConnector *o, PacketRecvInterface *input)
  159. {
  160. ASSERT(!o->input)
  161. ASSERT(PacketRecvInterface_GetMTU(input) <= o->output_mtu)
  162. // set input
  163. o->input = input;
  164. // init input
  165. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  166. // init input dead var
  167. DEAD_INIT(o->input_dead);
  168. // set input not blocking
  169. o->in_blocking = 0;
  170. // if we have an input packet, set continue job
  171. if (o->out_have) {
  172. BPending_Set(&o->continue_job);
  173. }
  174. }
  175. void PacketRecvConnector_DisconnectInput (PacketRecvConnector *o)
  176. {
  177. ASSERT(o->input)
  178. // unset continue job (in case it wasn't called yet)
  179. BPending_Unset(&o->continue_job);
  180. // free dead var
  181. DEAD_KILL(o->input_dead);
  182. // set no input
  183. o->input = NULL;
  184. }