FragmentProtoAssembler.h 5.0 KB

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