PacketRecvBlocker.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file PacketRecvBlocker.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 <misc/debug.h>
  23. #include <flow/PacketRecvBlocker.h>
  24. static int output_handler_recv (PacketRecvBlocker *o, uint8_t *data, int *data_len)
  25. {
  26. ASSERT(!o->out_have)
  27. // remember packet
  28. o->out_have = 1;
  29. o->out = data;
  30. o->out_input_blocking = 0;
  31. return 0;
  32. }
  33. static void input_handler_done (PacketRecvBlocker *o, int data_len)
  34. {
  35. ASSERT(o->out_have)
  36. ASSERT(o->out_input_blocking)
  37. // have no output packet
  38. o->out_have = 0;
  39. // inform output we received something
  40. PacketRecvInterface_Done(&o->output, data_len);
  41. return;
  42. }
  43. void PacketRecvBlocker_Init (PacketRecvBlocker *o, PacketRecvInterface *input)
  44. {
  45. // init arguments
  46. o->input = input;
  47. // init dead var
  48. DEAD_INIT(o->dead);
  49. // init output
  50. PacketRecvInterface_Init(&o->output, PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o);
  51. // have no output packet
  52. o->out_have = 0;
  53. // init input
  54. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  55. // init debug object
  56. DebugObject_Init(&o->d_obj);
  57. }
  58. void PacketRecvBlocker_Free (PacketRecvBlocker *o)
  59. {
  60. // free debug object
  61. DebugObject_Free(&o->d_obj);
  62. // free output
  63. PacketRecvInterface_Free(&o->output);
  64. // free dead var
  65. DEAD_KILL(o->dead);
  66. }
  67. PacketRecvInterface * PacketRecvBlocker_GetOutput (PacketRecvBlocker *o)
  68. {
  69. return &o->output;
  70. }
  71. void PacketRecvBlocker_AllowBlockedPacket (PacketRecvBlocker *o)
  72. {
  73. ASSERT(!PacketRecvInterface_InClient(o->input))
  74. if (!o->out_have || o->out_input_blocking) {
  75. return;
  76. }
  77. // receive from input
  78. int in_len;
  79. DEAD_ENTER(o->dead)
  80. int res = PacketRecvInterface_Receiver_Recv(o->input, o->out, &in_len);
  81. if (DEAD_LEAVE(o->dead)) {
  82. return;
  83. }
  84. ASSERT(res == 0 || res == 1)
  85. if (!res) {
  86. // input blocking, continue in input_handler_done
  87. o->out_input_blocking = 1;
  88. return;
  89. }
  90. // have no output packet
  91. o->out_have = 0;
  92. // inform output we received something
  93. PacketRecvInterface_Done(&o->output, in_len);
  94. return;
  95. }