BufferWriter.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @file BufferWriter.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. * Object for writing packets to a {@link PacketRecvInterface} client
  25. * in a best-effort fashion.
  26. */
  27. #ifndef BADVPN_FLOW_BUFFERWRITER_H
  28. #define BADVPN_FLOW_BUFFERWRITER_H
  29. #include <stdint.h>
  30. #include <system/DebugObject.h>
  31. #include <flow/PacketRecvInterface.h>
  32. /**
  33. * Object for writing packets to a {@link PacketRecvInterface} client
  34. * in a best-effort fashion.
  35. */
  36. typedef struct {
  37. PacketRecvInterface recv_interface;
  38. int out_have;
  39. uint8_t *out;
  40. DebugObject d_obj;
  41. #ifndef NDEBUG
  42. int d_mtu;
  43. int d_writing;
  44. #endif
  45. } BufferWriter;
  46. /**
  47. * Initializes the object.
  48. * The object is initialized in not writing state.
  49. *
  50. * @param o the object
  51. * @param mtu maximum input packet length
  52. * @param pg pending group
  53. */
  54. void BufferWriter_Init (BufferWriter *o, int mtu, BPendingGroup *pg);
  55. /**
  56. * Frees the object.
  57. *
  58. * @param o the object
  59. */
  60. void BufferWriter_Free (BufferWriter *o);
  61. /**
  62. * Returns the output interface.
  63. *
  64. * @param o the object
  65. * @return output interface
  66. */
  67. PacketRecvInterface * BufferWriter_GetOutput (BufferWriter *o);
  68. /**
  69. * Attempts to provide a memory location for writing a packet.
  70. * The object must be in not writing state.
  71. * On success, the object enters writing state.
  72. *
  73. * @param o the object
  74. * @param buf if not NULL, on success, the memory location will be stored here.
  75. * It will have space for MTU bytes.
  76. * @return 1 on success, 0 on failure
  77. */
  78. int BufferWriter_StartPacket (BufferWriter *o, uint8_t **buf);
  79. /**
  80. * Submits a packet written to the buffer.
  81. * The object must be in writing state.
  82. * Yhe object enters not writing state.
  83. *
  84. * @param o the object
  85. * @param len length of the packet that was written. Must be >=0 and
  86. * <=MTU.
  87. */
  88. void BufferWriter_EndPacket (BufferWriter *o, int len);
  89. #endif