fragmentproto.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include <misc/packed.h>
  46. typedef uint16_t fragmentproto_frameid;
  47. /**
  48. * FragmentProto chunk header.
  49. */
  50. B_START_PACKED
  51. struct fragmentproto_chunk_header {
  52. /**
  53. * Identifier of the frame this chunk belongs to.
  54. * Frames should be given ascending identifiers as they are encoded
  55. * into chunks (except when the ID wraps to zero).
  56. */
  57. fragmentproto_frameid frame_id;
  58. /**
  59. * Position in the frame where this chunk starts.
  60. */
  61. uint16_t chunk_start;
  62. /**
  63. * Length of the chunk's payload.
  64. */
  65. uint16_t chunk_len;
  66. /**
  67. * Whether this is the last chunk of the frame, i.e.
  68. * the total length of the frame is chunk_start + chunk_len.
  69. */
  70. uint8_t is_last;
  71. } B_PACKED;
  72. B_END_PACKED
  73. /**
  74. * Calculates how many chunks are needed at most for encoding one frame of the
  75. * given maximum size with FragmentProto onto a carrier with a given MTU.
  76. * This includes the case when the first chunk of a frame is not the first chunk
  77. * in a FragmentProto packet.
  78. *
  79. * @param carrier_mtu MTU of the carrier, i.e. maximum length of FragmentProto packets. Must be >sizeof(struct fragmentproto_chunk_header).
  80. * @param frame_mtu maximum frame size. Must be >=0.
  81. * @return maximum number of chunks needed. Will be >0.
  82. */
  83. static int fragmentproto_max_chunks_for_frame (int carrier_mtu, int frame_mtu)
  84. {
  85. ASSERT(carrier_mtu > sizeof(struct fragmentproto_chunk_header))
  86. ASSERT(frame_mtu >= 0)
  87. return (bdivide_up(frame_mtu, (carrier_mtu - sizeof(struct fragmentproto_chunk_header))) + 1);
  88. }
  89. #endif