KeepaliveIO.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @file KeepaliveIO.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 "KeepaliveIO.h"
  24. static void keepalive_handler (KeepaliveIO *o)
  25. {
  26. DebugObject_Access(&o->d_obj);
  27. PacketRecvBlocker_AllowBlockedPacket(&o->ka_blocker);
  28. }
  29. int KeepaliveIO_Init (KeepaliveIO *o, BReactor *reactor, PacketPassInterface *output, PacketRecvInterface *keepalive_input, btime_t keepalive_interval_ms)
  30. {
  31. ASSERT(PacketRecvInterface_GetMTU(keepalive_input) <= PacketPassInterface_GetMTU(output))
  32. ASSERT(keepalive_interval_ms > 0)
  33. // set arguments
  34. o->reactor = reactor;
  35. // init keep-alive sender
  36. PacketPassInactivityMonitor_Init(&o->kasender, output, o->reactor, keepalive_interval_ms, (PacketPassInactivityMonitor_handler)keepalive_handler, o);
  37. // init queue
  38. PacketPassPriorityQueue_Init(&o->queue, PacketPassInactivityMonitor_GetInput(&o->kasender), BReactor_PendingGroup(o->reactor), 0);
  39. // init keepalive flow
  40. PacketPassPriorityQueueFlow_Init(&o->ka_qflow, &o->queue, -1);
  41. // init keepalive blocker
  42. PacketRecvBlocker_Init(&o->ka_blocker, keepalive_input, BReactor_PendingGroup(reactor));
  43. // init keepalive buffer
  44. if (!SinglePacketBuffer_Init(&o->ka_buffer, PacketRecvBlocker_GetOutput(&o->ka_blocker), PacketPassPriorityQueueFlow_GetInput(&o->ka_qflow), BReactor_PendingGroup(o->reactor))) {
  45. goto fail1;
  46. }
  47. // init user flow
  48. PacketPassPriorityQueueFlow_Init(&o->user_qflow, &o->queue, 0);
  49. DebugObject_Init(&o->d_obj);
  50. return 1;
  51. fail1:
  52. PacketRecvBlocker_Free(&o->ka_blocker);
  53. PacketPassPriorityQueueFlow_Free(&o->ka_qflow);
  54. PacketPassPriorityQueue_Free(&o->queue);
  55. PacketPassInactivityMonitor_Free(&o->kasender);
  56. return 0;
  57. }
  58. void KeepaliveIO_Free (KeepaliveIO *o)
  59. {
  60. DebugObject_Free(&o->d_obj);
  61. // allow freeing queue flows
  62. PacketPassPriorityQueue_PrepareFree(&o->queue);
  63. // free user flow
  64. PacketPassPriorityQueueFlow_Free(&o->user_qflow);
  65. // free keepalive buffer
  66. SinglePacketBuffer_Free(&o->ka_buffer);
  67. // free keepalive blocker
  68. PacketRecvBlocker_Free(&o->ka_blocker);
  69. // free keepalive flow
  70. PacketPassPriorityQueueFlow_Free(&o->ka_qflow);
  71. // free queue
  72. PacketPassPriorityQueue_Free(&o->queue);
  73. // free keep-alive sender
  74. PacketPassInactivityMonitor_Free(&o->kasender);
  75. }
  76. PacketPassInterface * KeepaliveIO_GetInput (KeepaliveIO *o)
  77. {
  78. DebugObject_Access(&o->d_obj);
  79. return PacketPassPriorityQueueFlow_GetInput(&o->user_qflow);
  80. }