SinglePacketBuffer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file SinglePacketBuffer.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 <stdlib.h>
  23. #include <misc/debug.h>
  24. #include <flow/SinglePacketBuffer.h>
  25. static int io_loop (SinglePacketBuffer *o)
  26. {
  27. DEAD_DECLARE
  28. int res;
  29. while (1) {
  30. // receive packet
  31. int in_len;
  32. DEAD_ENTER2(o->dead)
  33. res = PacketRecvInterface_Receiver_Recv(o->input, o->buf, &in_len);
  34. if (DEAD_LEAVE(o->dead)) {
  35. return -1;
  36. }
  37. ASSERT(res == 0 || res == 1)
  38. if (!res) {
  39. // input blocking, continue in input_handler_done
  40. return 0;
  41. }
  42. // send packet
  43. DEAD_ENTER2(o->dead)
  44. res = PacketPassInterface_Sender_Send(o->output, o->buf, in_len);
  45. if (DEAD_LEAVE(o->dead)) {
  46. return -1;
  47. }
  48. ASSERT(res == 0 || res == 1)
  49. if (!res) {
  50. // output blocking, continue in output_handler_done
  51. return 0;
  52. }
  53. }
  54. }
  55. static void input_handler_done (SinglePacketBuffer *o, int in_len)
  56. {
  57. // send packet
  58. DEAD_ENTER(o->dead)
  59. int res = PacketPassInterface_Sender_Send(o->output, o->buf, in_len);
  60. if (DEAD_LEAVE(o->dead)) {
  61. return;
  62. }
  63. ASSERT(res == 0 || res == 1)
  64. if (!res) {
  65. // output blocking, continue in output_handler_done
  66. return;
  67. }
  68. io_loop(o);
  69. return;
  70. }
  71. static void output_handler_done (SinglePacketBuffer *o)
  72. {
  73. io_loop(o);
  74. return;
  75. }
  76. static void job_handler (SinglePacketBuffer *o)
  77. {
  78. io_loop(o);
  79. return;
  80. }
  81. int SinglePacketBuffer_Init (SinglePacketBuffer *o, PacketRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg)
  82. {
  83. ASSERT(PacketPassInterface_GetMTU(output) >= PacketRecvInterface_GetMTU(input))
  84. // init arguments
  85. o->input = input;
  86. o->output = output;
  87. // init dead var
  88. DEAD_INIT(o->dead);
  89. // init input
  90. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  91. // init output
  92. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  93. // init buffer
  94. if (!(o->buf = malloc(PacketRecvInterface_GetMTU(o->input)))) {
  95. goto fail1;
  96. }
  97. // init start job
  98. BPending_Init(&o->start_job, pg, (BPending_handler)job_handler, o);
  99. BPending_Set(&o->start_job);
  100. // init debug object
  101. DebugObject_Init(&o->d_obj);
  102. return 1;
  103. fail1:
  104. return 0;
  105. }
  106. void SinglePacketBuffer_Free (SinglePacketBuffer *o)
  107. {
  108. // free debug object
  109. DebugObject_Free(&o->d_obj);
  110. // free start job
  111. BPending_Free(&o->start_job);
  112. // free buffer
  113. free(o->buf);
  114. // free dead var
  115. DEAD_KILL(o->dead);
  116. }