BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file PacketCopier.c
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  
30 #include <string.h>
31  
32 #include <misc/debug.h>
33  
34 #include <flow/PacketCopier.h>
35  
36 static void input_handler_send (PacketCopier *o, uint8_t *data, int data_len)
37 {
38 ASSERT(o->in_len == -1)
39 ASSERT(data_len >= 0)
40 DebugObject_Access(&o->d_obj);
41  
42 if (!o->out_have) {
43 o->in_len = data_len;
44 o->in = data;
45 return;
46 }
47  
48 memcpy(o->out, data, data_len);
49  
50 // finish input packet
51 PacketPassInterface_Done(&o->input);
52  
53 // finish output packet
54 PacketRecvInterface_Done(&o->output, data_len);
55  
56 o->out_have = 0;
57 }
58  
59 static void input_handler_requestcancel (PacketCopier *o)
60 {
61 ASSERT(o->in_len >= 0)
62 ASSERT(!o->out_have)
63 DebugObject_Access(&o->d_obj);
64  
65 // finish input packet
66 PacketPassInterface_Done(&o->input);
67  
68 o->in_len = -1;
69 }
70  
71 static void output_handler_recv (PacketCopier *o, uint8_t *data)
72 {
73 ASSERT(!o->out_have)
74 DebugObject_Access(&o->d_obj);
75  
76 if (o->in_len < 0) {
77 o->out_have = 1;
78 o->out = data;
79 return;
80 }
81  
82 memcpy(data, o->in, o->in_len);
83  
84 // finish input packet
85 PacketPassInterface_Done(&o->input);
86  
87 // finish output packet
88 PacketRecvInterface_Done(&o->output, o->in_len);
89  
90 o->in_len = -1;
91 }
92  
93 void PacketCopier_Init (PacketCopier *o, int mtu, BPendingGroup *pg)
94 {
95 ASSERT(mtu >= 0)
96  
97 // init input
98 PacketPassInterface_Init(&o->input, mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
99 PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
100  
101 // init output
102 PacketRecvInterface_Init(&o->output, mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
103  
104 // set no input packet
105 o->in_len = -1;
106  
107 // set no output packet
108 o->out_have = 0;
109  
110 DebugObject_Init(&o->d_obj);
111 }
112  
113 void PacketCopier_Free (PacketCopier *o)
114 {
115 DebugObject_Free(&o->d_obj);
116  
117 // free output
118 PacketRecvInterface_Free(&o->output);
119  
120 // free input
121 PacketPassInterface_Free(&o->input);
122 }
123  
124 PacketPassInterface * PacketCopier_GetInput (PacketCopier *o)
125 {
126 DebugObject_Access(&o->d_obj);
127  
128 return &o->input;
129 }
130  
131 PacketRecvInterface * PacketCopier_GetOutput (PacketCopier *o)
132 {
133 DebugObject_Access(&o->d_obj);
134  
135 return &o->output;
136 }