aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_cmd_start_peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api_cmd_start_peer.c')
-rw-r--r--src/transport/transport_api_cmd_start_peer.c611
1 files changed, 0 insertions, 611 deletions
diff --git a/src/transport/transport_api_cmd_start_peer.c b/src/transport/transport_api_cmd_start_peer.c
deleted file mode 100644
index f4e92944b..000000000
--- a/src/transport/transport_api_cmd_start_peer.c
+++ /dev/null
@@ -1,611 +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_api_cmd_start_peer.c
23 * @brief cmd to start a peer.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29#include "gnunet_peerstore_service.h"
30#include "gnunet_transport_core_service.h"
31#include "gnunet_transport_application_service.h"
32#include "transport-testing-cmds.h"
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38
39
40static void
41retrieve_hello (void *cls);
42
43
44/**
45 * Callback delivering the hello of this peer from peerstore.
46 *
47 */
48static void
49hello_iter_cb (void *cb_cls,
50 const struct GNUNET_PEERSTORE_Record *record,
51 const char *emsg)
52{
53 struct StartPeerState *sps = cb_cls;
54 if (NULL == record)
55 {
56 sps->pic = NULL;
57 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
58 return;
59 }
60 // Check record type et al?
61 sps->hello_size = record->value_size;
62 sps->hello = GNUNET_malloc (sps->hello_size);
63 memcpy (sps->hello, record->value, sps->hello_size);
64 sps->hello[sps->hello_size - 1] = '\0';
65
66 GNUNET_PEERSTORE_iterate_cancel (sps->pic);
67 sps->pic = NULL;
68 sps->finished = GNUNET_YES;
69}
70
71
72/**
73 * Function to start the retrieval task to retrieve the hello of this peer
74 * from the peerstore.
75 *
76 */
77static void
78retrieve_hello (void *cls)
79{
80 struct StartPeerState *sps = cls;
81 sps->rh_task = NULL;
82 sps->pic = GNUNET_PEERSTORE_iterate (sps->ph,
83 "transport",
84 &sps->id,
85 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
86 hello_iter_cb,
87 sps);
88
89}
90
91
92/**
93 * This function checks StartPeerState#finished, which is set when the hello was retrieved.
94 *
95 */
96static int
97start_peer_finish (void *cls,
98 GNUNET_SCHEDULER_TaskCallback cont,
99 void *cont_cls)
100{
101 struct StartPeerState *sps = cls;
102
103 if (GNUNET_YES == sps->finished)
104 {
105 cont (cont_cls);
106 }
107
108 return sps->finished;
109}
110
111
112/**
113 * Disconnect callback for the connection to the core service.
114 *
115 */
116static void
117notify_disconnect (void *cls,
118 const struct GNUNET_PeerIdentity *peer,
119 void *handler_cls)
120{
121 struct StartPeerState *sps = cls;
122
123 LOG (GNUNET_ERROR_TYPE_DEBUG,
124 "Peer %s disconnected from peer %u (`%s')\n",
125 GNUNET_i2s (peer),
126 sps->no,
127 GNUNET_i2s (&sps->id));
128
129}
130
131
132/**
133 * Connect callback for the connection to the core service.
134 *
135 */
136static void *
137notify_connect (void *cls,
138 const struct GNUNET_PeerIdentity *peer,
139 struct GNUNET_MQ_Handle *mq)
140{
141 struct StartPeerState *sps = cls;
142 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
143 struct GNUNET_HashCode hc;
144 int node_number;
145
146 void *ret = NULL;
147
148
149 LOG (GNUNET_ERROR_TYPE_DEBUG,
150 "Peer %s connected to peer %u (`%s')\n",
151 GNUNET_i2s (peer),
152 sps->no,
153 GNUNET_i2s (&sps->id));
154
155 // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node.
156 node_number = 1;
157 GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc);
158
159
160 memcpy (key,
161 &hc,
162 sizeof (*key));
163 GNUNET_CONTAINER_multishortmap_put (sps->connected_peers_map,
164 key,
165 mq,
166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
167
168 GNUNET_free (key);
169 // TODO what does the handler function need?
170 return ret;
171}
172
173
174/**
175 * The run method of this cmd will start all services of a peer to test the transport service.
176 *
177 */
178static void
179start_peer_run (void *cls,
180 const struct GNUNET_TESTING_Command *cmd,
181 struct GNUNET_TESTING_Interpreter *is)
182{
183 struct StartPeerState *sps = cls;
184 char *emsg = NULL;
185 struct GNUNET_PeerIdentity dummy;
186 const struct GNUNET_TESTING_Command *system_cmd;
187 struct GNUNET_TESTING_System *tl_system;
188 char *home;
189 char *transport_unix_path;
190 char *communicator_unix_path;
191 char *bindto;
192
193 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
194 {
195 LOG (GNUNET_ERROR_TYPE_ERROR,
196 "File not found: `%s'\n",
197 sps->cfgname);
198 GNUNET_TESTING_interpreter_fail ();
199 return;
200 }
201
202
203 sps->cfg = GNUNET_CONFIGURATION_create ();
204 GNUNET_assert (GNUNET_OK ==
205 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
206
207 GNUNET_asprintf (&home,
208 "$GNUNET_TMP/test-transport/api-tcp-p%u",
209 sps->no);
210
211 GNUNET_asprintf (&transport_unix_path,
212 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
213 sps->no);
214
215 GNUNET_asprintf (&communicator_unix_path,
216 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
217 sps->no);
218
219 GNUNET_asprintf (&bindto,
220 "%s:60002",
221 sps->node_ip);
222
223
224 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
225 home);
226 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
227 transport_unix_path);
228 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
229 "BINDTO",
230 bindto);
231 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
232 "UNIXPATH",
233 communicator_unix_path);
234
235 system_cmd = GNUNET_TESTING_interpreter_lookup_command (sps->system_label);
236 GNUNET_TESTING_get_trait_test_system (system_cmd,
237 &tl_system);
238
239 sps->tl_system = tl_system;
240
241 if (GNUNET_SYSERR ==
242 GNUNET_TESTING_configuration_create (tl_system,
243 sps->cfg))
244 {
245 LOG (GNUNET_ERROR_TYPE_ERROR,
246 "Testing library failed to create unique configuration based on `%s'\n",
247 sps->cfgname);
248 GNUNET_CONFIGURATION_destroy (sps->cfg);
249 GNUNET_TESTING_interpreter_fail ();
250 return;
251 }
252
253 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system,
254 sps->cfg,
255 sps->no,
256 NULL,
257 &emsg);
258 if (NULL == sps->peer)
259 {
260 LOG (GNUNET_ERROR_TYPE_ERROR,
261 "Testing library failed to create unique configuration based on `%s': `%s'\n",
262 sps->cfgname,
263 emsg);
264 GNUNET_free (emsg);
265 GNUNET_TESTING_interpreter_fail ();
266 return;
267 }
268
269 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
270 {
271 LOG (GNUNET_ERROR_TYPE_ERROR,
272 "Testing library failed to create unique configuration based on `%s'\n",
273 sps->cfgname);
274 GNUNET_free (emsg);
275 GNUNET_TESTING_interpreter_fail ();
276 return;
277 }
278
279 memset (&dummy,
280 '\0',
281 sizeof(dummy));
282
283 GNUNET_TESTING_peer_get_identity (sps->peer,
284 &sps->id);
285
286 if (0 == memcmp (&dummy,
287 &sps->id,
288 sizeof(struct GNUNET_PeerIdentity)))
289 {
290 LOG (GNUNET_ERROR_TYPE_ERROR,
291 "Testing library failed to obtain peer identity for peer %u\n",
292 sps->no);
293 GNUNET_free (emsg);
294 GNUNET_TESTING_interpreter_fail ();
295 return;
296 }
297 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "Peer %u configured with identity `%s'\n",
299 sps->no,
300 GNUNET_i2s_full (&sps->id));
301
302 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
303 NULL,
304 sps->handlers,
305 sps,
306 &notify_connect,
307 &notify_disconnect);
308 if (NULL == sps->th)
309 {
310 LOG (GNUNET_ERROR_TYPE_ERROR,
311 "Failed to connect to transport service for peer `%s': `%s'\n",
312 sps->cfgname,
313 emsg);
314 GNUNET_free (emsg);
315 GNUNET_TESTING_interpreter_fail ();
316 return;
317 }
318
319 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
320 if (NULL == sps->th)
321 {
322 LOG (GNUNET_ERROR_TYPE_ERROR,
323 "Failed to connect to peerstore service for peer `%s': `%s'\n",
324 sps->cfgname,
325 emsg);
326 GNUNET_free (emsg);
327 GNUNET_TESTING_interpreter_fail ();
328 return;
329 }
330
331 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
332 if (NULL == sps->ah)
333 {
334 LOG (GNUNET_ERROR_TYPE_ERROR,
335 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
336 sps->cfgname,
337 emsg);
338 GNUNET_free (emsg);
339 GNUNET_TESTING_interpreter_fail ();
340 return;
341 }
342 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
343}
344
345
346/**
347 * The cleanup function of this cmd frees resources the cmd allocated.
348 *
349 */
350static void
351start_peer_cleanup (void *cls,
352 const struct GNUNET_TESTING_Command *cmd)
353{
354 struct StartPeerState *sps = cls;
355
356 if (NULL != sps->handlers)
357 {
358 GNUNET_free (sps->handlers);
359 sps->handlers = NULL;
360 }
361 if (NULL != sps->cfg)
362 {
363 GNUNET_CONFIGURATION_destroy (sps->cfg);
364 sps->cfg = NULL;
365 }
366 GNUNET_free (sps->hello);
367 GNUNET_free (sps->connected_peers_map);
368 GNUNET_free (sps);
369}
370
371
372/**
373 * This function prepares an array with traits.
374 *
375 */
376static int
377start_peer_traits (void *cls,
378 const void **ret,
379 const char *trait,
380 unsigned int index)
381{
382 struct StartPeerState *sps = cls;
383 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
384 struct GNUNET_PeerIdentity *id = &sps->id;
385 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
386 sps->connected_peers_map;
387 char *hello = sps->hello;
388 size_t hello_size = sps->hello_size;
389
390
391 struct GNUNET_TESTING_Trait traits[] = {
392 {
393 .index = 0,
394 .trait_name = "application_handle",
395 .ptr = (const void *) ah,
396 },
397 {
398 .index = 1,
399 .trait_name = "peer_id",
400 .ptr = (const void *) id,
401 },
402 {
403 .index = 2,
404 .trait_name = "connected_peers_map",
405 .ptr = (const void *) connected_peers_map,
406 },
407 {
408 .index = 3,
409 .trait_name = "hello",
410 .ptr = (const void *) hello,
411 },
412 {
413 .index = 4,
414 .trait_name = "hello_size",
415 .ptr = (const void *) hello_size,
416 },
417 {
418 .index = 5,
419 .trait_name = "state",
420 .ptr = (const void *) sps,
421 },
422 GNUNET_TESTING_trait_end ()
423 };
424
425 return GNUNET_TESTING_get_trait (traits,
426 ret,
427 trait,
428 index);
429}
430
431
432/**
433 * Function to get the trait with the struct StartPeerState.
434 *
435 * @param[out] sps struct StartPeerState.
436 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
437 *
438 */
439int
440GNUNET_TRANSPORT_get_trait_state (const struct
441 GNUNET_TESTING_Command
442 *cmd,
443 struct StartPeerState **sps)
444{
445 return cmd->traits (cmd->cls,
446 (const void **) sps,
447 "state",
448 (unsigned int) 5);
449}
450
451
452/**
453 * Function to get the trait with the size of the hello.
454 *
455 * @param[out] hello_size size of hello.
456 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
457 *
458 */
459int
460GNUNET_TRANSPORT_get_trait_hello_size (const struct
461 GNUNET_TESTING_Command
462 *cmd,
463 size_t **hello_size)
464{
465 return cmd->traits (cmd->cls,
466 (const void **) hello_size,
467 "hello_size",
468 (unsigned int) 4);
469}
470
471
472/**
473 * Function to get the trait with the hello.
474 *
475 * @param[out] hello The hello for the peer.
476 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
477 *
478 */
479int
480GNUNET_TRANSPORT_get_trait_hello (const struct
481 GNUNET_TESTING_Command
482 *cmd,
483 char **hello)
484{
485 return cmd->traits (cmd->cls,
486 (const void **) hello,
487 "hello",
488 (unsigned int) 3);
489}
490
491
492/**
493 * Function to get the trait with the map of connected peers.
494 *
495 * @param[out] connected_peers_map The map with connected peers.
496 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
497 *
498 */
499int
500GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct
501 GNUNET_TESTING_Command
502 *cmd,
503 struct
504 GNUNET_CONTAINER_MultiShortmap *
505 *
506 connected_peers_map)
507{
508 return cmd->traits (cmd->cls,
509 (const void **) connected_peers_map,
510 "connected_peers_map",
511 (unsigned int) 2);
512}
513
514
515/**
516 * Function to get the trait with the transport application handle.
517 *
518 * @param[out] ah The application handle.
519 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
520 */
521int
522GNUNET_TRANSPORT_get_trait_application_handle (const struct
523 GNUNET_TESTING_Command *cmd,
524 struct
525 GNUNET_TRANSPORT_ApplicationHandle
526 **ah)
527{
528 return cmd->traits (cmd->cls,
529 (const void **) ah,
530 "application_handle",
531 (unsigned int) 0);
532}
533
534
535/**
536 * Function to get the trait with the peer id.
537 *
538 * @param[out] id The peer id.
539 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
540 */
541int
542GNUNET_TRANSPORT_get_trait_peer_id (const struct
543 GNUNET_TESTING_Command *cmd,
544 struct GNUNET_PeerIdentity **id)
545{
546 return cmd->traits (cmd->cls,
547 (const void **) id,
548 "peer_id",
549 (unsigned int) 1);
550}
551
552
553/**
554 * Create command.
555 *
556 * @param label name for command.
557 * @param system_label Label of the cmd to setup a test environment.
558 * @param m The number of the local node of the actual network namespace.
559 * @param n The number of the actual namespace.
560 * @param local_m Number of local nodes in each namespace.
561 * @param handlers Handler for messages received by this peer.
562 * @param cfgname Configuration file name for this peer.
563 * @return command.
564 */
565struct GNUNET_TESTING_Command
566GNUNET_TRANSPORT_cmd_start_peer (const char *label,
567 const char *system_label,
568 char *m,
569 char *n,
570 char *local_m,
571 char *node_ip,
572 struct GNUNET_MQ_MessageHandler *handlers,
573 const char *cfgname)
574{
575 struct StartPeerState *sps;
576 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
577 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
578 unsigned int i;
579
580 sps = GNUNET_new (struct StartPeerState);
581 sps->m = m;
582 sps->n = n;
583 sps->local_m = local_m;
584 sps->no = (atoi (n) - 1) * atoi (sps->local_m) + atoi (m);
585 sps->system_label = system_label;
586 sps->connected_peers_map = connected_peers_map;
587 sps->cfgname = cfgname;
588 sps->node_ip = node_ip;
589
590 if (NULL != handlers)
591 {
592 for (i = 0; NULL != handlers[i].cb; i++)
593 ;
594 sps->handlers = GNUNET_new_array (i + 1,
595 struct GNUNET_MQ_MessageHandler);
596 GNUNET_memcpy (sps->handlers,
597 handlers,
598 i * sizeof(struct GNUNET_MQ_MessageHandler));
599 }
600
601 struct GNUNET_TESTING_Command cmd = {
602 .cls = sps,
603 .label = label,
604 .run = &start_peer_run,
605 .finish = &start_peer_finish,
606 .cleanup = &start_peer_cleanup,
607 .traits = &start_peer_traits
608 };
609
610 return cmd;
611}