BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file PacketBuffer.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 <stdlib.h>
31  
32 #include <misc/debug.h>
33 #include <misc/balloc.h>
34  
35 #include <flow/PacketBuffer.h>
36  
37 static void input_handler_done (PacketBuffer *buf, int in_len);
38 static void output_handler_done (PacketBuffer *buf);
39  
40 void input_handler_done (PacketBuffer *buf, int in_len)
41 {
42 ASSERT(in_len >= 0)
43 ASSERT(in_len <= buf->input_mtu)
44 DebugObject_Access(&buf->d_obj);
45  
46 // remember if buffer is empty
47 int was_empty = (buf->buf.output_avail < 0);
48  
49 // submit packet to buffer
50 ChunkBuffer2_SubmitPacket(&buf->buf, in_len);
51  
52 // if there is space, schedule receive
53 if (buf->buf.input_avail >= buf->input_mtu) {
54 PacketRecvInterface_Receiver_Recv(buf->input, buf->buf.input_dest);
55 }
56  
57 // if buffer was empty, schedule send
58 if (was_empty) {
59 PacketPassInterface_Sender_Send(buf->output, buf->buf.output_dest, buf->buf.output_avail);
60 }
61 }
62  
63 void output_handler_done (PacketBuffer *buf)
64 {
65 DebugObject_Access(&buf->d_obj);
66  
67 // remember if buffer is full
68 int was_full = (buf->buf.input_avail < buf->input_mtu);
69  
70 // remove packet from buffer
71 ChunkBuffer2_ConsumePacket(&buf->buf);
72  
73 // if buffer was full and there is space, schedule receive
74 if (was_full && buf->buf.input_avail >= buf->input_mtu) {
75 PacketRecvInterface_Receiver_Recv(buf->input, buf->buf.input_dest);
76 }
77  
78 // if there is more data, schedule send
79 if (buf->buf.output_avail >= 0) {
80 PacketPassInterface_Sender_Send(buf->output, buf->buf.output_dest, buf->buf.output_avail);
81 }
82 }
83  
84 int PacketBuffer_Init (PacketBuffer *buf, PacketRecvInterface *input, PacketPassInterface *output, int num_packets, BPendingGroup *pg)
85 {
86 ASSERT(PacketPassInterface_GetMTU(output) >= PacketRecvInterface_GetMTU(input))
87 ASSERT(num_packets > 0)
88  
89 // init arguments
90 buf->input = input;
91 buf->output = output;
92  
93 // init input
94 PacketRecvInterface_Receiver_Init(buf->input, (PacketRecvInterface_handler_done)input_handler_done, buf);
95  
96 // set input MTU
97 buf->input_mtu = PacketRecvInterface_GetMTU(buf->input);
98  
99 // init output
100 PacketPassInterface_Sender_Init(buf->output, (PacketPassInterface_handler_done)output_handler_done, buf);
101  
102 // allocate buffer
103 int num_blocks = ChunkBuffer2_calc_blocks(buf->input_mtu, num_packets);
104 if (num_blocks < 0) {
105 goto fail0;
106 }
107 if (!(buf->buf_data = (struct ChunkBuffer2_block *)BAllocArray(num_blocks, sizeof(buf->buf_data[0])))) {
108 goto fail0;
109 }
110  
111 // init buffer
112 ChunkBuffer2_Init(&buf->buf, buf->buf_data, num_blocks, buf->input_mtu);
113  
114 // schedule receive
115 PacketRecvInterface_Receiver_Recv(buf->input, buf->buf.input_dest);
116  
117 DebugObject_Init(&buf->d_obj);
118  
119 return 1;
120  
121 fail0:
122 return 0;
123 }
124  
125 void PacketBuffer_Free (PacketBuffer *buf)
126 {
127 DebugObject_Free(&buf->d_obj);
128  
129 // free buffer
130 BFree(buf->buf_data);
131 }