SinglePacketBuffer.c 2.2 KB

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