BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file RouteBuffer.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 | * Packet buffer for zero-copy packet routing. |
||
32 | */ |
||
33 | |||
34 | #ifndef BADVPN_FLOW_ROUTEBUFFER_H |
||
35 | #define BADVPN_FLOW_ROUTEBUFFER_H |
||
36 | |||
37 | #include <misc/debug.h> |
||
38 | #include <structure/LinkedList1.h> |
||
39 | #include <base/DebugObject.h> |
||
40 | #include <flow/PacketPassInterface.h> |
||
41 | |||
42 | struct RouteBuffer_packet { |
||
43 | LinkedList1Node node; |
||
44 | int len; |
||
45 | }; |
||
46 | |||
47 | /** |
||
48 | * Packet buffer for zero-copy packet routing. |
||
49 | * |
||
50 | * Packets are buffered using {@link RouteBufferSource} objects. |
||
51 | */ |
||
52 | typedef struct { |
||
53 | int mtu; |
||
54 | PacketPassInterface *output; |
||
55 | LinkedList1 packets_free; |
||
56 | LinkedList1 packets_used; |
||
57 | DebugObject d_obj; |
||
58 | } RouteBuffer; |
||
59 | |||
60 | /** |
||
61 | * Object through which packets are buffered into {@link RouteBuffer} objects. |
||
62 | * |
||
63 | * A packet is routed by calling {@link RouteBufferSource_Pointer}, writing it to |
||
64 | * the returned address, then calling {@link RouteBufferSource_Route}. |
||
65 | */ |
||
66 | typedef struct { |
||
67 | int mtu; |
||
68 | struct RouteBuffer_packet *current_packet; |
||
69 | DebugObject d_obj; |
||
70 | } RouteBufferSource; |
||
71 | |||
72 | /** |
||
73 | * Initializes the object. |
||
74 | * |
||
75 | * @param o the object |
||
76 | * @param mtu maximum packet size. Must be >=0. It will only be possible to route packets to this buffer |
||
77 | * from {@link RouteBufferSource}.s with the same MTU. |
||
78 | * @param output output interface. Its MTU must be >=mtu. |
||
79 | * @param buf_size size of the buffer in number of packet. Must be >0. |
||
80 | * @return 1 on success, 0 on failure |
||
81 | */ |
||
82 | int RouteBuffer_Init (RouteBuffer *o, int mtu, PacketPassInterface *output, int buf_size) WARN_UNUSED; |
||
83 | |||
84 | /** |
||
85 | * Frees the object. |
||
86 | */ |
||
87 | void RouteBuffer_Free (RouteBuffer *o); |
||
88 | |||
89 | /** |
||
90 | * Retuns the buffer's MTU (mtu argument to {@link RouteBuffer_Init}). |
||
91 | * |
||
92 | * @return MTU |
||
93 | */ |
||
94 | int RouteBuffer_GetMTU (RouteBuffer *o); |
||
95 | |||
96 | /** |
||
97 | * Initializes the object. |
||
98 | * |
||
99 | * @param o the object |
||
100 | * @param mtu maximum packet size. Must be >=0. The object will only be able to route packets |
||
101 | * to {@link RouteBuffer}'s with the same MTU. |
||
102 | * @return 1 on success, 0 on failure |
||
103 | */ |
||
104 | int RouteBufferSource_Init (RouteBufferSource *o, int mtu) WARN_UNUSED; |
||
105 | |||
106 | /** |
||
107 | * Frees the object. |
||
108 | * |
||
109 | * @param o the object |
||
110 | */ |
||
111 | void RouteBufferSource_Free (RouteBufferSource *o); |
||
112 | |||
113 | /** |
||
114 | * Returns a pointer to the current packet. |
||
115 | * The pointed to memory area will have space for MTU bytes. |
||
116 | * The pointer is only valid until {@link RouteBufferSource_Route} succeeds. |
||
117 | * |
||
118 | * @param o the object |
||
119 | * @return pointer to the current packet |
||
120 | */ |
||
121 | uint8_t * RouteBufferSource_Pointer (RouteBufferSource *o); |
||
122 | |||
123 | /** |
||
124 | * Routes the current packet to a given buffer. |
||
125 | * On success, this invalidates the pointer previously returned from |
||
126 | * {@link RouteBufferSource_Pointer}. |
||
127 | * |
||
128 | * @param o the object |
||
129 | * @param len length of the packet. Must be >=0 and <=MTU. |
||
130 | * @param b buffer to route to. Its MTU must equal this object's MTU. |
||
131 | * @param copy_offset Offset from the beginning for copying. Must be >=0 and |
||
132 | * <=mtu. |
||
133 | * @param copy_len Number of bytes to copy from the old current packet to the new one. |
||
134 | * Must be >=0 and <= mtu - copy_offset. |
||
135 | * @return 1 on success, 0 on failure |
||
136 | */ |
||
137 | int RouteBufferSource_Route (RouteBufferSource *o, int len, RouteBuffer *b, int copy_offset, int copy_len); |
||
138 | |||
139 | #endif |