BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file emncd.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 #include <string.h>
32  
33 #include <misc/version.h>
34 #include <misc/debug.h>
35 #include <base/BLog.h>
36 #include <system/BTime.h>
37 #include <system/BReactor.h>
38 #include <ncd/NCDInterpreter.h>
39 #include <ncd/NCDConfigParser.h>
40  
41 #include <generated/blog_channel_ncd.h>
42  
43 static BReactor reactor;
44 static int running;
45 static NCDInterpreter interpreter;
46  
47 static void interpreter_handler_finished (void *user, int exit_code)
48 {
49 ASSERT(running)
50  
51 fprintf(stderr, "--- interpreter terminated ---\n");
52  
53 NCDInterpreter_Free(&interpreter);
54  
55 running = 0;
56 }
57  
58 __attribute__((used))
59 int main ()
60 {
61 BLog_InitStderr();
62  
63 fprintf(stderr, "--- initializing emncd version "GLOBAL_VERSION" ---\n");
64  
65 BTime_Init();
66  
67 BReactor_EmscriptenInit(&reactor);
68  
69 running = 0;
70  
71 return 0;
72 }
73  
74 __attribute__((used))
75 void emncd_start (const char *program_text, int loglevel)
76 {
77 ASSERT(program_text);
78 ASSERT(loglevel >= 0);
79 ASSERT(loglevel <= BLOG_DEBUG);
80  
81 if (running) {
82 fprintf(stderr, "--- cannot start, interpreter is already running! ---\n");
83 return;
84 }
85  
86 for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
87 BLog_SetChannelLoglevel(i, loglevel);
88 }
89  
90 // parse program
91 NCDProgram program;
92 if (!NCDConfigParser_Parse((char *)program_text ,strlen(program_text), &program)) {
93 fprintf(stderr, "--- error: failed to parse the program ---\n");
94 return;
95 }
96  
97 // include commands are not implemented currently
98 if (NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE) || NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE_GUARD)) {
99 fprintf(stderr, "--- error: include mechanism is not supported in emncd ---\n");
100 NCDProgram_Free(&program);
101 return;
102 }
103  
104 fprintf(stderr, "--- starting interpreter ---\n");
105  
106 struct NCDInterpreter_params params;
107 params.handler_finished = interpreter_handler_finished;
108 params.user = NULL;
109 params.retry_time = 5000;
110 params.extra_args = NULL;
111 params.num_extra_args = 0;
112 params.reactor = &reactor;
113  
114 if (!NCDInterpreter_Init(&interpreter, program, params)) {
115 fprintf(stderr, "--- failed to initialize the interpreter ---\n");
116 return;
117 }
118  
119 running = 1;
120  
121 BReactor_EmscriptenSync(&reactor);
122 }
123  
124 __attribute__((used))
125 void emncd_stop (void)
126 {
127 if (!running) {
128 fprintf(stderr, "--- cannot request termination, interpreter is not running! ---\n");
129 return;
130 }
131  
132 fprintf(stderr, "--- requesting interpreter termination ---\n");
133  
134 NCDInterpreter_RequestShutdown(&interpreter, 0);
135  
136 BReactor_EmscriptenSync(&reactor);
137 }