fragmentproto.h 2.9 KB

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