aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_cmd_start_peer_v2.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api_cmd_start_peer_v2.c')
-rw-r--r--src/transport/transport_api_cmd_start_peer_v2.c605
1 files changed, 0 insertions, 605 deletions
diff --git a/src/transport/transport_api_cmd_start_peer_v2.c b/src/transport/transport_api_cmd_start_peer_v2.c
deleted file mode 100644
index 94799eddd..000000000
--- a/src/transport/transport_api_cmd_start_peer_v2.c
+++ /dev/null
@@ -1,605 +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_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 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 struct GNUNET_TESTING_Interpreter *is)
181{
182 struct StartPeerState_v2 *sps = cls;
183 char *emsg = NULL;
184 struct GNUNET_PeerIdentity dummy;
185 const struct GNUNET_TESTING_Command *system_cmd;
186 struct GNUNET_TESTING_System *tl_system;
187 char *home;
188 char *transport_unix_path;
189 char *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 (is);
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 (&communicator_unix_path,
215 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
216 sps->no);
217
218 GNUNET_asprintf (&bindto,
219 "%s:60002",
220 sps->node_ip);
221
222
223 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
224 home);
225 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
226 transport_unix_path);
227 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
228 "BINDTO",
229 bindto);
230 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
231 "UNIXPATH",
232 communicator_unix_path);
233
234 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
235 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 (is);
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 (is);
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 (is);
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 (is);
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 (is);
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 (is);
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 (is);
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{
353 struct StartPeerState_v2 *sps = cls;
354
355 if (NULL != sps->handlers)
356 {
357 GNUNET_free (sps->handlers);
358 sps->handlers = NULL;
359 }
360 if (NULL != sps->cfg)
361 {
362 GNUNET_CONFIGURATION_destroy (sps->cfg);
363 sps->cfg = NULL;
364 }
365 GNUNET_free (sps->hello);
366 GNUNET_free (sps->connected_peers_map);
367 GNUNET_free (sps);
368}
369
370
371/**
372 * This function prepares an array with traits.
373 *
374 */
375static int
376start_peer_traits (void *cls,
377 const void **ret,
378 const char *trait,
379 unsigned int index)
380{
381 struct StartPeerState_v2 *sps = cls;
382 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
383 struct GNUNET_PeerIdentity *id = &sps->id;
384 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
385 sps->connected_peers_map;
386 char *hello = sps->hello;
387 size_t hello_size = sps->hello_size;
388
389
390 struct GNUNET_TESTING_Trait traits[] = {
391 {
392 .index = 0,
393 .trait_name = "application_handle",
394 .ptr = (const void *) ah,
395 },
396 {
397 .index = 1,
398 .trait_name = "peer_id",
399 .ptr = (const void *) id,
400 },
401 {
402 .index = 2,
403 .trait_name = "connected_peers_map",
404 .ptr = (const void *) connected_peers_map,
405 },
406 {
407 .index = 3,
408 .trait_name = "hello",
409 .ptr = (const void *) hello,
410 },
411 {
412 .index = 4,
413 .trait_name = "hello_size",
414 .ptr = (const void *) hello_size,
415 },
416 {
417 .index = 5,
418 .trait_name = "state",
419 .ptr = (const void *) sps,
420 },
421 GNUNET_TESTING_trait_end ()
422 };
423
424 return GNUNET_TESTING_get_trait (traits,
425 ret,
426 trait,
427 index);
428}
429
430
431/**
432 * Function to get the trait with the struct StartPeerState_v2.
433 *
434 * @param[out] sps struct StartPeerState_v2.
435 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
436 *
437 */
438int
439GNUNET_TRANSPORT_get_trait_state_v2 (const struct
440 GNUNET_TESTING_Command
441 *cmd,
442 struct StartPeerState_v2 **sps)
443{
444 return cmd->traits (cmd->cls,
445 (const void **) sps,
446 "state",
447 (unsigned int) 5);
448}
449
450
451/**
452 * Function to get the trait with the size of the hello.
453 *
454 * @param[out] hello_size size of hello.
455 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
456 *
457 */
458int
459GNUNET_TRANSPORT_get_trait_hello_size_v2 (const struct
460 GNUNET_TESTING_Command
461 *cmd,
462 size_t **hello_size)
463{
464 return cmd->traits (cmd->cls,
465 (const void **) hello_size,
466 "hello_size",
467 (unsigned int) 4);
468}
469
470
471/**
472 * Function to get the trait with the hello.
473 *
474 * @param[out] hello The hello for the peer.
475 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
476 *
477 */
478int
479GNUNET_TRANSPORT_get_trait_hello_v2 (const struct
480 GNUNET_TESTING_Command
481 *cmd,
482 char **hello)
483{
484 return cmd->traits (cmd->cls,
485 (const void **) hello,
486 "hello",
487 (unsigned int) 3);
488}
489
490
491/**
492 * Function to get the trait with the map of connected peers.
493 *
494 * @param[out] connected_peers_map The map with connected peers.
495 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
496 *
497 */
498int
499GNUNET_TRANSPORT_get_trait_connected_peers_map_v2 (const struct
500 GNUNET_TESTING_Command
501 *cmd,
502 struct
503 GNUNET_CONTAINER_MultiShortmap
504 *
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_v2 (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_v2 (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_v2 (const char *label,
567 const char *system_label,
568 uint32_t no,
569 char *node_ip,
570 struct GNUNET_MQ_MessageHandler *handlers,
571 const char *cfgname)
572{
573 struct StartPeerState_v2 *sps;
574 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
575 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
576 unsigned int i;
577
578 sps = GNUNET_new (struct StartPeerState_v2);
579 sps->no = no;
580 sps->system_label = system_label;
581 sps->connected_peers_map = connected_peers_map;
582 sps->cfgname = cfgname;
583 sps->node_ip = node_ip;
584
585 if (NULL != handlers)
586 {
587 for (i = 0; NULL != handlers[i].cb; i++)
588 ;
589 sps->handlers = GNUNET_new_array (i + 1,
590 struct GNUNET_MQ_MessageHandler);
591 GNUNET_memcpy (sps->handlers,
592 handlers,
593 i * sizeof(struct GNUNET_MQ_MessageHandler));
594 }
595
596 struct GNUNET_TESTING_Command cmd = {
597 .cls = sps,
598 .label = label,
599 .run = &start_peer_run,
600 .cleanup = &start_peer_cleanup,
601 .traits = &start_peer_traits
602 };
603
604 return cmd;
605}