PacketCopier.c 3.0 KB

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