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.c558
1 files changed, 558 insertions, 0 deletions
diff --git a/src/transport/transport_api_cmd_start_peer.c b/src/transport/transport_api_cmd_start_peer.c
new file mode 100644
index 000000000..4077b7561
--- /dev/null
+++ b/src/transport/transport_api_cmd_start_peer.c
@@ -0,0 +1,558 @@
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
32/**
33 * Generic logging shortcut
34 */
35#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
36
37struct StartPeerState
38{
39 /**
40 * Receive callback
41 */
42 struct GNUNET_MQ_MessageHandler *handlers;
43
44 const char *cfgname;
45
46 /**
47 * Peer's configuration
48 */
49 struct GNUNET_CONFIGURATION_Handle *cfg;
50
51 struct GNUNET_TESTING_Peer *peer;
52
53 /**
54 * Peer identity
55 */
56 struct GNUNET_PeerIdentity *id;
57
58 /**
59 * Peer's transport service handle
60 */
61 struct GNUNET_TRANSPORT_CoreHandle *th;
62
63 /**
64 * Application handle
65 */
66 struct GNUNET_TRANSPORT_ApplicationHandle *ah;
67
68 /**
69 * Peer's PEERSTORE Handle
70 */
71 struct GNUNET_PEERSTORE_Handle *ph;
72
73 /**
74 * Hello get task
75 */
76 struct GNUNET_SCHEDULER_Task *rh_task;
77
78 /**
79 * Peer's transport get hello handle to retrieve peer's HELLO message
80 */
81 struct GNUNET_PEERSTORE_IterateContext *pic;
82
83 /**
84 * Hello
85 */
86 char *hello;
87
88 /**
89 * Hello size
90 */
91 size_t hello_size;
92
93 char *m;
94
95 char *n;
96
97 unsigned int finished;
98
99 const char *system_label;
100
101 /**
102 * An unique number to identify the peer
103 */
104 unsigned int no;
105
106 struct GNUNET_CONTAINER_MultiPeerMap *connected_peers_map;
107
108 struct GNUNET_TESTING_System *tl_system;
109
110};
111
112
113static void
114retrieve_hello (void *cls);
115
116static void
117hello_iter_cb (void *cb_cls,
118 const struct GNUNET_PEERSTORE_Record *record,
119 const char *emsg)
120{
121 struct StartPeerState *sps = cb_cls;
122 if (NULL == record)
123 {
124 sps->pic = NULL;
125 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
126 return;
127 }
128 // Check record type et al?
129 sps->hello_size = record->value_size;
130 sps->hello = GNUNET_malloc (sps->hello_size);
131 memcpy (sps->hello, record->value, sps->hello_size);
132 sps->hello[sps->hello_size - 1] = '\0';
133
134 GNUNET_PEERSTORE_iterate_cancel (sps->pic);
135 sps->pic = NULL;
136 sps->finished = GNUNET_YES;
137}
138
139
140static void
141retrieve_hello (void *cls)
142{
143 struct StartPeerState *sps = cls;
144 sps->rh_task = NULL;
145 sps->pic = GNUNET_PEERSTORE_iterate (sps->ph,
146 "transport",
147 sps->id,
148 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
149 hello_iter_cb,
150 sps);
151
152}
153
154static int
155start_peer_finish (void *cls,
156 GNUNET_SCHEDULER_TaskCallback cont,
157 void *cont_cls)
158{
159 struct StartPeerState *sps = cls;
160
161 if (GNUNET_YES == sps->finished)
162 {
163 cont (cont_cls);
164 }
165
166 return sps->finished;
167}
168
169
170static void
171notify_disconnect (void *cls,
172 const struct GNUNET_PeerIdentity *peer,
173 void *handler_cls)
174{
175 struct StartPeerState *sps = cls;
176
177 LOG (GNUNET_ERROR_TYPE_DEBUG,
178 "Peer %s disconnected from peer %u (`%s')\n",
179 GNUNET_i2s (peer),
180 sps->no,
181 GNUNET_i2s (sps->id));
182
183}
184
185
186static void *
187notify_connect (void *cls,
188 const struct GNUNET_PeerIdentity *peer,
189 struct GNUNET_MQ_Handle *mq)
190{
191 struct StartPeerState *sps = cls;
192
193
194 void *ret;
195
196
197 LOG (GNUNET_ERROR_TYPE_DEBUG,
198 "Peer %s connected to peer %u (`%s')\n",
199 GNUNET_i2s (peer),
200 sps->no,
201 GNUNET_i2s (sps->id));
202
203 GNUNET_CONTAINER_multipeermap_put (sps->connected_peers_map,
204 peer,
205 mq,
206 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
207
208 // TODO what does the handler function need?
209 return ret;
210}
211
212
213static void
214start_peer_run (void *cls,
215 const struct GNUNET_TESTING_Command *cmd,
216 struct GNUNET_TESTING_Interpreter *is)
217{
218 struct StartPeerState *sps = cls;
219 char *emsg = NULL;
220 struct GNUNET_PeerIdentity dummy;
221 const struct GNUNET_TESTING_Command *system_cmd;
222 struct GNUNET_TESTING_System *tl_system;
223
224 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
225 {
226 LOG (GNUNET_ERROR_TYPE_ERROR,
227 "File not found: `%s'\n",
228 sps->cfgname);
229 GNUNET_TESTING_interpreter_fail ();
230 return;
231 }
232
233
234 sps->cfg = GNUNET_CONFIGURATION_create ();
235 GNUNET_assert (GNUNET_OK ==
236 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
237
238 system_cmd = GNUNET_TESTING_interpreter_lookup_command (sps->system_label);
239 GNUNET_TESTING_get_trait_test_system (system_cmd,
240 &tl_system);
241
242 sps->tl_system = tl_system;
243
244 if (GNUNET_SYSERR ==
245 GNUNET_TESTING_configuration_create (tl_system,
246 sps->cfg))
247 {
248 LOG (GNUNET_ERROR_TYPE_ERROR,
249 "Testing library failed to create unique configuration based on `%s'\n",
250 sps->cfgname);
251 GNUNET_CONFIGURATION_destroy (sps->cfg);
252 GNUNET_TESTING_interpreter_fail ();
253 }
254
255 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system,
256 sps->cfg,
257 sps->no,
258 NULL,
259 &emsg);
260 if (NULL == sps->peer)
261 {
262 LOG (GNUNET_ERROR_TYPE_ERROR,
263 "Testing library failed to create unique configuration based on `%s': `%s'\n",
264 sps->cfgname,
265 emsg);
266 GNUNET_free (emsg);
267 GNUNET_TESTING_interpreter_fail ();
268 }
269
270 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
271 {
272 LOG (GNUNET_ERROR_TYPE_ERROR,
273 "Testing library failed to create unique configuration based on `%s'\n",
274 sps->cfgname);
275 GNUNET_free (emsg);
276 GNUNET_TESTING_interpreter_fail ();
277 }
278
279 memset (&dummy,
280 '\0',
281 sizeof(dummy));
282 GNUNET_TESTING_peer_get_identity (sps->peer,
283 sps->id);
284 if (0 == memcmp (&dummy,
285 &sps->id,
286 sizeof(struct GNUNET_PeerIdentity)))
287 {
288 LOG (GNUNET_ERROR_TYPE_ERROR,
289 "Testing library failed to obtain peer identity for peer %u\n",
290 sps->no);
291 GNUNET_free (emsg);
292 GNUNET_TESTING_interpreter_fail ();
293 }
294 LOG (GNUNET_ERROR_TYPE_DEBUG,
295 "Peer %u configured with identity `%s'\n",
296 sps->no,
297 GNUNET_i2s_full (sps->id));
298 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
299 NULL,
300 sps->handlers,
301 sps,
302 &notify_connect,
303 &notify_disconnect);
304 if (NULL == sps->th)
305 {
306 LOG (GNUNET_ERROR_TYPE_ERROR,
307 "Failed to connect to transport service for peer `%s': `%s'\n",
308 sps->cfgname,
309 emsg);
310 GNUNET_free (emsg);
311 GNUNET_TESTING_interpreter_fail ();
312 }
313 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
314 if (NULL == sps->th)
315 {
316 LOG (GNUNET_ERROR_TYPE_ERROR,
317 "Failed to connect to peerstore service for peer `%s': `%s'\n",
318 sps->cfgname,
319 emsg);
320 GNUNET_free (emsg);
321 GNUNET_TESTING_interpreter_fail ();
322 }
323 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
324 if (NULL == sps->ah)
325 {
326 LOG (GNUNET_ERROR_TYPE_ERROR,
327 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
328 sps->cfgname,
329 emsg);
330 GNUNET_free (emsg);
331 GNUNET_TESTING_interpreter_fail ();
332 }
333 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
334}
335
336
337static void
338start_peer_cleanup (void *cls,
339 const struct GNUNET_TESTING_Command *cmd)
340{
341 struct StartPeerState *sps = cls;
342
343 if (NULL != sps->rh_task)
344 GNUNET_SCHEDULER_cancel (sps->rh_task);
345 sps->rh_task = NULL;
346 if (NULL != sps->ah)
347 {
348 GNUNET_TRANSPORT_application_done (sps->ah);
349 sps->ah = NULL;
350 }
351 if (NULL != sps->ph)
352 {
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Disconnecting from PEERSTORE service\n");
355 GNUNET_PEERSTORE_disconnect (sps->ph, GNUNET_NO);
356 sps->ph = NULL;
357 }
358 if (NULL != sps->handlers)
359 {
360 GNUNET_free (sps->handlers);
361 sps->handlers = NULL;
362 }
363 if (NULL != sps->cfg)
364 {
365 GNUNET_CONFIGURATION_destroy (sps->cfg);
366 sps->cfg = NULL;
367 }
368 if (NULL != sps->peer)
369 {
370 if (GNUNET_OK !=
371 GNUNET_TESTING_peer_stop (sps->peer))
372 {
373 LOG (GNUNET_ERROR_TYPE_DEBUG,
374 "Testing lib failed to stop peer %u (`%s')\n",
375 sps->no,
376 GNUNET_i2s (sps->id));
377 }
378 GNUNET_TESTING_peer_destroy (sps->peer);
379 sps->peer = NULL;
380 }
381 if (NULL != sps->th)
382 {
383 GNUNET_TRANSPORT_core_disconnect (sps->th);
384 sps->th = NULL;
385 }
386 if (NULL != sps->tl_system)
387 {
388 GNUNET_free (sps->tl_system);
389 }
390 GNUNET_free (sps->hello);
391 GNUNET_free (sps->connected_peers_map);
392 GNUNET_free (sps);
393}
394
395
396static int
397start_peer_traits (void *cls,
398 const void **ret,
399 const char *trait,
400 unsigned int index)
401{
402 struct StartPeerState *sps = cls;
403 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
404 struct GNUNET_PeerIdentity *id = sps->id;
405 struct GNUNET_CONTAINER_MultiPeerMap *connected_peers_map =
406 sps->connected_peers_map;
407 char *hello = sps->hello;
408 size_t hello_size = sps->hello_size;
409
410
411 struct GNUNET_TESTING_Trait traits[] = {
412 {
413 .index = 0,
414 .trait_name = "application_handle",
415 .ptr = (const void *) ah,
416 },
417 {
418 .index = 1,
419 .trait_name = "peer_id",
420 .ptr = (const void *) id,
421 },
422 {
423 .index = 2,
424 .trait_name = "connected_peers_map",
425 .ptr = (const void *) connected_peers_map,
426 },
427 {
428 .index = 3,
429 .trait_name = "hello",
430 .ptr = (const void *) hello,
431 },
432 {
433 .index = 4,
434 .trait_name = "hello_size",
435 .ptr = (const void *) hello_size,
436 },
437 GNUNET_TESTING_trait_end ()
438 };
439
440 return GNUNET_TESTING_get_trait (traits,
441 ret,
442 trait,
443 index);
444}
445
446int
447GNUNET_TESTING_get_trait_hello_size (const struct
448 GNUNET_TESTING_Command
449 *cmd,
450 size_t **hello_size)
451{
452 return cmd->traits (cmd->cls,
453 (const void **) hello_size,
454 "hello_size",
455 (unsigned int) 4);
456}
457
458int
459GNUNET_TESTING_get_trait_hello (const struct
460 GNUNET_TESTING_Command
461 *cmd,
462 char **hello)
463{
464 return cmd->traits (cmd->cls,
465 (const void **) hello,
466 "hello",
467 (unsigned int) 3);
468}
469
470int
471GNUNET_TESTING_get_trait_connected_peers_map (const struct
472 GNUNET_TESTING_Command
473 *cmd,
474 struct
475 GNUNET_CONTAINER_MultiPeerMap **
476 connected_peers_map)
477{
478 return cmd->traits (cmd->cls,
479 (const void **) connected_peers_map,
480 "connected_peers_map",
481 (unsigned int) 2);
482}
483
484
485int
486GNUNET_TESTING_get_trait_application_handle (const struct
487 GNUNET_TESTING_Command *cmd,
488 struct
489 GNUNET_TRANSPORT_ApplicationHandle
490 **ah)
491{
492 return cmd->traits (cmd->cls,
493 (const void **) ah,
494 "application_handle",
495 (unsigned int) 0);
496}
497
498
499int
500GNUNET_TESTING_get_trait_peer_id (const struct
501 GNUNET_TESTING_Command *cmd,
502 struct GNUNET_PeerIdentity **id)
503{
504 return cmd->traits (cmd->cls,
505 (const void **) id,
506 "peer_id",
507 (unsigned int) 1);
508}
509
510
511/**
512 * Create command.
513 *
514 * @param label name for command.
515 * @return command.
516 */
517struct GNUNET_TESTING_Command
518GNUNET_TESTING_cmd_start_peer (const char *label,
519 const char *system_label,
520 char *m,
521 char *n,
522 struct GNUNET_MQ_MessageHandler *handlers,
523 const char *cfgname)
524{
525 struct StartPeerState *sps;
526 struct GNUNET_CONTAINER_MultiPeerMap *connected_peers_map =
527 GNUNET_CONTAINER_multipeermap_create (1,GNUNET_NO);
528 unsigned int i;
529
530 sps = GNUNET_new (struct StartPeerState);
531 sps->m = m;
532 sps->n = n;
533 sps->system_label = system_label;
534 sps->connected_peers_map = connected_peers_map;
535 sps->cfgname = cfgname;
536
537 if (NULL != handlers)
538 {
539 for (i = 0; NULL != handlers[i].cb; i++)
540 ;
541 sps->handlers = GNUNET_new_array (i + 1,
542 struct GNUNET_MQ_MessageHandler);
543 GNUNET_memcpy (sps->handlers,
544 handlers,
545 i * sizeof(struct GNUNET_MQ_MessageHandler));
546 }
547
548 struct GNUNET_TESTING_Command cmd = {
549 .cls = sps,
550 .label = label,
551 .run = &start_peer_run,
552 .finish = &start_peer_finish,
553 .cleanup = &start_peer_cleanup,
554 .traits = &start_peer_traits
555 };
556
557 return cmd;
558}