BadVPN – Blame information for rev 1

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