fragmentproto.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @file fragmentproto.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. * Definitions for FragmentProto, a protocol that allows sending of arbitrarily sized packets over
  32. * a link with a fixed MTU.
  33. *
  34. * All multi-byte integers in structs are little-endian, unless stated otherwise.
  35. *
  36. * A FragmentProto packet consists of a number of chunks.
  37. * Each chunk consists of:
  38. * - the chunk header (struct {@link fragmentproto_chunk_header})
  39. * - the chunk payload, i.e. part of the frame specified in the header
  40. */
  41. #ifndef BADVPN_PROTOCOL_FRAGMENTPROTO_H
  42. #define BADVPN_PROTOCOL_FRAGMENTPROTO_H
  43. #include <stdint.h>
  44. #include <misc/balign.h>
  45. typedef uint16_t fragmentproto_frameid;
  46. /**
  47. * FragmentProto chunk header.
  48. */
  49. struct fragmentproto_chunk_header {
  50. /**
  51. * Identifier of the frame this chunk belongs to.
  52. * Frames should be given ascending identifiers as they are encoded
  53. * into chunks (except when the ID wraps to zero).
  54. */
  55. fragmentproto_frameid frame_id;
  56. /**
  57. * Position in the frame where this chunk starts.
  58. */
  59. uint16_t chunk_start;
  60. /**
  61. * Length of the chunk's payload.
  62. */
  63. uint16_t chunk_len;
  64. /**
  65. * Whether this is the last chunk of the frame, i.e.
  66. * the total length of the frame is chunk_start + chunk_len.
  67. */
  68. uint8_t is_last;
  69. } __attribute__((packed));
  70. /**
  71. * Calculates how many chunks are needed at most for encoding one frame of the
  72. * given maximum size with FragmentProto onto a carrier with a given MTU.
  73. * This includes the case when the first chunk of a frame is not the first chunk
  74. * in a FragmentProto packet.
  75. *
  76. * @param carrier_mtu MTU of the carrier, i.e. maximum length of FragmentProto packets. Must be >sizeof(struct fragmentproto_chunk_header).
  77. * @param frame_mtu maximum frame size. Must be >=0.
  78. * @return maximum number of chunks needed. Will be >0.
  79. */
  80. static int fragmentproto_max_chunks_for_frame (int carrier_mtu, int frame_mtu)
  81. {
  82. ASSERT(carrier_mtu > sizeof(struct fragmentproto_chunk_header))
  83. ASSERT(frame_mtu >= 0)
  84. return (bdivide_up(frame_mtu, (carrier_mtu - sizeof(struct fragmentproto_chunk_header))) + 1);
  85. }
  86. #endif