FragmentProtoAssembler.h 3.6 KB

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