FragmentProtoAssembler.h 3.9 KB

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