PacketCopier.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_requestcancel (PacketCopier *o)
  43. {
  44. ASSERT(o->in_len >= 0)
  45. ASSERT(!o->out_have)
  46. DebugObject_Access(&o->d_obj);
  47. // finish input packet
  48. PacketPassInterface_Done(&o->input);
  49. o->in_len = -1;
  50. }
  51. static void output_handler_recv (PacketCopier *o, uint8_t *data)
  52. {
  53. ASSERT(!o->out_have)
  54. DebugObject_Access(&o->d_obj);
  55. if (o->in_len < 0) {
  56. o->out_have = 1;
  57. o->out = data;
  58. return;
  59. }
  60. memcpy(data, o->in, o->in_len);
  61. // finish input packet
  62. PacketPassInterface_Done(&o->input);
  63. // finish output packet
  64. PacketRecvInterface_Done(&o->output, o->in_len);
  65. o->in_len = -1;
  66. }
  67. void PacketCopier_Init (PacketCopier *o, int mtu, BPendingGroup *pg)
  68. {
  69. ASSERT(mtu >= 0)
  70. // init input
  71. PacketPassInterface_Init(&o->input, mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  72. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  73. // init output
  74. PacketRecvInterface_Init(&o->output, mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  75. // set no input packet
  76. o->in_len = -1;
  77. // set no output packet
  78. o->out_have = 0;
  79. DebugObject_Init(&o->d_obj);
  80. }
  81. void PacketCopier_Free (PacketCopier *o)
  82. {
  83. DebugObject_Free(&o->d_obj);
  84. // free output
  85. PacketRecvInterface_Free(&o->output);
  86. // free input
  87. PacketPassInterface_Free(&o->input);
  88. }
  89. PacketPassInterface * PacketCopier_GetInput (PacketCopier *o)
  90. {
  91. DebugObject_Access(&o->d_obj);
  92. return &o->input;
  93. }
  94. PacketRecvInterface * PacketCopier_GetOutput (PacketCopier *o)
  95. {
  96. DebugObject_Access(&o->d_obj);
  97. return &o->output;
  98. }