aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_cmd_start_peer.c
diff options
context:
space:
mode:
authort3sserakt <t3ss@posteo.de>2021-08-24 11:56:13 +0200
committert3sserakt <t3ss@posteo.de>2021-08-24 11:56:13 +0200
commit5ddaa3f8eb5e01882540f21ddf237f3a41311e8a (patch)
tree765617baebf4366c9584d71863c3077f5d107d49 /src/transport/transport_api_cmd_start_peer.c
parent9d5a8c05f3a33c49fd97e5b8ef99d58b96704c6d (diff)
downloadgnunet-5ddaa3f8eb5e01882540f21ddf237f3a41311e8a.tar.gz
gnunet-5ddaa3f8eb5e01882540f21ddf237f3a41311e8a.zip
- fixed mem leaks, added code doc, formatting, removed trace logs
Diffstat (limited to 'src/transport/transport_api_cmd_start_peer.c')
-rw-r--r--src/transport/transport_api_cmd_start_peer.c268
1 files changed, 117 insertions, 151 deletions
diff --git a/src/transport/transport_api_cmd_start_peer.c b/src/transport/transport_api_cmd_start_peer.c
index 8696a99dc..9277bc346 100644
--- a/src/transport/transport_api_cmd_start_peer.c
+++ b/src/transport/transport_api_cmd_start_peer.c
@@ -29,93 +29,22 @@
29#include "gnunet_peerstore_service.h" 29#include "gnunet_peerstore_service.h"
30#include "gnunet_transport_core_service.h" 30#include "gnunet_transport_core_service.h"
31#include "gnunet_transport_application_service.h" 31#include "gnunet_transport_application_service.h"
32#include "transport-testing-ng.h"
32 33
33/** 34/**
34 * Generic logging shortcut 35 * Generic logging shortcut
35 */ 36 */
36#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) 37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
37 38
38struct StartPeerState
39{
40 /**
41 * Receive callback
42 */
43 struct GNUNET_MQ_MessageHandler *handlers;
44
45 const char *cfgname;
46
47 /**
48 * Peer's configuration
49 */
50 struct GNUNET_CONFIGURATION_Handle *cfg;
51
52 struct GNUNET_TESTING_Peer *peer;
53
54 /**
55 * Peer identity
56 */
57 struct GNUNET_PeerIdentity id;
58
59 /**
60 * Peer's transport service handle
61 */
62 struct GNUNET_TRANSPORT_CoreHandle *th;
63
64 /**
65 * Application handle
66 */
67 struct GNUNET_TRANSPORT_ApplicationHandle *ah;
68
69 /**
70 * Peer's PEERSTORE Handle
71 */
72 struct GNUNET_PEERSTORE_Handle *ph;
73
74 /**
75 * Hello get task
76 */
77 struct GNUNET_SCHEDULER_Task *rh_task;
78
79 /**
80 * Peer's transport get hello handle to retrieve peer's HELLO message
81 */
82 struct GNUNET_PEERSTORE_IterateContext *pic;
83
84 /**
85 * Hello
86 */
87 char *hello;
88
89 /**
90 * Hello size
91 */
92 size_t hello_size;
93
94 char *m;
95
96 char *n;
97
98 char *local_m;
99
100 unsigned int finished;
101
102 const char *system_label;
103
104 /**
105 * An unique number to identify the peer
106 */
107 unsigned int no;
108
109 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map;
110
111 struct GNUNET_TESTING_System *tl_system;
112
113};
114
115 39
116static void 40static void
117retrieve_hello (void *cls); 41retrieve_hello (void *cls);
118 42
43
44/**
45 * Callback delivering the hello of this peer from peerstore.
46 *
47 */
119static void 48static void
120hello_iter_cb (void *cb_cls, 49hello_iter_cb (void *cb_cls,
121 const struct GNUNET_PEERSTORE_Record *record, 50 const struct GNUNET_PEERSTORE_Record *record,
@@ -140,6 +69,11 @@ hello_iter_cb (void *cb_cls,
140} 69}
141 70
142 71
72
73/**
74 * Function to start the retrival task to retrieve the hello of this peer from the peerstore.
75 *
76 */
143static void 77static void
144retrieve_hello (void *cls) 78retrieve_hello (void *cls)
145{ 79{
@@ -154,6 +88,11 @@ retrieve_hello (void *cls)
154 88
155} 89}
156 90
91
92/**
93 * This function checks StartPeerState#finished, which is set when the hello was retrieved.
94 *
95 */
157static int 96static int
158start_peer_finish (void *cls, 97start_peer_finish (void *cls,
159 GNUNET_SCHEDULER_TaskCallback cont, 98 GNUNET_SCHEDULER_TaskCallback cont,
@@ -170,6 +109,10 @@ start_peer_finish (void *cls,
170} 109}
171 110
172 111
112/**
113 * Disconnect callback for the connection to the core service.
114 *
115 */
173static void 116static void
174notify_disconnect (void *cls, 117notify_disconnect (void *cls,
175 const struct GNUNET_PeerIdentity *peer, 118 const struct GNUNET_PeerIdentity *peer,
@@ -186,6 +129,10 @@ notify_disconnect (void *cls,
186} 129}
187 130
188 131
132/**
133 * Connect callback for the connection to the core service.
134 *
135 */
189static void * 136static void *
190notify_connect (void *cls, 137notify_connect (void *cls,
191 const struct GNUNET_PeerIdentity *peer, 138 const struct GNUNET_PeerIdentity *peer,
@@ -206,23 +153,27 @@ notify_connect (void *cls,
206 GNUNET_i2s (&sps->id)); 153 GNUNET_i2s (&sps->id));
207 154
208 // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node. 155 // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node.
209 node_number = 1; 156 node_number = 1;
210 GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc); 157 GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc);
211 158
212 159
213 memcpy (key, 160 memcpy (key,
214 &hc, 161 &hc,
215 sizeof (*key)); 162 sizeof (*key));
216 GNUNET_CONTAINER_multishortmap_put (sps->connected_peers_map, 163 GNUNET_CONTAINER_multishortmap_put (sps->connected_peers_map,
217 key, 164 key,
218 mq, 165 mq,
219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
220 167
168 GNUNET_free (key);
221 // TODO what does the handler function need? 169 // TODO what does the handler function need?
222 return ret; 170 return ret;
223} 171}
224 172
225 173/**
174 * The run method of this cmd will start all services of a peer to test the transport service.
175 *
176 */
226static void 177static void
227start_peer_run (void *cls, 178start_peer_run (void *cls,
228 const struct GNUNET_TESTING_Command *cmd, 179 const struct GNUNET_TESTING_Command *cmd,
@@ -234,9 +185,6 @@ start_peer_run (void *cls,
234 const struct GNUNET_TESTING_Command *system_cmd; 185 const struct GNUNET_TESTING_Command *system_cmd;
235 struct GNUNET_TESTING_System *tl_system; 186 struct GNUNET_TESTING_System *tl_system;
236 187
237 LOG (GNUNET_ERROR_TYPE_ERROR,
238 "start peer 1\n");
239
240 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname)) 188 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
241 { 189 {
242 LOG (GNUNET_ERROR_TYPE_ERROR, 190 LOG (GNUNET_ERROR_TYPE_ERROR,
@@ -269,8 +217,6 @@ start_peer_run (void *cls,
269 return; 217 return;
270 } 218 }
271 219
272 LOG (GNUNET_ERROR_TYPE_ERROR,
273 "start peer 2\n");
274 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system, 220 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system,
275 sps->cfg, 221 sps->cfg,
276 sps->no, 222 sps->no,
@@ -287,8 +233,6 @@ start_peer_run (void *cls,
287 return; 233 return;
288 } 234 }
289 235
290 LOG (GNUNET_ERROR_TYPE_ERROR,
291 "start peer 3\n");
292 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer)) 236 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
293 { 237 {
294 LOG (GNUNET_ERROR_TYPE_ERROR, 238 LOG (GNUNET_ERROR_TYPE_ERROR,
@@ -298,17 +242,14 @@ start_peer_run (void *cls,
298 GNUNET_TESTING_interpreter_fail (); 242 GNUNET_TESTING_interpreter_fail ();
299 return; 243 return;
300 } 244 }
301LOG (GNUNET_ERROR_TYPE_ERROR, 245
302 "start peer 4\n");
303 memset (&dummy, 246 memset (&dummy,
304 '\0', 247 '\0',
305 sizeof(dummy)); 248 sizeof(dummy));
306 LOG (GNUNET_ERROR_TYPE_ERROR, 249
307 "start peer 4.1\n");
308 GNUNET_TESTING_peer_get_identity (sps->peer, 250 GNUNET_TESTING_peer_get_identity (sps->peer,
309 &sps->id); 251 &sps->id);
310 LOG (GNUNET_ERROR_TYPE_ERROR, 252
311 "start peer 4.2\n");
312 if (0 == memcmp (&dummy, 253 if (0 == memcmp (&dummy,
313 &sps->id, 254 &sps->id,
314 sizeof(struct GNUNET_PeerIdentity))) 255 sizeof(struct GNUNET_PeerIdentity)))
@@ -324,8 +265,7 @@ LOG (GNUNET_ERROR_TYPE_ERROR,
324 "Peer %u configured with identity `%s'\n", 265 "Peer %u configured with identity `%s'\n",
325 sps->no, 266 sps->no,
326 GNUNET_i2s_full (&sps->id)); 267 GNUNET_i2s_full (&sps->id));
327 LOG (GNUNET_ERROR_TYPE_ERROR, 268
328 "start peer 4.3\n");
329 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg, 269 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
330 NULL, 270 NULL,
331 sps->handlers, 271 sps->handlers,
@@ -342,8 +282,7 @@ LOG (GNUNET_ERROR_TYPE_ERROR,
342 GNUNET_TESTING_interpreter_fail (); 282 GNUNET_TESTING_interpreter_fail ();
343 return; 283 return;
344 } 284 }
345 LOG (GNUNET_ERROR_TYPE_ERROR, 285
346 "start peer 5\n");
347 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg); 286 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
348 if (NULL == sps->th) 287 if (NULL == sps->th)
349 { 288 {
@@ -355,8 +294,7 @@ LOG (GNUNET_ERROR_TYPE_ERROR,
355 GNUNET_TESTING_interpreter_fail (); 294 GNUNET_TESTING_interpreter_fail ();
356 return; 295 return;
357 } 296 }
358 LOG (GNUNET_ERROR_TYPE_ERROR, 297
359 "start peer 6\n");
360 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg); 298 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
361 if (NULL == sps->ah) 299 if (NULL == sps->ah)
362 { 300 {
@@ -371,28 +309,16 @@ LOG (GNUNET_ERROR_TYPE_ERROR,
371 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps); 309 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
372} 310}
373 311
374 312/**
313 * The cleanup function of this cmd frees resources the cmd allocated.
314 *
315 */
375static void 316static void
376start_peer_cleanup (void *cls, 317start_peer_cleanup (void *cls,
377 const struct GNUNET_TESTING_Command *cmd) 318 const struct GNUNET_TESTING_Command *cmd)
378{ 319{
379 struct StartPeerState *sps = cls; 320 struct StartPeerState *sps = cls;
380 321
381 if (NULL != sps->rh_task)
382 GNUNET_SCHEDULER_cancel (sps->rh_task);
383 sps->rh_task = NULL;
384 if (NULL != sps->ah)
385 {
386 GNUNET_TRANSPORT_application_done (sps->ah);
387 sps->ah = NULL;
388 }
389 if (NULL != sps->ph)
390 {
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Disconnecting from PEERSTORE service\n");
393 GNUNET_PEERSTORE_disconnect (sps->ph, GNUNET_NO);
394 sps->ph = NULL;
395 }
396 if (NULL != sps->handlers) 322 if (NULL != sps->handlers)
397 { 323 {
398 GNUNET_free (sps->handlers); 324 GNUNET_free (sps->handlers);
@@ -403,34 +329,16 @@ start_peer_cleanup (void *cls,
403 GNUNET_CONFIGURATION_destroy (sps->cfg); 329 GNUNET_CONFIGURATION_destroy (sps->cfg);
404 sps->cfg = NULL; 330 sps->cfg = NULL;
405 } 331 }
406 if (NULL != sps->peer)
407 {
408 if (GNUNET_OK !=
409 GNUNET_TESTING_peer_stop (sps->peer))
410 {
411 LOG (GNUNET_ERROR_TYPE_DEBUG,
412 "Testing lib failed to stop peer %u (`%s')\n",
413 sps->no,
414 GNUNET_i2s (&sps->id));
415 }
416 GNUNET_TESTING_peer_destroy (sps->peer);
417 sps->peer = NULL;
418 }
419 if (NULL != sps->th)
420 {
421 GNUNET_TRANSPORT_core_disconnect (sps->th);
422 sps->th = NULL;
423 }
424 if (NULL != sps->tl_system)
425 {
426 GNUNET_free (sps->tl_system);
427 }
428 GNUNET_free (sps->hello); 332 GNUNET_free (sps->hello);
429 GNUNET_free (sps->connected_peers_map); 333 GNUNET_free (sps->connected_peers_map);
430 GNUNET_free (sps); 334 GNUNET_free (sps);
431} 335}
432 336
433 337
338/**
339 * This function prepares an array with traits.
340 *
341 */
434static int 342static int
435start_peer_traits (void *cls, 343start_peer_traits (void *cls,
436 const void **ret, 344 const void **ret,
@@ -472,6 +380,11 @@ start_peer_traits (void *cls,
472 .trait_name = "hello_size", 380 .trait_name = "hello_size",
473 .ptr = (const void *) hello_size, 381 .ptr = (const void *) hello_size,
474 }, 382 },
383 {
384 .index = 5,
385 .trait_name = "state",
386 .ptr = (const void *) sps,
387 },
475 GNUNET_TESTING_trait_end () 388 GNUNET_TESTING_trait_end ()
476 }; 389 };
477 390
@@ -481,6 +394,34 @@ start_peer_traits (void *cls,
481 index); 394 index);
482} 395}
483 396
397
398/**
399 * Function to get the trait with the struct StartPeerState.
400 *
401 * @param[out] sps struct StartPeerState.
402 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
403 *
404 */
405int
406GNUNET_TRANSPORT_get_trait_state (const struct
407 GNUNET_TESTING_Command
408 *cmd,
409 struct StartPeerState **sps)
410{
411 return cmd->traits (cmd->cls,
412 (const void **) sps,
413 "state",
414 (unsigned int) 5);
415}
416
417
418/**
419 * Function to get the trait with the size of the hello.
420 *
421 * @param[out] hello_size size of hello.
422 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
423 *
424 */
484int 425int
485GNUNET_TRANSPORT_get_trait_hello_size (const struct 426GNUNET_TRANSPORT_get_trait_hello_size (const struct
486 GNUNET_TESTING_Command 427 GNUNET_TESTING_Command
@@ -493,6 +434,13 @@ GNUNET_TRANSPORT_get_trait_hello_size (const struct
493 (unsigned int) 4); 434 (unsigned int) 4);
494} 435}
495 436
437/**
438 * Function to get the trait with the hello.
439 *
440 * @param[out] hello The hello for the peer.
441 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
442 *
443 */
496int 444int
497GNUNET_TRANSPORT_get_trait_hello (const struct 445GNUNET_TRANSPORT_get_trait_hello (const struct
498 GNUNET_TESTING_Command 446 GNUNET_TESTING_Command
@@ -505,12 +453,21 @@ GNUNET_TRANSPORT_get_trait_hello (const struct
505 (unsigned int) 3); 453 (unsigned int) 3);
506} 454}
507 455
456
457/**
458 * Function to get the trait with the map of connected peers.
459 *
460 * @param[out] connected_peers_map The map with connected peers.
461 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
462 *
463 */
508int 464int
509GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct 465GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct
510 GNUNET_TESTING_Command 466 GNUNET_TESTING_Command
511 *cmd, 467 *cmd,
512 struct 468 struct
513 GNUNET_CONTAINER_MultiShortmap ** 469 GNUNET_CONTAINER_MultiShortmap *
470 *
514 connected_peers_map) 471 connected_peers_map)
515{ 472{
516 return cmd->traits (cmd->cls, 473 return cmd->traits (cmd->cls,
@@ -520,6 +477,12 @@ GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct
520} 477}
521 478
522 479
480/**
481 * Function to get the trait with the transport application handle.
482 *
483 * @param[out] ah The application handle.
484 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
485 */
523int 486int
524GNUNET_TRANSPORT_get_trait_application_handle (const struct 487GNUNET_TRANSPORT_get_trait_application_handle (const struct
525 GNUNET_TESTING_Command *cmd, 488 GNUNET_TESTING_Command *cmd,
@@ -534,6 +497,12 @@ GNUNET_TRANSPORT_get_trait_application_handle (const struct
534} 497}
535 498
536 499
500/**
501 * Function to get the trait with the peer id.
502 *
503 * @param[out] id The peer id.
504 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
505 */
537int 506int
538GNUNET_TRANSPORT_get_trait_peer_id (const struct 507GNUNET_TRANSPORT_get_trait_peer_id (const struct
539 GNUNET_TESTING_Command *cmd, 508 GNUNET_TESTING_Command *cmd,
@@ -550,6 +519,12 @@ GNUNET_TRANSPORT_get_trait_peer_id (const struct
550 * Create command. 519 * Create command.
551 * 520 *
552 * @param label name for command. 521 * @param label name for command.
522 * @param system_label Label of the cmd to setup a test environment.
523 * @param m The number of the local node of the actual network namespace.
524 * @param n The number of the actual namespace.
525 * @param local_m Number of local nodes in each namespace.
526 * @param handlers Handler for messages received by this peer.
527 * @param cfgname Configuration file name for this peer.
553 * @return command. 528 * @return command.
554 */ 529 */
555struct GNUNET_TESTING_Command 530struct GNUNET_TESTING_Command
@@ -566,10 +541,6 @@ GNUNET_TRANSPORT_cmd_start_peer (const char *label,
566 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO); 541 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
567 unsigned int i; 542 unsigned int i;
568 543
569 LOG (GNUNET_ERROR_TYPE_ERROR,
570 "start peer 0.1 with cfg: %s\n",
571 cfgname);
572
573 sps = GNUNET_new (struct StartPeerState); 544 sps = GNUNET_new (struct StartPeerState);
574 sps->m = m; 545 sps->m = m;
575 sps->n = n; 546 sps->n = n;
@@ -579,9 +550,6 @@ GNUNET_TRANSPORT_cmd_start_peer (const char *label,
579 sps->connected_peers_map = connected_peers_map; 550 sps->connected_peers_map = connected_peers_map;
580 sps->cfgname = cfgname; 551 sps->cfgname = cfgname;
581 552
582 LOG (GNUNET_ERROR_TYPE_ERROR,
583 "start peer 0.2\n");
584
585 if (NULL != handlers) 553 if (NULL != handlers)
586 { 554 {
587 for (i = 0; NULL != handlers[i].cb; i++) 555 for (i = 0; NULL != handlers[i].cb; i++)
@@ -593,8 +561,6 @@ GNUNET_TRANSPORT_cmd_start_peer (const char *label,
593 i * sizeof(struct GNUNET_MQ_MessageHandler)); 561 i * sizeof(struct GNUNET_MQ_MessageHandler));
594 } 562 }
595 563
596 LOG (GNUNET_ERROR_TYPE_ERROR,
597 "start peer 0.3\n");
598 struct GNUNET_TESTING_Command cmd = { 564 struct GNUNET_TESTING_Command cmd = {
599 .cls = sps, 565 .cls = sps,
600 .label = label, 566 .label = label,