PacketStreamSender.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 void send_data (PacketStreamSender *s)
  26. {
  27. ASSERT(s->in_len >= 0)
  28. if (s->in_used < s->in_len) {
  29. // send more data
  30. StreamPassInterface_Sender_Send(s->output, s->in + s->in_used, s->in_len - s->in_used);
  31. } else {
  32. // finish input packet
  33. s->in_len = -1;
  34. PacketPassInterface_Done(&s->input);
  35. }
  36. }
  37. static void input_handler_send (PacketStreamSender *s, uint8_t *data, int data_len)
  38. {
  39. ASSERT(s->in_len == -1)
  40. ASSERT(data_len >= 0)
  41. DebugObject_Access(&s->d_obj);
  42. // set input packet
  43. s->in_len = data_len;
  44. s->in = data;
  45. s->in_used = 0;
  46. // send
  47. send_data(s);
  48. }
  49. static void output_handler_done (PacketStreamSender *s, int data_len)
  50. {
  51. ASSERT(s->in_len >= 0)
  52. ASSERT(data_len > 0)
  53. ASSERT(data_len <= s->in_len - s->in_used)
  54. DebugObject_Access(&s->d_obj);
  55. // update number of bytes sent
  56. s->in_used += data_len;
  57. // send
  58. send_data(s);
  59. }
  60. void PacketStreamSender_Init (PacketStreamSender *s, StreamPassInterface *output, int mtu, BPendingGroup *pg)
  61. {
  62. ASSERT(mtu >= 0)
  63. // init arguments
  64. s->output = output;
  65. // init input
  66. PacketPassInterface_Init(&s->input, mtu, (PacketPassInterface_handler_send)input_handler_send, s, pg);
  67. // init output
  68. StreamPassInterface_Sender_Init(s->output, (StreamPassInterface_handler_done)output_handler_done, s);
  69. // have no input packet
  70. s->in_len = -1;
  71. DebugObject_Init(&s->d_obj);
  72. }
  73. void PacketStreamSender_Free (PacketStreamSender *s)
  74. {
  75. DebugObject_Free(&s->d_obj);
  76. // free input
  77. PacketPassInterface_Free(&s->input);
  78. }
  79. PacketPassInterface * PacketStreamSender_GetInput (PacketStreamSender *s)
  80. {
  81. DebugObject_Access(&s->d_obj);
  82. return &s->input;
  83. }