PacketPassConnector.c 3.0 KB

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