PacketProtoFlow.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @file PacketProtoFlow.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 <protocol/packetproto.h>
  23. #include <misc/debug.h>
  24. #include <flow/PacketProtoFlow.h>
  25. int PacketProtoFlow_Init (PacketProtoFlow *o, int input_mtu, int num_packets, PacketPassInterface *output, BPendingGroup *pg)
  26. {
  27. ASSERT(input_mtu >= 0)
  28. ASSERT(input_mtu <= PACKETPROTO_MAXPAYLOAD)
  29. ASSERT(num_packets > 0)
  30. ASSERT(PacketPassInterface_GetMTU(output) >= PACKETPROTO_ENCLEN(input_mtu))
  31. // init async input
  32. BufferWriter_Init(&o->ainput, input_mtu, pg);
  33. // init encoder
  34. PacketProtoEncoder_Init(&o->encoder, BufferWriter_GetOutput(&o->ainput), pg);
  35. // init buffer
  36. if (!PacketBuffer_Init(&o->buffer, PacketProtoEncoder_GetOutput(&o->encoder), output, num_packets, pg)) {
  37. goto fail0;
  38. }
  39. DebugObject_Init(&o->d_obj);
  40. return 1;
  41. fail0:
  42. PacketProtoEncoder_Free(&o->encoder);
  43. BufferWriter_Free(&o->ainput);
  44. return 0;
  45. }
  46. void PacketProtoFlow_Free (PacketProtoFlow *o)
  47. {
  48. DebugObject_Free(&o->d_obj);
  49. // free buffer
  50. PacketBuffer_Free(&o->buffer);
  51. // free encoder
  52. PacketProtoEncoder_Free(&o->encoder);
  53. // free async input
  54. BufferWriter_Free(&o->ainput);
  55. }
  56. BufferWriter * PacketProtoFlow_GetInput (PacketProtoFlow *o)
  57. {
  58. DebugObject_Access(&o->d_obj);
  59. return &o->ainput;
  60. }