PacketProtoFlow.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. PacketBufferAsyncInput_Init(&o->ainput, input_mtu);
  33. // init encoder
  34. PacketProtoEncoder_Init(&o->encoder, PacketBufferAsyncInput_GetOutput(&o->ainput));
  35. // init buffer
  36. if (!PacketBuffer_Init(&o->buffer, PacketProtoEncoder_GetOutput(&o->encoder), output, num_packets, pg)) {
  37. goto fail0;
  38. }
  39. // init debug object
  40. DebugObject_Init(&o->d_obj);
  41. return 1;
  42. fail0:
  43. PacketProtoEncoder_Free(&o->encoder);
  44. PacketBufferAsyncInput_Free(&o->ainput);
  45. return 0;
  46. }
  47. void PacketProtoFlow_Free (PacketProtoFlow *o)
  48. {
  49. DebugObject_Free(&o->d_obj);
  50. // free buffer
  51. PacketBuffer_Free(&o->buffer);
  52. // free encoder
  53. PacketProtoEncoder_Free(&o->encoder);
  54. // free async input
  55. PacketBufferAsyncInput_Free(&o->ainput);
  56. }
  57. BestEffortPacketWriteInterface * PacketProtoFlow_GetInput (PacketProtoFlow *o)
  58. {
  59. DebugObject_Access(&o->d_obj);
  60. return PacketBufferAsyncInput_GetInput(&o->ainput);
  61. }