aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport-testing2.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport-testing2.c')
-rw-r--r--src/transport/transport-testing2.c959
1 files changed, 0 insertions, 959 deletions
diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c
deleted file mode 100644
index 482aaf4d0..000000000
--- a/src/transport/transport-testing2.c
+++ /dev/null
@@ -1,959 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2015, 2016 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 * @file transport-testing.c
22 * @brief testing lib for transport service
23 * @author Matthias Wachs
24 * @author Christian Grothoff
25 */
26#include "transport-testing2.h"
27
28
29#define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing", __VA_ARGS__)
30
31
32static struct GNUNET_TRANSPORT_TESTING_PeerContext *
33find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
34 const struct GNUNET_PeerIdentity *peer)
35{
36 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
37
38 for (t = tth->p_head; NULL != t; t = t->next)
39 if (0 == memcmp (&t->id,
40 peer,
41 sizeof(struct GNUNET_PeerIdentity)))
42 return t;
43 return NULL;
44}
45
46
47/**
48 * Find any connecting context matching the given pair of peers.
49 *
50 * @param p1 first peer
51 * @param p2 second peer
52 * @param cb function to call
53 * @param cb_cls closure for @a cb
54 */
55void
56GNUNET_TRANSPORT_TESTING_find_connecting_context (struct
57 GNUNET_TRANSPORT_TESTING_PeerContext
58 *p1,
59 struct
60 GNUNET_TRANSPORT_TESTING_PeerContext
61 *p2,
62 GNUNET_TRANSPORT_TESTING_ConnectContextCallback
63 cb,
64 void *cb_cls)
65{
66 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
67 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
68 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
69
70 for (cc = tth->cc_head; NULL != cc; cc = ccn)
71 {
72 ccn = cc->next;
73 if ((cc->p1 == p1) &&
74 (cc->p2 == p2))
75 cb (cb_cls,
76 cc);
77 }
78}
79
80
81static void
82set_p1c (void *cls,
83 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
84{
85 int *found = cls;
86
87 if (NULL != found)
88 *found = GNUNET_YES;
89 cx->p1_c = GNUNET_YES;
90}
91
92
93static void
94set_mq (void *cls,
95 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
96{
97 struct GNUNET_MQ_Handle *mq = cls;
98
99 cx->mq = mq;
100}
101
102
103static void
104set_p2c (void *cls,
105 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
106{
107 int *found = cls;
108
109 if (NULL != found)
110 *found = GNUNET_YES;
111 cx->p2_c = GNUNET_YES;
112}
113
114
115static void
116clear_p1c (void *cls,
117 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
118{
119 int *found = cls;
120
121 if (NULL != found)
122 *found = GNUNET_YES;
123 cx->p1_c = GNUNET_NO;
124}
125
126
127static void
128clear_p2c (void *cls,
129 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
130{
131 int *found = cls;
132
133 if (NULL != found)
134 *found = GNUNET_YES;
135 cx->p2_c = GNUNET_NO;
136}
137
138
139static void *
140notify_connect (void *cls,
141 const struct GNUNET_PeerIdentity *peer,
142 struct GNUNET_MQ_Handle *mq)
143{
144 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
145 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
146 char *p2_s;
147 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
148 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
149 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
150 int found;
151 void *ret;
152
153 p2 = find_peer_context (p->tth,
154 peer);
155 if (NULL != p->nc)
156 ret = p->nc (p->cb_cls,
157 peer,
158 mq);
159 else
160 ret = NULL;
161
162 if (NULL != p2)
163 GNUNET_asprintf (&p2_s,
164 "%u (`%s')",
165 p2->no,
166 GNUNET_i2s (&p2->id));
167 else
168 GNUNET_asprintf (&p2_s,
169 "`%s'",
170 GNUNET_i2s (peer));
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 "Peers %s connected to peer %u (`%s')\n",
173 p2_s,
174 p->no,
175 GNUNET_i2s (&p->id));
176 GNUNET_free (p2_s);
177 /* update flags in connecting contexts */
178 found = GNUNET_NO;
179 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
180 p2,
181 &set_p1c,
182 &found);
183 if (GNUNET_NO == found)
184 {
185 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
186 cc->p1 = p;
187 cc->p2 = p2;
188 cc->p1_c = GNUNET_YES;
189 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
190 tth->cc_tail,
191 cc);
192 }
193 found = GNUNET_NO;
194 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
195 p,
196 &set_p2c,
197 &found);
198 if (GNUNET_NO == found)
199 {
200 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
201 cc->p1 = p2;
202 cc->p2 = p;
203 cc->p1_c = GNUNET_YES;
204 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
205 tth->cc_tail,
206 cc);
207 }
208 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
209 p2,
210 &set_mq,
211 mq);
212 /* update set connected flag for all requests */
213 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
214 {
215 if (GNUNET_YES == cc->connected)
216 continue;
217 if ((GNUNET_YES == cc->p1_c) &&
218 (GNUNET_YES == cc->p2_c))
219 {
220 cc->connected = GNUNET_YES;
221 /* stop trying to connect */
222 if (NULL != cc->tct)
223 {
224 GNUNET_SCHEDULER_cancel (cc->tct);
225 cc->tct = NULL;
226 }
227 if (NULL != cc->ats_sh)
228 {
229 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
230 cc->ats_sh = NULL;
231 }
232 }
233 }
234 /* then notify application */
235 for (cc = tth->cc_head; NULL != cc; cc = ccn)
236 {
237 ccn = cc->next;
238 if ((GNUNET_YES == cc->connected) &&
239 (NULL != cc->cb))
240 {
241 cc->cb (cc->cb_cls);
242 cc->cb = NULL; /* only notify once! */
243 }
244 }
245 return ret;
246}
247
248
249/**
250 * Offer the current HELLO of P2 to P1.
251 *
252 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
253 */
254static void
255offer_hello (void *cls);
256
257
258static void
259notify_disconnect (void *cls,
260 const struct GNUNET_PeerIdentity *peer,
261 void *handler_cls)
262{
263 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
264 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
265 char *p2_s;
266 /* Find PeerContext */
267 int no = 0;
268 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL;
269 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
270
271 p2 = find_peer_context (p->tth,
272 peer);
273 no = p->no;
274 if (NULL != p2)
275 GNUNET_asprintf (&p2_s,
276 "%u (`%s')",
277 p2->no,
278 GNUNET_i2s (&p2->id));
279 else
280 GNUNET_asprintf (&p2_s,
281 "`%s'",
282 GNUNET_i2s (peer));
283 LOG (GNUNET_ERROR_TYPE_DEBUG,
284 "Peers %s disconnected from peer %u (`%s')\n",
285 p2_s,
286 no,
287 GNUNET_i2s (&p->id));
288 GNUNET_free (p2_s);
289 /* notify about disconnect */
290 if (NULL != p->nd)
291 p->nd (p->cb_cls,
292 peer,
293 handler_cls);
294 if (NULL == p2)
295 return;
296 /* clear MQ, it is now invalid */
297 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
298 p2,
299 &set_mq,
300 NULL);
301 /* update set connected flags for all requests */
302 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
303 p2,
304 &clear_p1c,
305 NULL);
306 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
307 p,
308 &clear_p2c,
309 NULL);
310 /* resume connectivity requests as necessary */
311 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
312 {
313 if (GNUNET_NO == cc->connected)
314 continue;
315 if ((GNUNET_YES != cc->p1_c) ||
316 (GNUNET_YES != cc->p2_c))
317 {
318 cc->connected = GNUNET_NO;
319 /* start trying to connect */
320 if (NULL == cc->tct)
321 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
322 cc);
323 if (NULL == cc->ats_sh)
324 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
325 &p2->id,
326 1);
327 }
328 }
329}
330
331static void
332retrieve_hello (void *cls);
333
334static void
335hello_iter_cb (void *cb_cls,
336 const struct GNUNET_PEERSTORE_Record *record,
337 const char *emsg)
338{
339 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls;
340 if (NULL == record)
341 {
342 p->pic = NULL;
343 if (NULL != p->start_cb)
344 p->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, p);
345 return;
346 }
347 // Check record type et al?
348 p->hello_size = record->value_size;
349 p->hello = GNUNET_malloc (p->hello_size);
350 memcpy (p->hello, record->value, p->hello_size);
351 p->hello[p->hello_size - 1] = '\0';
352
353 GNUNET_PEERSTORE_iterate_cancel (p->pic);
354 p->pic = NULL;
355 if (NULL != p->start_cb)
356 {
357 LOG (GNUNET_ERROR_TYPE_DEBUG,
358 "Peer %u (`%s') successfully started\n",
359 p->no,
360 GNUNET_i2s (&p->id));
361 p->start_cb (p->start_cb_cls);
362 p->start_cb = NULL;
363 }
364}
365
366
367static void
368retrieve_hello (void *cls)
369{
370 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
371 p->rh_task = NULL;
372 p->pic = GNUNET_PEERSTORE_iterate (p->ph,
373 "transport",
374 &p->id,
375 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
376 hello_iter_cb,
377 p);
378
379}
380
381
382/**
383 * Start a peer with the given configuration
384 * @param tth the testing handle
385 * @param cfgname configuration file
386 * @param peer_id a unique number to identify the peer
387 * @param handlers functions for receiving messages
388 * @param nc connect callback
389 * @param nd disconnect callback
390 * @param cb_cls closure for callback
391 * @param start_cb start callback
392 * @param start_cb_cls closure for callback
393 * @return the peer context
394 */
395struct GNUNET_TRANSPORT_TESTING_PeerContext *
396GNUNET_TRANSPORT_TESTING_start_peer (struct
397 GNUNET_TRANSPORT_TESTING_Handle *tth,
398 const char *cfgname,
399 int peer_id,
400 const struct
401 GNUNET_MQ_MessageHandler *handlers,
402 GNUNET_TRANSPORT_NotifyConnect nc,
403 GNUNET_TRANSPORT_NotifyDisconnect nd,
404 void *cb_cls,
405 GNUNET_SCHEDULER_TaskCallback start_cb,
406 void *start_cb_cls)
407{
408 char *emsg = NULL;
409 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
410 struct GNUNET_PeerIdentity dummy;
411 unsigned int i;
412
413 if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
414 {
415 LOG (GNUNET_ERROR_TYPE_ERROR,
416 "File not found: `%s'\n",
417 cfgname);
418 return NULL;
419 }
420
421 p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext);
422 p->tth = tth;
423 p->nc = nc;
424 p->nd = nd;
425 if (NULL != handlers)
426 {
427 for (i = 0; NULL != handlers[i].cb; i++)
428 ;
429 p->handlers = GNUNET_new_array (i + 1,
430 struct GNUNET_MQ_MessageHandler);
431 GNUNET_memcpy (p->handlers,
432 handlers,
433 i * sizeof(struct GNUNET_MQ_MessageHandler));
434 }
435 if (NULL != cb_cls)
436 p->cb_cls = cb_cls;
437 else
438 p->cb_cls = p;
439 p->start_cb = start_cb;
440 if (NULL != start_cb_cls)
441 p->start_cb_cls = start_cb_cls;
442 else
443 p->start_cb_cls = p;
444 GNUNET_CONTAINER_DLL_insert (tth->p_head,
445 tth->p_tail,
446 p);
447
448 /* Create configuration and call testing lib to modify it */
449 p->cfg = GNUNET_CONFIGURATION_create ();
450 GNUNET_assert (GNUNET_OK ==
451 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
452 if (GNUNET_SYSERR ==
453 GNUNET_TESTING_configuration_create (tth->tl_system,
454 p->cfg))
455 {
456 LOG (GNUNET_ERROR_TYPE_ERROR,
457 "Testing library failed to create unique configuration based on `%s'\n",
458 cfgname);
459 GNUNET_CONFIGURATION_destroy (p->cfg);
460 GNUNET_free (p);
461 return NULL;
462 }
463
464 p->no = peer_id;
465 /* Configure peer with configuration */
466 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
467 p->cfg,
468 p->no,
469 NULL,
470 &emsg);
471 if (NULL == p->peer)
472 {
473 LOG (GNUNET_ERROR_TYPE_ERROR,
474 "Testing library failed to create unique configuration based on `%s': `%s'\n",
475 cfgname,
476 emsg);
477 GNUNET_TRANSPORT_TESTING_stop_peer (p);
478 GNUNET_free (emsg);
479 return NULL;
480 }
481 GNUNET_free (emsg);
482 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
483 {
484 LOG (GNUNET_ERROR_TYPE_ERROR,
485 "Testing library failed to create unique configuration based on `%s'\n",
486 cfgname);
487 GNUNET_TRANSPORT_TESTING_stop_peer (p);
488 return NULL;
489 }
490
491 memset (&dummy,
492 '\0',
493 sizeof(dummy));
494 GNUNET_TESTING_peer_get_identity (p->peer,
495 &p->id);
496 if (0 == memcmp (&dummy,
497 &p->id,
498 sizeof(struct GNUNET_PeerIdentity)))
499 {
500 LOG (GNUNET_ERROR_TYPE_ERROR,
501 "Testing library failed to obtain peer identity for peer %u\n",
502 p->no);
503 GNUNET_TRANSPORT_TESTING_stop_peer (p);
504 return NULL;
505 }
506 LOG (GNUNET_ERROR_TYPE_DEBUG,
507 "Peer %u configured with identity `%s'\n",
508 p->no,
509 GNUNET_i2s_full (&p->id));
510 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
511 NULL,
512 handlers,
513 p,
514 &notify_connect,
515 &notify_disconnect);
516 if (NULL == p->th)
517 {
518 LOG (GNUNET_ERROR_TYPE_ERROR,
519 "Failed to connect to transport service for peer `%s': `%s'\n",
520 cfgname,
521 emsg);
522 GNUNET_TRANSPORT_TESTING_stop_peer (p);
523 return NULL;
524 }
525 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
526 if (NULL == p->ats)
527 {
528 LOG (GNUNET_ERROR_TYPE_ERROR,
529 "Failed to connect to ATS service for peer `%s': `%s'\n",
530 cfgname,
531 emsg);
532 GNUNET_TRANSPORT_TESTING_stop_peer (p);
533 return NULL;
534 }
535 p->ph = GNUNET_PEERSTORE_connect (p->cfg);
536 // FIXME Error handling
537 p->ah = GNUNET_TRANSPORT_application_init (p->cfg);
538 GNUNET_assert (NULL != p->ah);
539 // FIXME Error handling
540 p->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, p);
541
542 return p;
543}
544
545
546/**
547 * Stops and restarts the given peer, sleeping (!) for 5s in between.
548 *
549 * @param p the peer
550 * @param restart_cb callback to call when restarted
551 * @param restart_cb_cls callback closure
552 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
553 */
554int
555GNUNET_TRANSPORT_TESTING_restart_peer (struct
556 GNUNET_TRANSPORT_TESTING_PeerContext *p,
557 GNUNET_SCHEDULER_TaskCallback restart_cb,
558 void *restart_cb_cls)
559{
560 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
561 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
562
563 /* shutdown */
564 LOG (GNUNET_ERROR_TYPE_DEBUG,
565 "Stopping peer %u (`%s')\n",
566 p->no,
567 GNUNET_i2s (&p->id));
568 if (NULL != p->pic)
569 {
570 GNUNET_PEERSTORE_iterate_cancel (p->pic);
571 p->pic = NULL;
572 }
573 if (NULL != p->th)
574 {
575 GNUNET_TRANSPORT_core_disconnect (p->th);
576 p->th = NULL;
577 }
578 for (cc = p->tth->cc_head; NULL != cc; cc = ccn)
579 {
580 ccn = cc->next;
581 if ((cc->p1 == p) ||
582 (cc->p2 == p))
583 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
584 }
585 if (NULL != p->ats)
586 {
587 GNUNET_ATS_connectivity_done (p->ats);
588 p->ats = NULL;
589 }
590 if (GNUNET_SYSERR ==
591 GNUNET_TESTING_peer_stop (p->peer))
592 {
593 LOG (GNUNET_ERROR_TYPE_ERROR,
594 "Failed to stop peer %u (`%s')\n",
595 p->no,
596 GNUNET_i2s (&p->id));
597 return GNUNET_SYSERR;
598 }
599
600 sleep (5); // YUCK!
601
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "Restarting peer %u (`%s')\n",
604 p->no,
605 GNUNET_i2s (&p->id));
606 /* restart */
607 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
608 {
609 LOG (GNUNET_ERROR_TYPE_ERROR,
610 "Failed to restart peer %u (`%s')\n",
611 p->no,
612 GNUNET_i2s (&p->id));
613 return GNUNET_SYSERR;
614 }
615
616 GNUNET_assert (NULL == p->start_cb);
617 p->start_cb = restart_cb;
618 p->start_cb_cls = restart_cb_cls;
619
620 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
621 NULL,
622 p->handlers,
623 p,
624 &notify_connect,
625 &notify_disconnect);
626 GNUNET_assert (NULL != p->th);
627 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
628 p->pic = GNUNET_PEERSTORE_iterate (p->ph,
629 "transport",
630 &p->id,
631 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
632 hello_iter_cb,
633 p);
634 GNUNET_assert (NULL != p->pic);
635 return GNUNET_OK;
636}
637
638
639/**
640 * Shutdown the given peer
641 *
642 * @param p the peer
643 */
644void
645GNUNET_TRANSPORT_TESTING_stop_peer (struct
646 GNUNET_TRANSPORT_TESTING_PeerContext *p)
647{
648 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
649 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
650 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
651 /* shutdown */
652 LOG (GNUNET_ERROR_TYPE_DEBUG,
653 "Stopping peer %u (`%s')\n",
654 p->no,
655 GNUNET_i2s (&p->id));
656
657 for (cc = tth->cc_head; NULL != cc; cc = ccn)
658 {
659 ccn = cc->next;
660 if ((cc->p1 == p) ||
661 (cc->p2 == p))
662 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
663 }
664 if (NULL != p->pic)
665 {
666 GNUNET_PEERSTORE_iterate_cancel (p->pic);
667 p->pic = NULL;
668 }
669 if (NULL != p->th)
670 {
671 GNUNET_TRANSPORT_core_disconnect (p->th);
672 p->th = NULL;
673 }
674 if (NULL != p->ats)
675 {
676 GNUNET_ATS_connectivity_done (p->ats);
677 p->ats = NULL;
678 }
679 if (NULL != p->ah)
680 {
681 GNUNET_TRANSPORT_application_done (p->ah);
682 p->ah = NULL;
683 }
684 if (NULL != p->ph)
685 {
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Disconnecting from PEERSTORE service\n");
688 GNUNET_PEERSTORE_disconnect (p->ph, GNUNET_NO);
689 p->ph = NULL;
690 }
691
692 if (NULL != p->peer)
693 {
694 if (GNUNET_OK !=
695 GNUNET_TESTING_peer_stop (p->peer))
696 {
697 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 "Testing lib failed to stop peer %u (`%s')\n",
699 p->no,
700 GNUNET_i2s (&p->id));
701 }
702 GNUNET_TESTING_peer_destroy (p->peer);
703 p->peer = NULL;
704 }
705 if (NULL != p->hello)
706 {
707 GNUNET_free (p->hello);
708 p->hello = NULL;
709 }
710 if (NULL != p->cfg)
711 {
712 GNUNET_CONFIGURATION_destroy (p->cfg);
713 p->cfg = NULL;
714 }
715 if (NULL != p->handlers)
716 {
717 GNUNET_free (p->handlers);
718 p->handlers = NULL;
719 }
720 GNUNET_CONTAINER_DLL_remove (tth->p_head,
721 tth->p_tail,
722 p);
723 LOG (GNUNET_ERROR_TYPE_DEBUG,
724 "Peer %u (`%s') stopped\n",
725 p->no,
726 GNUNET_i2s (&p->id));
727 if (NULL != p->rh_task)
728 GNUNET_SCHEDULER_cancel (p->rh_task);
729 p->rh_task = NULL;
730 GNUNET_free (p);
731}
732
733
734/**
735 * Function called after the HELLO was passed to the
736 * transport service.
737 * FIXME maybe schedule the application_validate somehow
738 */
739/*
740 static void
741 hello_offered (void *cls)
742 {
743 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
744
745 cc->oh = NULL;
746 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
747 &offer_hello,
748 cc);
749 }*/
750
751
752/**
753 * Offer the current HELLO of P2 to P1.
754 *
755 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
756 */
757static void
758offer_hello (void *cls)
759{
760 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
761 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1;
762 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2;
763 struct GNUNET_TIME_Absolute t;
764 enum GNUNET_NetworkType nt = 0;
765 char *addr;
766
767 cc->tct = NULL;
768 {
769 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
770
771 LOG (GNUNET_ERROR_TYPE_DEBUG,
772 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %s\n",
773 p1->no,
774 GNUNET_i2s (&p1->id),
775 p2->no,
776 p2_s,
777 p2->hello);
778 GNUNET_free (p2_s);
779 }
780
781 addr = GNUNET_HELLO_extract_address (p2->hello,
782 p2->hello_size,
783 &p2->id,
784 &nt,
785 &t);
786 GNUNET_assert (NULL != addr);
787 GNUNET_assert (NULL != p1->hello);
788 GNUNET_TRANSPORT_application_validate (p1->ah,
789 &p2->id,
790 nt,
791 addr);
792 GNUNET_free (addr);
793}
794
795
796/**
797 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
798 *
799 * Remarks: start_peer's notify_connect callback can be called before.
800 *
801 * @param tth transport testing handle
802 * @param p1 peer 1
803 * @param p2 peer 2
804 * @param cb the callback to call when both peers notified that they are connected
805 * @param cls callback cls
806 * @return a connect request handle
807 */
808struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
809GNUNET_TRANSPORT_TESTING_connect_peers (struct
810 GNUNET_TRANSPORT_TESTING_PeerContext *p1,
811 struct
812 GNUNET_TRANSPORT_TESTING_PeerContext *p2,
813 GNUNET_SCHEDULER_TaskCallback cb,
814 void *cls)
815{
816 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
817 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
818 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
819
820 ccn = NULL;
821 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
822 {
823 if ((cc->p1 == p1) &&
824 (cc->p2 == p2))
825 {
826 ccn = cc;
827 break;
828 }
829 }
830
831 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
832 cc->p1 = p1;
833 cc->p2 = p2;
834 cc->cb = cb;
835 if (NULL != cls)
836 cc->cb_cls = cls;
837 else
838 cc->cb_cls = cc;
839 if (NULL != ccn)
840 {
841 cc->p1_c = ccn->p1_c;
842 cc->p2_c = ccn->p2_c;
843 cc->connected = ccn->connected;
844 }
845 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
846 tth->cc_tail,
847 cc);
848 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
849 cc);
850 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
851 &p2->id,
852 1);
853 LOG (GNUNET_ERROR_TYPE_DEBUG,
854 "New connect request %p\n",
855 cc);
856 return cc;
857}
858
859
860/**
861 * Cancel the request to connect two peers
862 * Tou MUST cancel the request if you stop the peers before the peers connected successfully
863 *
864 * @param tth transport testing handle
865 * @param cc a connect request handle
866 */
867void
868GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
869 GNUNET_TRANSPORT_TESTING_ConnectRequest
870 *cc)
871{
872 struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth;
873
874 LOG (GNUNET_ERROR_TYPE_DEBUG,
875 "Canceling connect request!\n");
876 if (NULL != cc->tct)
877 {
878 GNUNET_SCHEDULER_cancel (cc->tct);
879 cc->tct = NULL;
880 }
881 if (NULL != cc->ats_sh)
882 {
883 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
884 cc->ats_sh = NULL;
885 }
886 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
887 tth->cc_tail,
888 cc);
889 GNUNET_free (cc);
890}
891
892
893/**
894 * Clean up the transport testing
895 *
896 * @param tth transport testing handle
897 */
898void
899GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth)
900{
901 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
902 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct;
903 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
904 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
905
906 if (NULL == tth)
907 return;
908 cc = tth->cc_head;
909 while (NULL != cc)
910 {
911 ct = cc->next;
912 LOG (GNUNET_ERROR_TYPE_ERROR,
913 "Developer forgot to cancel connect request!\n");
914 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
915 cc = ct;
916 }
917 p = tth->p_head;
918 while (NULL != p)
919 {
920 t = p->next;
921 LOG (GNUNET_ERROR_TYPE_ERROR,
922 "Developer forgot to stop peer!\n");
923 GNUNET_TRANSPORT_TESTING_stop_peer (p);
924 p = t;
925 }
926 GNUNET_TESTING_system_destroy (tth->tl_system,
927 GNUNET_YES);
928
929 GNUNET_free (tth);
930}
931
932
933/**
934 * Initialize the transport testing
935 *
936 * @return transport testing handle
937 */
938struct GNUNET_TRANSPORT_TESTING_Handle *
939GNUNET_TRANSPORT_TESTING_init ()
940{
941 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
942
943 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle);
944 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
945 NULL,
946 NULL,
947 NULL);
948 if (NULL == tth->tl_system)
949 {
950 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
951 "Failed to initialize testing library!\n");
952 GNUNET_free (tth);
953 return NULL;
954 }
955 return tth;
956}
957
958
959/* end of transport-testing.c */