PacketCopier.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 || o->out_got_len >= 0) {
  30. o->in_len = data_len;
  31. o->in = data;
  32. o->in_got = 0;
  33. return 0;
  34. }
  35. memcpy(o->out, data, data_len);
  36. o->out_got_len = data_len;
  37. BPending_Set(&o->continue_job_output);
  38. return 1;
  39. }
  40. static void input_handler_cancel (PacketCopier *o)
  41. {
  42. ASSERT(o->in_len >= 0)
  43. ASSERT(!o->out_have)
  44. o->in_len = -1;
  45. }
  46. static int output_handler_recv (PacketCopier *o, uint8_t *data, int *data_len)
  47. {
  48. ASSERT(!o->out_have)
  49. if (o->in_len < 0 || o->in_got) {
  50. o->out_have = 1;
  51. o->out = data;
  52. o->out_got_len = -1;
  53. return 0;
  54. }
  55. memcpy(data, o->in, o->in_len);
  56. o->in_got = 1;
  57. BPending_Set(&o->continue_job_input);
  58. *data_len = o->in_len;
  59. return 1;
  60. }
  61. static void input_job_handler (PacketCopier *o)
  62. {
  63. ASSERT(o->in_len >= 0)
  64. ASSERT(o->in_got)
  65. o->in_len = -1;
  66. PacketPassInterface_Done(&o->input);
  67. return;
  68. }
  69. static void output_job_handler (PacketCopier *o)
  70. {
  71. ASSERT(o->out_have)
  72. ASSERT(o->out_got_len >= 0)
  73. o->out_have = 0;
  74. PacketRecvInterface_Done(&o->output, o->out_got_len);
  75. return;
  76. }
  77. void PacketCopier_Init (PacketCopier *o, int mtu, BPendingGroup *pg)
  78. {
  79. ASSERT(mtu >= 0)
  80. // init dead var
  81. DEAD_INIT(o->dead);
  82. // init input
  83. PacketPassInterface_Init(&o->input, mtu, (PacketPassInterface_handler_send)input_handler_send, o);
  84. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  85. // init output
  86. PacketRecvInterface_Init(&o->output, mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o);
  87. // set no input packet
  88. o->in_len = -1;
  89. // set no output packet
  90. o->out_have = 0;
  91. // init continue jobs
  92. BPending_Init(&o->continue_job_input, pg, (BPending_handler)input_job_handler, o);
  93. BPending_Init(&o->continue_job_output, pg, (BPending_handler)output_job_handler, o);
  94. // init debug object
  95. DebugObject_Init(&o->d_obj);
  96. }
  97. void PacketCopier_Free (PacketCopier *o)
  98. {
  99. // free debug object
  100. DebugObject_Free(&o->d_obj);
  101. // free continue jobs
  102. BPending_Free(&o->continue_job_output);
  103. BPending_Free(&o->continue_job_input);
  104. // free output
  105. PacketRecvInterface_Free(&o->output);
  106. // free input
  107. PacketPassInterface_Free(&o->input);
  108. // free dead var
  109. DEAD_KILL(o->dead);
  110. }
  111. PacketPassInterface * PacketCopier_GetInput (PacketCopier *o)
  112. {
  113. return &o->input;
  114. }
  115. PacketRecvInterface * PacketCopier_GetOutput (PacketCopier *o)
  116. {
  117. return &o->output;
  118. }