BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file PacketProtoEncoder.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 <stddef.h>
31 #include <string.h>
32  
33 #include <protocol/packetproto.h>
34 #include <misc/balign.h>
35 #include <misc/debug.h>
36 #include <misc/byteorder.h>
37  
38 #include <flow/PacketProtoEncoder.h>
39  
40 static void output_handler_recv (PacketProtoEncoder *enc, uint8_t *data)
41 {
42 ASSERT(!enc->output_packet)
43 ASSERT(data)
44 DebugObject_Access(&enc->d_obj);
45  
46 // schedule receive
47 enc->output_packet = data;
48 PacketRecvInterface_Receiver_Recv(enc->input, enc->output_packet + sizeof(struct packetproto_header));
49 }
50  
51 static void input_handler_done (PacketProtoEncoder *enc, int in_len)
52 {
53 ASSERT(enc->output_packet)
54 DebugObject_Access(&enc->d_obj);
55  
56 // write length
57 struct packetproto_header pp;
58 pp.len = htol16(in_len);
59 memcpy(enc->output_packet, &pp, sizeof(pp));
60  
61 // finish output packet
62 enc->output_packet = NULL;
63 PacketRecvInterface_Done(&enc->output, PACKETPROTO_ENCLEN(in_len));
64 }
65  
66 void PacketProtoEncoder_Init (PacketProtoEncoder *enc, PacketRecvInterface *input, BPendingGroup *pg)
67 {
68 ASSERT(PacketRecvInterface_GetMTU(input) <= PACKETPROTO_MAXPAYLOAD)
69  
70 // init arguments
71 enc->input = input;
72  
73 // init input
74 PacketRecvInterface_Receiver_Init(enc->input, (PacketRecvInterface_handler_done)input_handler_done, enc);
75  
76 // init output
77 PacketRecvInterface_Init(
78 &enc->output, PACKETPROTO_ENCLEN(PacketRecvInterface_GetMTU(enc->input)),
79 (PacketRecvInterface_handler_recv)output_handler_recv, enc, pg
80 );
81  
82 // set no output packet
83 enc->output_packet = NULL;
84  
85 DebugObject_Init(&enc->d_obj);
86 }
87  
88 void PacketProtoEncoder_Free (PacketProtoEncoder *enc)
89 {
90 DebugObject_Free(&enc->d_obj);
91  
92 // free input
93 PacketRecvInterface_Free(&enc->output);
94 }
95  
96 PacketRecvInterface * PacketProtoEncoder_GetOutput (PacketProtoEncoder *enc)
97 {
98 DebugObject_Access(&enc->d_obj);
99  
100 return &enc->output;
101 }