PacketRecvNotifier.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 void output_handler_recv (PacketRecvNotifier *o, uint8_t *data);
  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. DebugIn_AmOut(&o->d_in_handler);
  31. DebugObject_Access(&o->d_obj);
  32. DebugIn_GoIn(&o->d_in_handler);
  33. DEAD_ENTER(o->dead)
  34. o->handler(o->handler_user, data, data_len);
  35. if (DEAD_LEAVE(o->dead)) {
  36. return -1;
  37. }
  38. DebugIn_GoOut(&o->d_in_handler);
  39. return 0;
  40. }
  41. void output_handler_recv (PacketRecvNotifier *o, uint8_t *data)
  42. {
  43. DebugIn_AmOut(&o->d_in_handler);
  44. DebugObject_Access(&o->d_obj);
  45. // schedule receive
  46. o->out = data;
  47. PacketRecvInterface_Receiver_Recv(o->input, o->out);
  48. }
  49. void input_handler_done (PacketRecvNotifier *o, int data_len)
  50. {
  51. DebugIn_AmOut(&o->d_in_handler);
  52. DebugObject_Access(&o->d_obj);
  53. // finish packet
  54. PacketRecvInterface_Done(&o->output, data_len);
  55. // if we have a handler, call it
  56. if (o->handler) {
  57. if (call_handler(o, o->out, data_len) < 0) {
  58. return;
  59. }
  60. }
  61. }
  62. void PacketRecvNotifier_Init (PacketRecvNotifier *o, PacketRecvInterface *input, BPendingGroup *pg)
  63. {
  64. // set arguments
  65. o->input = input;
  66. // init dead var
  67. DEAD_INIT(o->dead);
  68. // init output
  69. PacketRecvInterface_Init(&o->output, PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  70. // init input
  71. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  72. // set no handler
  73. o->handler = NULL;
  74. DebugObject_Init(&o->d_obj);
  75. DebugIn_Init(&o->d_in_handler);
  76. }
  77. void PacketRecvNotifier_Free (PacketRecvNotifier *o)
  78. {
  79. DebugObject_Free(&o->d_obj);
  80. // free output
  81. PacketRecvInterface_Free(&o->output);
  82. // free dead var
  83. DEAD_KILL(o->dead);
  84. }
  85. PacketRecvInterface * PacketRecvNotifier_GetOutput (PacketRecvNotifier *o)
  86. {
  87. DebugObject_Access(&o->d_obj);
  88. return &o->output;
  89. }
  90. void PacketRecvNotifier_SetHandler (PacketRecvNotifier *o, PacketRecvNotifier_handler_notify handler, void *user)
  91. {
  92. DebugObject_Access(&o->d_obj);
  93. o->handler = handler;
  94. o->handler_user = user;
  95. }