PacketPassConnector.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file PacketPassConnector.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/PacketPassConnector.h>
  25. static void input_handler_send (PacketPassConnector *o, uint8_t *data, int data_len)
  26. {
  27. ASSERT(data_len >= 0)
  28. ASSERT(data_len <= o->input_mtu)
  29. ASSERT(o->in_len == -1)
  30. if (o->output) {
  31. ASSERT(!o->out_blocking)
  32. }
  33. DebugObject_Access(&o->d_obj);
  34. // remember input packet
  35. o->in_len = data_len;
  36. o->in = data;
  37. if (o->output) {
  38. // schedule send
  39. PacketPassInterface_Sender_Send(o->output, o->in, o->in_len);
  40. o->out_blocking = 1;
  41. }
  42. }
  43. static void output_handler_done (PacketPassConnector *o)
  44. {
  45. ASSERT(o->in_len >= 0)
  46. ASSERT(o->output)
  47. ASSERT(o->out_blocking)
  48. DebugObject_Access(&o->d_obj);
  49. // output not blocking any more
  50. o->out_blocking = 0;
  51. // have no input packet
  52. o->in_len = -1;
  53. // allow input to send more packets
  54. PacketPassInterface_Done(&o->input);
  55. }
  56. void PacketPassConnector_Init (PacketPassConnector *o, int mtu, BPendingGroup *pg)
  57. {
  58. ASSERT(mtu >= 0)
  59. // init arguments
  60. o->input_mtu = mtu;
  61. // init input
  62. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  63. // have no input packet
  64. o->in_len = -1;
  65. // have no output
  66. o->output = NULL;
  67. DebugObject_Init(&o->d_obj);
  68. }
  69. void PacketPassConnector_Free (PacketPassConnector *o)
  70. {
  71. DebugObject_Free(&o->d_obj);
  72. // free input
  73. PacketPassInterface_Free(&o->input);
  74. }
  75. PacketPassInterface * PacketPassConnector_GetInput (PacketPassConnector *o)
  76. {
  77. DebugObject_Access(&o->d_obj);
  78. return &o->input;
  79. }
  80. void PacketPassConnector_ConnectOutput (PacketPassConnector *o, PacketPassInterface *output)
  81. {
  82. ASSERT(!o->output)
  83. ASSERT(PacketPassInterface_GetMTU(output) >= o->input_mtu)
  84. DebugObject_Access(&o->d_obj);
  85. // set output
  86. o->output = output;
  87. // init output
  88. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  89. // set output not blocking
  90. o->out_blocking = 0;
  91. // if we have an input packet, schedule send
  92. if (o->in_len >= 0) {
  93. PacketPassInterface_Sender_Send(o->output, o->in, o->in_len);
  94. o->out_blocking = 1;
  95. }
  96. }
  97. void PacketPassConnector_DisconnectOutput (PacketPassConnector *o)
  98. {
  99. ASSERT(o->output)
  100. DebugObject_Access(&o->d_obj);
  101. // set no output
  102. o->output = NULL;
  103. }