PacketStreamSender.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file PacketStreamSender.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/PacketStreamSender.h>
  25. static int send_data (PacketStreamSender *s)
  26. {
  27. ASSERT(s->in_len >= 0)
  28. while (s->in_used < s->in_len) {
  29. // attempt to send something
  30. DEAD_ENTER(s->dead)
  31. int res = StreamPassInterface_Sender_Send(s->output, s->in + s->in_used, s->in_len - s->in_used);
  32. if (DEAD_LEAVE(s->dead)) {
  33. return -1;
  34. }
  35. ASSERT(res >= 0)
  36. ASSERT(res <= s->in_len - s->in_used)
  37. if (res == 0) {
  38. // output busy, continue in output_handler_done
  39. return 0;
  40. }
  41. // update number of bytes sent
  42. s->in_used += res;
  43. }
  44. // everything sent
  45. s->in_len = -1;
  46. return 0;
  47. }
  48. static int input_handler_send (PacketStreamSender *s, uint8_t *data, int data_len)
  49. {
  50. ASSERT(s->in_len == -1)
  51. ASSERT(data_len >= 0)
  52. // set input packet
  53. s->in_len = data_len;
  54. s->in = data;
  55. s->in_used = 0;
  56. // try sending
  57. if (send_data(s) < 0) {
  58. return -1;
  59. }
  60. // if we couldn't send everything, block input
  61. if (s->in_len >= 0) {
  62. return 0;
  63. }
  64. return 1;
  65. }
  66. static void output_handler_done (PacketStreamSender *s, int data_len)
  67. {
  68. ASSERT(s->in_len >= 0)
  69. ASSERT(data_len > 0)
  70. ASSERT(data_len <= s->in_len - s->in_used)
  71. // update number of bytes sent
  72. s->in_used += data_len;
  73. // continue sending
  74. if (send_data(s) < 0) {
  75. return;
  76. }
  77. // if we couldn't send everything, keep input blocked
  78. if (s->in_len >= 0) {
  79. return;
  80. }
  81. // allow more input
  82. PacketPassInterface_Done(&s->input);
  83. return;
  84. }
  85. void PacketStreamSender_Init (PacketStreamSender *s, StreamPassInterface *output, int mtu)
  86. {
  87. ASSERT(mtu >= 0)
  88. // init arguments
  89. s->output = output;
  90. // init dead var
  91. DEAD_INIT(s->dead);
  92. // init input
  93. PacketPassInterface_Init(&s->input, mtu, (PacketPassInterface_handler_send)input_handler_send, s);
  94. // init output
  95. StreamPassInterface_Sender_Init(s->output, (StreamPassInterface_handler_done)output_handler_done, s);
  96. // have no input packet
  97. s->in_len = -1;
  98. // init debug object
  99. DebugObject_Init(&s->d_obj);
  100. }
  101. void PacketStreamSender_Free (PacketStreamSender *s)
  102. {
  103. // free debug object
  104. DebugObject_Free(&s->d_obj);
  105. // free input
  106. PacketPassInterface_Free(&s->input);
  107. // free dead var
  108. DEAD_KILL(s->dead);
  109. }
  110. PacketPassInterface * PacketStreamSender_GetInput (PacketStreamSender *s)
  111. {
  112. return &s->input;
  113. }