PacketPassNotifier.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. void input_handler_send (PacketPassNotifier *o, uint8_t *data, int data_len)
  25. {
  26. DebugObject_Access(&o->d_obj);
  27. // schedule send
  28. PacketPassInterface_Sender_Send(o->output, data, data_len);
  29. // if we have a handler, call it
  30. if (o->handler) {
  31. o->handler(o->handler_user, data, data_len);
  32. return;
  33. }
  34. }
  35. void input_handler_requestcancel (PacketPassNotifier *o)
  36. {
  37. DebugObject_Access(&o->d_obj);
  38. PacketPassInterface_Sender_RequestCancel(o->output);
  39. }
  40. void output_handler_done (PacketPassNotifier *o)
  41. {
  42. DebugObject_Access(&o->d_obj);
  43. PacketPassInterface_Done(&o->input);
  44. }
  45. void PacketPassNotifier_Init (PacketPassNotifier *o, PacketPassInterface *output, BPendingGroup *pg)
  46. {
  47. // init arguments
  48. o->output = output;
  49. // init input
  50. PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
  51. if (PacketPassInterface_HasCancel(o->output)) {
  52. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  53. }
  54. // init output
  55. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  56. // set no handler
  57. o->handler = NULL;
  58. DebugObject_Init(&o->d_obj);
  59. }
  60. void PacketPassNotifier_Free (PacketPassNotifier *o)
  61. {
  62. DebugObject_Free(&o->d_obj);
  63. // free input
  64. PacketPassInterface_Free(&o->input);
  65. }
  66. PacketPassInterface * PacketPassNotifier_GetInput (PacketPassNotifier *o)
  67. {
  68. DebugObject_Access(&o->d_obj);
  69. return &o->input;
  70. }
  71. void PacketPassNotifier_SetHandler (PacketPassNotifier *o, PacketPassNotifier_handler_notify handler, void *user)
  72. {
  73. DebugObject_Access(&o->d_obj);
  74. o->handler = handler;
  75. o->handler_user = user;
  76. }