PacketPassNotifier.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @file PacketPassNotifier.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 <stddef.h>
  23. #include <flow/PacketPassNotifier.h>
  24. static int call_handler (PacketPassNotifier *o, uint8_t *data, int data_len);
  25. static void input_handler_send (PacketPassNotifier *o, uint8_t *data, int data_len);
  26. static void input_handler_cancel (PacketPassNotifier *o);
  27. static void output_handler_done (PacketPassNotifier *o);
  28. int call_handler (PacketPassNotifier *o, uint8_t *data, int data_len)
  29. {
  30. ASSERT(o->handler)
  31. DebugIn_AmOut(&o->d_in_handler);
  32. DebugObject_Access(&o->d_obj);
  33. DebugIn_GoIn(&o->d_in_handler);
  34. DEAD_ENTER(o->dead)
  35. o->handler(o->handler_user, data, data_len);
  36. if (DEAD_LEAVE(o->dead)) {
  37. return -1;
  38. }
  39. DebugIn_GoOut(&o->d_in_handler);
  40. return 0;
  41. }
  42. void input_handler_send (PacketPassNotifier *o, uint8_t *data, int data_len)
  43. {
  44. ASSERT(!o->d_in_have)
  45. DebugIn_AmOut(&o->d_in_handler);
  46. DebugObject_Access(&o->d_obj);
  47. // schedule send
  48. PacketPassInterface_Sender_Send(o->output, data, data_len);
  49. // if we have a handler, call it
  50. if (o->handler) {
  51. if (call_handler(o, data, data_len) < 0) {
  52. return;
  53. }
  54. }
  55. #ifndef NDEBUG
  56. o->d_in_have = 1;
  57. #endif
  58. }
  59. void input_handler_cancel (PacketPassNotifier *o)
  60. {
  61. ASSERT(o->d_in_have)
  62. DebugIn_AmOut(&o->d_in_handler);
  63. DebugObject_Access(&o->d_obj);
  64. PacketPassInterface_Sender_Cancel(o->output);
  65. #ifndef NDEBUG
  66. o->d_in_have = 0;
  67. #endif
  68. }
  69. void output_handler_done (PacketPassNotifier *o)
  70. {
  71. ASSERT(o->d_in_have)
  72. DebugIn_AmOut(&o->d_in_handler);
  73. DebugObject_Access(&o->d_obj);
  74. PacketPassInterface_Done(&o->input);
  75. #ifndef NDEBUG
  76. o->d_in_have = 0;
  77. #endif
  78. }
  79. void PacketPassNotifier_Init (PacketPassNotifier *o, PacketPassInterface *output, BPendingGroup *pg)
  80. {
  81. // init arguments
  82. o->output = output;
  83. // init dead var
  84. DEAD_INIT(o->dead);
  85. // init input
  86. PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
  87. if (PacketPassInterface_HasCancel(o->output)) {
  88. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  89. }
  90. // init output
  91. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  92. // set no handler
  93. o->handler = NULL;
  94. DebugObject_Init(&o->d_obj);
  95. DebugIn_Init(&o->d_in_handler);
  96. #ifndef NDEBUG
  97. o->d_in_have = 0;
  98. #endif
  99. }
  100. void PacketPassNotifier_Free (PacketPassNotifier *o)
  101. {
  102. DebugObject_Free(&o->d_obj);
  103. // free input
  104. PacketPassInterface_Free(&o->input);
  105. // free dead var
  106. DEAD_KILL(o->dead);
  107. }
  108. PacketPassInterface * PacketPassNotifier_GetInput (PacketPassNotifier *o)
  109. {
  110. DebugObject_Access(&o->d_obj);
  111. return &o->input;
  112. }
  113. void PacketPassNotifier_SetHandler (PacketPassNotifier *o, PacketPassNotifier_handler_notify handler, void *user)
  114. {
  115. DebugObject_Access(&o->d_obj);
  116. o->handler = handler;
  117. o->handler_user = user;
  118. }