PacketRecvNotifier.h 2.8 KB

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