aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_cmd_start_peer_v3.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api_cmd_start_peer_v3.c')
-rw-r--r--src/transport/transport_api_cmd_start_peer_v3.c619
1 files changed, 619 insertions, 0 deletions
diff --git a/src/transport/transport_api_cmd_start_peer_v3.c b/src/transport/transport_api_cmd_start_peer_v3.c
new file mode 100644
index 000000000..b5cefecc8
--- /dev/null
+++ b/src/transport/transport_api_cmd_start_peer_v3.c
@@ -0,0 +1,619 @@
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_v2 *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_v2 *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_v2#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_v2 *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_v2 *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_v2 *sps = cls;
142 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
143 struct GNUNET_HashCode hc;
144 struct GNUNET_CRYPTO_EddsaPublicKey public_key = peer->public_key;
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 GNUNET_CRYPTO_hash (&public_key, sizeof(public_key), &hc);
156
157
158 memcpy (key,
159 &hc,
160 sizeof (*key));
161 GNUNET_CONTAINER_multishortmap_put (sps->connected_peers_map,
162 key,
163 mq,
164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
165
166 GNUNET_free (key);
167 // TODO what does the handler function need?
168 return ret;
169}
170
171
172/**
173 * The run method of this cmd will start all services of a peer to test the transport service.
174 *
175 */
176static void
177start_peer_run (void *cls,
178 const struct GNUNET_TESTING_Command *cmd,
179 struct GNUNET_TESTING_Interpreter *is)
180{
181 struct StartPeerState_v2 *sps = cls;
182 char *emsg = NULL;
183 struct GNUNET_PeerIdentity dummy;
184 const struct GNUNET_TESTING_Command *system_cmd;
185 struct GNUNET_TESTING_System *tl_system;
186 char *home;
187 char *transport_unix_path;
188 char *tcp_communicator_unix_path;
189 char *udp_communicator_unix_path;
190 char *bindto;
191
192 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
193 {
194 LOG (GNUNET_ERROR_TYPE_ERROR,
195 "File not found: `%s'\n",
196 sps->cfgname);
197 GNUNET_TESTING_interpreter_fail ();
198 return;
199 }
200
201
202 sps->cfg = GNUNET_CONFIGURATION_create ();
203 GNUNET_assert (GNUNET_OK ==
204 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
205
206 GNUNET_asprintf (&home,
207 "$GNUNET_TMP/test-transport/api-tcp-p%u",
208 sps->no);
209
210 GNUNET_asprintf (&transport_unix_path,
211 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
212 sps->no);
213
214 GNUNET_asprintf (&tcp_communicator_unix_path,
215 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
216 sps->no);
217
218 GNUNET_asprintf (&udp_communicator_unix_path,
219 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
220 sps->no);
221
222 GNUNET_asprintf (&bindto,
223 "%s:60002",
224 sps->node_ip);
225
226 LOG (GNUNET_ERROR_TYPE_ERROR,
227 "node_ip %s\n",
228 bindto);
229
230 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
231 home);
232 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
233 transport_unix_path);
234 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
235 "BINDTO",
236 bindto);
237 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
238 "BINDTO",
239 bindto);
240 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
241 "UNIXPATH",
242 tcp_communicator_unix_path);
243 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
244 "UNIXPATH",
245 udp_communicator_unix_path);
246
247 system_cmd = GNUNET_TESTING_interpreter_lookup_command (sps->system_label);
248 GNUNET_TESTING_get_trait_test_system (system_cmd,
249 &tl_system);
250
251 sps->tl_system = tl_system;
252
253 if (GNUNET_SYSERR ==
254 GNUNET_TESTING_configuration_create (tl_system,
255 sps->cfg))
256 {
257 LOG (GNUNET_ERROR_TYPE_ERROR,
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 ();
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'\n",
274 sps->cfgname,
275 emsg);
276 GNUNET_free (emsg);
277 GNUNET_TESTING_interpreter_fail ();
278 return;
279 }
280
281 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
282 {
283 LOG (GNUNET_ERROR_TYPE_ERROR,
284 "Testing library failed to create unique configuration based on `%s'\n",
285 sps->cfgname);
286 GNUNET_free (emsg);
287 GNUNET_TESTING_interpreter_fail ();
288 return;
289 }
290
291 memset (&dummy,
292 '\0',
293 sizeof(dummy));
294
295 GNUNET_TESTING_peer_get_identity (sps->peer,
296 &sps->id);
297
298 if (0 == memcmp (&dummy,
299 &sps->id,
300 sizeof(struct GNUNET_PeerIdentity)))
301 {
302 LOG (GNUNET_ERROR_TYPE_ERROR,
303 "Testing library failed to obtain peer identity for peer %u\n",
304 sps->no);
305 GNUNET_free (emsg);
306 GNUNET_TESTING_interpreter_fail ();
307 return;
308 }
309 LOG (GNUNET_ERROR_TYPE_DEBUG,
310 "Peer %u configured with identity `%s'\n",
311 sps->no,
312 GNUNET_i2s_full (&sps->id));
313
314 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
315 NULL,
316 sps->handlers,
317 sps,
318 &notify_connect,
319 &notify_disconnect);
320 if (NULL == sps->th)
321 {
322 LOG (GNUNET_ERROR_TYPE_ERROR,
323 "Failed to connect to transport 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->ph = GNUNET_PEERSTORE_connect (sps->cfg);
332 if (NULL == sps->th)
333 {
334 LOG (GNUNET_ERROR_TYPE_ERROR,
335 "Failed to connect to peerstore service for peer `%s': `%s'\n",
336 sps->cfgname,
337 emsg);
338 GNUNET_free (emsg);
339 GNUNET_TESTING_interpreter_fail ();
340 return;
341 }
342
343 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
344 if (NULL == sps->ah)
345 {
346 LOG (GNUNET_ERROR_TYPE_ERROR,
347 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
348 sps->cfgname,
349 emsg);
350 GNUNET_free (emsg);
351 GNUNET_TESTING_interpreter_fail ();
352 return;
353 }
354 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
355}
356
357
358/**
359 * The cleanup function of this cmd frees resources the cmd allocated.
360 *
361 */
362static void
363start_peer_cleanup (void *cls,
364 const struct GNUNET_TESTING_Command *cmd)
365{
366 struct StartPeerState_v2 *sps = cls;
367
368 if (NULL != sps->handlers)
369 {
370 GNUNET_free (sps->handlers);
371 sps->handlers = NULL;
372 }
373 if (NULL != sps->cfg)
374 {
375 GNUNET_CONFIGURATION_destroy (sps->cfg);
376 sps->cfg = NULL;
377 }
378 GNUNET_free (sps->hello);
379 GNUNET_free (sps->connected_peers_map);
380 GNUNET_free (sps);
381}
382
383
384/**
385 * This function prepares an array with traits.
386 *
387 */
388static int
389start_peer_traits (void *cls,
390 const void **ret,
391 const char *trait,
392 unsigned int index)
393{
394 struct StartPeerState_v2 *sps = cls;
395 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
396 struct GNUNET_PeerIdentity *id = &sps->id;
397 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
398 sps->connected_peers_map;
399 char *hello = sps->hello;
400 size_t hello_size = sps->hello_size;
401
402
403 struct GNUNET_TESTING_Trait traits[] = {
404 {
405 .index = 0,
406 .trait_name = "application_handle",
407 .ptr = (const void *) ah,
408 },
409 {
410 .index = 1,
411 .trait_name = "peer_id",
412 .ptr = (const void *) id,
413 },
414 {
415 .index = 2,
416 .trait_name = "connected_peers_map",
417 .ptr = (const void *) connected_peers_map,
418 },
419 {
420 .index = 3,
421 .trait_name = "hello",
422 .ptr = (const void *) hello,
423 },
424 {
425 .index = 4,
426 .trait_name = "hello_size",
427 .ptr = (const void *) hello_size,
428 },
429 {
430 .index = 5,
431 .trait_name = "state",
432 .ptr = (const void *) sps,
433 },
434 GNUNET_TESTING_trait_end ()
435 };
436
437 return GNUNET_TESTING_get_trait (traits,
438 ret,
439 trait,
440 index);
441}
442
443
444/**
445 * Function to get the trait with the struct StartPeerState_v2.
446 *
447 * @param[out] sps struct StartPeerState_v2.
448 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
449 *
450 */
451int
452GNUNET_TRANSPORT_get_trait_state_v3 (const struct
453 GNUNET_TESTING_Command
454 *cmd,
455 struct StartPeerState_v2 **sps)
456{
457 return cmd->traits (cmd->cls,
458 (const void **) sps,
459 "state",
460 (unsigned int) 5);
461}
462
463
464/**
465 * Function to get the trait with the size of the hello.
466 *
467 * @param[out] hello_size size of hello.
468 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
469 *
470 */
471int
472GNUNET_TRANSPORT_get_trait_hello_size_v3 (const struct
473 GNUNET_TESTING_Command
474 *cmd,
475 size_t **hello_size)
476{
477 return cmd->traits (cmd->cls,
478 (const void **) hello_size,
479 "hello_size",
480 (unsigned int) 4);
481}
482
483
484/**
485 * Function to get the trait with the hello.
486 *
487 * @param[out] hello The hello for the peer.
488 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
489 *
490 */
491int
492GNUNET_TRANSPORT_get_trait_hello_v3 (const struct
493 GNUNET_TESTING_Command
494 *cmd,
495 char **hello)
496{
497 return cmd->traits (cmd->cls,
498 (const void **) hello,
499 "hello",
500 (unsigned int) 3);
501}
502
503
504/**
505 * Function to get the trait with the map of connected peers.
506 *
507 * @param[out] connected_peers_map The map with connected peers.
508 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
509 *
510 */
511int
512GNUNET_TRANSPORT_get_trait_connected_peers_map_v3 (const struct
513 GNUNET_TESTING_Command
514 *cmd,
515 struct
516 GNUNET_CONTAINER_MultiShortmap
517 *
518 *
519 connected_peers_map)
520{
521 return cmd->traits (cmd->cls,
522 (const void **) connected_peers_map,
523 "connected_peers_map",
524 (unsigned int) 2);
525}
526
527
528/**
529 * Function to get the trait with the transport application handle.
530 *
531 * @param[out] ah The application handle.
532 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
533 */
534int
535GNUNET_TRANSPORT_get_trait_application_handle_v3 (const struct
536 GNUNET_TESTING_Command *cmd,
537 struct
538 GNUNET_TRANSPORT_ApplicationHandle
539 **ah)
540{
541 return cmd->traits (cmd->cls,
542 (const void **) ah,
543 "application_handle",
544 (unsigned int) 0);
545}
546
547
548/**
549 * Function to get the trait with the peer id.
550 *
551 * @param[out] id The peer id.
552 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
553 */
554int
555GNUNET_TRANSPORT_get_trait_peer_id_v3 (const struct
556 GNUNET_TESTING_Command *cmd,
557 struct GNUNET_PeerIdentity **id)
558{
559 return cmd->traits (cmd->cls,
560 (const void **) id,
561 "peer_id",
562 (unsigned int) 1);
563}
564
565
566/**
567 * Create command.
568 *
569 * @param label name for command.
570 * @param system_label Label of the cmd to setup a test environment.
571 * @param m The number of the local node of the actual network namespace.
572 * @param n The number of the actual namespace.
573 * @param local_m Number of local nodes in each namespace.
574 * @param handlers Handler for messages received by this peer.
575 * @param cfgname Configuration file name for this peer.
576 * @return command.
577 */
578struct GNUNET_TESTING_Command
579GNUNET_TRANSPORT_cmd_start_peer_v3 (const char *label,
580 const char *system_label,
581 uint32_t no,
582 char *node_ip,
583 struct GNUNET_MQ_MessageHandler *handlers,
584 const char *cfgname)
585{
586 struct StartPeerState_v2 *sps;
587 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
588 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
589 unsigned int i;
590
591 sps = GNUNET_new (struct StartPeerState_v2);
592 sps->no = no;
593 sps->system_label = system_label;
594 sps->connected_peers_map = connected_peers_map;
595 sps->cfgname = cfgname;
596 sps->node_ip = node_ip;
597
598 if (NULL != handlers)
599 {
600 for (i = 0; NULL != handlers[i].cb; i++)
601 ;
602 sps->handlers = GNUNET_new_array (i + 1,
603 struct GNUNET_MQ_MessageHandler);
604 GNUNET_memcpy (sps->handlers,
605 handlers,
606 i * sizeof(struct GNUNET_MQ_MessageHandler));
607 }
608
609 struct GNUNET_TESTING_Command cmd = {
610 .cls = sps,
611 .label = label,
612 .run = &start_peer_run,
613 .finish = &start_peer_finish,
614 .cleanup = &start_peer_cleanup,
615 .traits = &start_peer_traits
616 };
617
618 return cmd;
619}