2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/gnunet-service-testbed_oc.c
23 * @brief code for handling overlay connect operations
24 * @author Sree Harsha Totakura
27 #include "gnunet-service-testbed.h"
30 * Redefine LOG with a changed log component string
35 #define LOG(kind,...) \
36 GNUNET_log_from (kind, "testbed-OC", __VA_ARGS__)
40 * Context information for requesting TRANSPORT to connect to a peer
42 struct TryConnectContext
45 * The identity of the peer to which the transport has to attempt a connection
47 struct GNUNET_PeerIdentity *pid;
50 * The transport handle obtained from cache. Do NOT close/disconnect.
52 struct GNUNET_TRANSPORT_Handle *th_;
55 * The GetCacheHandle for the p1th transport handle
57 struct GSTCacheGetHandle *cgh_th;
60 * the try connect handle
62 struct GNUNET_TRANSPORT_TryConnectHandle *tch;
67 GNUNET_SCHEDULER_TaskIdentifier task;
70 * The id of the operation which is resposible for this context
75 * The number of times we attempted to connect
83 * Context information for connecting 2 peers in overlay.
85 struct OverlayConnectContext
88 * The next pointer for maintaining a DLL of all OverlayConnectContexts
90 struct OverlayConnectContext *next;
93 * The prev pointer for maintaining a DLL of all OverlayConnectContexts
95 struct OverlayConnectContext *prev;
98 * The client which has requested for overlay connection. This is used to send
99 * either a success of failure message
101 struct GNUNET_SERVER_Client *client;
104 * the first peer which is to expect an overlay connection from the second peer.
109 * Transport handle of the first peer obtained from cache to get its HELLO. Do
110 * NOT close/disconnect.
112 struct GNUNET_TRANSPORT_Handle *p1th_;
115 * The CacheGetHandle for the p1th transport handle
117 struct GSTCacheGetHandle *cgh_p1th;
120 * The GetCacheHandle for registering callback to notify CORE level peer
121 * connects and to get our identity.
123 struct GSTCacheGetHandle *cgh_ch;
126 * HELLO of the first peer. This should be sent to the second peer.
128 struct GNUNET_MessageHeader *hello;
131 * Get GetHelloHandle to acquire a HELLO of the first peer
133 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
136 * The handle for offering the HELLO of the first peer to the second
137 * peer. This is only used if the second peer is a local peer.
139 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
142 * The error message we send if this overlay connect operation has timed out
147 * Operation context for the suboperation we start to get the identity of the
148 * second peer if it is a remote peer
150 struct OperationContext *opc;
153 * Controller of peer 2; NULL if the peer is a local peer
155 struct GNUNET_TESTBED_Controller *peer2_controller;
158 * The transport TryConnectContext. This will be NULL if the second peer is a
161 struct TryConnectContext tcc;
164 * The peer identity of the first peer
166 struct GNUNET_PeerIdentity peer_identity;
169 * The peer identity of the other peer
171 struct GNUNET_PeerIdentity other_peer_identity;
174 * The id of the operation responsible for creating this context
179 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
182 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
185 * The id of the overlay connect timeout task
187 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
190 * The id of the cleanup task
192 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
195 * The id of the second peer which is has to connect to the first peer
197 uint32_t other_peer_id;
203 * Context information for remote overlay connect operations. Remote overlay
204 * connections are used when peers A and B reside on different hosts. In these
205 * operations the host controller for peer B is asked by the host controller of
206 * peer A to make peer B connect to peer A by sending the controller of peer B
207 * the HELLO of peer A.
209 struct RemoteOverlayConnectCtx
212 * the next pointer for DLL
214 struct RemoteOverlayConnectCtx *next;
217 * the prev pointer for DLL
219 struct RemoteOverlayConnectCtx *prev;
222 * The peer handle of peer B
229 struct GNUNET_MessageHeader *hello;
232 * The handle for offering HELLO
234 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
237 * The transport try connect context
239 struct TryConnectContext tcc;
242 * The peer identity of peer A
244 struct GNUNET_PeerIdentity a_id;
247 * Task for offering HELLO of A to B and doing try_connect
249 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
252 * Task to timeout RequestOverlayConnect
254 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
257 * The id of the operation responsible for creating this context
264 * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
266 static struct OverlayConnectContext *occq_head;
269 * DLL tail for OverlayConnectContext DLL
271 static struct OverlayConnectContext *occq_tail;
274 * DLL head for RequectOverlayConnectContext DLL - to be used to clean up during
277 static struct RemoteOverlayConnectCtx *roccq_head;
280 * DLL tail for RequectOverlayConnectContext DLL
282 static struct RemoteOverlayConnectCtx *roccq_tail;
286 * Cleans up ForwardedOverlayConnectContext
288 * @param focc the ForwardedOverlayConnectContext to cleanup
291 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc)
293 GNUNET_free_non_null (focc->orig_msg);
299 * Timeout task for cancelling a forwarded overlay connect connect
301 * @param cls the ForwardedOverlayConnectContext
302 * @param tc the task context from the scheduler
305 forwarded_overlay_connect_timeout (void *cls,
306 const struct GNUNET_SCHEDULER_TaskContext
309 struct ForwardedOperationContext *fopc = cls;
310 struct RegisteredHostContext *rhc;
311 struct ForwardedOverlayConnectContext *focc;
314 focc = rhc->focc_dll_head;
315 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
316 GST_cleanup_focc (focc);
317 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n", focc->peer1,
319 GST_forwarded_operation_timeout (cls, tc);
320 if (NULL != rhc->focc_dll_head)
321 GST_process_next_focc (rhc);
326 * Callback to be called when forwarded overlay connection operation has a reply
327 * from the sub-controller successfull. We have to relay the reply msg back to
330 * @param cls ForwardedOperationContext
331 * @param msg the peer create success message
334 forwarded_overlay_connect_listener (void *cls,
335 const struct GNUNET_MessageHeader *msg)
337 struct ForwardedOperationContext *fopc = cls;
338 struct RegisteredHostContext *rhc;
339 struct ForwardedOverlayConnectContext *focc;
342 GST_forwarded_operation_reply_relay (cls, msg);
343 focc = rhc->focc_dll_head;
344 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
345 GST_cleanup_focc (focc);
346 if (NULL != rhc->focc_dll_head)
347 GST_process_next_focc (rhc);
352 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
354 * @param rhc the RegisteredHostContext
357 GST_process_next_focc (struct RegisteredHostContext *rhc)
359 struct ForwardedOperationContext *fopc;
360 struct ForwardedOverlayConnectContext *focc;
362 focc = rhc->focc_dll_head;
363 GNUNET_assert (NULL != focc);
364 GNUNET_assert (RHC_OL_CONNECT == rhc->state);
365 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
366 GNUNET_SERVER_client_keep (rhc->client);
367 fopc->client = rhc->client;
368 fopc->operation_id = focc->operation_id;
370 fopc->type = OP_OVERLAY_CONNECT;
372 GNUNET_TESTBED_forward_operation_msg_ (rhc->gateway->controller,
373 focc->operation_id, focc->orig_msg,
374 &forwarded_overlay_connect_listener,
376 GNUNET_free (focc->orig_msg);
377 focc->orig_msg = NULL;
379 GNUNET_SCHEDULER_add_delayed (GST_timeout, &forwarded_overlay_connect_timeout,
381 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
386 * Cleanup overlay connect context structure
388 * @param occ the overlay connect context
391 cleanup_occ (struct OverlayConnectContext *occ)
393 struct Peer *other_peer;
395 LOG_DEBUG ("0x%llx: Cleaning up occ\n", occ->op_id);
396 GNUNET_free_non_null (occ->emsg);
397 GNUNET_free_non_null (occ->hello);
398 GNUNET_SERVER_client_drop (occ->client);
399 if (NULL != occ->opc)
400 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
401 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
402 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
403 if (GNUNET_SCHEDULER_NO_TASK != occ->cleanup_task)
404 GNUNET_SCHEDULER_cancel (occ->cleanup_task);
405 if (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task)
406 GNUNET_SCHEDULER_cancel (occ->timeout_task);
407 if (NULL != occ->cgh_ch)
408 GST_cache_get_handle_done (occ->cgh_ch);
409 if (NULL != occ->ghh)
410 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
411 if (NULL != occ->ohh)
412 GNUNET_TRANSPORT_offer_hello_cancel (occ->ohh);
413 if (GNUNET_SCHEDULER_NO_TASK != occ->tcc.task)
414 GNUNET_SCHEDULER_cancel (occ->tcc.task);
415 if (NULL != occ->tcc.tch)
416 GNUNET_TRANSPORT_try_connect_cancel (occ->tcc.tch);
417 if (NULL != occ->cgh_p1th)
418 GST_cache_get_handle_done (occ->cgh_p1th);
419 if (NULL != occ->tcc.cgh_th)
420 GST_cache_get_handle_done (occ->tcc.cgh_th);
421 GNUNET_assert (NULL != GST_peer_list);
422 GNUNET_assert (occ->peer->reference_cnt > 0);
423 occ->peer->reference_cnt--;
424 if ( (GNUNET_YES == occ->peer->destroy_flag) &&
425 (0 == occ->peer->reference_cnt) )
426 GST_destroy_peer (occ->peer);
427 if (NULL == occ->peer2_controller)
429 other_peer = GST_peer_list[occ->other_peer_id];
430 GNUNET_assert (NULL != other_peer);
431 GNUNET_assert (other_peer->reference_cnt > 0);
432 other_peer->reference_cnt--;
433 if ( (GNUNET_YES == other_peer->destroy_flag) &&
434 (0 == other_peer->reference_cnt) )
435 GST_destroy_peer (other_peer);
437 GNUNET_CONTAINER_DLL_remove (occq_head, occq_tail, occ);
443 * Task for cleaing up overlay connect context structure
445 * @param cls the overlay connect context
446 * @param tc the task context
449 do_cleanup_occ (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
451 struct OverlayConnectContext *occ = cls;
453 occ->cleanup_task = GNUNET_SCHEDULER_NO_TASK;
459 * Task which will be run when overlay connect request has been timed out
461 * @param cls the OverlayConnectContext
462 * @param tc the TaskContext
465 timeout_overlay_connect (void *cls,
466 const struct GNUNET_SCHEDULER_TaskContext *tc)
468 struct OverlayConnectContext *occ = cls;
470 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
471 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
472 /* LOG (GNUNET_ERROR_TYPE_WARNING, */
473 /* "0x%llx: Timeout while connecting peers %u and %u: %s\n", occ->op_id, */
474 /* occ->peer->id, occ->other_peer_id, occ->emsg); */
475 GST_send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
481 send_overlay_connect_success_msg (struct OverlayConnectContext *occ)
483 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
485 LOG_DEBUG ("0x%llx: Peers connected - Sending overlay connect success\n",
487 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
489 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
490 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONNECT_EVENT);
491 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
492 msg->peer1 = htonl (occ->peer->id);
493 msg->peer2 = htonl (occ->other_peer_id);
494 msg->operation_id = GNUNET_htonll (occ->op_id);
495 GST_queue_message (occ->client, &msg->header);
500 * Function called to notify transport users that another
501 * peer connected to us.
504 * @param new_peer the peer that connected
507 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer)
509 struct OverlayConnectContext *occ = cls;
511 char *other_peer_str;
513 //LOG_DEBUG ("Overlay connect notify\n");
515 memcmp (new_peer, &occ->peer_identity,
516 sizeof (struct GNUNET_PeerIdentity)))
518 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
519 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
521 memcmp (new_peer, &occ->other_peer_identity,
522 sizeof (struct GNUNET_PeerIdentity)))
524 /* LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n", */
525 /* new_peer_str, other_peer_str); */
526 GNUNET_free (new_peer_str);
527 GNUNET_free (other_peer_str);
530 GNUNET_free (new_peer_str);
531 LOG_DEBUG ("0x%llx: Peer %4s connected to peer %4s\n", occ->op_id,
532 other_peer_str, GNUNET_i2s (&occ->peer_identity));
533 GNUNET_free (other_peer_str);
534 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
536 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
537 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
539 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
540 GNUNET_SCHEDULER_cancel (occ->timeout_task);
541 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
542 if (GNUNET_SCHEDULER_NO_TASK != occ->tcc.task)
544 GNUNET_SCHEDULER_cancel (occ->tcc.task);
545 occ->tcc.task = GNUNET_SCHEDULER_NO_TASK;
547 GNUNET_free_non_null (occ->emsg);
549 send_overlay_connect_success_msg (occ);
550 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
556 * Task to ask transport of a peer to connect to another peer
558 * @param cls the TryConnectContext
559 * @param tc the scheduler task context
562 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
566 * Callback to be called with result of the try connect request.
568 * @param cls the overlay connect context
569 * @param result GNUNET_OK if message was transmitted to transport service
570 * GNUNET_SYSERR if message was not transmitted to transport service
573 try_connect_cb (void *cls, const int result)
575 struct TryConnectContext *tcc = cls;
578 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == tcc->task);
580 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
581 (GNUNET_TIME_UNIT_MILLISECONDS,
582 500 + pow (2, ++tcc->retries)),
583 &try_connect_task, tcc);
588 * Task to ask transport of a peer to connect to another peer
590 * @param cls the TryConnectContext
591 * @param tc the scheduler task context
594 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
596 struct TryConnectContext *tcc = cls;
598 tcc->task = GNUNET_SCHEDULER_NO_TASK;
599 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
601 GNUNET_assert (NULL == tcc->tch);
602 GNUNET_assert (NULL != tcc->pid);
603 GNUNET_assert (NULL != tcc->th_);
604 GNUNET_assert (NULL != tcc->cgh_th);
605 LOG_DEBUG ("0x%llx: Trail %u to connect to peer %4s\n", tcc->op_id,
606 tcc->retries, GNUNET_i2s (tcc->pid));
608 GNUNET_TRANSPORT_try_connect (tcc->th_, tcc->pid, &try_connect_cb, tcc);
613 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
616 * @param cls the OverlayConnectContext
617 * @param tc the TaskContext from scheduler
620 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
624 * Task that is run when hello has been sent
626 * @param cls the overlay connect context
627 * @param tc the scheduler task context; if tc->reason =
628 * GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
629 * GNUNET_SCHEDULER_REASON_READ_READY is succeeded
632 occ_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
634 struct OverlayConnectContext *occ = cls;
637 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == occ->send_hello_task);
638 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
640 GNUNET_free_non_null (occ->emsg);
641 GNUNET_asprintf (&occ->emsg,
642 "0x%llx: Timeout while offering HELLO to other peer",
644 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
647 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
649 GNUNET_free_non_null (occ->emsg);
650 GNUNET_asprintf (&occ->emsg,
651 "0x%llx: Timeout during TRANSPORT_try_connect() "
652 "at peer %4s", occ->op_id,
653 GNUNET_i2s(&occ->peer_identity));
654 occ->tcc.pid = &occ->peer_identity;
655 occ->tcc.op_id = occ->op_id;
656 occ->tcc.task = GNUNET_SCHEDULER_add_now (&try_connect_task, &occ->tcc);
661 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
664 * @param cls the OverlayConnectContext
665 * @param tc the TaskContext from scheduler
668 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
670 struct OverlayConnectContext *occ = cls;
671 char *other_peer_str;
673 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
674 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
676 GNUNET_assert (NULL != occ->hello);
677 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
678 if (NULL != occ->peer2_controller)
680 struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg;
684 LOG_DEBUG ("0x%llx: Offering HELLO of %s (size: %u) to %s via Remote "
685 "Overlay Request\n", occ->op_id,
686 GNUNET_i2s (&occ->peer_identity), ntohs (occ->hello->size),
688 hello_size = ntohs (occ->hello->size);
690 sizeof (struct GNUNET_TESTBED_RemoteOverlayConnectMessage) + hello_size;
691 msg = GNUNET_malloc (msize);
693 htons (GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT);
694 msg->header.size = htons (msize);
695 msg->peer = htonl (occ->other_peer_id);
696 msg->operation_id = GNUNET_htonll (occ->op_id);
697 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
698 sizeof (struct GNUNET_PeerIdentity));
699 memcpy (msg->hello, occ->hello, hello_size);
700 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
704 LOG_DEBUG ("0x%llx: Offering HELLO of %s to %s\n", occ->op_id,
705 GNUNET_i2s (&occ->peer_identity), other_peer_str);
707 GNUNET_TRANSPORT_offer_hello (occ->tcc.th_, occ->hello,
708 occ_hello_sent_cb, occ);
709 if (NULL == occ->ohh)
712 occ->send_hello_task =
713 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
714 (GNUNET_TIME_UNIT_MILLISECONDS,
716 GNUNET_CRYPTO_random_u32
717 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
721 GNUNET_free (other_peer_str);
726 * Callback from cache with needed handles set
728 * @param cls the closure passed to GST_cache_get_handle_transport()
729 * @param ch the handle to CORE. Can be NULL if it is not requested
730 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
731 * @param ignore_ peer identity which is ignored in this callback
734 p2_transport_connect_cache_callback (void *cls, struct GNUNET_CORE_Handle *ch,
735 struct GNUNET_TRANSPORT_Handle *th,
736 const struct GNUNET_PeerIdentity *ignore_)
738 struct OverlayConnectContext *occ = cls;
742 GNUNET_asprintf (&occ->emsg, "0x%llx: Cannot connect to TRANSPORT of %s",
743 occ->op_id, GNUNET_i2s (&occ->other_peer_identity));
744 GNUNET_SCHEDULER_cancel (occ->timeout_task);
746 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
750 GNUNET_asprintf (&occ->emsg, "0x%llx: Timeout while offering HELLO to %s",
751 occ->op_id, GNUNET_i2s (&occ->other_peer_identity));
752 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
757 * Connects to the transport of the other peer if it is a local peer and
758 * schedules the send hello task
760 * @param occ the overlay connect context
763 p2_transport_connect (struct OverlayConnectContext *occ)
765 GNUNET_assert (NULL == occ->emsg);
766 GNUNET_assert (NULL != occ->hello);
767 GNUNET_assert (NULL == occ->ghh);
768 GNUNET_assert (NULL == occ->p1th_);
769 GNUNET_assert (NULL == occ->cgh_p1th);
770 if (NULL == occ->peer2_controller)
772 GNUNET_assert (NULL != GST_peer_list[occ->other_peer_id]);
774 GST_cache_get_handle_transport (occ->other_peer_id,
775 GST_peer_list[occ->other_peer_id]
777 &p2_transport_connect_cache_callback,
778 occ, NULL, NULL, NULL);
781 GNUNET_asprintf (&occ->emsg, "0x%llx: Timeout while offering HELLO to %s",
782 occ->op_id, GNUNET_i2s (&occ->other_peer_identity));
783 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
788 * Test for checking whether HELLO message is empty
790 * @param cls empty flag to set
791 * @param address the HELLO
792 * @param expiration expiration of the HELLO
796 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
797 struct GNUNET_TIME_Absolute expiration)
807 * Function called whenever there is an update to the HELLO of peers in the
808 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
809 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
812 * @param hello our updated HELLO
815 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
817 struct OverlayConnectContext *occ = cls;
821 msize = ntohs (hello->size);
823 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
824 hello, GNUNET_NO, &test_address,
826 if (GNUNET_YES == empty)
828 LOG_DEBUG ("0x%llx: HELLO of %s is empty\n", occ->op_id,
829 GNUNET_i2s (&occ->peer_identity));
832 LOG_DEBUG ("0x%llx: Received HELLO of %s\n", occ->op_id,
833 GNUNET_i2s (&occ->peer_identity));
834 occ->hello = GNUNET_malloc (msize);
835 GST_cache_add_hello (occ->peer->id, hello);
836 memcpy (occ->hello, hello, msize);
837 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
839 GST_cache_get_handle_done (occ->cgh_p1th);
840 occ->cgh_p1th = NULL;
842 GNUNET_free_non_null (occ->emsg);
844 p2_transport_connect (occ);
849 * Callback from cache with needed handles set
851 * @param cls the closure passed to GST_cache_get_handle_transport()
852 * @param ch the handle to CORE. Can be NULL if it is not requested
853 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
854 * @param ignore_ peer identity which is ignored in this callback
857 p1_transport_connect_cache_callback (void *cls, struct GNUNET_CORE_Handle *ch,
858 struct GNUNET_TRANSPORT_Handle *th,
859 const struct GNUNET_PeerIdentity *ignore_)
861 struct OverlayConnectContext *occ = cls;
863 GNUNET_free_non_null (occ->emsg);
867 GNUNET_asprintf (&occ->emsg, "0x%llx: Cannot connect to TRANSPORT of %s",
868 occ->op_id, GNUNET_i2s (&occ->peer_identity));
869 GNUNET_SCHEDULER_cancel (occ->timeout_task);
871 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
874 GNUNET_assert (NULL == occ->p1th_);
875 GNUNET_assert (NULL != occ->cgh_p1th);
877 GNUNET_asprintf (&occ->emsg,
878 "0x%llx: Timeout while acquiring HELLO of peer %4s",
879 occ->op_id, GNUNET_i2s (&occ->peer_identity));
880 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th_, &hello_update_cb, occ);
885 * Callback from cache with needed handles set
887 * @param cls the closure passed to GST_cache_get_handle_transport()
888 * @param ch the handle to CORE. Can be NULL if it is not requested
889 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
890 * @param my_identity the identity of our peer
893 occ_cache_get_handle_core_cb (void *cls, struct GNUNET_CORE_Handle *ch,
894 struct GNUNET_TRANSPORT_Handle *th,
895 const struct GNUNET_PeerIdentity *my_identity)
897 struct OverlayConnectContext *occ = cls;
898 const struct GNUNET_MessageHeader *hello;
900 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
901 GNUNET_free_non_null (occ->emsg);
902 if ((NULL == ch) || (NULL == my_identity))
904 (void) GNUNET_asprintf (&occ->emsg,
905 "0x%llx: Failed to connect to CORE of peer with "
906 "id: %u", occ->op_id, occ->peer->id);
907 GNUNET_SCHEDULER_cancel (occ->timeout_task);
909 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
915 GNUNET_CORE_is_peer_connected_sync (ch, &occ->other_peer_identity))
917 LOG_DEBUG ("0x%llx: Target peer already connected\n", occ->op_id);
918 GNUNET_SCHEDULER_cancel (occ->timeout_task);
919 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
920 send_overlay_connect_success_msg (occ);
921 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
924 memcpy (&occ->peer_identity, my_identity,
925 sizeof (struct GNUNET_PeerIdentity));
926 LOG_DEBUG ("0x%llx: Acquiring HELLO of peer %s\n", occ->op_id,
927 GNUNET_i2s (&occ->peer_identity));
928 /* Lookup for HELLO in hello cache */
929 if (NULL != (hello = GST_cache_lookup_hello (occ->peer->id)))
931 LOG_DEBUG ("0x%llx: HELLO of peer %s found in cache\n", occ->op_id,
932 GNUNET_i2s (&occ->peer_identity));
933 occ->hello = GNUNET_copy_message (hello);
934 p2_transport_connect (occ);
937 GNUNET_asprintf (&occ->emsg,
938 "0x%llx: Timeout while acquiring TRANSPORT of %s from cache",
939 occ->op_id, GNUNET_i2s (&occ->peer_identity));
941 GST_cache_get_handle_transport (occ->peer->id,
942 occ->peer->details.local.cfg,
943 p1_transport_connect_cache_callback, occ,
950 * Callback to be called when forwarded get peer config operation as part of
951 * overlay connect is successfull. Connection to Peer 1's core is made and is
952 * checked for new connection from peer 2
954 * @param cls ForwardedOperationContext
955 * @param msg the peer create success message
958 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
960 struct OverlayConnectContext *occ = cls;
961 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
964 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
965 if (GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONFIGURATION != ntohs (msg->type))
967 GNUNET_SCHEDULER_cancel (occ->timeout_task);
969 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
972 (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *) msg;
973 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
974 sizeof (struct GNUNET_PeerIdentity));
975 GNUNET_free_non_null (occ->emsg);
976 GNUNET_asprintf (&occ->emsg,
977 "0x%llx: Timeout while connecting to CORE of peer with "
978 "id: %u", occ->op_id, occ->peer->id);
980 GST_cache_get_handle_core (occ->peer->id, occ->peer->details.local.cfg,
981 occ_cache_get_handle_core_cb, occ,
982 &occ->other_peer_identity,
983 &overlay_connect_notify, occ);
989 * Callback which will be called after a host registration succeeded or failed
991 * @param cls the RegisteredHostContext
992 * @param emsg the error message; NULL if host registration is successful
995 registeredhost_registration_completion (void *cls, const char *emsg)
997 struct RegisteredHostContext *rhc = cls;
998 const struct GNUNET_CONFIGURATION_Handle *cfg;
999 uint32_t peer2_host_id;
1001 /* if (NULL != rhc->focc_dll_head) */
1002 /* TESTBED_process_next_focc (rhc); */
1003 peer2_host_id = GNUNET_TESTBED_host_get_id_ (rhc->reg_host);
1004 GNUNET_assert (RHC_INIT == rhc->state);
1005 GNUNET_assert (NULL == rhc->sub_op);
1006 if ((NULL == rhc->gateway2) || ((peer2_host_id < GST_host_list_size) /* Check if we have the needed config */
1007 && (NULL != GST_host_list[peer2_host_id])))
1009 rhc->state = RHC_LINK;
1012 rhc->gateway2) ? our_config
1013 : GNUNET_TESTBED_host_get_cfg_ (GST_host_list[peer2_host_id]);
1015 GNUNET_TESTBED_controller_link (rhc, rhc->gateway->controller,
1016 rhc->reg_host, rhc->host, cfg,
1020 rhc->state = RHC_GET_CFG;
1022 GNUNET_TESTBED_get_slave_config (rhc, rhc->gateway2->controller,
1028 * Iterator to match a registered host context
1030 * @param cls pointer 2 pointer of RegisteredHostContext
1031 * @param key current key code
1032 * @param value value in the hash map
1033 * @return GNUNET_YES if we should continue to
1038 reghost_match_iterator (void *cls, const struct GNUNET_HashCode *key,
1041 struct RegisteredHostContext **rh = cls;
1042 struct RegisteredHostContext *rh_val = value;
1044 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
1055 * Function to generate the hashcode corresponding to a RegisteredHostContext
1057 * @param reg_host the host which is being registered in RegisteredHostContext
1058 * @param host the host of the controller which has to connect to the above rhost
1059 * @return the hashcode
1061 static struct GNUNET_HashCode
1062 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
1063 struct GNUNET_TESTBED_Host *host)
1065 struct GNUNET_HashCode hash;
1066 uint32_t host_ids[2];
1068 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
1069 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
1070 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
1076 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1079 * @param client identification of the client
1080 * @param message the actual message
1083 GST_handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
1084 const struct GNUNET_MessageHeader *message)
1086 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1088 struct OverlayConnectContext *occ;
1089 struct GNUNET_TESTBED_Controller *peer2_controller;
1090 uint64_t operation_id;
1093 uint32_t peer2_host_id;
1095 if (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage) !=
1096 ntohs (message->size))
1099 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1102 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1103 p1 = ntohl (msg->peer1);
1104 p2 = ntohl (msg->peer2);
1105 if ((p1 >= GST_peer_list_size) || (NULL == GST_peer_list[p1]))
1108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1111 peer = GST_peer_list[p1];
1112 peer2_host_id = ntohl (msg->peer2_host_id);
1113 operation_id = GNUNET_ntohll (msg->operation_id);
1115 ("Received overlay connect for peers %u and %u with op id: 0x%llx\n", p1,
1117 if (GNUNET_YES == peer->is_remote)
1119 struct ForwardedOperationContext *fopc;
1120 struct Route *route_to_peer2_host;
1121 struct Route *route_to_peer1_host;
1123 LOG_DEBUG ("0x%llx: Forwarding overlay connect\n", operation_id);
1124 route_to_peer2_host = NULL;
1125 route_to_peer1_host = NULL;
1126 route_to_peer2_host = GST_find_dest_route (peer2_host_id);
1127 if ((NULL != route_to_peer2_host) ||
1128 (peer2_host_id == GST_context->host_id))
1130 /* Peer 2 either below us OR with us */
1131 route_to_peer1_host =
1132 GST_find_dest_route (GST_peer_list[p1]->details.
1133 remote.remote_host_id);
1134 /* Because we get this message only if we know where peer 1 is */
1135 GNUNET_assert (NULL != route_to_peer1_host);
1136 if ((peer2_host_id == GST_context->host_id) ||
1137 (route_to_peer2_host->dest != route_to_peer1_host->dest))
1139 /* Peer2 is either with us OR peer1 and peer2 can be reached through
1140 * different gateways */
1141 struct GNUNET_HashCode hash;
1142 struct RegisteredHostContext *rhc;
1145 rhc = GNUNET_malloc (sizeof (struct RegisteredHostContext));
1146 rhc->type = CLOSURE_TYPE_RHC;
1147 if (NULL != route_to_peer2_host)
1148 rhc->reg_host = GST_host_list[route_to_peer2_host->dest];
1150 rhc->reg_host = GST_host_list[GST_context->host_id];
1151 rhc->host = GST_host_list[route_to_peer1_host->dest];
1152 GNUNET_assert (NULL != rhc->reg_host);
1153 GNUNET_assert (NULL != rhc->host);
1154 rhc->gateway = peer->details.remote.slave;
1157 route_to_peer2_host) ? NULL :
1158 GST_slave_list[route_to_peer2_host->dest];
1159 rhc->state = RHC_INIT;
1160 GNUNET_SERVER_client_keep (client);
1161 rhc->client = client;
1162 hash = hash_hosts (rhc->reg_host, rhc->host);
1163 skip_focc = GNUNET_NO;
1165 GNUNET_CONTAINER_multihashmap_contains (peer->details.
1166 remote.slave->reghost_map,
1169 GNUNET_CONTAINER_multihashmap_get_multiple (peer->details.remote.
1172 reghost_match_iterator,
1175 /* create and add a new registerd host context */
1176 /* add the focc to its queue */
1177 GNUNET_CONTAINER_multihashmap_put (peer->details.remote.
1178 slave->reghost_map, &hash, rhc,
1179 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1180 GNUNET_assert (NULL != GST_host_list[peer2_host_id]);
1181 GST_queue_host_registration (peer->details.remote.slave,
1182 registeredhost_registration_completion,
1183 rhc, GST_host_list[peer2_host_id]);
1187 /* rhc is now set to the existing one from the hash map by
1188 * reghost_match_iterator() */
1189 /* if queue is empty then ignore creating focc and proceed with
1190 * normal forwarding */
1191 if (RHC_OL_CONNECT == rhc->state)
1192 skip_focc = GNUNET_YES;
1194 if (GNUNET_NO == skip_focc)
1196 struct ForwardedOverlayConnectContext *focc;
1198 focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
1201 focc->peer2_host_id = peer2_host_id;
1202 focc->orig_msg = GNUNET_copy_message (message);
1203 focc->operation_id = operation_id;
1204 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
1205 rhc->focc_dll_tail, focc);
1206 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1211 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1212 GNUNET_SERVER_client_keep (client);
1213 fopc->client = client;
1214 fopc->operation_id = operation_id;
1215 fopc->type = OP_OVERLAY_CONNECT;
1217 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1218 slave->controller, operation_id,
1220 &GST_forwarded_operation_reply_relay,
1222 fopc->timeout_task =
1223 GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1225 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1226 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1230 peer2_controller = NULL;
1231 if ((p2 >= GST_peer_list_size) || (NULL == GST_peer_list[p2]))
1233 if ((peer2_host_id >= GST_slave_list_size) ||
1234 (NULL == GST_slave_list[peer2_host_id]))
1236 LOG (GNUNET_ERROR_TYPE_WARNING,
1237 "0x%llx: Configuration of peer2's controller missing for connecting peers"
1238 "%u and %u\n", operation_id, p1, p2);
1239 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1242 peer2_controller = GST_slave_list[peer2_host_id]->controller;
1243 if (NULL == peer2_controller)
1245 GNUNET_break (0); /* What's going on? */
1246 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1252 if (GNUNET_YES == GST_peer_list[p2]->is_remote)
1253 peer2_controller = GST_peer_list[p2]->details.remote.slave->controller;
1255 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1256 GNUNET_CONTAINER_DLL_insert_tail (occq_head, occq_tail, occ);
1257 GNUNET_SERVER_client_keep (client);
1258 occ->client = client;
1259 occ->other_peer_id = p2;
1260 GST_peer_list[p1]->reference_cnt++;
1261 occ->peer = GST_peer_list[p1];
1262 occ->op_id = GNUNET_ntohll (msg->operation_id);
1263 occ->peer2_controller = peer2_controller;
1264 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == occ->timeout_task);
1266 GNUNET_SCHEDULER_add_delayed (GST_timeout, &timeout_overlay_connect, occ);
1267 /* Get the identity of the second peer */
1268 if (NULL != occ->peer2_controller)
1270 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
1273 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
1275 htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_CONFIGURATION);
1276 cmsg.peer_id = msg->peer2;
1277 cmsg.operation_id = msg->operation_id;
1279 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
1280 occ->op_id, &cmsg.header,
1281 &overlay_connect_get_config,
1283 GNUNET_asprintf (&occ->emsg,
1284 "0x%llx: Timeout while getting peer identity of peer "
1285 "with id: %u", occ->op_id, occ->other_peer_id);
1286 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1289 GST_peer_list[occ->other_peer_id]->reference_cnt++;
1290 GNUNET_TESTING_peer_get_identity (GST_peer_list[occ->other_peer_id]->
1292 &occ->other_peer_identity);
1293 GNUNET_asprintf (&occ->emsg,
1294 "0x%llx: Timeout while connecting to CORE of peer with "
1295 "id: %u", occ->op_id, occ->peer->id);
1297 GST_cache_get_handle_core (occ->peer->id, occ->peer->details.local.cfg,
1298 occ_cache_get_handle_core_cb, occ,
1299 &occ->other_peer_identity,
1300 &overlay_connect_notify, occ);
1301 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1306 * Function to cleanup RemoteOverlayConnectCtx and any associated tasks
1309 * @param rocc the RemoteOverlayConnectCtx
1312 cleanup_rocc (struct RemoteOverlayConnectCtx *rocc)
1314 LOG_DEBUG ("0x%llx: Cleaning up rocc\n", rocc->op_id);
1315 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
1316 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
1317 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
1318 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
1319 if (NULL != rocc->ohh)
1320 GNUNET_TRANSPORT_offer_hello_cancel (rocc->ohh);
1321 if (NULL != rocc->tcc.tch)
1322 GNUNET_TRANSPORT_try_connect_cancel (rocc->tcc.tch);
1323 if (GNUNET_SCHEDULER_NO_TASK != rocc->tcc.task)
1324 GNUNET_SCHEDULER_cancel (rocc->tcc.task);
1325 //GNUNET_TRANSPORT_disconnect (rocc->tcc.th_);
1326 GST_cache_get_handle_done (rocc->tcc.cgh_th);
1327 GNUNET_assert (rocc->peer->reference_cnt > 0);
1328 rocc->peer->reference_cnt--;
1329 if ((GNUNET_YES == rocc->peer->destroy_flag) &&
1330 (0 == rocc->peer->reference_cnt))
1331 GST_destroy_peer (rocc->peer);
1332 GNUNET_free_non_null (rocc->hello);
1333 GNUNET_CONTAINER_DLL_remove (roccq_head, roccq_tail, rocc);
1339 * Task to timeout rocc and cleanit up
1341 * @param cls the RemoteOverlayConnectCtx
1342 * @param tc the TaskContext from scheduler
1345 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1347 struct RemoteOverlayConnectCtx *rocc = cls;
1349 GNUNET_assert (rocc->timeout_rocc_task_id != GNUNET_SCHEDULER_NO_TASK);
1350 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
1351 LOG_DEBUG ("0x%llx: rocc timed out\n", rocc->op_id);
1352 cleanup_rocc (rocc);
1357 * Function called to notify transport users that another
1358 * peer connected to us.
1360 * @param cls the RemoteOverlayConnectContext
1361 * @param new_peer the peer that connected
1364 cache_transport_peer_connect_notify (void *cls,
1365 const struct GNUNET_PeerIdentity *new_peer)
1367 struct RemoteOverlayConnectCtx *rocc = cls;
1369 LOG_DEBUG ("0x%llx: Request Overlay connect notify\n", rocc->op_id);
1371 memcmp (new_peer, &rocc->a_id,
1372 sizeof (struct GNUNET_PeerIdentity)));
1373 LOG_DEBUG ("0x%llx: Peer %4s connected\n", rocc->op_id,
1374 GNUNET_i2s (&rocc->a_id));
1375 cleanup_rocc (rocc);
1380 * Task to offer the HELLO message to the peer and ask it to connect to the peer
1381 * whose identity is in RemoteOverlayConnectCtx
1383 * @param cls the RemoteOverlayConnectCtx
1384 * @param tc the TaskContext from scheduler
1387 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1391 * Task that is run when hello has been sent
1393 * @param cls the overlay connect context
1394 * @param tc the scheduler task context; if tc->reason =
1395 * GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
1396 * GNUNET_SCHEDULER_REASON_READ_READY is succeeded
1399 rocc_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1401 struct RemoteOverlayConnectCtx *rocc = cls;
1404 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rocc->attempt_connect_task_id);
1405 LOG_DEBUG ("0x%llx: HELLO of peer %4s sent to local peer with id: %u\n",
1406 rocc->op_id, GNUNET_i2s (&rocc->a_id), rocc->peer->id);
1407 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
1410 rocc->attempt_connect_task_id =
1411 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
1414 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
1419 rocc->tcc.task = GNUNET_SCHEDULER_add_now (&try_connect_task, &rocc->tcc);
1424 * Task to offer the HELLO message to the peer and ask it to connect to the peer
1425 * whose identity is in RemoteOverlayConnectCtx
1427 * @param cls the RemoteOverlayConnectCtx
1428 * @param tc the TaskContext from scheduler
1431 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1433 struct RemoteOverlayConnectCtx *rocc = cls;
1435 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id);
1436 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
1437 LOG_DEBUG ("0x%llx: Offering HELLO of peer %4s to local peer with id: %u\n",
1438 rocc->op_id, GNUNET_i2s (&rocc->a_id), rocc->peer->id);
1440 GNUNET_TRANSPORT_offer_hello (rocc->tcc.th_, rocc->hello,
1441 rocc_hello_sent_cb, rocc);
1442 if (NULL == rocc->ohh)
1443 rocc->attempt_connect_task_id =
1444 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1445 (GNUNET_TIME_UNIT_MILLISECONDS,
1447 GNUNET_CRYPTO_random_u32
1448 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
1449 &attempt_connect_task, rocc);
1454 * Callback from cache with needed handles set
1456 * @param cls the closure passed to GST_cache_get_handle_transport()
1457 * @param ch the handle to CORE. Can be NULL if it is not requested
1458 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
1459 * @param ignore_ peer identity which is ignored in this callback
1462 rocc_cache_get_handle_transport_cb (void *cls, struct GNUNET_CORE_Handle *ch,
1463 struct GNUNET_TRANSPORT_Handle *th,
1464 const struct GNUNET_PeerIdentity *ignore_)
1466 struct RemoteOverlayConnectCtx *rocc = cls;
1470 rocc->timeout_rocc_task_id =
1471 GNUNET_SCHEDULER_add_now (&timeout_rocc_task, rocc);
1475 rocc->tcc.pid = &rocc->a_id;
1477 GNUNET_TRANSPORT_check_neighbour_connected (rocc->tcc.th_, rocc->tcc.pid))
1479 LOG_DEBUG ("0x%llx: Target peer %4s already connected to local peer: %u\n",
1480 rocc->op_id, GNUNET_i2s (&rocc->a_id), rocc->peer->id);
1481 cleanup_rocc (rocc);
1484 rocc->attempt_connect_task_id =
1485 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
1490 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
1493 * @param client identification of the client
1494 * @param message the actual message
1497 GST_handle_remote_overlay_connect (void *cls,
1498 struct GNUNET_SERVER_Client *client,
1499 const struct GNUNET_MessageHeader *message)
1501 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg;
1502 struct RemoteOverlayConnectCtx *rocc;
1508 msize = ntohs (message->size);
1509 if (sizeof (struct GNUNET_TESTBED_RemoteOverlayConnectMessage) >= msize)
1512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1515 msg = (const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *) message;
1516 if ((NULL == msg->hello) ||
1517 ((GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)) &&
1518 (GNUNET_MESSAGE_TYPE_FRIEND_HELLO != ntohs (msg->hello->type))))
1521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1524 hsize = ntohs (msg->hello->size);
1525 if ((sizeof (struct GNUNET_TESTBED_RemoteOverlayConnectMessage) + hsize) !=
1529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1532 peer_id = ntohl (msg->peer);
1533 if ((peer_id >= GST_peer_list_size) ||
1534 (NULL == (peer = GST_peer_list[peer_id])))
1536 GNUNET_break_op (0);
1537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1540 if (GNUNET_YES == peer->is_remote)
1542 struct GNUNET_MessageHeader *msg2;
1544 msg2 = GNUNET_copy_message (message);
1545 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller,
1547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1550 rocc = GNUNET_malloc (sizeof (struct RemoteOverlayConnectCtx));
1551 rocc->op_id = GNUNET_ntohll (msg->operation_id);
1552 GNUNET_CONTAINER_DLL_insert_tail (roccq_head, roccq_tail, rocc);
1553 memcpy (&rocc->a_id, &msg->peer_identity,
1554 sizeof (struct GNUNET_PeerIdentity));
1555 LOG_DEBUG ("Received request for overlay connection with op_id: 0x%llx "
1556 "from local peer %u to peer %4s with hello size: %u\n",
1557 rocc->op_id, peer_id, GNUNET_i2s (&rocc->a_id), hsize);
1559 rocc->peer->reference_cnt++;
1560 rocc->hello = GNUNET_malloc (hsize);
1561 memcpy (rocc->hello, msg->hello, hsize);
1563 GST_cache_get_handle_transport (peer_id, rocc->peer->details.local.cfg,
1564 &rocc_cache_get_handle_transport_cb, rocc,
1566 &cache_transport_peer_connect_notify,
1568 rocc->timeout_rocc_task_id =
1569 GNUNET_SCHEDULER_add_delayed (GST_timeout, &timeout_rocc_task, rocc);
1570 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1575 * Clears all pending overlay connect contexts in queue
1580 struct OverlayConnectContext *occ;
1582 while (NULL != (occ = occq_head))
1588 * Clears all pending remote overlay connect contexts in queue
1593 struct RemoteOverlayConnectCtx *rocc;
1595 while (NULL != (rocc = roccq_head))
1596 cleanup_rocc (rocc);