FragmentProtoAssembler.h 3.6 KB

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