SinglePacketBuffer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 void input_handler_done (SinglePacketBuffer *o, int in_len)
  26. {
  27. DebugObject_Access(&o->d_obj);
  28. PacketPassInterface_Sender_Send(o->output, o->buf, in_len);
  29. }
  30. static void output_handler_done (SinglePacketBuffer *o)
  31. {
  32. DebugObject_Access(&o->d_obj);
  33. PacketRecvInterface_Receiver_Recv(o->input, o->buf);
  34. }
  35. int SinglePacketBuffer_Init (SinglePacketBuffer *o, PacketRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg)
  36. {
  37. ASSERT(PacketPassInterface_GetMTU(output) >= PacketRecvInterface_GetMTU(input))
  38. // init arguments
  39. o->input = input;
  40. o->output = output;
  41. // init input
  42. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  43. // init output
  44. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  45. // init buffer
  46. if (!(o->buf = malloc(PacketRecvInterface_GetMTU(o->input)))) {
  47. goto fail1;
  48. }
  49. // schedule receive
  50. PacketRecvInterface_Receiver_Recv(o->input, o->buf);
  51. DebugObject_Init(&o->d_obj);
  52. return 1;
  53. fail1:
  54. return 0;
  55. }
  56. void SinglePacketBuffer_Free (SinglePacketBuffer *o)
  57. {
  58. DebugObject_Free(&o->d_obj);
  59. // free buffer
  60. free(o->buf);
  61. }