FragmentProtoAssembler.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @file FragmentProtoAssembler.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 which decodes packets according to FragmentProto.
  32. */
  33. #ifndef BADVPN_CLIENT_FRAGMENTPROTOASSEMBLER_H
  34. #define BADVPN_CLIENT_FRAGMENTPROTOASSEMBLER_H
  35. #include <stdint.h>
  36. #include <protocol/fragmentproto.h>
  37. #include <misc/debug.h>
  38. #include <base/DebugObject.h>
  39. #include <base/BLog.h>
  40. #include <structure/LinkedList2.h>
  41. #include <structure/BAVL.h>
  42. #include <flow/PacketPassInterface.h>
  43. #define FPA_MAX_TIME UINT32_MAX
  44. struct FragmentProtoAssembler_chunk {
  45. int start;
  46. int len;
  47. };
  48. struct FragmentProtoAssembler_frame {
  49. LinkedList2Node list_node; // node in free or used list
  50. struct FragmentProtoAssembler_chunk *chunks; // array of chunks, up to num_chunks
  51. uint8_t *buffer; // buffer with frame data, size output_mtu
  52. // everything below only defined when frame entry is used
  53. fragmentproto_frameid id; // frame identifier
  54. uint32_t time; // packet time when the last chunk was received
  55. BAVLNode tree_node; // node in tree for searching frames by id
  56. int num_chunks; // number of valid chunks
  57. int sum; // sum of all chunks' lengths
  58. int length; // length of the frame, or -1 if not yet known
  59. int length_so_far; // if length=-1, current data set's upper bound
  60. };
  61. /**
  62. * Object which decodes packets according to FragmentProto.
  63. *
  64. * Input is with {@link PacketPassInterface}.
  65. * Output is with {@link PacketPassInterface}.
  66. */
  67. typedef struct {
  68. void *user;
  69. BLog_logfunc logfunc;
  70. PacketPassInterface input;
  71. PacketPassInterface *output;
  72. int output_mtu;
  73. int num_chunks;
  74. uint32_t time;
  75. int time_tolerance;
  76. struct FragmentProtoAssembler_frame *frames_entries;
  77. struct FragmentProtoAssembler_chunk *frames_chunks;
  78. uint8_t *frames_buffer;
  79. LinkedList2 frames_free;
  80. LinkedList2 frames_used;
  81. BAVL frames_used_tree;
  82. int in_len;
  83. uint8_t *in;
  84. int in_pos;
  85. DebugObject d_obj;
  86. } FragmentProtoAssembler;
  87. /**
  88. * Initializes the object.
  89. * {@link BLog_Init} must have been done.
  90. *
  91. * @param o the object
  92. * @param input_mtu maximum input packet size. Must be >=0.
  93. * @param output output interface
  94. * @param num_frames number of frames we can hold. Must be >0 and < FPA_MAX_TIME.
  95. * To make the assembler tolerate out-of-order input of degree D, set to D+2.
  96. * Here, D is the minimum size of a hypothetical buffer needed to order the input.
  97. * @param num_chunks maximum number of chunks a frame can come in. Must be >0.
  98. * @param pg pending group
  99. * @param user argument to handlers
  100. * @param logfunc function which prepends the log prefix using {@link BLog_Append}
  101. * @return 1 on success, 0 on failure
  102. */
  103. int FragmentProtoAssembler_Init (FragmentProtoAssembler *o, int input_mtu, PacketPassInterface *output, int num_frames, int num_chunks, BPendingGroup *pg, void *user, BLog_logfunc logfunc) WARN_UNUSED;
  104. /**
  105. * Frees the object.
  106. *
  107. * @param o the object
  108. */
  109. void FragmentProtoAssembler_Free (FragmentProtoAssembler *o);
  110. /**
  111. * Returns the input interface.
  112. *
  113. * @param o the object
  114. * @return input interface
  115. */
  116. PacketPassInterface * FragmentProtoAssembler_GetInput (FragmentProtoAssembler *o);
  117. #endif