diff options
Diffstat (limited to 'src/testbed/testbed_api_cmd_netjail_start_testbed.c')
-rw-r--r-- | src/testbed/testbed_api_cmd_netjail_start_testbed.c | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/src/testbed/testbed_api_cmd_netjail_start_testbed.c b/src/testbed/testbed_api_cmd_netjail_start_testbed.c new file mode 100644 index 000000000..da51350a1 --- /dev/null +++ b/src/testbed/testbed_api_cmd_netjail_start_testbed.c | |||
@@ -0,0 +1,258 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2021 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file testing/testing_api_cmd_hello_world.c | ||
23 | * @brief Command to start the netjail peers. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_testing_ng_lib.h" | ||
28 | #include "gnunet_testbed_ng_service.h" | ||
29 | #include "testbed_api.h" | ||
30 | |||
31 | #define NETJAIL_EXEC_SCRIPT "./netjail_exec.sh" | ||
32 | |||
33 | struct NetJailState | ||
34 | { | ||
35 | |||
36 | /** | ||
37 | * The process handle | ||
38 | */ | ||
39 | struct GNUNET_HELPER_Handle *helper; | ||
40 | |||
41 | GNUNET_MessageTokenizerCallback cb; | ||
42 | |||
43 | GNUNET_HELPER_ExceptionCallback exp_cb; | ||
44 | |||
45 | char *binary_name; | ||
46 | |||
47 | char *local_m; | ||
48 | |||
49 | char *global_n; | ||
50 | |||
51 | char **binary_argv; | ||
52 | |||
53 | /** | ||
54 | * The send handle for the helper | ||
55 | */ | ||
56 | struct GNUNET_HELPER_SendHandle *shandle; | ||
57 | |||
58 | /** | ||
59 | * The message corresponding to send handle | ||
60 | */ | ||
61 | struct GNUNET_MessageHeader *msg; | ||
62 | }; | ||
63 | |||
64 | |||
65 | /** | ||
66 | * | ||
67 | * | ||
68 | * @param cls closure | ||
69 | * @param cmd current CMD being cleaned up. | ||
70 | */ | ||
71 | static void | ||
72 | netjail_exec_cleanup (void *cls, | ||
73 | const struct GNUNET_TESTING_Command *cmd) | ||
74 | { | ||
75 | struct NetJailState *ns = cls; | ||
76 | |||
77 | GNUNET_free (ns->binary_name); | ||
78 | } | ||
79 | |||
80 | |||
81 | /** | ||
82 | * | ||
83 | * | ||
84 | * @param cls closure. | ||
85 | * @param[out] ret result | ||
86 | * @param trait name of the trait. | ||
87 | * @param index index number of the object to offer. | ||
88 | * @return #GNUNET_OK on success. | ||
89 | */ | ||
90 | static int | ||
91 | netjail_exec_traits (void *cls, | ||
92 | const void **ret, | ||
93 | const char *trait, | ||
94 | unsigned int index) | ||
95 | { | ||
96 | return GNUNET_OK; | ||
97 | } | ||
98 | |||
99 | |||
100 | // TODO This would be a useful macro. | ||
101 | /** | ||
102 | * Function to join NULL terminated list of arguments | ||
103 | * | ||
104 | * @param argv1 the NULL terminated list of arguments. Cannot be NULL. | ||
105 | * @param argv2 the NULL terminated list of arguments. Cannot be NULL. | ||
106 | * @return the joined NULL terminated arguments | ||
107 | */ | ||
108 | static char ** | ||
109 | join_argv (const char *const *argv1, const char *const *argv2) | ||
110 | { | ||
111 | char **argvj; | ||
112 | char *argv; | ||
113 | unsigned int carg = 0; | ||
114 | unsigned int cnt; | ||
115 | |||
116 | carg = 0; | ||
117 | argvj = NULL; | ||
118 | for (cnt = 0; NULL != argv1[cnt]; cnt++) | ||
119 | { | ||
120 | argv = GNUNET_strdup (argv1[cnt]); | ||
121 | GNUNET_array_append (argvj, carg, argv); | ||
122 | } | ||
123 | for (cnt = 0; NULL != argv2[cnt]; cnt++) | ||
124 | { | ||
125 | argv = GNUNET_strdup (argv2[cnt]); | ||
126 | GNUNET_array_append (argvj, carg, argv); | ||
127 | } | ||
128 | GNUNET_array_append (argvj, carg, NULL); | ||
129 | return argvj; | ||
130 | } | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Continuation function from GNUNET_HELPER_send() | ||
135 | * | ||
136 | * @param cls closure | ||
137 | * @param result GNUNET_OK on success, | ||
138 | * GNUNET_NO if helper process died | ||
139 | * GNUNET_SYSERR during GNUNET_HELPER_stop | ||
140 | */ | ||
141 | static void | ||
142 | clear_msg (void *cls, int result) | ||
143 | { | ||
144 | struct NetJailState *ns = cls; | ||
145 | |||
146 | GNUNET_assert (NULL != ns->shandle); | ||
147 | ns->shandle = NULL; | ||
148 | GNUNET_free (ns->msg); | ||
149 | ns->msg = NULL; | ||
150 | } | ||
151 | |||
152 | |||
153 | /** | ||
154 | * Run the "hello world" CMD. | ||
155 | * | ||
156 | * @param cls closure. | ||
157 | * @param cmd CMD being run. | ||
158 | * @param is interpreter state. | ||
159 | */ | ||
160 | static void | ||
161 | netjail_exec_run (void *cls, | ||
162 | const struct GNUNET_TESTING_Command *cmd, | ||
163 | struct GNUNET_TESTING_Interpreter *is) | ||
164 | { | ||
165 | struct NetJailState *ns = cls; | ||
166 | char **helper_argv; | ||
167 | struct GNUNET_TESTBED_HelperInit *msg; | ||
168 | struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create (); | ||
169 | char *const script_argv[] = {NETJAIL_EXEC_SCRIPT, | ||
170 | ns->local_m, | ||
171 | "1", | ||
172 | "1", | ||
173 | NULL}; | ||
174 | GNUNET_MessageTokenizerCallback cb = ns->cb; | ||
175 | GNUNET_HELPER_ExceptionCallback exp_cb = ns->exp_cb; | ||
176 | |||
177 | if ((GNUNET_YES != GNUNET_DISK_file_test ("test_testbed_api.conf")) || | ||
178 | (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, | ||
179 | "test_testbed_api.conf"))) | ||
180 | { | ||
181 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
182 | _ ( | ||
183 | "Unreadable or malformed configuration file `%s', exit ...\n"), | ||
184 | "test_testbed_api.conf"); | ||
185 | } | ||
186 | |||
187 | helper_argv = join_argv ((const char **) script_argv, | ||
188 | (const char **) ns->binary_argv); | ||
189 | |||
190 | ns->helper = GNUNET_HELPER_start (GNUNET_YES, | ||
191 | NETJAIL_EXEC_SCRIPT, | ||
192 | helper_argv, | ||
193 | cb, | ||
194 | exp_cb, | ||
195 | ns); | ||
196 | |||
197 | msg = GNUNET_TESTBED_create_helper_init_msg_ ("127.0.0.1", NULL, cfg); | ||
198 | ns->msg = &msg->header; | ||
199 | ns->shandle = GNUNET_HELPER_send (ns->helper, &msg->header, GNUNET_NO, | ||
200 | &clear_msg, ns); | ||
201 | if (NULL == ns->shandle) | ||
202 | { | ||
203 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
204 | "Send handle is NULL!\n"); | ||
205 | GNUNET_free (msg); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | |||
210 | /** | ||
211 | * Create command. | ||
212 | * | ||
213 | * @param label name for command. | ||
214 | * @param binaryname to exec. | ||
215 | * @return command. | ||
216 | */ | ||
217 | struct GNUNET_TESTING_Command | ||
218 | GNUNET_TESTBED_cmd_netjail_start_testbed (const char *label, | ||
219 | char *const binary_argv[], | ||
220 | char *local_m, | ||
221 | char *global_n, | ||
222 | GNUNET_MessageTokenizerCallback cb, | ||
223 | GNUNET_HELPER_ExceptionCallback exp_cb) | ||
224 | { | ||
225 | struct NetJailState *ns; | ||
226 | unsigned int append_cnt; | ||
227 | char **argvj; | ||
228 | char *argv; | ||
229 | unsigned int carg = 0; | ||
230 | |||
231 | ns = GNUNET_new (struct NetJailState); | ||
232 | argvj = NULL; | ||
233 | for (append_cnt = 0; NULL != binary_argv[append_cnt]; append_cnt++) | ||
234 | { | ||
235 | argv = GNUNET_strdup (binary_argv[append_cnt]); | ||
236 | GNUNET_array_append (argvj, | ||
237 | carg, | ||
238 | argv); | ||
239 | } | ||
240 | GNUNET_array_append (argvj, carg, NULL); | ||
241 | |||
242 | ns->binary_argv = argvj; | ||
243 | |||
244 | ns->local_m = local_m; | ||
245 | ns->global_n = global_n; | ||
246 | ns->cb = cb; | ||
247 | ns->exp_cb = exp_cb; | ||
248 | |||
249 | struct GNUNET_TESTING_Command cmd = { | ||
250 | .cls = ns, | ||
251 | .label = label, | ||
252 | .run = &netjail_exec_run, | ||
253 | .cleanup = &netjail_exec_cleanup, | ||
254 | .traits = &netjail_exec_traits | ||
255 | }; | ||
256 | |||
257 | return cmd; | ||
258 | } | ||