SinglePacketSender.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @file SinglePacketSender.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 <misc/debug.h>
  23. #include <flow/SinglePacketSender.h>
  24. static void call_handler (SinglePacketSender *o)
  25. {
  26. #ifndef NDEBUG
  27. DEAD_ENTER(o->dead)
  28. #endif
  29. o->handler(o->user);
  30. #ifndef NDEBUG
  31. ASSERT(DEAD_KILLED)
  32. DEAD_LEAVE(o->dead);
  33. #endif
  34. }
  35. static void output_handler_done (SinglePacketSender *o)
  36. {
  37. DebugObject_Access(&o->d_obj);
  38. // notify user
  39. call_handler(o);
  40. return;
  41. }
  42. static void job_handler (SinglePacketSender *o)
  43. {
  44. DebugObject_Access(&o->d_obj);
  45. DEAD_ENTER(o->dead)
  46. int res = PacketPassInterface_Sender_Send(o->output, o->packet, o->packet_len);
  47. if (DEAD_LEAVE(o->dead)) {
  48. return;
  49. }
  50. ASSERT(res == 0 || res == 1)
  51. if (!res) {
  52. return;
  53. }
  54. // notify user
  55. call_handler(o);
  56. return;
  57. }
  58. void SinglePacketSender_Init (SinglePacketSender *o, uint8_t *packet, int packet_len, PacketPassInterface *output, SinglePacketSender_handler handler, void *user, BPendingGroup *pg)
  59. {
  60. ASSERT(packet_len >= 0)
  61. ASSERT(packet_len <= PacketPassInterface_GetMTU(output))
  62. // init arguments
  63. o->packet = packet;
  64. o->packet_len = packet_len;
  65. o->output = output;
  66. o->handler = handler;
  67. o->user = user;
  68. // init dead var
  69. DEAD_INIT(o->dead);
  70. // init output
  71. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  72. // init start job
  73. BPending_Init(&o->start_job, pg, (BPending_handler)job_handler, o);
  74. BPending_Set(&o->start_job);
  75. DebugObject_Init(&o->d_obj);
  76. }
  77. void SinglePacketSender_Free (SinglePacketSender *o)
  78. {
  79. DebugObject_Free(&o->d_obj);
  80. // free start job
  81. BPending_Free(&o->start_job);
  82. // free dead var
  83. DEAD_KILL(o->dead);
  84. }