BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file PacketRouter.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 | * Object which simplifies routing packets to {@link RouteBuffer}'s from a |
||
32 | * {@link PacketRecvInterface} input. |
||
33 | */ |
||
34 | |||
35 | #ifndef BADVPN_FLOW_PACKETROUTER_H |
||
36 | #define BADVPN_FLOW_PACKETROUTER_H |
||
37 | |||
38 | #include <base/DebugObject.h> |
||
39 | #include <base/BPending.h> |
||
40 | #include <flow/PacketRecvInterface.h> |
||
41 | #include <flow/RouteBuffer.h> |
||
42 | |||
43 | /** |
||
44 | * Handler called when a packet is received, allowing the user to route it |
||
45 | * to one or more buffers using {@link PacketRouter_Route}. |
||
46 | * |
||
47 | * @param user as in {@link PacketRouter_Init} |
||
48 | * @param buf the buffer for the packet. May be modified by the user. |
||
49 | * Will have space for mtu bytes. Only valid in the job context of |
||
50 | * this handler, until {@link PacketRouter_Route} is called successfully. |
||
51 | * @param recv_len length of the input packet (located at recv_offset bytes offset) |
||
52 | */ |
||
53 | typedef void (*PacketRouter_handler) (void *user, uint8_t *buf, int recv_len); |
||
54 | |||
55 | /** |
||
56 | * Object which simplifies routing packets to {@link RouteBuffer}'s from a |
||
57 | * {@link PacketRecvInterface} input. |
||
58 | * |
||
59 | * Packets are routed by calling {@link PacketRouter_Route} (possibly multiple times) |
||
60 | * from the job context of the {@link PacketRouter_handler} handler. |
||
61 | */ |
||
62 | typedef struct { |
||
63 | int mtu; |
||
64 | int recv_offset; |
||
65 | PacketRecvInterface *input; |
||
66 | PacketRouter_handler handler; |
||
67 | void *user; |
||
68 | RouteBufferSource rbs; |
||
69 | BPending next_job; |
||
70 | DebugObject d_obj; |
||
71 | } PacketRouter; |
||
72 | |||
73 | /** |
||
74 | * Initializes the object. |
||
75 | * |
||
76 | * @param o the object |
||
77 | * @param mtu maximum packet size. Must be >=0. It will only be possible to route packets to |
||
78 | * {@link RouteBuffer}'s with the same MTU. |
||
79 | * @param recv_offset offset from the beginning for receiving input packets. |
||
80 | * Must be >=0 and <=mtu. The leading space should be initialized |
||
81 | * by the user before routing a packet. |
||
82 | * @param input input interface. Its MTU must be <= mtu - recv_offset. |
||
83 | * @param handler handler called when a packet is received to allow the user to route it |
||
84 | * @param user value passed to handler |
||
85 | * @param pg pending group |
||
86 | * @return 1 on success, 0 on failure |
||
87 | */ |
||
88 | int PacketRouter_Init (PacketRouter *o, int mtu, int recv_offset, PacketRecvInterface *input, PacketRouter_handler handler, void *user, BPendingGroup *pg) WARN_UNUSED; |
||
89 | |||
90 | /** |
||
91 | * Frees the object. |
||
92 | * |
||
93 | * @param o the object |
||
94 | */ |
||
95 | void PacketRouter_Free (PacketRouter *o); |
||
96 | |||
97 | /** |
||
98 | * Routes the current packet to the given buffer. |
||
99 | * Must be called from the job context of the {@link PacketRouter_handler} handler. |
||
100 | * On success, copies part of the current packet to next one (regardless if next_buf |
||
101 | * is provided or not; if not, copies before receiving another packet). |
||
102 | * |
||
103 | * @param o the object |
||
104 | * @param len total packet length (e.g. recv_offset + (recv_len from handler)). |
||
105 | * Must be >=0 and <=mtu. |
||
106 | * @param output buffer to route to. Its MTU must be the same as of this object. |
||
107 | * @param next_buf if not NULL, on success, will be set to the address of a new current |
||
108 | * packet that can be routed. The pointer will be valid in the job context of |
||
109 | * the calling handler, until this function is called successfully again |
||
110 | * (as for the original pointer provided by the handler). |
||
111 | * @param copy_offset Offset from the beginning for copying to the next packet. |
||
112 | * Must be >=0 and <=mtu. |
||
113 | * @param copy_len Number of bytes to copy from the old current |
||
114 | * packet to the next one. Must be >=0 and <= mtu - copy_offset. |
||
115 | * @return 1 on success, 0 on failure (buffer full) |
||
116 | */ |
||
117 | int PacketRouter_Route (PacketRouter *o, int len, RouteBuffer *output, uint8_t **next_buf, int copy_offset, int copy_len); |
||
118 | |||
119 | /** |
||
120 | * Asserts that {@link PacketRouter_Route} can be called. |
||
121 | * |
||
122 | * @param o the object |
||
123 | */ |
||
124 | void PacketRouter_AssertRoute (PacketRouter *o); |
||
125 | |||
126 | #endif |