KeepaliveIO.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <flow/KeepaliveIO.h>
  24. static void keepalive_handler (KeepaliveIO *o)
  25. {
  26. PacketRecvBlocker_AllowBlockedPacket(&o->ka_blocker);
  27. return;
  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 dead var
  36. DEAD_INIT(o->dead);
  37. // init keep-alive sender
  38. PacketPassInactivityMonitor_Init(&o->kasender, output, o->reactor, keepalive_interval_ms, (PacketPassInactivityMonitor_handler)keepalive_handler, o);
  39. // init queue
  40. PacketPassPriorityQueue_Init(&o->queue, PacketPassInactivityMonitor_GetInput(&o->kasender), BReactor_PendingGroup(o->reactor));
  41. // init keepalive flow
  42. PacketPassPriorityQueueFlow_Init(&o->ka_qflow, &o->queue, -1);
  43. // init keepalive blocker
  44. PacketRecvBlocker_Init(&o->ka_blocker, keepalive_input);
  45. // init keepalive buffer
  46. if (!SinglePacketBuffer_Init(&o->ka_buffer, PacketRecvBlocker_GetOutput(&o->ka_blocker), PacketPassPriorityQueueFlow_GetInput(&o->ka_qflow), BReactor_PendingGroup(o->reactor))) {
  47. goto fail1;
  48. }
  49. // init user flow
  50. PacketPassPriorityQueueFlow_Init(&o->user_qflow, &o->queue, 0);
  51. // init debug object
  52. DebugObject_Init(&o->d_obj);
  53. return 1;
  54. fail1:
  55. PacketRecvBlocker_Free(&o->ka_blocker);
  56. PacketPassPriorityQueueFlow_Free(&o->ka_qflow);
  57. PacketPassPriorityQueue_Free(&o->queue);
  58. PacketPassInactivityMonitor_Free(&o->kasender);
  59. return 0;
  60. }
  61. void KeepaliveIO_Free (KeepaliveIO *o)
  62. {
  63. // free debug object
  64. DebugObject_Free(&o->d_obj);
  65. // allow freeing queue flows
  66. PacketPassPriorityQueue_PrepareFree(&o->queue);
  67. // free user flow
  68. PacketPassPriorityQueueFlow_Free(&o->user_qflow);
  69. // free keepalive buffer
  70. SinglePacketBuffer_Free(&o->ka_buffer);
  71. // free keepalive blocker
  72. PacketRecvBlocker_Free(&o->ka_blocker);
  73. // free keepalive flow
  74. PacketPassPriorityQueueFlow_Free(&o->ka_qflow);
  75. // free queue
  76. PacketPassPriorityQueue_Free(&o->queue);
  77. // free keep-alive sender
  78. PacketPassInactivityMonitor_Free(&o->kasender);
  79. // free dead var
  80. DEAD_KILL(o->dead);
  81. }
  82. PacketPassInterface * KeepaliveIO_GetInput (KeepaliveIO *o)
  83. {
  84. return PacketPassPriorityQueueFlow_GetInput(&o->user_qflow);
  85. }