2 This file is part of GNUnet.
3 Copyright (C) 2008--2015 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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
28 #include "gnunet-service-testbed_connectionpool.h"
31 * Redefine LOG with a changed log component string
36 #define LOG(kind,...) \
37 GNUNET_log_from (kind, "testbed-OC", __VA_ARGS__)
41 * Context information for requesting ATS to connect to a peer
43 struct ConnectivitySuggestContext
47 * The transport handle obtained from cache. Do NOT close/disconnect.
49 struct GNUNET_TRANSPORT_Handle *th_;
52 * The GetCacheHandle for the peer2's transport handle
53 * (used to offer the HELLO to the peer).
55 struct GST_ConnectionPool_GetHandle *cgh_p2_th;
58 * The GetCacheHandle for the peer2's ATS handle.
60 struct GST_ConnectionPool_GetHandle *cgh_p2_ats;
63 * The ATS handle for the connectivity suggestion.
65 struct GNUNET_ATS_ConnectivitySuggestHandle *csh;
71 * Types for context information we create for overlay connect requests
73 enum OverlayConnectContextType
76 * This type is used if the overlay connection is local i.e. the connection
77 * has to be made between local peers
82 * Type to be used when the first peer is local and the other peer is on a slave
83 * controller started by us
85 OCC_TYPE_REMOTE_SLAVE,
88 * Type to be used when the first peer is local and the other peer is on a
89 * controller which is not started by us.
91 OCC_TYPE_REMOTE_LATERAL
96 * Context data for operations on second peer in local overlay connection
99 struct LocalPeer2Context
102 * The handle for offering the HELLO of the first peer to the second
105 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
108 * The transport ConnectivitySuggestContext
110 struct ConnectivitySuggestContext tcc;
115 * Context data for operations on second peer in remote overlay connection
118 struct RemotePeer2Context
121 * Controller of peer 2; If #OCC_TYPE_REMOTE_LATERAL is the type of overlay
122 * connection then this can be NULL until the connection to the controller is
125 struct GNUNET_TESTBED_Controller *p2c;
128 * Operation context for the suboperation we start to get the identity of the
131 struct OperationContext *opc;
134 * Notification handle acquire to connect to a remote controller. Only used
135 * if the type of overlay connection is #OCC_TYPE_REMOTE_LATERAL.
137 struct NeighbourConnectNotification *ncn;
140 * The neighbour handle. Only used if the type of overlay connection is
141 * #OCC_TYPE_REMOTE_LATERAL.
143 struct Neighbour *p2n;
147 * Context information for connecting 2 peers in overlay.
149 struct OverlayConnectContext
152 * The next pointer for maintaining a DLL of all OverlayConnectContexts
154 struct OverlayConnectContext *next;
157 * The prev pointer for maintaining a DLL of all OverlayConnectContexts
159 struct OverlayConnectContext *prev;
162 * The client which has requested for overlay connection. This is used to send
163 * either a success of failure message
165 struct GNUNET_SERVER_Client *client;
168 * the first peer which is to expect an overlay connection from the second peer.
173 * Transport handle of the first peer obtained from cache to get its HELLO. Do
174 * NOT close/disconnect.
176 struct GNUNET_TRANSPORT_Handle *p1th_;
179 * The #GST_ConnectionPool_GetHandle for the peer1's transport handle
181 struct GST_ConnectionPool_GetHandle *cgh_p1th;
184 * The #GST_ConnectionPool_GetHandle for registering callback to notify CORE
185 * level peer connects and to get our identity.
187 struct GST_ConnectionPool_GetHandle *cgh_ch;
190 * HELLO of the first peer. This should be sent to the second peer.
192 struct GNUNET_MessageHeader *hello;
195 * Get GetHelloHandle to acquire a HELLO of the first peer
197 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
200 * The error message we send if this overlay connect operation has timed out
205 * Context information for operations on the second peer
210 * Context information to be used if the second peer is local
212 struct LocalPeer2Context local;
215 * Context information to be used if the second peer is remote
217 struct RemotePeer2Context remote;
222 * The peer identity of the first peer
224 struct GNUNET_PeerIdentity peer_identity;
227 * The peer identity of the other peer
229 struct GNUNET_PeerIdentity other_peer_identity;
232 * The id of the operation responsible for creating this context
237 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
240 struct GNUNET_SCHEDULER_Task *send_hello_task;
243 * The id of the overlay connect timeout task
245 struct GNUNET_SCHEDULER_Task *timeout_task;
248 * The id of the cleanup task
250 struct GNUNET_SCHEDULER_Task *cleanup_task;
253 * The type of this context information
255 enum OverlayConnectContextType type;
258 * The id of the second peer which is has to connect to the first peer
260 uint32_t other_peer_id;
265 * Context information for remote overlay connect operations. Remote overlay
266 * connections are used when peers A and B reside on different hosts. In these
267 * operations the host controller for peer B is asked by the host controller of
268 * peer A to make peer B connect to peer A by sending the controller of peer B
269 * the HELLO of peer A.
271 struct RemoteOverlayConnectCtx
274 * the next pointer for DLL
276 struct RemoteOverlayConnectCtx *next;
279 * the prev pointer for DLL
281 struct RemoteOverlayConnectCtx *prev;
284 * The peer handle of peer B
291 struct GNUNET_MessageHeader *hello;
294 * The handle for offering HELLO
296 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
299 * The transport try connect context
301 struct ConnectivitySuggestContext tcc;
304 * The peer identity of peer A
306 struct GNUNET_PeerIdentity a_id;
309 * Task for offering HELLO of A to B and doing try_connect
311 struct GNUNET_SCHEDULER_Task *attempt_connect_task_id;
314 * Task to timeout RequestOverlayConnect
316 struct GNUNET_SCHEDULER_Task *timeout_rocc_task_id;
319 * The id of the operation responsible for creating this context
326 * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
328 static struct OverlayConnectContext *occq_head;
331 * DLL tail for OverlayConnectContext DLL
333 static struct OverlayConnectContext *occq_tail;
336 * DLL head for RequectOverlayConnectContext DLL - to be used to clean up during
339 static struct RemoteOverlayConnectCtx *roccq_head;
342 * DLL tail for RequectOverlayConnectContext DLL
344 static struct RemoteOverlayConnectCtx *roccq_tail;
348 * Cleans up ForwardedOverlayConnectContext
350 * @param focc the ForwardedOverlayConnectContext to cleanup
353 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc)
355 GNUNET_SERVER_client_drop (focc->client);
356 GNUNET_free_non_null (focc->orig_msg);
362 * Timeout task for cancelling a forwarded overlay connect connect
364 * @param cls the ForwardedOverlayConnectContext
365 * @param tc the task context from the scheduler
368 forwarded_overlay_connect_timeout (void *cls,
369 const struct GNUNET_SCHEDULER_TaskContext
372 struct ForwardedOperationContext *fopc = cls;
373 struct RegisteredHostContext *rhc;
374 struct ForwardedOverlayConnectContext *focc;
377 focc = rhc->focc_dll_head;
378 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
379 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n", focc->peer1,
381 GST_cleanup_focc (focc);
382 GST_forwarded_operation_timeout (fopc, tc);
383 if (NULL != rhc->focc_dll_head)
384 GST_process_next_focc (rhc);
389 * Callback to be called when forwarded overlay connection operation has a reply
390 * from the sub-controller successfull. We have to relay the reply msg back to
393 * @param cls ForwardedOperationContext
394 * @param msg the peer create success message
397 forwarded_overlay_connect_listener (void *cls,
398 const struct GNUNET_MessageHeader *msg)
400 struct ForwardedOperationContext *fopc = cls;
401 struct RegisteredHostContext *rhc;
402 struct ForwardedOverlayConnectContext *focc;
405 GST_forwarded_operation_reply_relay (cls, msg);
406 focc = rhc->focc_dll_head;
407 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
408 GST_cleanup_focc (focc);
409 if (NULL != rhc->focc_dll_head)
410 GST_process_next_focc (rhc);
415 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
417 * @param rhc the RegisteredHostContext
420 GST_process_next_focc (struct RegisteredHostContext *rhc)
422 struct ForwardedOperationContext *fopc;
423 struct ForwardedOverlayConnectContext *focc;
427 focc = rhc->focc_dll_head;
428 GNUNET_assert (NULL != focc);
429 GNUNET_assert (RHC_DONE == rhc->state);
430 GNUNET_assert (VALID_PEER_ID (focc->peer1));
431 peer = GST_peer_list[focc->peer1];
432 GNUNET_assert (GNUNET_YES == peer->is_remote);
433 GNUNET_assert (NULL != (slave = peer->details.remote.slave));
434 fopc = GNUNET_new (struct ForwardedOperationContext);
435 GNUNET_SERVER_client_keep (focc->client);
436 fopc->client = focc->client;
437 fopc->operation_id = focc->operation_id;
439 fopc->type = OP_OVERLAY_CONNECT;
441 GNUNET_TESTBED_forward_operation_msg_ (slave->controller,
442 focc->operation_id, focc->orig_msg,
443 &forwarded_overlay_connect_listener,
445 GNUNET_free (focc->orig_msg);
446 focc->orig_msg = NULL;
448 GNUNET_SCHEDULER_add_delayed (GST_timeout, &forwarded_overlay_connect_timeout,
450 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
455 * Cleans up any used handles in local peer2 context
457 * @param lp2c the local peer2 context information
460 cleanup_occ_lp2c (struct LocalPeer2Context *lp2c)
462 if (NULL != lp2c->ohh)
464 GNUNET_TRANSPORT_offer_hello_cancel (lp2c->ohh);
467 if (NULL != lp2c->tcc.cgh_p2_th)
469 GST_connection_pool_get_handle_done (lp2c->tcc.cgh_p2_th);
470 lp2c->tcc.cgh_p2_th = NULL;
472 if (NULL != lp2c->tcc.cgh_p2_ats)
474 GST_connection_pool_get_handle_done (lp2c->tcc.cgh_p2_ats);
475 lp2c->tcc.cgh_p2_ats = NULL;
477 if (NULL != lp2c->tcc.csh)
479 GNUNET_ATS_connectivity_suggest_cancel (lp2c->tcc.csh);
480 lp2c->tcc.csh = NULL;
486 * Cleans up any used handles in remote peer2 context. Relinquishes the
487 * remote controller connection if it has been established on-demand.
489 * @param rp2c the remote peer2 context information
492 cleanup_occ_rp2c (struct RemotePeer2Context *rp2c)
494 if (NULL != rp2c->opc)
496 GNUNET_TESTBED_forward_operation_msg_cancel_ (rp2c->opc);
499 if (NULL != rp2c->ncn)
501 GST_neighbour_get_connection_cancel (rp2c->ncn);
504 if ( (NULL != rp2c->p2c) && (NULL != rp2c->p2n) )
506 GST_neighbour_release_connection (rp2c->p2n);
512 * Condition for checking if given peer is ready to be destroyed
514 * @param peer the peer to check
516 #define PEER_EXPIRED(peer) \
517 ( (GNUNET_YES == peer->destroy_flag) && (0 == peer->reference_cnt) )
520 * Cleanup overlay connect context structure
522 * @param occ the overlay connect context
525 cleanup_occ (struct OverlayConnectContext *occ)
529 LOG_DEBUG ("0x%llx: Cleaning up occ\n",
531 GNUNET_free_non_null (occ->emsg);
532 GNUNET_free_non_null (occ->hello);
533 GNUNET_SERVER_client_drop (occ->client);
534 if (NULL != occ->send_hello_task)
535 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
536 if (NULL != occ->cleanup_task)
537 GNUNET_SCHEDULER_cancel (occ->cleanup_task);
538 if (NULL != occ->timeout_task)
539 GNUNET_SCHEDULER_cancel (occ->timeout_task);
540 if (NULL != occ->cgh_ch)
541 GST_connection_pool_get_handle_done (occ->cgh_ch);
542 if (NULL != occ->ghh)
543 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
544 GST_connection_pool_get_handle_done (occ->cgh_p1th);
545 GNUNET_assert (NULL != GST_peer_list);
546 GNUNET_assert (occ->peer->reference_cnt > 0);
547 occ->peer->reference_cnt--;
548 if (PEER_EXPIRED (occ->peer))
549 GST_destroy_peer (occ->peer);
553 peer2 = GST_peer_list[occ->other_peer_id];
554 GNUNET_assert (peer2->reference_cnt > 0);
555 peer2->reference_cnt--;
556 if (PEER_EXPIRED (peer2))
557 GST_destroy_peer (peer2);
558 cleanup_occ_lp2c (&occ->p2ctx.local);
560 case OCC_TYPE_REMOTE_SLAVE:
561 case OCC_TYPE_REMOTE_LATERAL:
562 cleanup_occ_rp2c (&occ->p2ctx.remote);
565 GNUNET_CONTAINER_DLL_remove (occq_head,
573 * Task for cleaing up overlay connect context structure
575 * @param cls the overlay connect context
576 * @param tc the task context
579 do_cleanup_occ (void *cls,
580 const struct GNUNET_SCHEDULER_TaskContext *tc)
582 struct OverlayConnectContext *occ = cls;
584 occ->cleanup_task = NULL;
590 * Task which will be run when overlay connect request has been timed out
592 * @param cls the OverlayConnectContext
593 * @param tc the TaskContext
596 timeout_overlay_connect (void *cls,
597 const struct GNUNET_SCHEDULER_TaskContext *tc)
599 struct OverlayConnectContext *occ = cls;
601 GNUNET_assert (NULL != occ->timeout_task);
602 occ->timeout_task = NULL;
603 /* LOG (GNUNET_ERROR_TYPE_WARNING, */
604 /* "0x%llx: Timeout while connecting peers %u and %u: %s\n", occ->op_id, */
605 /* occ->peer->id, occ->other_peer_id, occ->emsg); */
606 GST_send_operation_fail_msg (occ->client,
617 send_overlay_connect_success_msg (struct OverlayConnectContext *occ)
619 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
621 LOG_DEBUG ("0x%llx: Peers connected - Sending overlay connect success\n",
623 msg = GNUNET_new (struct GNUNET_TESTBED_ConnectionEventMessage);
625 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
626 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONNECT_EVENT);
627 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
628 msg->peer1 = htonl (occ->peer->id);
629 msg->peer2 = htonl (occ->other_peer_id);
630 msg->operation_id = GNUNET_htonll (occ->op_id);
631 GST_queue_message (occ->client, &msg->header);
636 * Function called to notify transport users that another
637 * peer connected to us.
640 * @param new_peer the peer that connected
643 overlay_connect_notify (void *cls,
644 const struct GNUNET_PeerIdentity *new_peer)
646 struct OverlayConnectContext *occ = cls;
648 char *other_peer_str;
650 LOG_DEBUG ("Overlay connect notify\n");
652 memcmp (new_peer, &occ->peer_identity,
653 sizeof (struct GNUNET_PeerIdentity)))
655 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
656 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
659 &occ->other_peer_identity,
660 sizeof (struct GNUNET_PeerIdentity)))
662 LOG_DEBUG ("Unexpected peer %s connected when expecting peer %s\n",
665 GNUNET_free (new_peer_str);
666 GNUNET_free (other_peer_str);
669 GNUNET_free (new_peer_str);
670 LOG_DEBUG ("0x%llx: Peer %s connected to peer %s\n",
673 GNUNET_i2s (&occ->peer_identity));
674 GNUNET_free (other_peer_str);
675 if (NULL != occ->send_hello_task)
677 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
678 occ->send_hello_task = NULL;
680 GNUNET_assert (NULL != occ->timeout_task);
681 GNUNET_SCHEDULER_cancel (occ->timeout_task);
682 occ->timeout_task = NULL;
686 cleanup_occ_lp2c (&occ->p2ctx.local);
688 case OCC_TYPE_REMOTE_SLAVE:
689 case OCC_TYPE_REMOTE_LATERAL:
690 cleanup_occ_rp2c (&occ->p2ctx.remote);
693 GNUNET_free_non_null (occ->emsg);
695 send_overlay_connect_success_msg (occ);
696 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ,
702 * Callback from cache with needed ATS handle set
704 * @param cls a `struct OverlayConnectCtx *`
705 * @param ch the handle to CORE. Can be NULL if it is not requested
706 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
707 * @param ac the handle to ATS. Can be NULL if it is not requested
708 * @param my_identity the identity of our peer
711 occ_cache_get_handle_ats_occ_cb (void *cls,
712 struct GNUNET_CORE_Handle *ch,
713 struct GNUNET_TRANSPORT_Handle *th,
714 struct GNUNET_ATS_ConnectivityHandle *ac,
715 const struct GNUNET_PeerIdentity *my_identity)
717 struct OverlayConnectContext *occ = cls;
718 struct LocalPeer2Context *lp2c;
720 GNUNET_assert (OCC_TYPE_LOCAL == occ->type);
721 GNUNET_assert (NULL != occ->timeout_task);
722 GNUNET_free_non_null (occ->emsg);
725 GNUNET_asprintf (&occ->emsg,
726 "0x%llx: Failed to connect to ATS of peer with id: %u",
729 GNUNET_SCHEDULER_cancel (occ->timeout_task);
731 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect,
737 GNUNET_asprintf (&occ->emsg,
738 "0x%llx: Timeout during GNUNET_ATS_connectivity_suggest() at peer %s",
740 GNUNET_i2s (&occ->other_peer_identity));
742 lp2c = &occ->p2ctx.local;
744 GNUNET_ATS_connectivity_suggest (ac,
751 * Callback from cache with needed ATS handle set
753 * @param cls a `struct RemoteOverlayConnectCtx *`
754 * @param ch the handle to CORE. Can be NULL if it is not requested
755 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
756 * @param ac the handle to ATS. Can be NULL if it is not requested
757 * @param my_identity the identity of our peer
760 occ_cache_get_handle_ats_rocc_cb (void *cls,
761 struct GNUNET_CORE_Handle *ch,
762 struct GNUNET_TRANSPORT_Handle *th,
763 struct GNUNET_ATS_ConnectivityHandle *ac,
764 const struct GNUNET_PeerIdentity *my_identity)
766 struct RemoteOverlayConnectCtx *rocc = cls;
769 GNUNET_ATS_connectivity_suggest (ac,
776 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
779 * @param cls the OverlayConnectContext
780 * @param tc the TaskContext from scheduler
783 send_hello (void *cls,
784 const struct GNUNET_SCHEDULER_TaskContext *tc);
788 * Task that is run when hello has been sent
790 * @param cls the overlay connect context
791 * @param tc the scheduler task context; if tc->reason =
792 * #GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
793 * #GNUNET_SCHEDULER_REASON_READ_READY is succeeded
796 occ_hello_sent_cb (void *cls,
797 const struct GNUNET_SCHEDULER_TaskContext *tc)
799 struct OverlayConnectContext *occ = cls;
800 struct LocalPeer2Context *lp2c;
803 GNUNET_assert (OCC_TYPE_LOCAL == occ->type);
804 GNUNET_assert (NULL != occ->timeout_task);
805 lp2c = &occ->p2ctx.local;
808 GNUNET_assert (NULL == occ->send_hello_task);
809 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
811 GNUNET_free_non_null (occ->emsg);
812 GNUNET_asprintf (&occ->emsg,
813 "0x%llx: Timeout while offering HELLO to other peer",
815 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello,
819 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
821 GNUNET_free_non_null (occ->emsg);
823 GNUNET_asprintf (&occ->emsg,
824 "0x%llx: Timeout while acquiring ATS of %s from cache",
826 GNUNET_i2s (&occ->other_peer_identity));
827 GNUNET_assert (NULL != (peer2 = GST_peer_list[occ->other_peer_id]));
828 lp2c->tcc.cgh_p2_ats =
829 GST_connection_pool_get_handle (occ->other_peer_id,
830 peer2->details.local.cfg,
831 GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY,
832 &occ_cache_get_handle_ats_occ_cb,
833 occ, NULL, NULL, NULL);
838 * Sends the HELLO of peer1 to peer2's controller through remote overlay connect
841 * @param occ the overlay connect context. Its type must be either
842 * #OCC_TYPE_REMOTE_SLAVE or #OCC_TYPE_REMOTE_LATERAL
845 send_hello_thru_rocc (struct OverlayConnectContext *occ)
847 struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg;
848 char *other_peer_str;
852 GNUNET_assert (OCC_TYPE_LOCAL != occ->type);
853 GNUNET_assert (NULL != occ->hello);
854 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
855 LOG_DEBUG ("0x%llx: Offering HELLO of %s (size: %u) to %s via Remote Overlay Request\n",
857 GNUNET_i2s (&occ->peer_identity),
858 ntohs (occ->hello->size),
860 GNUNET_free (other_peer_str);
861 hello_size = ntohs (occ->hello->size);
862 msize = sizeof (struct GNUNET_TESTBED_RemoteOverlayConnectMessage) + hello_size;
863 msg = GNUNET_malloc (msize);
865 htons (GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT);
866 msg->header.size = htons (msize);
867 msg->peer = htonl (occ->other_peer_id);
868 msg->operation_id = GNUNET_htonll (occ->op_id);
869 msg->peer_identity = occ->peer_identity;
873 GNUNET_TESTBED_queue_message_ (occ->p2ctx.remote.p2c,
879 * Task to offer HELLO of peer 1 to peer 2. If peer2 is local it is offered
880 * using its TRANSPORT connection; if remote the HELLO is sent remotely by using
881 * send_hello_thru_rocc()
883 * @param cls the OverlayConnectContext
884 * @param tc the TaskContext from scheduler
887 send_hello (void *cls,
888 const struct GNUNET_SCHEDULER_TaskContext *tc)
890 struct OverlayConnectContext *occ = cls;
891 struct LocalPeer2Context *lp2c;
892 char *other_peer_str;
894 occ->send_hello_task = NULL;
895 GNUNET_assert (NULL != occ->timeout_task);
896 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
898 GNUNET_assert (NULL != occ->hello);
899 if (OCC_TYPE_LOCAL != occ->type)
901 send_hello_thru_rocc (occ);
904 lp2c = &occ->p2ctx.local;
905 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
906 LOG_DEBUG ("0x%llx: Offering HELLO of %s to %s\n",
908 GNUNET_i2s (&occ->peer_identity),
910 GNUNET_free (other_peer_str);
912 GNUNET_TRANSPORT_offer_hello (lp2c->tcc.th_,
916 if (NULL == lp2c->ohh)
919 occ->send_hello_task =
920 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
921 (GNUNET_TIME_UNIT_MILLISECONDS,
923 GNUNET_CRYPTO_random_u32
924 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
931 * Callback from cache with needed handles set
933 * @param cls the closure passed to GST_cache_get_handle_transport()
934 * @param ch the handle to CORE. Can be NULL if it is not requested
935 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
936 * @param ac the handle to ATS. Can be NULL if it is not requested
937 * @param ignore_ peer identity which is ignored in this callback
940 p2_transport_connect_cache_callback (void *cls,
941 struct GNUNET_CORE_Handle *ch,
942 struct GNUNET_TRANSPORT_Handle *th,
943 struct GNUNET_ATS_ConnectivityHandle *ac,
944 const struct GNUNET_PeerIdentity *ignore_)
946 struct OverlayConnectContext *occ = cls;
948 GNUNET_assert (OCC_TYPE_LOCAL == occ->type);
951 GNUNET_asprintf (&occ->emsg,
952 "0x%llx: Cannot connect to TRANSPORT of %s",
954 GNUNET_i2s (&occ->other_peer_identity));
955 GNUNET_SCHEDULER_cancel (occ->timeout_task);
957 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
960 occ->p2ctx.local.tcc.th_ = th;
961 GNUNET_asprintf (&occ->emsg,
962 "0x%llx: Timeout while offering HELLO to %s",
964 GNUNET_i2s (&occ->other_peer_identity));
965 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
970 * Connects to the transport of the other peer if it is a local peer and
971 * schedules the send hello task
973 * @param occ the overlay connect context
976 p2_transport_connect (struct OverlayConnectContext *occ)
980 GNUNET_assert (NULL == occ->emsg);
981 GNUNET_assert (NULL != occ->hello);
982 GNUNET_assert (NULL == occ->ghh);
983 GNUNET_assert (NULL == occ->p1th_);
984 GNUNET_assert (NULL == occ->cgh_p1th);
985 if (OCC_TYPE_LOCAL == occ->type)
987 GNUNET_assert (NULL != (peer2 = GST_peer_list[occ->other_peer_id]));
988 occ->p2ctx.local.tcc.cgh_p2_th =
989 GST_connection_pool_get_handle (occ->other_peer_id,
990 peer2->details.local.cfg,
991 GST_CONNECTIONPOOL_SERVICE_TRANSPORT,
992 &p2_transport_connect_cache_callback,
993 occ, NULL, NULL, NULL);
996 GNUNET_asprintf (&occ->emsg,
997 "0x%llx: Timeout while offering HELLO to %s",
999 GNUNET_i2s (&occ->other_peer_identity));
1000 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
1005 * Test for checking whether HELLO message is empty
1007 * @param cls empty flag to set
1008 * @param address the HELLO
1009 * @param expiration expiration of the HELLO
1010 * @return #GNUNET_OK
1013 test_address (void *cls,
1014 const struct GNUNET_HELLO_Address *address,
1015 struct GNUNET_TIME_Absolute expiration)
1025 * Function called whenever there is an update to the HELLO of peers in the
1026 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
1027 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
1029 * @param cls closure
1030 * @param hello our updated HELLO
1033 hello_update_cb (void *cls,
1034 const struct GNUNET_MessageHeader *hello)
1036 struct OverlayConnectContext *occ = cls;
1040 msize = ntohs (hello->size);
1042 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
1046 if (GNUNET_YES == empty)
1048 LOG_DEBUG ("0x%llx: HELLO of %s is empty\n",
1050 GNUNET_i2s (&occ->peer_identity));
1053 LOG_DEBUG ("0x%llx: Received HELLO of %s\n",
1055 GNUNET_i2s (&occ->peer_identity));
1056 occ->hello = GNUNET_malloc (msize);
1057 GST_cache_add_hello (occ->peer->id, hello);
1058 memcpy (occ->hello, hello, msize);
1059 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1061 GST_connection_pool_get_handle_done (occ->cgh_p1th);
1062 occ->cgh_p1th = NULL;
1064 GNUNET_free_non_null (occ->emsg);
1066 p2_transport_connect (occ);
1071 * Callback from cache with needed handles set
1073 * @param cls the closure passed to GST_cache_get_handle_transport()
1074 * @param ch the handle to CORE. Can be NULL if it is not requested
1075 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
1076 * @param ac the handle to ATS. Can be NULL if it is not requested
1077 * @param ignore_ peer identity which is ignored in this callback
1080 p1_transport_connect_cache_callback (void *cls,
1081 struct GNUNET_CORE_Handle *ch,
1082 struct GNUNET_TRANSPORT_Handle *th,
1083 struct GNUNET_ATS_ConnectivityHandle *ac,
1084 const struct GNUNET_PeerIdentity *ignore_)
1086 struct OverlayConnectContext *occ = cls;
1088 GNUNET_free_non_null (occ->emsg);
1092 GNUNET_asprintf (&occ->emsg,
1093 "0x%llx: Cannot connect to TRANSPORT of %s",
1095 GNUNET_i2s (&occ->peer_identity));
1096 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1098 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1101 GNUNET_assert (NULL == occ->p1th_);
1102 GNUNET_assert (NULL != occ->cgh_p1th);
1104 GNUNET_asprintf (&occ->emsg,
1105 "0x%llx: Timeout while acquiring HELLO of peer %s",
1107 GNUNET_i2s (&occ->peer_identity));
1108 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th_,
1115 * Callback from cache with needed CORE handle set
1117 * @param cls the closure passed to GST_cache_get_handle_transport()
1118 * @param ch the handle to CORE. Can be NULL if it is not requested
1119 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
1120 * @param ac the handle to ATS. Can be NULL if it is not requested
1121 * @param my_identity the identity of our peer
1124 occ_cache_get_handle_core_cb (void *cls,
1125 struct GNUNET_CORE_Handle *ch,
1126 struct GNUNET_TRANSPORT_Handle *th,
1127 struct GNUNET_ATS_ConnectivityHandle *ac,
1128 const struct GNUNET_PeerIdentity *my_identity)
1130 struct OverlayConnectContext *occ = cls;
1131 const struct GNUNET_MessageHeader *hello;
1133 GNUNET_assert (NULL != occ->timeout_task);
1134 GNUNET_free_non_null (occ->emsg);
1135 if ((NULL == ch) || (NULL == my_identity))
1137 GNUNET_asprintf (&occ->emsg,
1138 "0x%llx: Failed to connect to CORE of peer with "
1142 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1144 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1149 GNUNET_CORE_is_peer_connected_sync (ch,
1150 &occ->other_peer_identity))
1152 LOG_DEBUG ("0x%llx: Target peer already connected\n",
1154 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1155 occ->timeout_task = NULL;
1156 send_overlay_connect_success_msg (occ);
1157 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
1160 occ->peer_identity = *my_identity;
1161 LOG_DEBUG ("0x%llx: Acquiring HELLO of peer %s\n",
1163 GNUNET_i2s (&occ->peer_identity));
1164 /* Lookup for HELLO in hello cache */
1165 if (NULL != (hello = GST_cache_lookup_hello (occ->peer->id)))
1167 LOG_DEBUG ("0x%llx: HELLO of peer %s found in cache\n",
1169 GNUNET_i2s (&occ->peer_identity));
1170 occ->hello = GNUNET_copy_message (hello);
1171 p2_transport_connect (occ);
1174 GNUNET_asprintf (&occ->emsg,
1175 "0x%llx: Timeout while acquiring TRANSPORT of %s from cache",
1177 GNUNET_i2s (&occ->peer_identity));
1179 GST_connection_pool_get_handle (occ->peer->id,
1180 occ->peer->details.local.cfg,
1181 GST_CONNECTIONPOOL_SERVICE_TRANSPORT,
1182 p1_transport_connect_cache_callback,
1189 * Callback to be called when forwarded get peer config operation as part of
1190 * overlay connect is successfull. Connection to Peer 1's core is made and is
1191 * checked for new connection from peer 2
1193 * @param cls ForwardedOperationContext
1194 * @param msg the peer create success message
1197 overlay_connect_get_config (void *cls,
1198 const struct GNUNET_MessageHeader *msg)
1200 struct OverlayConnectContext *occ = cls;
1201 struct RemotePeer2Context *rp2c;
1202 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
1204 GNUNET_assert (OCC_TYPE_LOCAL != occ->type);
1205 rp2c = &occ->p2ctx.remote;
1207 GNUNET_assert (NULL != occ->timeout_task);
1208 if (GNUNET_MESSAGE_TYPE_TESTBED_PEER_INFORMATION != ntohs (msg->type))
1210 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1212 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1215 (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *) msg;
1216 occ->other_peer_identity = cmsg->peer_identity;
1217 GNUNET_free_non_null (occ->emsg);
1218 GNUNET_asprintf (&occ->emsg,
1219 "0x%llx: Timeout while connecting to CORE of peer with "
1224 GST_connection_pool_get_handle (occ->peer->id,
1225 occ->peer->details.local.cfg,
1226 GST_CONNECTIONPOOL_SERVICE_CORE,
1227 occ_cache_get_handle_core_cb,
1229 &occ->other_peer_identity,
1230 &overlay_connect_notify,
1237 * Callback which will be called after a host registration succeeded or failed
1239 * @param cls the RegisteredHostContext
1240 * @param emsg the error message; NULL if host registration is successful
1243 host_registration_comp (void *cls, const char *emsg)
1245 struct RegisteredHostContext *rhc = cls;
1247 rhc->state = RHC_DONE;
1248 GST_process_next_focc (rhc);
1253 * Iterator to match a registered host context
1255 * @param cls pointer 2 pointer of RegisteredHostContext
1256 * @param key current key code
1257 * @param value value in the hash map
1258 * @return #GNUNET_YES if we should continue to
1260 * #GNUNET_NO if not.
1263 reghost_match_iterator (void *cls,
1264 const struct GNUNET_HashCode *key,
1267 struct RegisteredHostContext **rh = cls;
1268 struct RegisteredHostContext *rh_val = value;
1270 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
1281 * Function to generate the hashcode corresponding to a RegisteredHostContext
1283 * @param reg_host the host which is being registered in RegisteredHostContext
1284 * @param host the host of the controller which has to connect to the above rhost
1285 * @return the hashcode
1287 static struct GNUNET_HashCode
1288 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
1289 struct GNUNET_TESTBED_Host *host)
1291 struct GNUNET_HashCode hash;
1292 uint32_t host_ids[2];
1294 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
1295 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
1296 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
1302 * Checks if the given host is registered at the given slave.
1304 * @param slave the slave where registration has to be checked. The check is
1305 * actually done through a locally maintained hashmap. No
1306 * communication with the slave is involved.
1307 * @param host the host to register
1308 * @return If the given host is not registered already or the registration is
1309 * pending, it returns the registration context. Any overlay connects
1310 * to be forwarded should be queued in the context so that they can be
1311 * executed when the registration is completed. If the given host is
1312 * already registered, NULL is returned.
1314 static struct RegisteredHostContext *
1315 register_host (struct Slave *slave,
1316 struct GNUNET_TESTBED_Host *host)
1318 struct GNUNET_HashCode hash;
1319 struct RegisteredHostContext *rhc;
1321 rhc = GNUNET_new (struct RegisteredHostContext);
1322 rhc->reg_host = host;
1323 rhc->host = GST_host_list[slave->host_id];
1324 GNUNET_assert (NULL != rhc->reg_host);
1325 GNUNET_assert (NULL != rhc->host);
1326 rhc->state = RHC_INIT;
1327 hash = hash_hosts (rhc->reg_host, rhc->host);
1329 GNUNET_CONTAINER_multihashmap_contains (slave->reghost_map, &hash)) ||
1331 GNUNET_CONTAINER_multihashmap_get_multiple (slave->reghost_map,
1333 reghost_match_iterator,
1336 /* create and add a new registerd host context */
1337 /* add the focc to its queue */
1338 GNUNET_CONTAINER_multihashmap_put (slave->reghost_map, &hash, rhc,
1339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1340 GST_queue_host_registration (slave, host_registration_comp,
1341 rhc, rhc->reg_host);
1345 /* rhc is now set to the existing one from the hash map by
1346 * reghost_match_iterator() */
1347 /* if queue is empty then ignore creating focc and proceed with normal
1349 if (RHC_DONE == rhc->state)
1357 * Forwards the overlay connect request to a slave controller. Before
1358 * forwarding, any hosts which are needed to be known by the slave controller to
1359 * execute the overlay connect request are registered at slave.
1361 * @param msg the overlay connect request message to be forwarded
1362 * @param client the client to which the status of the forwarded request has to
1366 forward_overlay_connect (const struct GNUNET_TESTBED_OverlayConnectMessage *msg,
1367 struct GNUNET_SERVER_Client *client)
1369 struct ForwardedOperationContext *fopc;
1370 struct Route *route_to_peer2_host;
1371 struct Route *route_to_peer1_host;
1373 struct RegisteredHostContext *rhc;
1374 struct ForwardedOverlayConnectContext *focc;
1376 uint32_t peer2_host_id;
1380 p1 = ntohl (msg->peer1);
1381 p2 = ntohl (msg->peer2);
1382 op_id = GNUNET_ntohll (msg->operation_id);
1383 peer2_host_id = ntohl (msg->peer2_host_id);
1384 GNUNET_assert (VALID_PEER_ID (p1));
1385 GNUNET_assert (VALID_HOST_ID (peer2_host_id));
1386 peer = GST_peer_list[p1];
1387 GNUNET_assert (GNUNET_YES == peer->is_remote);
1388 LOG_DEBUG ("0x%llx: Forwarding overlay connect\n", op_id);
1389 route_to_peer2_host = GST_find_dest_route (peer2_host_id);
1390 route_to_peer1_host = GST_find_dest_route
1391 (peer->details.remote.remote_host_id);
1392 GNUNET_assert (NULL != route_to_peer1_host);
1393 if ((NULL != route_to_peer2_host) &&
1394 (route_to_peer1_host->dest == route_to_peer2_host->dest))
1396 /* Peer2 is either with us OR peer1 and peer2 can be reached through
1397 different subtrees OR peer2 is on a subtree unknown to us */
1398 if (NULL != (rhc = register_host (peer->details.remote.slave,
1399 GST_host_list[peer2_host_id])))
1401 LOG_DEBUG ("Queueing forwarding FOCC for connecting peers %u and %u\n", p1, p2);
1402 focc = GNUNET_new (struct ForwardedOverlayConnectContext);
1405 focc->peer2_host_id = peer2_host_id;
1406 focc->orig_msg = GNUNET_copy_message (&msg->header);
1407 focc->operation_id = op_id;
1408 focc->client = client;
1409 GNUNET_SERVER_client_keep (client);
1410 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head, rhc->focc_dll_tail,
1416 LOG_DEBUG ("Forwarding without FOCC for connecting peers %u and %u\n", p1, p2);
1417 fopc = GNUNET_new (struct ForwardedOperationContext);
1418 GNUNET_SERVER_client_keep (client);
1419 fopc->client = client;
1420 fopc->operation_id = op_id;
1421 fopc->type = OP_OVERLAY_CONNECT;
1423 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1424 slave->controller, op_id,
1426 &GST_forwarded_operation_reply_relay,
1428 fopc->timeout_task =
1429 GNUNET_SCHEDULER_add_delayed (GST_timeout,
1430 &GST_forwarded_operation_timeout,
1432 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head,
1439 * Callback called when a connection to the controller of peer2 has been
1442 * @param cls the overlay connect contexts
1443 * @param c handle to the controller connection
1446 p2_controller_connect_cb (void *cls,
1447 struct GNUNET_TESTBED_Controller *c)
1449 struct OverlayConnectContext *occ = cls;
1450 struct RemotePeer2Context *rp2c;
1451 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
1453 GNUNET_assert (OCC_TYPE_LOCAL != occ->type);
1454 rp2c = &occ->p2ctx.remote;
1458 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
1460 htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_INFORMATION);
1461 cmsg.peer_id = htonl (occ->other_peer_id);
1462 cmsg.operation_id = GNUNET_htonll (occ->op_id);
1464 GNUNET_TESTBED_forward_operation_msg_ (rp2c->p2c,
1465 occ->op_id, &cmsg.header,
1466 &overlay_connect_get_config,
1468 GNUNET_free_non_null (occ->emsg);
1469 GNUNET_asprintf (&occ->emsg,
1470 "0x%llx: Timeout while getting peer identity of peer "
1473 occ->other_peer_id);
1478 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1481 * @param client identification of the client
1482 * @param message the actual message
1485 GST_handle_overlay_connect (void *cls,
1486 struct GNUNET_SERVER_Client *client,
1487 const struct GNUNET_MessageHeader *message)
1489 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1492 struct OverlayConnectContext *occ;
1493 struct Neighbour *p2n;
1494 uint64_t operation_id;
1497 uint32_t peer2_host_id;
1499 if (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage) !=
1500 ntohs (message->size))
1503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1506 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1507 p1 = ntohl (msg->peer1);
1508 p2 = ntohl (msg->peer2);
1509 if (!VALID_PEER_ID (p1))
1512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1515 peer = GST_peer_list[p1];
1516 operation_id = GNUNET_ntohll (msg->operation_id);
1518 ("Received overlay connect for peers %u and %u with op id: 0x%llx\n",
1522 peer2_host_id = ntohl (msg->peer2_host_id);
1523 if (GNUNET_YES == peer->is_remote)
1525 if (! VALID_HOST_ID (peer2_host_id))
1528 GNUNET_SERVER_receive_done (client,
1532 forward_overlay_connect (msg, client);
1533 GNUNET_SERVER_receive_done (client,
1538 occ = GNUNET_new (struct OverlayConnectContext);
1539 occ->type = OCC_TYPE_LOCAL;
1540 if (!VALID_PEER_ID (p2)) /* May be peer2 is on a another controller */
1542 if (NULL == (p2n = GST_get_neighbour (peer2_host_id)))
1544 if (!VALID_HOST_ID (peer2_host_id))
1547 LOG (GNUNET_ERROR_TYPE_WARNING,
1548 "0x%llx: Peer %u's host not in our neighbours list\n",
1550 GNUNET_SERVER_receive_done (client,
1555 p2n = GST_create_neighbour (GST_host_list[peer2_host_id]);
1557 occ->type = OCC_TYPE_REMOTE_LATERAL;
1558 occ->p2ctx.remote.p2n = p2n;
1560 else if (GNUNET_YES == GST_peer_list[p2]->is_remote)
1562 occ->type = OCC_TYPE_REMOTE_SLAVE;
1563 occ->p2ctx.remote.p2c = GST_peer_list[p2]->details.remote.slave->controller;
1565 GNUNET_CONTAINER_DLL_insert_tail (occq_head,
1568 GNUNET_SERVER_client_keep (client);
1569 occ->client = client;
1570 occ->other_peer_id = p2;
1571 GST_peer_list[p1]->reference_cnt++;
1572 occ->peer = GST_peer_list[p1];
1573 occ->op_id = operation_id;
1574 GNUNET_assert (NULL == occ->timeout_task);
1576 GNUNET_SCHEDULER_add_delayed (GST_timeout,
1577 &timeout_overlay_connect,
1581 case OCC_TYPE_REMOTE_LATERAL:
1582 GNUNET_asprintf (&occ->emsg,
1583 "0x%llx: Timeout while acquiring connection to peer %u's "
1588 occ->p2ctx.remote.ncn =
1589 GST_neighbour_get_connection (p2n, &p2_controller_connect_cb, occ);
1591 case OCC_TYPE_REMOTE_SLAVE:
1592 p2_controller_connect_cb (occ, occ->p2ctx.remote.p2c);
1594 case OCC_TYPE_LOCAL:
1595 peer2 = GST_peer_list[occ->other_peer_id];
1596 peer2->reference_cnt++;
1597 GNUNET_TESTING_peer_get_identity (peer2->details.local.peer,
1598 &occ->other_peer_identity);
1599 GNUNET_asprintf (&occ->emsg,
1600 "0x%llx: Timeout while connecting to CORE of peer with "
1605 GST_connection_pool_get_handle (occ->peer->id,
1606 occ->peer->details.local.cfg,
1607 GST_CONNECTIONPOOL_SERVICE_CORE,
1608 occ_cache_get_handle_core_cb, occ,
1609 &occ->other_peer_identity,
1610 &overlay_connect_notify, occ);
1613 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1618 * Function to cleanup RemoteOverlayConnectCtx and any associated tasks
1621 * @param rocc the RemoteOverlayConnectCtx
1624 cleanup_rocc (struct RemoteOverlayConnectCtx *rocc)
1626 LOG_DEBUG ("0x%llx: Cleaning up rocc\n",
1628 if (NULL != rocc->attempt_connect_task_id)
1629 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
1630 if (NULL != rocc->timeout_rocc_task_id)
1631 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
1632 if (NULL != rocc->ohh)
1633 GNUNET_TRANSPORT_offer_hello_cancel (rocc->ohh);
1634 if (NULL != rocc->tcc.csh)
1635 GNUNET_ATS_connectivity_suggest_cancel (rocc->tcc.csh);
1636 GST_connection_pool_get_handle_done (rocc->tcc.cgh_p2_th);
1637 GST_connection_pool_get_handle_done (rocc->tcc.cgh_p2_ats);
1638 GNUNET_assert (rocc->peer->reference_cnt > 0);
1639 rocc->peer->reference_cnt--;
1640 if ((GNUNET_YES == rocc->peer->destroy_flag) &&
1641 (0 == rocc->peer->reference_cnt))
1642 GST_destroy_peer (rocc->peer);
1643 GNUNET_free_non_null (rocc->hello);
1644 GNUNET_CONTAINER_DLL_remove (roccq_head,
1652 * Task to timeout rocc and cleanit up
1654 * @param cls the RemoteOverlayConnectCtx
1655 * @param tc the TaskContext from scheduler
1658 timeout_rocc_task (void *cls,
1659 const struct GNUNET_SCHEDULER_TaskContext *tc)
1661 struct RemoteOverlayConnectCtx *rocc = cls;
1663 GNUNET_assert (rocc->timeout_rocc_task_id != NULL);
1664 rocc->timeout_rocc_task_id = NULL;
1665 LOG_DEBUG ("0x%llx: rocc timed out\n",
1667 cleanup_rocc (rocc);
1672 * Function called to notify transport users that another
1673 * peer connected to us.
1675 * @param cls the RemoteOverlayConnectContext
1676 * @param new_peer the peer that connected
1679 cache_transport_peer_connect_notify (void *cls,
1680 const struct GNUNET_PeerIdentity *new_peer)
1682 struct RemoteOverlayConnectCtx *rocc = cls;
1684 LOG_DEBUG ("0x%llx: Request Overlay connect notify\n",
1687 memcmp (new_peer, &rocc->a_id,
1688 sizeof (struct GNUNET_PeerIdentity)));
1689 LOG_DEBUG ("0x%llx: Peer %s connected\n",
1691 GNUNET_i2s (&rocc->a_id));
1692 cleanup_rocc (rocc);
1697 * Task to offer the HELLO message to the peer and ask it to connect to the peer
1698 * whose identity is in RemoteOverlayConnectCtx
1700 * @param cls the RemoteOverlayConnectCtx
1701 * @param tc the TaskContext from scheduler
1704 attempt_connect_task (void *cls,
1705 const struct GNUNET_SCHEDULER_TaskContext *tc);
1709 * Task that is run when hello has been sent
1711 * @param cls the overlay connect context
1712 * @param tc the scheduler task context; if tc->reason =
1713 * #GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
1714 * #GNUNET_SCHEDULER_REASON_READ_READY is succeeded
1717 rocc_hello_sent_cb (void *cls,
1718 const struct GNUNET_SCHEDULER_TaskContext *tc)
1720 struct RemoteOverlayConnectCtx *rocc = cls;
1723 GNUNET_assert (NULL == rocc->attempt_connect_task_id);
1724 LOG_DEBUG ("0x%llx: HELLO of peer %s delivered to local peer with id: %u\n",
1726 GNUNET_i2s (&rocc->a_id),
1728 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
1731 rocc->attempt_connect_task_id =
1732 GNUNET_SCHEDULER_add_now (&attempt_connect_task,
1736 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
1742 rocc->tcc.cgh_p2_ats =
1743 GST_connection_pool_get_handle (rocc->peer->id,
1744 rocc->peer->details.local.cfg,
1745 GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY,
1746 &occ_cache_get_handle_ats_rocc_cb,
1747 rocc, NULL, NULL, NULL);
1753 * Task to offer the HELLO message to the peer and ask it to connect to the peer
1754 * whose identity is in RemoteOverlayConnectCtx
1756 * @param cls the RemoteOverlayConnectCtx
1757 * @param tc the TaskContext from scheduler
1760 attempt_connect_task (void *cls,
1761 const struct GNUNET_SCHEDULER_TaskContext *tc)
1763 struct RemoteOverlayConnectCtx *rocc = cls;
1765 GNUNET_assert (NULL != rocc->attempt_connect_task_id);
1766 rocc->attempt_connect_task_id = NULL;
1767 LOG_DEBUG ("0x%llx: Offering HELLO of peer %s to remote peer with id: %u\n",
1769 GNUNET_i2s (&rocc->a_id),
1772 GNUNET_TRANSPORT_offer_hello (rocc->tcc.th_,
1774 rocc_hello_sent_cb, rocc);
1775 if (NULL == rocc->ohh)
1776 rocc->attempt_connect_task_id =
1777 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1778 (GNUNET_TIME_UNIT_MILLISECONDS,
1780 GNUNET_CRYPTO_random_u32
1781 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
1782 &attempt_connect_task, rocc);
1787 * Callback from cache with needed handles set
1789 * @param cls the closure passed to GST_cache_get_handle_transport()
1790 * @param ch the handle to CORE. Can be NULL if it is not requested
1791 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
1792 * @param ac the handle to ATS. Can be NULL if it is not requested
1793 * @param ignore_ peer identity which is ignored in this callback
1796 rocc_cache_get_handle_transport_cb (void *cls,
1797 struct GNUNET_CORE_Handle *ch,
1798 struct GNUNET_TRANSPORT_Handle *th,
1799 struct GNUNET_ATS_ConnectivityHandle *ac,
1800 const struct GNUNET_PeerIdentity *ignore_)
1802 struct RemoteOverlayConnectCtx *rocc = cls;
1806 rocc->timeout_rocc_task_id =
1807 GNUNET_SCHEDULER_add_now (&timeout_rocc_task, rocc);
1812 GNUNET_TRANSPORT_check_peer_connected (rocc->tcc.th_,
1815 LOG_DEBUG ("0x%llx: Target peer %s already connected to local peer: %u\n",
1817 GNUNET_i2s (&rocc->a_id),
1819 cleanup_rocc (rocc);
1822 rocc->attempt_connect_task_id =
1823 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
1828 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_REQUEST_CONNECT messages
1831 * @param client identification of the client
1832 * @param message the actual message
1835 GST_handle_remote_overlay_connect (void *cls,
1836 struct GNUNET_SERVER_Client *client,
1837 const struct GNUNET_MessageHeader *message)
1839 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg;
1840 struct RemoteOverlayConnectCtx *rocc;
1842 struct GNUNET_PeerIdentity pid;
1843 static char pid_str[16];
1848 msize = ntohs (message->size);
1849 if (sizeof (struct GNUNET_TESTBED_RemoteOverlayConnectMessage) >= msize)
1852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1855 msg = (const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *) message;
1856 if ((NULL == msg->hello) ||
1857 ((GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type))))
1860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1863 hsize = ntohs (msg->hello->size);
1864 if ((sizeof (struct GNUNET_TESTBED_RemoteOverlayConnectMessage) + hsize) !=
1868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1871 peer_id = ntohl (msg->peer);
1872 if ((peer_id >= GST_peer_list_size) ||
1873 (NULL == (peer = GST_peer_list[peer_id])))
1875 GNUNET_break_op (0);
1876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1879 if (GNUNET_YES == peer->is_remote)
1881 struct GNUNET_MessageHeader *msg2;
1883 msg2 = GNUNET_copy_message (message);
1884 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller,
1886 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1889 rocc = GNUNET_new (struct RemoteOverlayConnectCtx);
1890 rocc->op_id = GNUNET_ntohll (msg->operation_id);
1891 GNUNET_CONTAINER_DLL_insert_tail (roccq_head,
1894 rocc->a_id = msg->peer_identity;
1895 GNUNET_TESTING_peer_get_identity (peer->details.local.peer,
1897 (void) strncpy (pid_str,
1900 LOG_DEBUG ("0x%llx: Remote overlay connect %s to peer %s with hello size: %u\n",
1903 GNUNET_i2s (&rocc->a_id),
1906 rocc->peer->reference_cnt++;
1907 rocc->hello = GNUNET_malloc (hsize);
1908 memcpy (rocc->hello, msg->hello, hsize);
1909 rocc->tcc.cgh_p2_th =
1910 GST_connection_pool_get_handle (peer_id,
1911 rocc->peer->details.local.cfg,
1912 GST_CONNECTIONPOOL_SERVICE_TRANSPORT,
1913 &rocc_cache_get_handle_transport_cb,
1916 &cache_transport_peer_connect_notify,
1918 rocc->timeout_rocc_task_id =
1919 GNUNET_SCHEDULER_add_delayed (GST_timeout,
1922 GNUNET_SERVER_receive_done (client,
1928 * Clears all pending overlay connect contexts in queue
1933 struct OverlayConnectContext *occ;
1935 while (NULL != (occ = occq_head))
1941 * Clears all pending remote overlay connect contexts in queue
1946 struct RemoteOverlayConnectCtx *rocc;
1948 while (NULL != (rocc = roccq_head))
1949 cleanup_rocc (rocc);