aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_cmd_netjail_start_testbed.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/testbed_api_cmd_netjail_start_testbed.c')
-rw-r--r--src/testbed/testbed_api_cmd_netjail_start_testbed.c544
1 files changed, 0 insertions, 544 deletions
diff --git a/src/testbed/testbed_api_cmd_netjail_start_testbed.c b/src/testbed/testbed_api_cmd_netjail_start_testbed.c
deleted file mode 100644
index 3d1a1d893..000000000
--- a/src/testbed/testbed_api_cmd_netjail_start_testbed.c
+++ /dev/null
@@ -1,544 +0,0 @@
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#include "testbed_api_hosts.h"
31#include "testbed_helper.h"
32
33#define NETJAIL_EXEC_SCRIPT "./../testing/netjail_exec.sh"
34
35struct HelperMessage;
36
37struct HelperMessage
38{
39
40 struct HelperMessage *next;
41
42 struct HelperMessage *prev;
43
44 /**
45 * Size of the original message.
46 */
47 uint16_t bytes_msg;
48
49 /* Followed by @e bytes_msg of msg.*/
50};
51
52
53
54struct NetJailState
55{
56
57 unsigned int *rv;
58
59 struct HelperMessage *hp_messages_head;
60
61 struct HelperMessage *hp_messages_tail;
62
63 /**
64 * The process handle
65 */
66 struct GNUNET_HELPER_Handle **helper;
67
68 unsigned int n_helper;
69
70 char *binary_name;
71
72 char *local_m;
73
74 char *global_n;
75
76 /**
77 * The send handle for the helper
78 */
79 struct GNUNET_HELPER_SendHandle **shandle;
80
81 unsigned int n_shandle;
82
83 /**
84 * The message corresponding to send handle
85 */
86 struct GNUNET_MessageHeader **msg;
87
88 unsigned int n_msg;
89
90 unsigned int number_of_testsystems_started;
91
92 unsigned int number_of_peers_started;
93
94 unsigned int number_of_local_test_finished;
95
96 char *plugin_name;
97};
98
99struct TestingSystemCount
100{
101 unsigned int count;
102
103 struct NetJailState *ns;
104};
105
106/**
107*
108*
109* @param cls closure
110* @param cmd current CMD being cleaned up.
111*/
112static void
113netjail_exec_cleanup (void *cls,
114 const struct GNUNET_TESTING_Command *cmd)
115{
116 struct NetJailState *ns = cls;
117
118 GNUNET_free (ns->binary_name);
119}
120
121
122/**
123*
124*
125* @param cls closure.
126* @param[out] ret result
127* @param trait name of the trait.
128* @param index index number of the object to offer.
129* @return #GNUNET_OK on success.
130*/
131static int
132netjail_exec_traits (void *cls,
133 const void **ret,
134 const char *trait,
135 unsigned int index)
136{
137 struct NetJailState *ns = cls;
138 struct GNUNET_HELPER_Handle **helper = ns->helper;
139 struct HelperMessage *hp_messages_head = ns->hp_messages_head;
140
141
142 struct GNUNET_TESTING_Trait traits[] = {
143 {
144 .index = 0,
145 .trait_name = "helper_handles",
146 .ptr = (const void *) helper,
147 },
148 {
149 .index = 1,
150 .trait_name = "hp_msgs_head",
151 .ptr = (const void *) hp_messages_head,
152 },
153 GNUNET_TESTING_trait_end ()
154 };
155
156 return GNUNET_TESTING_get_trait (traits,
157 ret,
158 trait,
159 index);
160}
161
162
163/**
164 * Offer handles to testing cmd helper from trait
165 *
166 * @param cmd command to extract the message from.
167 * @param pt pointer to message.
168 * @return #GNUNET_OK on success.
169 */
170int
171GNUNET_TESTING_get_trait_helper_handles (const struct
172 GNUNET_TESTING_Command *cmd,
173 struct GNUNET_HELPER_Handle ***helper)
174{
175 return cmd->traits (cmd->cls,
176 (const void **) helper,
177 "helper_handles",
178 (unsigned int) 0);
179}
180
181/**
182 * Offer messages received via testing cmd helper from trait
183 *
184 * @param cmd command to extract the message from.
185 * @param pt pointer to message.
186 * @return #GNUNET_OK on success.
187 */
188int
189GNUNET_TESTING_get_trait_helper_messages (const struct
190 GNUNET_TESTING_Command *cmd,
191 struct HelperMessage ***
192 hp_messages_head)
193{
194 return cmd->traits (cmd->cls,
195 (const void **) hp_messages_head,
196 "hp_msgs_head",
197 (unsigned int) 1);
198}
199
200
201/**
202 * Continuation function from GNUNET_HELPER_send()
203 *
204 * @param cls closure
205 * @param result GNUNET_OK on success,
206 * GNUNET_NO if helper process died
207 * GNUNET_SYSERR during GNUNET_HELPER_stop
208 */
209static void
210clear_msg (void *cls, int result)
211{
212 struct TestingSystemCount *tbc = cls;
213 struct NetJailState *ns = tbc->ns;
214
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "clear_msg tbc->count: %d\n",
217 tbc->count);
218 GNUNET_assert (NULL != ns->shandle[tbc->count - 1]);
219 ns->shandle[tbc->count - 1] = NULL;
220 GNUNET_free (ns->msg[tbc->count - 1]);
221 ns->msg[tbc->count - 1] = NULL;
222}
223
224
225/**
226 * Functions with this signature are called whenever a
227 * complete message is received by the tokenizer.
228 *
229 * Do not call GNUNET_SERVER_mst_destroy in callback
230 *
231 * @param cls closure
232 * @param client identification of the client
233 * @param message the actual message
234 *
235 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
236 */
237static int
238helper_mst (void *cls, const struct GNUNET_MessageHeader *message)
239{
240 struct TestingSystemCount *tbc = cls;
241 struct NetJailState *ns = tbc->ns;
242 struct HelperMessage *hp_msg;
243
244 if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY == ntohs (message->type))
245 {
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "helper_mst tbc->count: %d\n",
248 tbc->count);
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "Received message from helper.\n");
251 ns->number_of_testsystems_started++;
252 }
253 else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_PEER_STARTED == ntohs (
254 message->type))
255 {
256 ns->number_of_peers_started++;
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 "number_of_peers_started: %d\n",
259 ns->number_of_peers_started);
260 }
261 else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED == ntohs (
262 message->type))
263 {
264 ns->number_of_local_test_finished++;
265 }
266 else
267 {
268 hp_msg = GNUNET_new (struct HelperMessage);
269 hp_msg->bytes_msg = message->size;
270 memcpy (&hp_msg[1], message, message->size);
271 GNUNET_CONTAINER_DLL_insert (ns->hp_messages_head, ns->hp_messages_tail,
272 hp_msg);
273 }
274
275 return GNUNET_OK;
276}
277
278
279static void
280exp_cb (void *cls)
281{
282 struct NetJailState *ns = cls;
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called exp_cb.\n");
284 *ns->rv = 1;
285}
286
287
288static struct GNUNET_CMDS_HelperInit *
289create_helper_init_msg_ (char *m_char,
290 char *n_char,
291 const char *plugin_name)
292{
293 struct GNUNET_CMDS_HelperInit *msg;
294 uint16_t plugin_name_len;
295 uint16_t msg_size;
296
297 GNUNET_assert (NULL != plugin_name);
298 plugin_name_len = strlen (plugin_name);
299 msg_size = sizeof(struct GNUNET_CMDS_HelperInit) + plugin_name_len;
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "msg_size: %d \n",
302 msg_size);
303 msg = GNUNET_malloc (msg_size);
304 msg->header.size = htons (msg_size);
305 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_INIT);
306 msg->plugin_name_size = htons (plugin_name_len);
307 GNUNET_memcpy ((char *) &msg[1],
308 plugin_name,
309 plugin_name_len);
310 return msg;
311}
312
313
314static void
315start_helper (struct NetJailState *ns, struct
316 GNUNET_CONFIGURATION_Handle *config,
317 char *m_char,
318 char *n_char)
319{
320 // struct GNUNET_CONFIGURATION_Handle *cfg;
321 struct GNUNET_CMDS_HelperInit *msg;
322 struct TestingSystemCount *tbc;
323 char *const script_argv[] = {NETJAIL_EXEC_SCRIPT,
324 m_char,
325 n_char,
326 GNUNET_OS_get_libexec_binary_path (
327 HELPER_CMDS_BINARY),
328 ns->global_n,
329 ns->local_m,
330 NULL};
331 unsigned int m = atoi (m_char);
332 unsigned int n = atoi (n_char);
333 unsigned int helper_check = GNUNET_OS_check_helper_binary (
334 NETJAIL_EXEC_SCRIPT,
335 GNUNET_YES,
336 NULL);
337
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "m: %d n: %d\n",
340 m,
341 n);
342
343 tbc = GNUNET_new (struct TestingSystemCount);
344 tbc->ns = ns;
345 tbc->count = (n - 1) * atoi (ns->local_m) + m;
346
347
348 if (GNUNET_NO == helper_check)
349 {
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 "No SUID for %s!\n",
352 NETJAIL_EXEC_SCRIPT);
353 *ns->rv = 1;
354 }
355 else if (GNUNET_NO == helper_check)
356 {
357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
358 "%s not found!\n",
359 NETJAIL_EXEC_SCRIPT);
360 *ns->rv = 1;
361 }
362
363 GNUNET_array_append (ns->helper, ns->n_helper, GNUNET_HELPER_start (
364 GNUNET_YES,
365 NETJAIL_EXEC_SCRIPT,
366 script_argv,
367 &helper_mst,
368 &exp_cb,
369 tbc));
370
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "First using helper %d %d\n",
373 tbc->count - 1,
374 ns->n_helper);
375 struct GNUNET_HELPER_Handle *helper = ns->helper[tbc->count - 1];
376
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "First using helper %d %d %p\n",
379 tbc->count - 1,
380 ns->n_helper,
381 helper);
382
383 msg = create_helper_init_msg_ (m_char,
384 n_char,
385 ns->plugin_name);
386 GNUNET_array_append (ns->msg, ns->n_msg, &msg->header);
387
388 GNUNET_array_append (ns->shandle, ns->n_shandle, GNUNET_HELPER_send (
389 helper,
390 &msg->header,
391 GNUNET_NO,
392 &clear_msg,
393 tbc));
394
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Message %d send!\n",
397 tbc->count);
398
399 if (NULL == ns->shandle[tbc->count - 1])
400 {
401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
402 "Send handle is NULL!\n");
403 GNUNET_free (msg);
404 *ns->rv = 1;
405 }
406}
407
408
409/**
410* Run the "hello world" CMD.
411*
412* @param cls closure.
413* @param cmd CMD being run.
414* @param is interpreter state.
415*/
416static void
417netjail_exec_run (void *cls,
418 const struct GNUNET_TESTING_Command *cmd,
419 struct GNUNET_TESTING_Interpreter *is)
420{
421 char str_m[12];
422 char str_n[12];
423 struct NetJailState *ns = cls;
424 struct GNUNET_CONFIGURATION_Handle *config =
425 GNUNET_CONFIGURATION_create ();
426
427 for (int i = 1; i <= atoi (ns->global_n); i++) {
428 for (int j = 1; j <= atoi (ns->local_m); j++)
429 {
430 sprintf (str_n, "%d", i);
431 sprintf (str_m, "%d", j);
432 start_helper (ns, config,
433 str_m,
434 str_n);
435 }
436 }
437}
438
439
440static int
441netjail_start_finish (void *cls,
442 GNUNET_SCHEDULER_TaskCallback cont,
443 void *cont_cls)
444{
445 unsigned int ret = GNUNET_NO;
446 struct NetJailState *ns = cls;
447 unsigned int total_number = atoi (ns->local_m) * atoi (ns->global_n);
448 struct GNUNET_CMDS_ALL_PEERS_STARTED *reply;
449 size_t msg_length;
450 struct GNUNET_HELPER_Handle *helper;
451 struct TestingSystemCount *tbc;
452
453 if (ns->number_of_local_test_finished == total_number)
454 {
455 ret = GNUNET_YES;
456 cont (cont_cls);
457 }
458
459 if (ns->number_of_testsystems_started == total_number)
460 {
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "All helpers started!\n");
463 ns->number_of_testsystems_started = 0;
464 }
465
466 if (ns->number_of_peers_started == total_number)
467 {
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469 "All peers started!\n");
470
471 for (int i = 1; i <= atoi (ns->global_n); i++) {
472 for (int j = 1; j <= atoi (ns->local_m); j++)
473 {
474 tbc = GNUNET_new (struct TestingSystemCount);
475 tbc->ns = ns;
476 // TODO This needs to be more generic. As we send more messages back and forth, we can not grow the arrays again and again, because this is to error prone.
477 tbc->count = (i - 1) * atoi (ns->local_m) + j + total_number;
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Second using helper %d %d %d\n",
480 tbc->count - 1 - total_number,
481 i,
482 j);
483 helper = ns->helper[tbc->count - 1 - total_number];
484 msg_length = sizeof(struct GNUNET_CMDS_ALL_PEERS_STARTED);
485 reply = GNUNET_new (struct GNUNET_CMDS_ALL_PEERS_STARTED);
486 reply->header.type = htons (
487 GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED);
488 reply->header.size = htons ((uint16_t) msg_length);
489
490 GNUNET_array_append (ns->msg, ns->n_msg, &reply->header);
491
492 struct GNUNET_HELPER_SendHandle *sh = GNUNET_HELPER_send (
493 helper,
494 &reply->header,
495 GNUNET_NO,
496 &clear_msg,
497 tbc);
498
499 GNUNET_array_append (ns->shandle, ns->n_shandle, sh);
500
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "All peers started message %d send!\n",
503 tbc->count);
504 }
505 }
506 ns->number_of_peers_started = 0;
507 }
508 return ret;
509}
510
511
512/**
513 * Create command.
514 *
515 * @param label name for command.
516 * @param binaryname to exec.
517 * @return command.
518 */
519struct GNUNET_TESTING_Command
520GNUNET_TESTING_cmd_netjail_start_testing_system (const char *label,
521 char *local_m,
522 char *global_n,
523 char *plugin_name,
524 unsigned int *rv)
525{
526 struct NetJailState *ns;
527
528 ns = GNUNET_new (struct NetJailState);
529 ns->local_m = local_m;
530 ns->global_n = global_n;
531 ns->plugin_name = plugin_name;
532 ns->rv = rv;
533
534 struct GNUNET_TESTING_Command cmd = {
535 .cls = ns,
536 .label = label,
537 .run = &netjail_exec_run,
538 .finish = &netjail_start_finish,
539 .cleanup = &netjail_exec_cleanup,
540 .traits = &netjail_exec_traits
541 };
542
543 return cmd;
544}