BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file RandomPacketSink.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 | |||
30 | #ifndef _RANDOMPACKETSINK_H |
||
31 | #define _RANDOMPACKETSINK_H |
||
32 | |||
33 | #include <stdio.h> |
||
34 | |||
35 | #include <misc/debug.h> |
||
36 | #include <security/BRandom.h> |
||
37 | #include <system/BReactor.h> |
||
38 | #include <base/DebugObject.h> |
||
39 | #include <flow/PacketPassInterface.h> |
||
40 | |||
41 | typedef struct { |
||
42 | BReactor *reactor; |
||
43 | PacketPassInterface input; |
||
44 | BTimer timer; |
||
45 | DebugObject d_obj; |
||
46 | } RandomPacketSink; |
||
47 | |||
48 | static void _RandomPacketSink_input_handler_send (RandomPacketSink *s, uint8_t *data, int data_len) |
||
49 | { |
||
50 | DebugObject_Access(&s->d_obj); |
||
51 | |||
52 | printf("sink: send '"); |
||
53 | size_t res = fwrite(data, data_len, 1, stdout); |
||
54 | B_USE(res) |
||
55 | |||
56 | uint8_t r; |
||
57 | BRandom_randomize(&r, sizeof(r)); |
||
58 | if (r&(uint8_t)1) { |
||
59 | printf("' accepting\n"); |
||
60 | PacketPassInterface_Done(&s->input); |
||
61 | } else { |
||
62 | printf("' delaying\n"); |
||
63 | BReactor_SetTimer(s->reactor, &s->timer); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | static void _RandomPacketSink_input_handler_requestcancel (RandomPacketSink *s) |
||
68 | { |
||
69 | DebugObject_Access(&s->d_obj); |
||
70 | |||
71 | printf("sink: cancelled\n"); |
||
72 | BReactor_RemoveTimer(s->reactor, &s->timer); |
||
73 | PacketPassInterface_Done(&s->input); |
||
74 | } |
||
75 | |||
76 | static void _RandomPacketSink_timer_handler (RandomPacketSink *s) |
||
77 | { |
||
78 | DebugObject_Access(&s->d_obj); |
||
79 | |||
80 | PacketPassInterface_Done(&s->input); |
||
81 | } |
||
82 | |||
83 | static void RandomPacketSink_Init (RandomPacketSink *s, BReactor *reactor, int mtu, int ms) |
||
84 | { |
||
85 | // init arguments |
||
86 | s->reactor = reactor; |
||
87 | |||
88 | // init input |
||
89 | PacketPassInterface_Init(&s->input, mtu, (PacketPassInterface_handler_send)_RandomPacketSink_input_handler_send, s, BReactor_PendingGroup(reactor)); |
||
90 | PacketPassInterface_EnableCancel(&s->input, (PacketPassInterface_handler_requestcancel)_RandomPacketSink_input_handler_requestcancel); |
||
91 | |||
92 | // init timer |
||
93 | BTimer_Init(&s->timer, ms, (BTimer_handler)_RandomPacketSink_timer_handler, s); |
||
94 | |||
95 | DebugObject_Init(&s->d_obj); |
||
96 | } |
||
97 | |||
98 | static void RandomPacketSink_Free (RandomPacketSink *s) |
||
99 | { |
||
100 | DebugObject_Free(&s->d_obj); |
||
101 | |||
102 | // free timer |
||
103 | BReactor_RemoveTimer(s->reactor, &s->timer); |
||
104 | |||
105 | // free input |
||
106 | PacketPassInterface_Free(&s->input); |
||
107 | } |
||
108 | |||
109 | static PacketPassInterface * RandomPacketSink_GetInput (RandomPacketSink *s) |
||
110 | { |
||
111 | DebugObject_Access(&s->d_obj); |
||
112 | |||
113 | return &s->input; |
||
114 | } |
||
115 | |||
116 | #endif |