PacketRecvBlocker.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 void output_handler_recv (PacketRecvBlocker *o, uint8_t *data)
  25. {
  26. ASSERT(!o->out_have)
  27. DebugObject_Access(&o->d_obj);
  28. // remember packet
  29. o->out_have = 1;
  30. o->out = data;
  31. o->out_input_blocking = 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. DebugObject_Access(&o->d_obj);
  38. // schedule done
  39. o->out_have = 0;
  40. PacketRecvInterface_Done(&o->output, data_len);
  41. }
  42. void PacketRecvBlocker_Init (PacketRecvBlocker *o, PacketRecvInterface *input, BPendingGroup *pg)
  43. {
  44. // init arguments
  45. o->input = input;
  46. // init output
  47. PacketRecvInterface_Init(&o->output, PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  48. // have no output packet
  49. o->out_have = 0;
  50. // init input
  51. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  52. DebugObject_Init(&o->d_obj);
  53. }
  54. void PacketRecvBlocker_Free (PacketRecvBlocker *o)
  55. {
  56. DebugObject_Free(&o->d_obj);
  57. // free output
  58. PacketRecvInterface_Free(&o->output);
  59. }
  60. PacketRecvInterface * PacketRecvBlocker_GetOutput (PacketRecvBlocker *o)
  61. {
  62. DebugObject_Access(&o->d_obj);
  63. return &o->output;
  64. }
  65. void PacketRecvBlocker_AllowBlockedPacket (PacketRecvBlocker *o)
  66. {
  67. DebugObject_Access(&o->d_obj);
  68. if (!o->out_have || o->out_input_blocking) {
  69. return;
  70. }
  71. // schedule receive
  72. o->out_input_blocking = 1;
  73. PacketRecvInterface_Receiver_Recv(o->input, o->out);
  74. }