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.c274
1 files changed, 146 insertions, 128 deletions
diff --git a/src/transport/transport_api_cmd_start_peer.c b/src/transport/transport_api_cmd_start_peer.c
index c9af497ae..729d981c0 100644
--- a/src/transport/transport_api_cmd_start_peer.c
+++ b/src/transport/transport_api_cmd_start_peer.c
@@ -29,91 +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 unsigned int finished;
99
100 const char *system_label;
101
102 /**
103 * An unique number to identify the peer
104 */
105 unsigned int no;
106
107 struct GNUNET_CONTAINER_MultiPeerMap *connected_peers_map;
108
109 struct GNUNET_TESTING_System *tl_system;
110
111};
112
113 39
114static void 40static void
115retrieve_hello (void *cls); 41retrieve_hello (void *cls);
116 42
43
44/**
45 * Callback delivering the hello of this peer from peerstore.
46 *
47 */
117static void 48static void
118hello_iter_cb (void *cb_cls, 49hello_iter_cb (void *cb_cls,
119 const struct GNUNET_PEERSTORE_Record *record, 50 const struct GNUNET_PEERSTORE_Record *record,
@@ -138,6 +69,11 @@ hello_iter_cb (void *cb_cls,
138} 69}
139 70
140 71
72/**
73 * Function to start the retrieval task to retrieve the hello of this peer
74 * from the peerstore.
75 *
76 */
141static void 77static void
142retrieve_hello (void *cls) 78retrieve_hello (void *cls)
143{ 79{
@@ -145,13 +81,18 @@ retrieve_hello (void *cls)
145 sps->rh_task = NULL; 81 sps->rh_task = NULL;
146 sps->pic = GNUNET_PEERSTORE_iterate (sps->ph, 82 sps->pic = GNUNET_PEERSTORE_iterate (sps->ph,
147 "transport", 83 "transport",
148 sps->id, 84 &sps->id,
149 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY, 85 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
150 hello_iter_cb, 86 hello_iter_cb,
151 sps); 87 sps);
152 88
153} 89}
154 90
91
92/**
93 * This function checks StartPeerState#finished, which is set when the hello was retrieved.
94 *
95 */
155static int 96static int
156start_peer_finish (void *cls, 97start_peer_finish (void *cls,
157 GNUNET_SCHEDULER_TaskCallback cont, 98 GNUNET_SCHEDULER_TaskCallback cont,
@@ -168,6 +109,10 @@ start_peer_finish (void *cls,
168} 109}
169 110
170 111
112/**
113 * Disconnect callback for the connection to the core service.
114 *
115 */
171static void 116static void
172notify_disconnect (void *cls, 117notify_disconnect (void *cls,
173 const struct GNUNET_PeerIdentity *peer, 118 const struct GNUNET_PeerIdentity *peer,
@@ -179,38 +124,57 @@ notify_disconnect (void *cls,
179 "Peer %s disconnected from peer %u (`%s')\n", 124 "Peer %s disconnected from peer %u (`%s')\n",
180 GNUNET_i2s (peer), 125 GNUNET_i2s (peer),
181 sps->no, 126 sps->no,
182 GNUNET_i2s (sps->id)); 127 GNUNET_i2s (&sps->id));
183 128
184} 129}
185 130
186 131
132/**
133 * Connect callback for the connection to the core service.
134 *
135 */
187static void * 136static void *
188notify_connect (void *cls, 137notify_connect (void *cls,
189 const struct GNUNET_PeerIdentity *peer, 138 const struct GNUNET_PeerIdentity *peer,
190 struct GNUNET_MQ_Handle *mq) 139 struct GNUNET_MQ_Handle *mq)
191{ 140{
192 struct StartPeerState *sps = cls; 141 struct StartPeerState *sps = cls;
142 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
143 struct GNUNET_HashCode hc;
144 int node_number;
193 145
194 146 void *ret = NULL;
195 void *ret;
196 147
197 148
198 LOG (GNUNET_ERROR_TYPE_DEBUG, 149 LOG (GNUNET_ERROR_TYPE_DEBUG,
199 "Peer %s connected to peer %u (`%s')\n", 150 "Peer %s connected to peer %u (`%s')\n",
200 GNUNET_i2s (peer), 151 GNUNET_i2s (peer),
201 sps->no, 152 sps->no,
202 GNUNET_i2s (sps->id)); 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
203 159
204 GNUNET_CONTAINER_multipeermap_put (sps->connected_peers_map, 160 memcpy (key,
205 peer, 161 &hc,
206 mq, 162 sizeof (*key));
207 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 163 GNUNET_CONTAINER_multishortmap_put (sps->connected_peers_map,
164 key,
165 mq,
166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
208 167
168 GNUNET_free (key);
209 // TODO what does the handler function need? 169 // TODO what does the handler function need?
210 return ret; 170 return ret;
211} 171}
212 172
213 173
174/**
175 * The run method of this cmd will start all services of a peer to test the transport service.
176 *
177 */
214static void 178static void
215start_peer_run (void *cls, 179start_peer_run (void *cls,
216 const struct GNUNET_TESTING_Command *cmd, 180 const struct GNUNET_TESTING_Command *cmd,
@@ -251,6 +215,7 @@ start_peer_run (void *cls,
251 sps->cfgname); 215 sps->cfgname);
252 GNUNET_CONFIGURATION_destroy (sps->cfg); 216 GNUNET_CONFIGURATION_destroy (sps->cfg);
253 GNUNET_TESTING_interpreter_fail (); 217 GNUNET_TESTING_interpreter_fail ();
218 return;
254 } 219 }
255 220
256 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system, 221 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system,
@@ -266,6 +231,7 @@ start_peer_run (void *cls,
266 emsg); 231 emsg);
267 GNUNET_free (emsg); 232 GNUNET_free (emsg);
268 GNUNET_TESTING_interpreter_fail (); 233 GNUNET_TESTING_interpreter_fail ();
234 return;
269 } 235 }
270 236
271 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer)) 237 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
@@ -275,13 +241,16 @@ start_peer_run (void *cls,
275 sps->cfgname); 241 sps->cfgname);
276 GNUNET_free (emsg); 242 GNUNET_free (emsg);
277 GNUNET_TESTING_interpreter_fail (); 243 GNUNET_TESTING_interpreter_fail ();
244 return;
278 } 245 }
279 246
280 memset (&dummy, 247 memset (&dummy,
281 '\0', 248 '\0',
282 sizeof(dummy)); 249 sizeof(dummy));
250
283 GNUNET_TESTING_peer_get_identity (sps->peer, 251 GNUNET_TESTING_peer_get_identity (sps->peer,
284 sps->id); 252 &sps->id);
253
285 if (0 == memcmp (&dummy, 254 if (0 == memcmp (&dummy,
286 &sps->id, 255 &sps->id,
287 sizeof(struct GNUNET_PeerIdentity))) 256 sizeof(struct GNUNET_PeerIdentity)))
@@ -291,11 +260,13 @@ start_peer_run (void *cls,
291 sps->no); 260 sps->no);
292 GNUNET_free (emsg); 261 GNUNET_free (emsg);
293 GNUNET_TESTING_interpreter_fail (); 262 GNUNET_TESTING_interpreter_fail ();
263 return;
294 } 264 }
295 LOG (GNUNET_ERROR_TYPE_DEBUG, 265 LOG (GNUNET_ERROR_TYPE_DEBUG,
296 "Peer %u configured with identity `%s'\n", 266 "Peer %u configured with identity `%s'\n",
297 sps->no, 267 sps->no,
298 GNUNET_i2s_full (sps->id)); 268 GNUNET_i2s_full (&sps->id));
269
299 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg, 270 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
300 NULL, 271 NULL,
301 sps->handlers, 272 sps->handlers,
@@ -310,7 +281,9 @@ start_peer_run (void *cls,
310 emsg); 281 emsg);
311 GNUNET_free (emsg); 282 GNUNET_free (emsg);
312 GNUNET_TESTING_interpreter_fail (); 283 GNUNET_TESTING_interpreter_fail ();
284 return;
313 } 285 }
286
314 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg); 287 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
315 if (NULL == sps->th) 288 if (NULL == sps->th)
316 { 289 {
@@ -320,7 +293,9 @@ start_peer_run (void *cls,
320 emsg); 293 emsg);
321 GNUNET_free (emsg); 294 GNUNET_free (emsg);
322 GNUNET_TESTING_interpreter_fail (); 295 GNUNET_TESTING_interpreter_fail ();
296 return;
323 } 297 }
298
324 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg); 299 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
325 if (NULL == sps->ah) 300 if (NULL == sps->ah)
326 { 301 {
@@ -330,32 +305,22 @@ start_peer_run (void *cls,
330 emsg); 305 emsg);
331 GNUNET_free (emsg); 306 GNUNET_free (emsg);
332 GNUNET_TESTING_interpreter_fail (); 307 GNUNET_TESTING_interpreter_fail ();
308 return;
333 } 309 }
334 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps); 310 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
335} 311}
336 312
337 313
314/**
315 * The cleanup function of this cmd frees resources the cmd allocated.
316 *
317 */
338static void 318static void
339start_peer_cleanup (void *cls, 319start_peer_cleanup (void *cls,
340 const struct GNUNET_TESTING_Command *cmd) 320 const struct GNUNET_TESTING_Command *cmd)
341{ 321{
342 struct StartPeerState *sps = cls; 322 struct StartPeerState *sps = cls;
343 323
344 if (NULL != sps->rh_task)
345 GNUNET_SCHEDULER_cancel (sps->rh_task);
346 sps->rh_task = NULL;
347 if (NULL != sps->ah)
348 {
349 GNUNET_TRANSPORT_application_done (sps->ah);
350 sps->ah = NULL;
351 }
352 if (NULL != sps->ph)
353 {
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Disconnecting from PEERSTORE service\n");
356 GNUNET_PEERSTORE_disconnect (sps->ph, GNUNET_NO);
357 sps->ph = NULL;
358 }
359 if (NULL != sps->handlers) 324 if (NULL != sps->handlers)
360 { 325 {
361 GNUNET_free (sps->handlers); 326 GNUNET_free (sps->handlers);
@@ -366,34 +331,16 @@ start_peer_cleanup (void *cls,
366 GNUNET_CONFIGURATION_destroy (sps->cfg); 331 GNUNET_CONFIGURATION_destroy (sps->cfg);
367 sps->cfg = NULL; 332 sps->cfg = NULL;
368 } 333 }
369 if (NULL != sps->peer)
370 {
371 if (GNUNET_OK !=
372 GNUNET_TESTING_peer_stop (sps->peer))
373 {
374 LOG (GNUNET_ERROR_TYPE_DEBUG,
375 "Testing lib failed to stop peer %u (`%s')\n",
376 sps->no,
377 GNUNET_i2s (sps->id));
378 }
379 GNUNET_TESTING_peer_destroy (sps->peer);
380 sps->peer = NULL;
381 }
382 if (NULL != sps->th)
383 {
384 GNUNET_TRANSPORT_core_disconnect (sps->th);
385 sps->th = NULL;
386 }
387 if (NULL != sps->tl_system)
388 {
389 GNUNET_free (sps->tl_system);
390 }
391 GNUNET_free (sps->hello); 334 GNUNET_free (sps->hello);
392 GNUNET_free (sps->connected_peers_map); 335 GNUNET_free (sps->connected_peers_map);
393 GNUNET_free (sps); 336 GNUNET_free (sps);
394} 337}
395 338
396 339
340/**
341 * This function prepares an array with traits.
342 *
343 */
397static int 344static int
398start_peer_traits (void *cls, 345start_peer_traits (void *cls,
399 const void **ret, 346 const void **ret,
@@ -402,8 +349,8 @@ start_peer_traits (void *cls,
402{ 349{
403 struct StartPeerState *sps = cls; 350 struct StartPeerState *sps = cls;
404 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah; 351 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
405 struct GNUNET_PeerIdentity *id = sps->id; 352 struct GNUNET_PeerIdentity *id = &sps->id;
406 struct GNUNET_CONTAINER_MultiPeerMap *connected_peers_map = 353 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
407 sps->connected_peers_map; 354 sps->connected_peers_map;
408 char *hello = sps->hello; 355 char *hello = sps->hello;
409 size_t hello_size = sps->hello_size; 356 size_t hello_size = sps->hello_size;
@@ -435,6 +382,11 @@ start_peer_traits (void *cls,
435 .trait_name = "hello_size", 382 .trait_name = "hello_size",
436 .ptr = (const void *) hello_size, 383 .ptr = (const void *) hello_size,
437 }, 384 },
385 {
386 .index = 5,
387 .trait_name = "state",
388 .ptr = (const void *) sps,
389 },
438 GNUNET_TESTING_trait_end () 390 GNUNET_TESTING_trait_end ()
439 }; 391 };
440 392
@@ -444,6 +396,34 @@ start_peer_traits (void *cls,
444 index); 396 index);
445} 397}
446 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 */
447int 427int
448GNUNET_TRANSPORT_get_trait_hello_size (const struct 428GNUNET_TRANSPORT_get_trait_hello_size (const struct
449 GNUNET_TESTING_Command 429 GNUNET_TESTING_Command
@@ -456,6 +436,14 @@ GNUNET_TRANSPORT_get_trait_hello_size (const struct
456 (unsigned int) 4); 436 (unsigned int) 4);
457} 437}
458 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 */
459int 447int
460GNUNET_TRANSPORT_get_trait_hello (const struct 448GNUNET_TRANSPORT_get_trait_hello (const struct
461 GNUNET_TESTING_Command 449 GNUNET_TESTING_Command
@@ -468,12 +456,21 @@ GNUNET_TRANSPORT_get_trait_hello (const struct
468 (unsigned int) 3); 456 (unsigned int) 3);
469} 457}
470 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 */
471int 467int
472GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct 468GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct
473 GNUNET_TESTING_Command 469 GNUNET_TESTING_Command
474 *cmd, 470 *cmd,
475 struct 471 struct
476 GNUNET_CONTAINER_MultiPeerMap ** 472 GNUNET_CONTAINER_MultiShortmap *
473 *
477 connected_peers_map) 474 connected_peers_map)
478{ 475{
479 return cmd->traits (cmd->cls, 476 return cmd->traits (cmd->cls,
@@ -483,6 +480,12 @@ GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct
483} 480}
484 481
485 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 */
486int 489int
487GNUNET_TRANSPORT_get_trait_application_handle (const struct 490GNUNET_TRANSPORT_get_trait_application_handle (const struct
488 GNUNET_TESTING_Command *cmd, 491 GNUNET_TESTING_Command *cmd,
@@ -497,6 +500,12 @@ GNUNET_TRANSPORT_get_trait_application_handle (const struct
497} 500}
498 501
499 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 */
500int 509int
501GNUNET_TRANSPORT_get_trait_peer_id (const struct 510GNUNET_TRANSPORT_get_trait_peer_id (const struct
502 GNUNET_TESTING_Command *cmd, 511 GNUNET_TESTING_Command *cmd,
@@ -513,6 +522,12 @@ GNUNET_TRANSPORT_get_trait_peer_id (const struct
513 * Create command. 522 * Create command.
514 * 523 *
515 * @param label name for command. 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.
516 * @return command. 531 * @return command.
517 */ 532 */
518struct GNUNET_TESTING_Command 533struct GNUNET_TESTING_Command
@@ -520,17 +535,20 @@ GNUNET_TRANSPORT_cmd_start_peer (const char *label,
520 const char *system_label, 535 const char *system_label,
521 char *m, 536 char *m,
522 char *n, 537 char *n,
538 char *local_m,
523 struct GNUNET_MQ_MessageHandler *handlers, 539 struct GNUNET_MQ_MessageHandler *handlers,
524 const char *cfgname) 540 const char *cfgname)
525{ 541{
526 struct StartPeerState *sps; 542 struct StartPeerState *sps;
527 struct GNUNET_CONTAINER_MultiPeerMap *connected_peers_map = 543 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
528 GNUNET_CONTAINER_multipeermap_create (1,GNUNET_NO); 544 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
529 unsigned int i; 545 unsigned int i;
530 546
531 sps = GNUNET_new (struct StartPeerState); 547 sps = GNUNET_new (struct StartPeerState);
532 sps->m = m; 548 sps->m = m;
533 sps->n = n; 549 sps->n = n;
550 sps->local_m = local_m;
551 sps->no = (atoi (n) - 1) * atoi (sps->local_m) + atoi (m);
534 sps->system_label = system_label; 552 sps->system_label = system_label;
535 sps->connected_peers_map = connected_peers_map; 553 sps->connected_peers_map = connected_peers_map;
536 sps->cfgname = cfgname; 554 sps->cfgname = cfgname;