FragmentProtoAssembler.h 4.8 KB

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