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.c577
1 files changed, 0 insertions, 577 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 729d981c0..000000000
--- a/src/transport/transport_api_cmd_start_peer.c
+++ /dev/null
@@ -1,577 +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-ng.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
189 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
190 {
191 LOG (GNUNET_ERROR_TYPE_ERROR,
192 "File not found: `%s'\n",
193 sps->cfgname);
194 GNUNET_TESTING_interpreter_fail ();
195 return;
196 }
197
198
199 sps->cfg = GNUNET_CONFIGURATION_create ();
200 GNUNET_assert (GNUNET_OK ==
201 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
202
203 system_cmd = GNUNET_TESTING_interpreter_lookup_command (sps->system_label);
204 GNUNET_TESTING_get_trait_test_system (system_cmd,
205 &tl_system);
206
207 sps->tl_system = tl_system;
208
209 if (GNUNET_SYSERR ==
210 GNUNET_TESTING_configuration_create (tl_system,
211 sps->cfg))
212 {
213 LOG (GNUNET_ERROR_TYPE_ERROR,
214 "Testing library failed to create unique configuration based on `%s'\n",
215 sps->cfgname);
216 GNUNET_CONFIGURATION_destroy (sps->cfg);
217 GNUNET_TESTING_interpreter_fail ();
218 return;
219 }
220
221 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system,
222 sps->cfg,
223 sps->no,
224 NULL,
225 &emsg);
226 if (NULL == sps->peer)
227 {
228 LOG (GNUNET_ERROR_TYPE_ERROR,
229 "Testing library failed to create unique configuration based on `%s': `%s'\n",
230 sps->cfgname,
231 emsg);
232 GNUNET_free (emsg);
233 GNUNET_TESTING_interpreter_fail ();
234 return;
235 }
236
237 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
238 {
239 LOG (GNUNET_ERROR_TYPE_ERROR,
240 "Testing library failed to create unique configuration based on `%s'\n",
241 sps->cfgname);
242 GNUNET_free (emsg);
243 GNUNET_TESTING_interpreter_fail ();
244 return;
245 }
246
247 memset (&dummy,
248 '\0',
249 sizeof(dummy));
250
251 GNUNET_TESTING_peer_get_identity (sps->peer,
252 &sps->id);
253
254 if (0 == memcmp (&dummy,
255 &sps->id,
256 sizeof(struct GNUNET_PeerIdentity)))
257 {
258 LOG (GNUNET_ERROR_TYPE_ERROR,
259 "Testing library failed to obtain peer identity for peer %u\n",
260 sps->no);
261 GNUNET_free (emsg);
262 GNUNET_TESTING_interpreter_fail ();
263 return;
264 }
265 LOG (GNUNET_ERROR_TYPE_DEBUG,
266 "Peer %u configured with identity `%s'\n",
267 sps->no,
268 GNUNET_i2s_full (&sps->id));
269
270 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
271 NULL,
272 sps->handlers,
273 sps,
274 &notify_connect,
275 &notify_disconnect);
276 if (NULL == sps->th)
277 {
278 LOG (GNUNET_ERROR_TYPE_ERROR,
279 "Failed to connect to transport service for peer `%s': `%s'\n",
280 sps->cfgname,
281 emsg);
282 GNUNET_free (emsg);
283 GNUNET_TESTING_interpreter_fail ();
284 return;
285 }
286
287 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
288 if (NULL == sps->th)
289 {
290 LOG (GNUNET_ERROR_TYPE_ERROR,
291 "Failed to connect to peerstore service for peer `%s': `%s'\n",
292 sps->cfgname,
293 emsg);
294 GNUNET_free (emsg);
295 GNUNET_TESTING_interpreter_fail ();
296 return;
297 }
298
299 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
300 if (NULL == sps->ah)
301 {
302 LOG (GNUNET_ERROR_TYPE_ERROR,
303 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
304 sps->cfgname,
305 emsg);
306 GNUNET_free (emsg);
307 GNUNET_TESTING_interpreter_fail ();
308 return;
309 }
310 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
311}
312
313
314/**
315 * The cleanup function of this cmd frees resources the cmd allocated.
316 *
317 */
318static void
319start_peer_cleanup (void *cls,
320 const struct GNUNET_TESTING_Command *cmd)
321{
322 struct StartPeerState *sps = cls;
323
324 if (NULL != sps->handlers)
325 {
326 GNUNET_free (sps->handlers);
327 sps->handlers = NULL;
328 }
329 if (NULL != sps->cfg)
330 {
331 GNUNET_CONFIGURATION_destroy (sps->cfg);
332 sps->cfg = NULL;
333 }
334 GNUNET_free (sps->hello);
335 GNUNET_free (sps->connected_peers_map);
336 GNUNET_free (sps);
337}
338
339
340/**
341 * This function prepares an array with traits.
342 *
343 */
344static int
345start_peer_traits (void *cls,
346 const void **ret,
347 const char *trait,
348 unsigned int index)
349{
350 struct StartPeerState *sps = cls;
351 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
352 struct GNUNET_PeerIdentity *id = &sps->id;
353 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
354 sps->connected_peers_map;
355 char *hello = sps->hello;
356 size_t hello_size = sps->hello_size;
357
358
359 struct GNUNET_TESTING_Trait traits[] = {
360 {
361 .index = 0,
362 .trait_name = "application_handle",
363 .ptr = (const void *) ah,
364 },
365 {
366 .index = 1,
367 .trait_name = "peer_id",
368 .ptr = (const void *) id,
369 },
370 {
371 .index = 2,
372 .trait_name = "connected_peers_map",
373 .ptr = (const void *) connected_peers_map,
374 },
375 {
376 .index = 3,
377 .trait_name = "hello",
378 .ptr = (const void *) hello,
379 },
380 {
381 .index = 4,
382 .trait_name = "hello_size",
383 .ptr = (const void *) hello_size,
384 },
385 {
386 .index = 5,
387 .trait_name = "state",
388 .ptr = (const void *) sps,
389 },
390 GNUNET_TESTING_trait_end ()
391 };
392
393 return GNUNET_TESTING_get_trait (traits,
394 ret,
395 trait,
396 index);
397}
398
399
400/**
401 * Function to get the trait with the struct StartPeerState.
402 *
403 * @param[out] sps struct StartPeerState.
404 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
405 *
406 */
407int
408GNUNET_TRANSPORT_get_trait_state (const struct
409 GNUNET_TESTING_Command
410 *cmd,
411 struct StartPeerState **sps)
412{
413 return cmd->traits (cmd->cls,
414 (const void **) sps,
415 "state",
416 (unsigned int) 5);
417}
418
419
420/**
421 * Function to get the trait with the size of the hello.
422 *
423 * @param[out] hello_size size of hello.
424 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
425 *
426 */
427int
428GNUNET_TRANSPORT_get_trait_hello_size (const struct
429 GNUNET_TESTING_Command
430 *cmd,
431 size_t **hello_size)
432{
433 return cmd->traits (cmd->cls,
434 (const void **) hello_size,
435 "hello_size",
436 (unsigned int) 4);
437}
438
439
440/**
441 * Function to get the trait with the hello.
442 *
443 * @param[out] hello The hello for the peer.
444 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
445 *
446 */
447int
448GNUNET_TRANSPORT_get_trait_hello (const struct
449 GNUNET_TESTING_Command
450 *cmd,
451 char **hello)
452{
453 return cmd->traits (cmd->cls,
454 (const void **) hello,
455 "hello",
456 (unsigned int) 3);
457}
458
459
460/**
461 * Function to get the trait with the map of connected peers.
462 *
463 * @param[out] connected_peers_map The map with connected peers.
464 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
465 *
466 */
467int
468GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct
469 GNUNET_TESTING_Command
470 *cmd,
471 struct
472 GNUNET_CONTAINER_MultiShortmap *
473 *
474 connected_peers_map)
475{
476 return cmd->traits (cmd->cls,
477 (const void **) connected_peers_map,
478 "connected_peers_map",
479 (unsigned int) 2);
480}
481
482
483/**
484 * Function to get the trait with the transport application handle.
485 *
486 * @param[out] ah The application handle.
487 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
488 */
489int
490GNUNET_TRANSPORT_get_trait_application_handle (const struct
491 GNUNET_TESTING_Command *cmd,
492 struct
493 GNUNET_TRANSPORT_ApplicationHandle
494 **ah)
495{
496 return cmd->traits (cmd->cls,
497 (const void **) ah,
498 "application_handle",
499 (unsigned int) 0);
500}
501
502
503/**
504 * Function to get the trait with the peer id.
505 *
506 * @param[out] id The peer id.
507 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
508 */
509int
510GNUNET_TRANSPORT_get_trait_peer_id (const struct
511 GNUNET_TESTING_Command *cmd,
512 struct GNUNET_PeerIdentity **id)
513{
514 return cmd->traits (cmd->cls,
515 (const void **) id,
516 "peer_id",
517 (unsigned int) 1);
518}
519
520
521/**
522 * Create command.
523 *
524 * @param label name for command.
525 * @param system_label Label of the cmd to setup a test environment.
526 * @param m The number of the local node of the actual network namespace.
527 * @param n The number of the actual namespace.
528 * @param local_m Number of local nodes in each namespace.
529 * @param handlers Handler for messages received by this peer.
530 * @param cfgname Configuration file name for this peer.
531 * @return command.
532 */
533struct GNUNET_TESTING_Command
534GNUNET_TRANSPORT_cmd_start_peer (const char *label,
535 const char *system_label,
536 char *m,
537 char *n,
538 char *local_m,
539 struct GNUNET_MQ_MessageHandler *handlers,
540 const char *cfgname)
541{
542 struct StartPeerState *sps;
543 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
544 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
545 unsigned int i;
546
547 sps = GNUNET_new (struct StartPeerState);
548 sps->m = m;
549 sps->n = n;
550 sps->local_m = local_m;
551 sps->no = (atoi (n) - 1) * atoi (sps->local_m) + atoi (m);
552 sps->system_label = system_label;
553 sps->connected_peers_map = connected_peers_map;
554 sps->cfgname = cfgname;
555
556 if (NULL != handlers)
557 {
558 for (i = 0; NULL != handlers[i].cb; i++)
559 ;
560 sps->handlers = GNUNET_new_array (i + 1,
561 struct GNUNET_MQ_MessageHandler);
562 GNUNET_memcpy (sps->handlers,
563 handlers,
564 i * sizeof(struct GNUNET_MQ_MessageHandler));
565 }
566
567 struct GNUNET_TESTING_Command cmd = {
568 .cls = sps,
569 .label = label,
570 .run = &start_peer_run,
571 .finish = &start_peer_finish,
572 .cleanup = &start_peer_cleanup,
573 .traits = &start_peer_traits
574 };
575
576 return cmd;
577}