PacketRecvNotifier.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @file PacketRecvNotifier.h
  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. * @section DESCRIPTION
  23. *
  24. * A {@link PacketRecvInterface} layer that calls a handler function before
  25. * providing a packet to output.
  26. */
  27. #ifndef BADVPN_FLOW_PACKETRECVNOTIFIER_H
  28. #define BADVPN_FLOW_PACKETRECVNOTIFIER_H
  29. #include <stdint.h>
  30. #include <system/DebugObject.h>
  31. #include <flow/PacketRecvInterface.h>
  32. /**
  33. * Handler function called when input has provided a packet (i.e. by returning
  34. * 1 from Recv or calling Done), but before passing the packet on to output.
  35. *
  36. * @param user value specified in {@link PacketRecvNotifier_SetHandler}
  37. * @param data packet provided by output (buffer provided by input)
  38. * @param data_len size of the packet
  39. */
  40. typedef void (*PacketRecvNotifier_handler_notify) (void *user, uint8_t *data, int data_len);
  41. /**
  42. * A {@link PacketRecvInterface} layer that calls a handler function before
  43. * providing a packet to output.
  44. */
  45. typedef struct {
  46. PacketRecvInterface output;
  47. PacketRecvInterface *input;
  48. PacketRecvNotifier_handler_notify handler;
  49. void *handler_user;
  50. uint8_t *out;
  51. DebugObject d_obj;
  52. } PacketRecvNotifier;
  53. /**
  54. * Initializes the object.
  55. *
  56. * @param o the object
  57. * @param input input interface
  58. * @param pg pending group
  59. */
  60. void PacketRecvNotifier_Init (PacketRecvNotifier *o, PacketRecvInterface *input, BPendingGroup *pg);
  61. /**
  62. * Frees the object.
  63. *
  64. * @param o the object
  65. */
  66. void PacketRecvNotifier_Free (PacketRecvNotifier *o);
  67. /**
  68. * Returns the output interface.
  69. * The MTU of the output interface will be the same as of the input interface.
  70. *
  71. * @param o the object
  72. * @return output interface
  73. */
  74. PacketRecvInterface * PacketRecvNotifier_GetOutput (PacketRecvNotifier *o);
  75. /**
  76. * Configures a handler function to invoke before passing output packets to input.
  77. *
  78. * @param o the object
  79. * @param handler handler function, or NULL to disable.
  80. * @param user value to pass to handler function. Ignored if handler is NULL.
  81. */
  82. void PacketRecvNotifier_SetHandler (PacketRecvNotifier *o, PacketRecvNotifier_handler_notify handler, void *user);
  83. #endif