PacketRecvNotifier.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 void output_handler_recv (PacketRecvNotifier *o, uint8_t *data);
  25. static void input_handler_done (PacketRecvNotifier *o, int data_len);
  26. void output_handler_recv (PacketRecvNotifier *o, uint8_t *data)
  27. {
  28. DebugObject_Access(&o->d_obj);
  29. // schedule receive
  30. o->out = data;
  31. PacketRecvInterface_Receiver_Recv(o->input, o->out);
  32. }
  33. void input_handler_done (PacketRecvNotifier *o, int data_len)
  34. {
  35. DebugObject_Access(&o->d_obj);
  36. // finish packet
  37. PacketRecvInterface_Done(&o->output, data_len);
  38. // if we have a handler, call it
  39. if (o->handler) {
  40. o->handler(o->handler_user, o->out, data_len);
  41. return;
  42. }
  43. }
  44. void PacketRecvNotifier_Init (PacketRecvNotifier *o, PacketRecvInterface *input, BPendingGroup *pg)
  45. {
  46. // set arguments
  47. o->input = input;
  48. // init output
  49. PacketRecvInterface_Init(&o->output, PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  50. // init input
  51. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  52. // set no handler
  53. o->handler = NULL;
  54. DebugObject_Init(&o->d_obj);
  55. }
  56. void PacketRecvNotifier_Free (PacketRecvNotifier *o)
  57. {
  58. DebugObject_Free(&o->d_obj);
  59. // free output
  60. PacketRecvInterface_Free(&o->output);
  61. }
  62. PacketRecvInterface * PacketRecvNotifier_GetOutput (PacketRecvNotifier *o)
  63. {
  64. DebugObject_Access(&o->d_obj);
  65. return &o->output;
  66. }
  67. void PacketRecvNotifier_SetHandler (PacketRecvNotifier *o, PacketRecvNotifier_handler_notify handler, void *user)
  68. {
  69. DebugObject_Access(&o->d_obj);
  70. o->handler = handler;
  71. o->handler_user = user;
  72. }