BufferWriter.h 2.6 KB

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