PacketPassNotifier.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 void input_handler_send (PacketPassNotifier *o, uint8_t *data, int data_len);
  25. static void input_handler_cancel (PacketPassNotifier *o);
  26. static void output_handler_done (PacketPassNotifier *o);
  27. void input_handler_send (PacketPassNotifier *o, uint8_t *data, int data_len)
  28. {
  29. ASSERT(!o->d_in_have)
  30. DebugObject_Access(&o->d_obj);
  31. #ifndef NDEBUG
  32. o->d_in_have = 1;
  33. #endif
  34. // schedule send
  35. PacketPassInterface_Sender_Send(o->output, data, data_len);
  36. // if we have a handler, call it
  37. if (o->handler) {
  38. o->handler(o->handler_user, data, data_len);
  39. return;
  40. }
  41. }
  42. void input_handler_cancel (PacketPassNotifier *o)
  43. {
  44. ASSERT(o->d_in_have)
  45. DebugObject_Access(&o->d_obj);
  46. PacketPassInterface_Sender_Cancel(o->output);
  47. #ifndef NDEBUG
  48. o->d_in_have = 0;
  49. #endif
  50. }
  51. void output_handler_done (PacketPassNotifier *o)
  52. {
  53. ASSERT(o->d_in_have)
  54. DebugObject_Access(&o->d_obj);
  55. PacketPassInterface_Done(&o->input);
  56. #ifndef NDEBUG
  57. o->d_in_have = 0;
  58. #endif
  59. }
  60. void PacketPassNotifier_Init (PacketPassNotifier *o, PacketPassInterface *output, BPendingGroup *pg)
  61. {
  62. // init arguments
  63. o->output = output;
  64. // init input
  65. PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
  66. if (PacketPassInterface_HasCancel(o->output)) {
  67. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  68. }
  69. // init output
  70. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  71. // set no handler
  72. o->handler = NULL;
  73. DebugObject_Init(&o->d_obj);
  74. #ifndef NDEBUG
  75. o->d_in_have = 0;
  76. #endif
  77. }
  78. void PacketPassNotifier_Free (PacketPassNotifier *o)
  79. {
  80. DebugObject_Free(&o->d_obj);
  81. // free input
  82. PacketPassInterface_Free(&o->input);
  83. }
  84. PacketPassInterface * PacketPassNotifier_GetInput (PacketPassNotifier *o)
  85. {
  86. DebugObject_Access(&o->d_obj);
  87. return &o->input;
  88. }
  89. void PacketPassNotifier_SetHandler (PacketPassNotifier *o, PacketPassNotifier_handler_notify handler, void *user)
  90. {
  91. DebugObject_Access(&o->d_obj);
  92. o->handler = handler;
  93. o->handler_user = user;
  94. }