PacketCopier.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file PacketCopier.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 <string.h>
  23. #include <misc/debug.h>
  24. #include <flow/PacketCopier.h>
  25. static void input_handler_send (PacketCopier *o, uint8_t *data, int data_len)
  26. {
  27. ASSERT(o->in_len == -1)
  28. ASSERT(data_len >= 0)
  29. DebugObject_Access(&o->d_obj);
  30. if (!o->out_have) {
  31. o->in_len = data_len;
  32. o->in = data;
  33. return;
  34. }
  35. memcpy(o->out, data, data_len);
  36. // finish input packet
  37. PacketPassInterface_Done(&o->input);
  38. // finish output packet
  39. PacketRecvInterface_Done(&o->output, data_len);
  40. o->out_have = 0;
  41. }
  42. static void input_handler_cancel (PacketCopier *o)
  43. {
  44. ASSERT(o->in_len >= 0)
  45. ASSERT(!o->out_have)
  46. DebugObject_Access(&o->d_obj);
  47. o->in_len = -1;
  48. }
  49. static void output_handler_recv (PacketCopier *o, uint8_t *data)
  50. {
  51. ASSERT(!o->out_have)
  52. DebugObject_Access(&o->d_obj);
  53. if (o->in_len < 0) {
  54. o->out_have = 1;
  55. o->out = data;
  56. return;
  57. }
  58. memcpy(data, o->in, o->in_len);
  59. // finish input packet
  60. PacketPassInterface_Done(&o->input);
  61. // finish output packet
  62. PacketRecvInterface_Done(&o->output, o->in_len);
  63. o->in_len = -1;
  64. }
  65. void PacketCopier_Init (PacketCopier *o, int mtu, BPendingGroup *pg)
  66. {
  67. ASSERT(mtu >= 0)
  68. // init input
  69. PacketPassInterface_Init(&o->input, mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  70. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  71. // init output
  72. PacketRecvInterface_Init(&o->output, mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  73. // set no input packet
  74. o->in_len = -1;
  75. // set no output packet
  76. o->out_have = 0;
  77. DebugObject_Init(&o->d_obj);
  78. }
  79. void PacketCopier_Free (PacketCopier *o)
  80. {
  81. DebugObject_Free(&o->d_obj);
  82. // free output
  83. PacketRecvInterface_Free(&o->output);
  84. // free input
  85. PacketPassInterface_Free(&o->input);
  86. }
  87. PacketPassInterface * PacketCopier_GetInput (PacketCopier *o)
  88. {
  89. DebugObject_Access(&o->d_obj);
  90. return &o->input;
  91. }
  92. PacketRecvInterface * PacketCopier_GetOutput (PacketCopier *o)
  93. {
  94. DebugObject_Access(&o->d_obj);
  95. return &o->output;
  96. }