SinglePacketSource.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @file SinglePacketSource.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 <string.h>
  23. #include <misc/debug.h>
  24. #include "SinglePacketSource.h"
  25. static void output_handler_recv (SinglePacketSource *o, uint8_t *data)
  26. {
  27. DebugObject_Access(&o->d_obj);
  28. // if we already sent one packet, stop
  29. if (o->sent) {
  30. return;
  31. }
  32. // set sent
  33. o->sent = 1;
  34. // write packet
  35. memcpy(data, o->packet, o->packet_len);
  36. // done
  37. PacketRecvInterface_Done(&o->output, o->packet_len);
  38. }
  39. void SinglePacketSource_Init (SinglePacketSource *o, uint8_t *packet, int packet_len, BPendingGroup *pg)
  40. {
  41. ASSERT(packet_len >= 0)
  42. // init arguments
  43. o->packet = packet;
  44. o->packet_len = packet_len;
  45. // set not sent
  46. o->sent = 0;
  47. // init output
  48. PacketRecvInterface_Init(&o->output, o->packet_len, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  49. DebugObject_Init(&o->d_obj);
  50. }
  51. void SinglePacketSource_Free (SinglePacketSource *o)
  52. {
  53. DebugObject_Free(&o->d_obj);
  54. // free output
  55. PacketRecvInterface_Free(&o->output);
  56. }
  57. PacketRecvInterface * SinglePacketSource_GetOutput (SinglePacketSource *o)
  58. {
  59. DebugObject_Access(&o->d_obj);
  60. return &o->output;
  61. }