BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file PacketRecvInterface.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 * Interface allowing a packet receiver to receive data packets from a packet sender.
32 */
33  
34 #ifndef BADVPN_FLOW_PACKETRECVINTERFACE_H
35 #define BADVPN_FLOW_PACKETRECVINTERFACE_H
36  
37 #include <stdint.h>
38 #include <stddef.h>
39  
40 #include <misc/debug.h>
41 #include <base/DebugObject.h>
42 #include <base/BPending.h>
43  
44 #define PRI_STATE_NONE 1
45 #define PRI_STATE_OPERATION_PENDING 2
46 #define PRI_STATE_BUSY 3
47 #define PRI_STATE_DONE_PENDING 4
48  
49 typedef void (*PacketRecvInterface_handler_recv) (void *user, uint8_t *data);
50  
51 typedef void (*PacketRecvInterface_handler_done) (void *user, int data_len);
52  
53 typedef struct {
54 // provider data
55 int mtu;
56 PacketRecvInterface_handler_recv handler_operation;
57 void *user_provider;
58  
59 // user data
60 PacketRecvInterface_handler_done handler_done;
61 void *user_user;
62  
63 // operation job
64 BPending job_operation;
65 uint8_t *job_operation_data;
66  
67 // done job
68 BPending job_done;
69 int job_done_len;
70  
71 // state
72 int state;
73  
74 DebugObject d_obj;
75 } PacketRecvInterface;
76  
77 static void PacketRecvInterface_Init (PacketRecvInterface *i, int mtu, PacketRecvInterface_handler_recv handler_operation, void *user, BPendingGroup *pg);
78  
79 static void PacketRecvInterface_Free (PacketRecvInterface *i);
80  
81 static void PacketRecvInterface_Done (PacketRecvInterface *i, int data_len);
82  
83 static int PacketRecvInterface_GetMTU (PacketRecvInterface *i);
84  
85 static void PacketRecvInterface_Receiver_Init (PacketRecvInterface *i, PacketRecvInterface_handler_done handler_done, void *user);
86  
87 static void PacketRecvInterface_Receiver_Recv (PacketRecvInterface *i, uint8_t *data);
88  
89 void _PacketRecvInterface_job_operation (PacketRecvInterface *i);
90 void _PacketRecvInterface_job_done (PacketRecvInterface *i);
91  
92 void PacketRecvInterface_Init (PacketRecvInterface *i, int mtu, PacketRecvInterface_handler_recv handler_operation, void *user, BPendingGroup *pg)
93 {
94 ASSERT(mtu >= 0)
95  
96 // init arguments
97 i->mtu = mtu;
98 i->handler_operation = handler_operation;
99 i->user_provider = user;
100  
101 // set no user
102 i->handler_done = NULL;
103  
104 // init jobs
105 BPending_Init(&i->job_operation, pg, (BPending_handler)_PacketRecvInterface_job_operation, i);
106 BPending_Init(&i->job_done, pg, (BPending_handler)_PacketRecvInterface_job_done, i);
107  
108 // set state
109 i->state = PRI_STATE_NONE;
110  
111 DebugObject_Init(&i->d_obj);
112 }
113  
114 void PacketRecvInterface_Free (PacketRecvInterface *i)
115 {
116 DebugObject_Free(&i->d_obj);
117  
118 // free jobs
119 BPending_Free(&i->job_done);
120 BPending_Free(&i->job_operation);
121 }
122  
123 void PacketRecvInterface_Done (PacketRecvInterface *i, int data_len)
124 {
125 ASSERT(data_len >= 0)
126 ASSERT(data_len <= i->mtu)
127 ASSERT(i->state == PRI_STATE_BUSY)
128 DebugObject_Access(&i->d_obj);
129  
130 // schedule done
131 i->job_done_len = data_len;
132 BPending_Set(&i->job_done);
133  
134 // set state
135 i->state = PRI_STATE_DONE_PENDING;
136 }
137  
138 int PacketRecvInterface_GetMTU (PacketRecvInterface *i)
139 {
140 DebugObject_Access(&i->d_obj);
141  
142 return i->mtu;
143 }
144  
145 void PacketRecvInterface_Receiver_Init (PacketRecvInterface *i, PacketRecvInterface_handler_done handler_done, void *user)
146 {
147 ASSERT(handler_done)
148 ASSERT(!i->handler_done)
149 DebugObject_Access(&i->d_obj);
150  
151 i->handler_done = handler_done;
152 i->user_user = user;
153 }
154  
155 void PacketRecvInterface_Receiver_Recv (PacketRecvInterface *i, uint8_t *data)
156 {
157 ASSERT(!(i->mtu > 0) || data)
158 ASSERT(i->state == PRI_STATE_NONE)
159 ASSERT(i->handler_done)
160 DebugObject_Access(&i->d_obj);
161  
162 // schedule operation
163 i->job_operation_data = data;
164 BPending_Set(&i->job_operation);
165  
166 // set state
167 i->state = PRI_STATE_OPERATION_PENDING;
168 }
169  
170 #endif