BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file backtrack.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 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Synopsis: |
||
32 | * backtrack_point() |
||
33 | * backtrack_point::go() |
||
34 | * backtrack_point::rgo() |
||
35 | * |
||
36 | * Description: |
||
37 | * The backtrack_point() statement creates a backtrack point, going up immedietely. |
||
38 | * The go() method triggers backtracking to the backtrack point, i.e. makes the |
||
39 | * backtrack_point() statement go down and back up at atomically. The go() method |
||
40 | * itself goes up immedietely, but side effects of triggering backtracking have |
||
41 | * priority. |
||
42 | * The rgo() method triggers backtracking when it deinitializes. In this case, |
||
43 | * the immediate effects of triggering backtracking in the backtrack_point() have |
||
44 | * priority over the immediate effects of rgo() deinitialization completion. |
||
45 | */ |
||
46 | |||
47 | #include <ncd/module_common.h> |
||
48 | |||
49 | #include <generated/blog_channel_ncd_backtrack.h> |
||
50 | |||
51 | struct rgo_instance { |
||
52 | NCDModuleInst *i; |
||
53 | NCDModuleRef bp_ref; |
||
54 | }; |
||
55 | |||
56 | static void func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
57 | { |
||
58 | // check arguments |
||
59 | if (!NCDVal_ListRead(params->args, 0)) { |
||
60 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
61 | goto fail0; |
||
62 | } |
||
63 | |||
64 | // go up |
||
65 | NCDModuleInst_Backend_Up(i); |
||
66 | return; |
||
67 | |||
68 | fail0: |
||
69 | NCDModuleInst_Backend_DeadError(i); |
||
70 | } |
||
71 | |||
72 | static void go_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
73 | { |
||
74 | // check arguments |
||
75 | if (!NCDVal_ListRead(params->args, 0)) { |
||
76 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
77 | goto fail0; |
||
78 | } |
||
79 | |||
80 | // get backtrack point |
||
81 | NCDModuleInst *backtrack_point_inst = params->method_user; |
||
82 | |||
83 | // go up (after toggling) |
||
84 | NCDModuleInst_Backend_Up(i); |
||
85 | |||
86 | // toggle backtrack point |
||
87 | NCDModuleInst_Backend_DownUp(backtrack_point_inst); |
||
88 | return; |
||
89 | |||
90 | fail0: |
||
91 | NCDModuleInst_Backend_DeadError(i); |
||
92 | } |
||
93 | |||
94 | static void rgo_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
95 | { |
||
96 | struct rgo_instance *o = vo; |
||
97 | o->i = i; |
||
98 | |||
99 | // check arguments |
||
100 | if (!NCDVal_ListRead(params->args, 0)) { |
||
101 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
102 | goto fail0; |
||
103 | } |
||
104 | |||
105 | // get backtrack point |
||
106 | NCDModuleInst *backtrack_point_inst = params->method_user; |
||
107 | |||
108 | // init object reference to the backtrack_point |
||
109 | NCDModuleRef_Init(&o->bp_ref, backtrack_point_inst); |
||
110 | |||
111 | // go up |
||
112 | NCDModuleInst_Backend_Up(i); |
||
113 | return; |
||
114 | |||
115 | fail0: |
||
116 | NCDModuleInst_Backend_DeadError(i); |
||
117 | } |
||
118 | |||
119 | static void rgo_func_die (void *vo) |
||
120 | { |
||
121 | struct rgo_instance *o = vo; |
||
122 | |||
123 | // deref backtrack_point |
||
124 | NCDModuleInst *backtrack_point_inst = NCDModuleRef_Deref(&o->bp_ref); |
||
125 | |||
126 | // free object reference |
||
127 | NCDModuleRef_Free(&o->bp_ref); |
||
128 | |||
129 | // die |
||
130 | NCDModuleInst_Backend_Dead(o->i); |
||
131 | |||
132 | // toggle backtrack_point |
||
133 | if (backtrack_point_inst) { |
||
134 | NCDModuleInst_Backend_DownUp(backtrack_point_inst); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | static struct NCDModule modules[] = { |
||
139 | { |
||
140 | .type = "backtrack_point", |
||
141 | .func_new2 = func_new |
||
142 | }, { |
||
143 | .type = "backtrack_point::go", |
||
144 | .func_new2 = go_func_new |
||
145 | }, { |
||
146 | .type = "backtrack_point::rgo", |
||
147 | .func_new2 = rgo_func_new, |
||
148 | .func_die = rgo_func_die, |
||
149 | .alloc_size = sizeof(struct rgo_instance) |
||
150 | }, { |
||
151 | .type = NULL |
||
152 | } |
||
153 | }; |
||
154 | |||
155 | const struct NCDModuleGroup ncdmodule_backtrack = { |
||
156 | .modules = modules |
||
157 | }; |