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