PacketPassNotifier.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 int 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. ASSERT(!o->in_handler)
  32. #ifndef NDEBUG
  33. o->in_handler = 1;
  34. #endif
  35. DEAD_ENTER(o->dead)
  36. o->handler(o->handler_user, data, data_len);
  37. if (DEAD_LEAVE(o->dead)) {
  38. return -1;
  39. }
  40. #ifndef NDEBUG
  41. o->in_handler = 0;
  42. #endif
  43. return 0;
  44. }
  45. int input_handler_send (PacketPassNotifier *o, uint8_t *data, int data_len)
  46. {
  47. ASSERT(!o->in_have)
  48. ASSERT(!o->in_handler)
  49. // if we have a handler, call it
  50. if (o->handler) {
  51. if (call_handler(o, data, data_len) < 0) {
  52. return -1;
  53. }
  54. }
  55. // call send on output
  56. DEAD_ENTER(o->dead)
  57. int res = PacketPassInterface_Sender_Send(o->output, data, data_len);
  58. if (DEAD_LEAVE(o->dead)) {
  59. return -1;
  60. }
  61. ASSERT(res == 0 || res == 1)
  62. if (!res) {
  63. // output blocking, continue in output_handler_done
  64. #ifndef NDEBUG
  65. o->in_have = 1;
  66. #endif
  67. return 0;
  68. }
  69. return 1;
  70. }
  71. void input_handler_cancel (PacketPassNotifier *o)
  72. {
  73. ASSERT(o->in_have)
  74. ASSERT(!o->in_handler)
  75. #ifndef NDEBUG
  76. o->in_have = 0;
  77. #endif
  78. PacketPassInterface_Sender_Cancel(o->output);
  79. return;
  80. }
  81. void output_handler_done (PacketPassNotifier *o)
  82. {
  83. ASSERT(o->in_have)
  84. ASSERT(!o->in_handler)
  85. #ifndef NDEBUG
  86. o->in_have = 0;
  87. #endif
  88. PacketPassInterface_Done(&o->input);
  89. return;
  90. }
  91. void PacketPassNotifier_Init (PacketPassNotifier *o, PacketPassInterface *output)
  92. {
  93. // init arguments
  94. o->output = output;
  95. // init dead var
  96. DEAD_INIT(o->dead);
  97. // init input
  98. PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o);
  99. if (PacketPassInterface_HasCancel(o->output)) {
  100. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  101. }
  102. // init output
  103. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  104. // set no handler
  105. o->handler = NULL;
  106. // init debugging
  107. #ifndef NDEBUG
  108. o->in_have = 0;
  109. o->in_handler = 0;
  110. #endif
  111. // init debug object
  112. DebugObject_Init(&o->d_obj);
  113. }
  114. void PacketPassNotifier_Free (PacketPassNotifier *o)
  115. {
  116. // free debug object
  117. DebugObject_Free(&o->d_obj);
  118. // free input
  119. PacketPassInterface_Free(&o->input);
  120. // free dead var
  121. DEAD_KILL(o->dead);
  122. }
  123. PacketPassInterface * PacketPassNotifier_GetInput (PacketPassNotifier *o)
  124. {
  125. return &o->input;
  126. }
  127. void PacketPassNotifier_SetHandler (PacketPassNotifier *o, PacketPassNotifier_handler_notify handler, void *user)
  128. {
  129. o->handler = handler;
  130. o->handler_user = user;
  131. }