BufferWriter.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @file BufferWriter.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Object for writing packets to a {@link PacketRecvInterface} client
  32. * in a best-effort fashion.
  33. */
  34. #ifndef BADVPN_FLOW_BUFFERWRITER_H
  35. #define BADVPN_FLOW_BUFFERWRITER_H
  36. #include <stdint.h>
  37. #include <misc/debug.h>
  38. #include <base/DebugObject.h>
  39. #include <flow/PacketRecvInterface.h>
  40. /**
  41. * Object for writing packets to a {@link PacketRecvInterface} client
  42. * in a best-effort fashion.
  43. */
  44. typedef struct {
  45. PacketRecvInterface recv_interface;
  46. int out_have;
  47. uint8_t *out;
  48. DebugObject d_obj;
  49. #ifndef NDEBUG
  50. int d_mtu;
  51. int d_writing;
  52. #endif
  53. } BufferWriter;
  54. /**
  55. * Initializes the object.
  56. * The object is initialized in not writing state.
  57. *
  58. * @param o the object
  59. * @param mtu maximum input packet length
  60. * @param pg pending group
  61. */
  62. void BufferWriter_Init (BufferWriter *o, int mtu, BPendingGroup *pg);
  63. /**
  64. * Frees the object.
  65. *
  66. * @param o the object
  67. */
  68. void BufferWriter_Free (BufferWriter *o);
  69. /**
  70. * Returns the output interface.
  71. *
  72. * @param o the object
  73. * @return output interface
  74. */
  75. PacketRecvInterface * BufferWriter_GetOutput (BufferWriter *o);
  76. /**
  77. * Attempts to provide a memory location for writing a packet.
  78. * The object must be in not writing state.
  79. * On success, the object enters writing state.
  80. *
  81. * @param o the object
  82. * @param buf if not NULL, on success, the memory location will be stored here.
  83. * It will have space for MTU bytes.
  84. * @return 1 on success, 0 on failure
  85. */
  86. int BufferWriter_StartPacket (BufferWriter *o, uint8_t **buf) WARN_UNUSED;
  87. /**
  88. * Submits a packet written to the buffer.
  89. * The object must be in writing state.
  90. * Yhe object enters not writing state.
  91. *
  92. * @param o the object
  93. * @param len length of the packet that was written. Must be >=0 and
  94. * <=MTU.
  95. */
  96. void BufferWriter_EndPacket (BufferWriter *o, int len);
  97. #endif