PacketRecvNotifier.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file PacketRecvNotifier.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/PacketRecvNotifier.h>
  24. static int call_handler (PacketRecvNotifier *o, uint8_t *data, int data_len);
  25. static int output_handler_recv (PacketRecvNotifier *o, uint8_t *data, int *data_len);
  26. static void input_handler_done (PacketRecvNotifier *o, int data_len);
  27. int call_handler (PacketRecvNotifier *o, uint8_t *data, int data_len)
  28. {
  29. ASSERT(o->handler)
  30. ASSERT(!o->in_handler)
  31. #ifndef NDEBUG
  32. o->in_handler = 1;
  33. #endif
  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. #ifndef NDEBUG
  40. o->in_handler = 0;
  41. #endif
  42. return 0;
  43. }
  44. int output_handler_recv (PacketRecvNotifier *o, uint8_t *data, int *data_len)
  45. {
  46. ASSERT(!o->out_have)
  47. ASSERT(!o->in_handler)
  48. DEAD_DECLARE
  49. // call recv on input
  50. DEAD_ENTER2(o->dead)
  51. int res = PacketRecvInterface_Receiver_Recv(o->input, data, data_len);
  52. if (DEAD_LEAVE(o->dead)) {
  53. return -1;
  54. }
  55. ASSERT(res == 0 || res == 1)
  56. if (!res) {
  57. // input blocking, continue in input_handler_done
  58. #ifndef NDEBUG
  59. o->out_have = 1;
  60. #endif
  61. o->out = data;
  62. return 0;
  63. }
  64. // if we have a handler, call it
  65. if (o->handler) {
  66. if (call_handler(o, data, *data_len) < 0) {
  67. return -1;
  68. }
  69. }
  70. return 1;
  71. }
  72. void input_handler_done (PacketRecvNotifier *o, int data_len)
  73. {
  74. ASSERT(o->out_have)
  75. ASSERT(!o->in_handler)
  76. #ifndef NDEBUG
  77. o->out_have = 0;
  78. #endif
  79. // if we have a handler, call it
  80. if (o->handler) {
  81. if (call_handler(o, o->out, data_len) < 0) {
  82. return;
  83. }
  84. }
  85. PacketRecvInterface_Done(&o->output, data_len);
  86. return;
  87. }
  88. void PacketRecvNotifier_Init (PacketRecvNotifier *o, PacketRecvInterface *input)
  89. {
  90. // set arguments
  91. o->input = input;
  92. // init dead var
  93. DEAD_INIT(o->dead);
  94. // init output
  95. PacketRecvInterface_Init(&o->output, PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o);
  96. // init input
  97. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  98. // set no handler
  99. o->handler = NULL;
  100. // init debugging
  101. #ifndef NDEBUG
  102. o->out_have = 0;
  103. o->in_handler = 0;
  104. #endif
  105. // init debug object
  106. DebugObject_Init(&o->d_obj);
  107. }
  108. void PacketRecvNotifier_Free (PacketRecvNotifier *o)
  109. {
  110. // free debug object
  111. DebugObject_Free(&o->d_obj);
  112. // free output
  113. PacketRecvInterface_Free(&o->output);
  114. // free dead var
  115. DEAD_KILL(o->dead);
  116. }
  117. PacketRecvInterface * PacketRecvNotifier_GetOutput (PacketRecvNotifier *o)
  118. {
  119. return &o->output;
  120. }
  121. void PacketRecvNotifier_SetHandler (PacketRecvNotifier *o, PacketRecvNotifier_handler_notify handler, void *user)
  122. {
  123. o->handler = handler;
  124. o->handler_user = user;
  125. }