2 This file is part of GNUnet.
3 Copyright (C) 2008--2016 GNUnet e.V.
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"
29 #include "gnunet_transport_hello_service.h"
32 * Redefine LOG with a changed log component string
37 #define LOG(kind,...) \
38 GNUNET_log_from (kind, "testbed-OC", __VA_ARGS__)
42 * Context information for requesting ATS to connect to a peer
44 struct ConnectivitySuggestContext
48 * The transport handle obtained from cache. Do NOT close/disconnect.
50 struct GNUNET_TRANSPORT_CoreHandle *th_;
53 * Configuration of the peer from cache. Do not free!
55 const struct GNUNET_CONFIGURATION_Handle *cfg;
58 * The GetCacheHandle for the peer2's transport handle
59 * (used to offer the HELLO to the peer).
61 struct GST_ConnectionPool_GetHandle *cgh_p2_th;
64 * The GetCacheHandle for the peer2's ATS handle.
66 struct GST_ConnectionPool_GetHandle *cgh_p2_ats;
69 * The ATS handle for the connectivity suggestion.
71 struct GNUNET_ATS_ConnectivitySuggestHandle *csh;
77 * Types for context information we create for overlay connect requests
79 enum OverlayConnectContextType
82 * This type is used if the overlay connection is local i.e. the connection
83 * has to be made between local peers
88 * Type to be used when the first peer is local and the other peer is on a slave
89 * controller started by us
91 OCC_TYPE_REMOTE_SLAVE,
94 * Type to be used when the first peer is local and the other peer is on a
95 * controller which is not started by us.
97 OCC_TYPE_REMOTE_LATERAL
102 * Context data for operations on second peer in local overlay connection
105 struct LocalPeer2Context
108 * The handle for offering the HELLO of the first peer to the second
111 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
114 * The transport ConnectivitySuggestContext
116 struct ConnectivitySuggestContext tcc;
121 * Context data for operations on second peer in remote overlay connection
124 struct RemotePeer2Context
127 * Controller of peer 2; If #OCC_TYPE_REMOTE_LATERAL is the type of overlay
128 * connection then this can be NULL until the connection to the controller is
131 struct GNUNET_TESTBED_Controller *p2c;
134 * Operation context for the suboperation we start to get the identity of the
137 struct OperationContext *opc;
140 * Notification handle acquire to connect to a remote controller. Only used
141 * if the type of overlay connection is #OCC_TYPE_REMOTE_LATERAL.
143 struct NeighbourConnectNotification *ncn;
146 * The neighbour handle. Only used if the type of overlay connection is
147 * #OCC_TYPE_REMOTE_LATERAL.
149 struct Neighbour *p2n;
153 * Context information for connecting 2 peers in overlay.
155 struct OverlayConnectContext
158 * The next pointer for maintaining a DLL of all OverlayConnectContexts
160 struct OverlayConnectContext *next;
163 * The prev pointer for maintaining a DLL of all OverlayConnectContexts
165 struct OverlayConnectContext *prev;
168 * The client which has requested for overlay connection. This is used to send
169 * either a success of failure message
171 struct GNUNET_SERVICE_Client *client;
174 * the first peer which is to expect an overlay connection from the second peer.
179 * Transport handle of the first peer obtained from cache to get its HELLO. Do
180 * NOT close/disconnect.
182 struct GNUNET_TRANSPORT_CoreHandle *p1th_;
185 * The #GST_ConnectionPool_GetHandle for the peer1's transport handle
187 struct GST_ConnectionPool_GetHandle *cgh_p1th;
190 * The #GST_ConnectionPool_GetHandle for registering callback to notify CORE
191 * level peer connects and to get our identity.
193 struct GST_ConnectionPool_GetHandle *cgh_ch;
196 * HELLO of the first peer. This should be sent to the second peer.
198 struct GNUNET_MessageHeader *hello;
201 * Get GetHelloHandle to acquire a HELLO of the first peer
203 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
206 * The error message we send if this overlay connect operation has timed out
211 * Context information for operations on the second peer
216 * Context information to be used if the second peer is local
218 struct LocalPeer2Context local;
221 * Context information to be used if the second peer is remote
223 struct RemotePeer2Context remote;
228 * The peer identity of the first peer
230 struct GNUNET_PeerIdentity peer_identity;
233 * The peer identity of the other peer
235 struct GNUNET_PeerIdentity other_peer_identity;
238 * The id of the operation responsible for creating this context
243 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
246 struct GNUNET_SCHEDULER_Task *send_hello_task;
249 * The id of the overlay connect timeout task
251 struct GNUNET_SCHEDULER_Task *timeout_task;
254 * The id of the cleanup task
256 struct GNUNET_SCHEDULER_Task *cleanup_task;
259 * The type of this context information
261 enum OverlayConnectContextType type;
264 * The id of the second peer which is has to connect to the first peer
266 uint32_t other_peer_id;
271 * Context information for remote overlay connect operations. Remote overlay
272 * connections are used when peers A and B reside on different hosts. In these
273 * operations the host controller for peer B is asked by the host controller of
274 * peer A to make peer B connect to peer A by sending the controller of peer B
275 * the HELLO of peer A.
277 struct RemoteOverlayConnectCtx
280 * the next pointer for DLL
282 struct RemoteOverlayConnectCtx *next;
285 * the prev pointer for DLL
287 struct RemoteOverlayConnectCtx *prev;
290 * The peer handle of peer B
297 struct GNUNET_MessageHeader *hello;
300 * The handle for offering HELLO
302 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
305 * The transport try connect context
307 struct ConnectivitySuggestContext tcc;
310 * The peer identity of peer A
312 struct GNUNET_PeerIdentity a_id;
315 * Task for offering HELLO of A to B and doing try_connect
317 struct GNUNET_SCHEDULER_Task *attempt_connect_task_id;
320 * Task to timeout RequestOverlayConnect
322 struct GNUNET_SCHEDULER_Task *timeout_rocc_task_id;
325 * The id of the operation responsible for creating this context
332 * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
334 static struct OverlayConnectContext *occq_head;
337 * DLL tail for OverlayConnectContext DLL
339 static struct OverlayConnectContext *occq_tail;
342 * DLL head for RequectOverlayConnectContext DLL - to be used to clean up during
345 static struct RemoteOverlayConnectCtx *roccq_head;
348 * DLL tail for RequectOverlayConnectContext DLL
350 static struct RemoteOverlayConnectCtx *roccq_tail;
354 * Cleans up ForwardedOverlayConnectContext
356 * @param focc the ForwardedOverlayConnectContext to cleanup
359 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc)
361 struct RegisteredHostContext *rhc = focc->rhc;
363 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head,
366 GNUNET_free_non_null (focc->orig_msg);
372 * Timeout task for cancelling a forwarded overlay connect connect
374 * @param cls the ForwardedOverlayConnectContext
377 forwarded_overlay_connect_timeout (void *cls)
379 struct ForwardedOperationContext *fopc = cls;
380 struct RegisteredHostContext *rhc;
381 struct ForwardedOverlayConnectContext *focc;
383 fopc->timeout_task = NULL;
385 focc = rhc->focc_dll_head;
386 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n",
389 GST_cleanup_focc (focc);
390 GST_forwarded_operation_timeout (fopc);
391 if (NULL != rhc->focc_dll_head)
392 GST_process_next_focc (rhc);
397 * Callback to be called when forwarded overlay connection operation has a reply
398 * from the sub-controller successfull. We have to relay the reply msg back to
401 * @param cls ForwardedOperationContext
402 * @param msg the peer create success message
405 forwarded_overlay_connect_listener (void *cls,
406 const struct GNUNET_MessageHeader *msg)
408 struct ForwardedOperationContext *fopc = cls;
409 struct RegisteredHostContext *rhc;
410 struct ForwardedOverlayConnectContext *focc;
413 GST_forwarded_operation_reply_relay (cls, msg);
414 focc = rhc->focc_dll_head;
415 GST_cleanup_focc (focc);
416 if (NULL != rhc->focc_dll_head)
417 GST_process_next_focc (rhc);
422 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
424 * @param rhc the RegisteredHostContext
427 GST_process_next_focc (struct RegisteredHostContext *rhc)
429 struct ForwardedOperationContext *fopc;
430 struct ForwardedOverlayConnectContext *focc;
434 focc = rhc->focc_dll_head;
435 GNUNET_assert (NULL != focc);
436 GNUNET_assert (RHC_DONE == rhc->state);
437 GNUNET_assert (VALID_PEER_ID (focc->peer1));
438 peer = GST_peer_list[focc->peer1];
439 GNUNET_assert (GNUNET_YES == peer->is_remote);
440 GNUNET_assert (NULL != (slave = peer->details.remote.slave));
441 fopc = GNUNET_new (struct ForwardedOperationContext);
442 fopc->client = focc->client;
443 fopc->operation_id = focc->operation_id;
445 fopc->type = OP_OVERLAY_CONNECT;
447 GNUNET_TESTBED_forward_operation_msg_ (slave->controller,
450 &forwarded_overlay_connect_listener,
452 GNUNET_free (focc->orig_msg);
453 focc->orig_msg = NULL;
454 fopc->timeout_task = GNUNET_SCHEDULER_add_delayed (GST_timeout,
455 &forwarded_overlay_connect_timeout,
457 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head,
464 * Cleans up any used handles in local peer2 context
466 * @param lp2c the local peer2 context information
469 cleanup_occ_lp2c (struct LocalPeer2Context *lp2c)
471 if (NULL != lp2c->ohh)
473 GNUNET_TRANSPORT_offer_hello_cancel (lp2c->ohh);
476 if (NULL != lp2c->tcc.cgh_p2_th)
478 GST_connection_pool_get_handle_done (lp2c->tcc.cgh_p2_th);
479 lp2c->tcc.cgh_p2_th = NULL;
481 if (NULL != lp2c->tcc.cgh_p2_ats)
483 GST_connection_pool_get_handle_done (lp2c->tcc.cgh_p2_ats);
484 lp2c->tcc.cgh_p2_ats = NULL;
486 if (NULL != lp2c->tcc.csh)
488 GNUNET_ATS_connectivity_suggest_cancel (lp2c->tcc.csh);
489 lp2c->tcc.csh = NULL;
495 * Cleans up any used handles in remote peer2 context. Relinquishes the
496 * remote controller connection if it has been established on-demand.
498 * @param rp2c the remote peer2 context information
501 cleanup_occ_rp2c (struct RemotePeer2Context *rp2c)
503 if (NULL != rp2c->opc)
505 GNUNET_TESTBED_forward_operation_msg_cancel_ (rp2c->opc);
508 if (NULL != rp2c->ncn)
510 GST_neighbour_get_connection_cancel (rp2c->ncn);
513 if ( (NULL != rp2c->p2c) && (NULL != rp2c->p2n) )
515 GST_neighbour_release_connection (rp2c->p2n);
521 * Condition for checking if given peer is ready to be destroyed
523 * @param peer the peer to check
525 #define PEER_EXPIRED(peer) \
526 ( (GNUNET_YES == peer->destroy_flag) && (0 == peer->reference_cnt) )
529 * Cleanup overlay connect context structure
531 * @param occ the overlay connect context
534 cleanup_occ (struct OverlayConnectContext *occ)
538 LOG_DEBUG ("0x%llx: Cleaning up occ\n",
540 GNUNET_free_non_null (occ->emsg);
541 GNUNET_free_non_null (occ->hello);
542 if (NULL != occ->send_hello_task)
543 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
544 if (NULL != occ->cleanup_task)
545 GNUNET_SCHEDULER_cancel (occ->cleanup_task);
546 if (NULL != occ->timeout_task)
547 GNUNET_SCHEDULER_cancel (occ->timeout_task);
548 if (NULL != occ->cgh_ch)
549 GST_connection_pool_get_handle_done (occ->cgh_ch);
550 if (NULL != occ->ghh)
551 GNUNET_TRANSPORT_hello_get_cancel (occ->ghh);
552 GST_connection_pool_get_handle_done (occ->cgh_p1th);
553 GNUNET_assert (NULL != GST_peer_list);
554 GNUNET_assert (occ->peer->reference_cnt > 0);
555 occ->peer->reference_cnt--;
556 if (PEER_EXPIRED (occ->peer))
557 GST_destroy_peer (occ->peer);
561 peer2 = GST_peer_list[occ->other_peer_id];
562 GNUNET_assert (peer2->reference_cnt > 0);
563 peer2->reference_cnt--;
564 if (PEER_EXPIRED (peer2))
565 GST_destroy_peer (peer2);
566 cleanup_occ_lp2c (&occ->p2ctx.local);
568 case OCC_TYPE_REMOTE_SLAVE:
569 case OCC_TYPE_REMOTE_LATERAL:
570 cleanup_occ_rp2c (&occ->p2ctx.remote);
573 GNUNET_CONTAINER_DLL_remove (occq_head,
581 * Task for cleaing up overlay connect context structure
583 * @param cls the overlay connect context
586 do_cleanup_occ (void *cls)
588 struct OverlayConnectContext *occ = cls;
590 occ->cleanup_task = NULL;
596 * Task which will be run when overlay connect request has been timed out
598 * @param cls the OverlayConnectContext
601 timeout_overlay_connect (void *cls)
603 struct OverlayConnectContext *occ = cls;
605 GNUNET_assert (NULL != occ->timeout_task);
606 occ->timeout_task = NULL;
607 /* LOG (GNUNET_ERROR_TYPE_WARNING, */
608 /* "0x%llx: Timeout while connecting peers %u and %u: %s\n", occ->op_id, */
609 /* occ->peer->id, occ->other_peer_id, occ->emsg); */
610 GST_send_operation_fail_msg (occ->client,
618 * Notify OC subsystem that @a client disconnected.
620 * @param client the client that disconnected
623 GST_notify_client_disconnect_oc (struct GNUNET_SERVICE_Client *client)
625 struct ForwardedOperationContext *fopc;
626 struct ForwardedOperationContext *fopcn;
627 struct OverlayConnectContext *occ;
628 struct OverlayConnectContext *occn;
630 for (fopc = fopcq_head; NULL != fopc; fopc = fopcn)
633 if (fopc->client == client)
635 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
636 GST_forwarded_operation_timeout (fopc);
639 for (occ = occq_head; NULL != occ; occ = occn)
642 if (occ->client == client)
645 // FIXME: implement clean up for client_keep replacements!
655 send_overlay_connect_success_msg (struct OverlayConnectContext *occ)
657 struct GNUNET_MQ_Envelope *env;
658 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
660 LOG_DEBUG ("0x%llx: Peers connected - Sending overlay connect success\n",
662 env = GNUNET_MQ_msg (msg,
663 GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONNECT_EVENT);
664 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
665 msg->peer1 = htonl (occ->peer->id);
666 msg->peer2 = htonl (occ->other_peer_id);
667 msg->operation_id = GNUNET_htonll (occ->op_id);
668 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (occ->client),
674 * Function called to notify transport users that another
675 * peer connected to us.
678 * @param new_peer the peer that connected
681 overlay_connect_notify (void *cls,
682 const struct GNUNET_PeerIdentity *new_peer)
684 struct OverlayConnectContext *occ = cls;
686 char *other_peer_str;
688 LOG_DEBUG ("Overlay connect notify\n");
690 memcmp (new_peer, &occ->peer_identity,
691 sizeof (struct GNUNET_PeerIdentity)))
693 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
694 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
697 &occ->other_peer_identity,
698 sizeof (struct GNUNET_PeerIdentity)))
700 LOG_DEBUG ("Unexpected peer %s connected when expecting peer %s\n",
703 GNUNET_free (new_peer_str);
704 GNUNET_free (other_peer_str);
707 GNUNET_free (new_peer_str);
708 LOG_DEBUG ("0x%llx: Peer %s connected to peer %s\n",
711 GNUNET_i2s (&occ->peer_identity));
712 GNUNET_free (other_peer_str);
713 if (NULL != occ->send_hello_task)
715 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
716 occ->send_hello_task = NULL;
718 GNUNET_assert (NULL != occ->timeout_task);
719 GNUNET_SCHEDULER_cancel (occ->timeout_task);
720 occ->timeout_task = NULL;
724 cleanup_occ_lp2c (&occ->p2ctx.local);
726 case OCC_TYPE_REMOTE_SLAVE:
727 case OCC_TYPE_REMOTE_LATERAL:
728 cleanup_occ_rp2c (&occ->p2ctx.remote);
731 GNUNET_free_non_null (occ->emsg);
733 send_overlay_connect_success_msg (occ);
734 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ,
740 * Callback from cache with needed ATS handle set
742 * @param cls a `struct OverlayConnectCtx *`
743 * @param ch the handle to CORE. Can be NULL if it is not requested
744 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
745 * @param ac the handle to ATS. Can be NULL if it is not requested
746 * @param my_identity the identity of our peer
747 * @param cfg configuration of the peer
750 occ_cache_get_handle_ats_occ_cb (void *cls,
751 struct GNUNET_CORE_Handle *ch,
752 struct GNUNET_TRANSPORT_CoreHandle *th,
753 struct GNUNET_ATS_ConnectivityHandle *ac,
754 const struct GNUNET_PeerIdentity *my_identity,
755 const struct GNUNET_CONFIGURATION_Handle *cfg)
757 struct OverlayConnectContext *occ = cls;
758 struct LocalPeer2Context *lp2c;
760 GNUNET_assert (OCC_TYPE_LOCAL == occ->type);
761 GNUNET_assert (NULL != occ->timeout_task);
762 GNUNET_free_non_null (occ->emsg);
765 GNUNET_asprintf (&occ->emsg,
766 "0x%llx: Failed to connect to ATS of peer with id: %u",
769 GNUNET_SCHEDULER_cancel (occ->timeout_task);
771 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect,
777 GNUNET_asprintf (&occ->emsg,
778 "0x%llx: Timeout during GNUNET_ATS_connectivity_suggest() at peer %s",
780 GNUNET_i2s (&occ->other_peer_identity));
782 lp2c = &occ->p2ctx.local;
784 GNUNET_ATS_connectivity_suggest (ac,
791 * Callback from cache with needed ATS handle set
793 * @param cls a `struct RemoteOverlayConnectCtx *`
794 * @param ch the handle to CORE. Can be NULL if it is not requested
795 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
796 * @param ac the handle to ATS. Can be NULL if it is not requested
797 * @param my_identity the identity of our peer
800 occ_cache_get_handle_ats_rocc_cb (void *cls,
801 struct GNUNET_CORE_Handle *ch,
802 struct GNUNET_TRANSPORT_CoreHandle *th,
803 struct GNUNET_ATS_ConnectivityHandle *ac,
804 const struct GNUNET_PeerIdentity *my_identity,
805 const struct GNUNET_CONFIGURATION_Handle *cfg)
807 struct RemoteOverlayConnectCtx *rocc = cls;
810 GNUNET_ATS_connectivity_suggest (ac,
817 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
820 * @param cls the OverlayConnectContext
823 send_hello (void *cls);
827 * Task that is run when hello has been sent If tc->reason =
828 * #GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
829 * #GNUNET_SCHEDULER_REASON_READ_READY is succeeded
831 * @param cls the overlay connect context
834 occ_hello_sent_cb (void *cls)
836 struct OverlayConnectContext *occ = cls;
837 struct LocalPeer2Context *lp2c;
840 GNUNET_assert (OCC_TYPE_LOCAL == occ->type);
841 GNUNET_assert (NULL != occ->timeout_task);
842 lp2c = &occ->p2ctx.local;
845 GNUNET_assert (NULL == occ->send_hello_task);
846 GNUNET_free_non_null (occ->emsg);
848 GNUNET_asprintf (&occ->emsg,
849 "0x%llx: Timeout while acquiring ATS of %s from cache",
851 GNUNET_i2s (&occ->other_peer_identity));
852 GNUNET_assert (NULL != (peer2 = GST_peer_list[occ->other_peer_id]));
853 lp2c->tcc.cgh_p2_ats =
854 GST_connection_pool_get_handle (occ->other_peer_id,
855 peer2->details.local.cfg,
856 GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY,
857 &occ_cache_get_handle_ats_occ_cb,
858 occ, NULL, NULL, NULL);
863 * Sends the HELLO of peer1 to peer2's controller through remote overlay connect
866 * @param occ the overlay connect context. Its type must be either
867 * #OCC_TYPE_REMOTE_SLAVE or #OCC_TYPE_REMOTE_LATERAL
870 send_hello_thru_rocc (struct OverlayConnectContext *occ)
872 struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg;
873 char *other_peer_str;
877 GNUNET_assert (OCC_TYPE_LOCAL != occ->type);
878 GNUNET_assert (NULL != occ->hello);
879 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
880 LOG_DEBUG ("0x%llx: Offering HELLO of %s (size: %u) to %s via Remote Overlay Request\n",
882 GNUNET_i2s (&occ->peer_identity),
883 ntohs (occ->hello->size),
885 GNUNET_free (other_peer_str);
886 hello_size = ntohs (occ->hello->size);
887 msize = sizeof (struct GNUNET_TESTBED_RemoteOverlayConnectMessage) + hello_size;
888 msg = GNUNET_malloc (msize);
890 htons (GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT);
891 msg->header.size = htons (msize);
892 msg->peer = htonl (occ->other_peer_id);
893 msg->operation_id = GNUNET_htonll (occ->op_id);
894 msg->peer_identity = occ->peer_identity;
895 GNUNET_memcpy (msg->hello,
898 GNUNET_TESTBED_queue_message_ (occ->p2ctx.remote.p2c,
904 * Task to offer HELLO of peer 1 to peer 2. If peer2 is local it is offered
905 * using its TRANSPORT connection; if remote the HELLO is sent remotely by using
906 * send_hello_thru_rocc()
908 * @param cls the OverlayConnectContext
911 send_hello (void *cls)
913 struct OverlayConnectContext *occ = cls;
914 struct LocalPeer2Context *lp2c;
915 char *other_peer_str;
917 occ->send_hello_task = NULL;
918 GNUNET_assert (NULL != occ->timeout_task);
919 GNUNET_assert (NULL != occ->hello);
920 if (OCC_TYPE_LOCAL != occ->type)
922 send_hello_thru_rocc (occ);
925 lp2c = &occ->p2ctx.local;
926 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
927 LOG_DEBUG ("0x%llx: Offering HELLO of %s to %s\n",
929 GNUNET_i2s (&occ->peer_identity),
931 GNUNET_free (other_peer_str);
933 GNUNET_TRANSPORT_offer_hello (lp2c->tcc.cfg,
937 if (NULL == lp2c->ohh)
940 occ->send_hello_task =
941 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
942 (GNUNET_TIME_UNIT_MILLISECONDS,
944 GNUNET_CRYPTO_random_u32
945 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
952 * Callback from cache with needed handles set
954 * @param cls the closure passed to GST_cache_get_handle_transport()
955 * @param ch the handle to CORE. Can be NULL if it is not requested
956 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
957 * @param ac the handle to ATS. Can be NULL if it is not requested
958 * @param ignore_ peer identity which is ignored in this callback
959 * @param cfg configuration of the peer
962 p2_transport_connect_cache_callback (void *cls,
963 struct GNUNET_CORE_Handle *ch,
964 struct GNUNET_TRANSPORT_CoreHandle *th,
965 struct GNUNET_ATS_ConnectivityHandle *ac,
966 const struct GNUNET_PeerIdentity *ignore_,
967 const struct GNUNET_CONFIGURATION_Handle *cfg)
969 struct OverlayConnectContext *occ = cls;
971 GNUNET_assert (OCC_TYPE_LOCAL == occ->type);
974 GNUNET_asprintf (&occ->emsg,
975 "0x%llx: Cannot connect to TRANSPORT of %s",
977 GNUNET_i2s (&occ->other_peer_identity));
978 GNUNET_SCHEDULER_cancel (occ->timeout_task);
980 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
983 occ->p2ctx.local.tcc.th_ = th;
984 occ->p2ctx.local.tcc.cfg = cfg;
985 GNUNET_asprintf (&occ->emsg,
986 "0x%llx: Timeout while offering HELLO to %s",
988 GNUNET_i2s (&occ->other_peer_identity));
989 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
994 * Connects to the transport of the other peer if it is a local peer and
995 * schedules the send hello task
997 * @param occ the overlay connect context
1000 p2_transport_connect (struct OverlayConnectContext *occ)
1004 GNUNET_assert (NULL == occ->emsg);
1005 GNUNET_assert (NULL != occ->hello);
1006 GNUNET_assert (NULL == occ->ghh);
1007 GNUNET_assert (NULL == occ->p1th_);
1008 GNUNET_assert (NULL == occ->cgh_p1th);
1009 if (OCC_TYPE_LOCAL == occ->type)
1011 GNUNET_assert (NULL != (peer2 = GST_peer_list[occ->other_peer_id]));
1012 occ->p2ctx.local.tcc.cgh_p2_th =
1013 GST_connection_pool_get_handle (occ->other_peer_id,
1014 peer2->details.local.cfg,
1015 GST_CONNECTIONPOOL_SERVICE_TRANSPORT,
1016 &p2_transport_connect_cache_callback,
1017 occ, NULL, NULL, NULL);
1020 GNUNET_asprintf (&occ->emsg,
1021 "0x%llx: Timeout while offering HELLO to %s",
1023 GNUNET_i2s (&occ->other_peer_identity));
1024 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
1029 * Test for checking whether HELLO message is empty
1031 * @param cls empty flag to set
1032 * @param address the HELLO
1033 * @param expiration expiration of the HELLO
1034 * @return #GNUNET_OK
1037 test_address (void *cls,
1038 const struct GNUNET_HELLO_Address *address,
1039 struct GNUNET_TIME_Absolute expiration)
1049 * Function called whenever there is an update to the HELLO of peers in the
1050 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
1051 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
1053 * @param cls closure
1054 * @param hello our updated HELLO
1057 hello_update_cb (void *cls,
1058 const struct GNUNET_MessageHeader *hello)
1060 struct OverlayConnectContext *occ = cls;
1064 msize = ntohs (hello->size);
1066 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
1070 if (GNUNET_YES == empty)
1072 LOG_DEBUG ("0x%llx: HELLO of %s is empty\n",
1074 GNUNET_i2s (&occ->peer_identity));
1077 LOG_DEBUG ("0x%llx: Received HELLO of %s\n",
1079 GNUNET_i2s (&occ->peer_identity));
1080 occ->hello = GNUNET_malloc (msize);
1081 GST_cache_add_hello (occ->peer->id, hello);
1082 GNUNET_memcpy (occ->hello, hello, msize);
1083 GNUNET_TRANSPORT_hello_get_cancel (occ->ghh);
1085 GST_connection_pool_get_handle_done (occ->cgh_p1th);
1086 occ->cgh_p1th = NULL;
1088 GNUNET_free_non_null (occ->emsg);
1090 p2_transport_connect (occ);
1095 * Callback from cache with needed handles set
1097 * @param cls the closure passed to GST_cache_get_handle_transport()
1098 * @param ch the handle to CORE. Can be NULL if it is not requested
1099 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
1100 * @param ac the handle to ATS. Can be NULL if it is not requested
1101 * @param ignore_ peer identity which is ignored in this callback
1104 p1_transport_connect_cache_callback (void *cls,
1105 struct GNUNET_CORE_Handle *ch,
1106 struct GNUNET_TRANSPORT_CoreHandle *th,
1107 struct GNUNET_ATS_ConnectivityHandle *ac,
1108 const struct GNUNET_PeerIdentity *ignore_,
1109 const struct GNUNET_CONFIGURATION_Handle *cfg)
1111 struct OverlayConnectContext *occ = cls;
1113 GNUNET_free_non_null (occ->emsg);
1117 GNUNET_asprintf (&occ->emsg,
1118 "0x%llx: Cannot connect to TRANSPORT of %s",
1120 GNUNET_i2s (&occ->peer_identity));
1121 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1123 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1126 GNUNET_assert (NULL == occ->p1th_);
1127 GNUNET_assert (NULL != occ->cgh_p1th);
1129 GNUNET_asprintf (&occ->emsg,
1130 "0x%llx: Timeout while acquiring HELLO of peer %s",
1132 GNUNET_i2s (&occ->peer_identity));
1133 occ->ghh = GNUNET_TRANSPORT_hello_get (cfg,
1134 GNUNET_TRANSPORT_AC_ANY,
1141 * Callback from cache with needed CORE handle set
1143 * @param cls the closure passed to GST_cache_get_handle_transport()
1144 * @param ch the handle to CORE. Can be NULL if it is not requested
1145 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
1146 * @param ac the handle to ATS. Can be NULL if it is not requested
1147 * @param my_identity the identity of our peer
1150 occ_cache_get_handle_core_cb (void *cls,
1151 struct GNUNET_CORE_Handle *ch,
1152 struct GNUNET_TRANSPORT_CoreHandle *th,
1153 struct GNUNET_ATS_ConnectivityHandle *ac,
1154 const struct GNUNET_PeerIdentity *my_identity,
1155 const struct GNUNET_CONFIGURATION_Handle *cfg)
1157 struct OverlayConnectContext *occ = cls;
1158 const struct GNUNET_MessageHeader *hello;
1160 GNUNET_assert (NULL != occ->timeout_task);
1161 GNUNET_free_non_null (occ->emsg);
1162 if ((NULL == ch) || (NULL == my_identity))
1164 GNUNET_asprintf (&occ->emsg,
1165 "0x%llx: Failed to connect to CORE of peer with "
1169 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1171 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1176 GNUNET_CORE_get_mq (ch,
1177 &occ->other_peer_identity))
1179 LOG_DEBUG ("0x%llx: Target peer already connected\n",
1181 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1182 occ->timeout_task = NULL;
1183 send_overlay_connect_success_msg (occ);
1184 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
1187 occ->peer_identity = *my_identity;
1188 LOG_DEBUG ("0x%llx: Acquiring HELLO of peer %s\n",
1190 GNUNET_i2s (&occ->peer_identity));
1191 /* Lookup for HELLO in hello cache */
1192 if (NULL != (hello = GST_cache_lookup_hello (occ->peer->id)))
1194 LOG_DEBUG ("0x%llx: HELLO of peer %s found in cache\n",
1196 GNUNET_i2s (&occ->peer_identity));
1197 occ->hello = GNUNET_copy_message (hello);
1198 p2_transport_connect (occ);
1201 GNUNET_asprintf (&occ->emsg,
1202 "0x%llx: Timeout while acquiring TRANSPORT of %s from cache",
1204 GNUNET_i2s (&occ->peer_identity));
1206 GST_connection_pool_get_handle (occ->peer->id,
1207 occ->peer->details.local.cfg,
1208 GST_CONNECTIONPOOL_SERVICE_TRANSPORT,
1209 p1_transport_connect_cache_callback,
1216 * Callback to be called when forwarded get peer config operation as part of
1217 * overlay connect is successfull. Connection to Peer 1's core is made and is
1218 * checked for new connection from peer 2
1220 * @param cls ForwardedOperationContext
1221 * @param msg the peer create success message
1224 overlay_connect_get_config (void *cls,
1225 const struct GNUNET_MessageHeader *msg)
1227 struct OverlayConnectContext *occ = cls;
1228 struct RemotePeer2Context *rp2c;
1229 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
1231 GNUNET_assert (OCC_TYPE_LOCAL != occ->type);
1232 rp2c = &occ->p2ctx.remote;
1234 GNUNET_assert (NULL != occ->timeout_task);
1235 if (GNUNET_MESSAGE_TYPE_TESTBED_PEER_INFORMATION != ntohs (msg->type))
1237 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1239 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1242 (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *) msg;
1243 occ->other_peer_identity = cmsg->peer_identity;
1244 GNUNET_free_non_null (occ->emsg);
1245 GNUNET_asprintf (&occ->emsg,
1246 "0x%llx: Timeout while connecting to CORE of peer with "
1251 GST_connection_pool_get_handle (occ->peer->id,
1252 occ->peer->details.local.cfg,
1253 GST_CONNECTIONPOOL_SERVICE_CORE,
1254 occ_cache_get_handle_core_cb,
1256 &occ->other_peer_identity,
1257 &overlay_connect_notify,
1264 * Callback which will be called after a host registration succeeded or failed
1266 * @param cls the RegisteredHostContext
1267 * @param emsg the error message; NULL if host registration is successful
1270 host_registration_comp (void *cls, const char *emsg)
1272 struct RegisteredHostContext *rhc = cls;
1274 rhc->state = RHC_DONE;
1275 GST_process_next_focc (rhc);
1280 * Iterator to match a registered host context
1282 * @param cls pointer 2 pointer of RegisteredHostContext
1283 * @param key current key code
1284 * @param value value in the hash map
1285 * @return #GNUNET_YES if we should continue to
1287 * #GNUNET_NO if not.
1290 reghost_match_iterator (void *cls,
1291 const struct GNUNET_HashCode *key,
1294 struct RegisteredHostContext **rh = cls;
1295 struct RegisteredHostContext *rh_val = value;
1297 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
1308 * Function to generate the hashcode corresponding to a RegisteredHostContext
1310 * @param reg_host the host which is being registered in RegisteredHostContext
1311 * @param host the host of the controller which has to connect to the above rhost
1312 * @return the hashcode
1314 static struct GNUNET_HashCode
1315 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
1316 struct GNUNET_TESTBED_Host *host)
1318 struct GNUNET_HashCode hash;
1319 uint32_t host_ids[2];
1321 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
1322 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
1323 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
1329 * Checks if the given host is registered at the given slave.
1331 * @param slave the slave where registration has to be checked. The check is
1332 * actually done through a locally maintained hashmap. No
1333 * communication with the slave is involved.
1334 * @param host the host to register
1335 * @return If the given host is not registered already or the registration is
1336 * pending, it returns the registration context. Any overlay connects
1337 * to be forwarded should be queued in the context so that they can be
1338 * executed when the registration is completed. If the given host is
1339 * already registered, NULL is returned.
1341 static struct RegisteredHostContext *
1342 register_host (struct Slave *slave,
1343 struct GNUNET_TESTBED_Host *host)
1345 struct GNUNET_HashCode hash;
1346 struct RegisteredHostContext *rhc;
1348 rhc = GNUNET_new (struct RegisteredHostContext);
1349 rhc->reg_host = host;
1350 rhc->host = GST_host_list[slave->host_id];
1351 GNUNET_assert (NULL != rhc->reg_host);
1352 GNUNET_assert (NULL != rhc->host);
1353 rhc->state = RHC_INIT;
1354 hash = hash_hosts (rhc->reg_host, rhc->host);
1356 GNUNET_CONTAINER_multihashmap_contains (slave->reghost_map,
1359 GNUNET_CONTAINER_multihashmap_get_multiple (slave->reghost_map,
1361 reghost_match_iterator,
1364 /* create and add a new registerd host context */
1365 /* add the focc to its queue */
1366 GNUNET_CONTAINER_multihashmap_put (slave->reghost_map,
1369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1370 GST_queue_host_registration (slave,
1371 host_registration_comp,
1377 /* rhc is now set to the existing one from the hash map by
1378 * reghost_match_iterator() */
1379 /* if queue is empty then ignore creating focc and proceed with normal
1381 if (RHC_DONE == rhc->state)
1389 * Forwards the overlay connect request to a slave controller. Before
1390 * forwarding, any hosts which are needed to be known by the slave controller to
1391 * execute the overlay connect request are registered at slave.
1393 * @param msg the overlay connect request message to be forwarded
1394 * @param client the client to which the status of the forwarded request has to
1398 forward_overlay_connect (const struct GNUNET_TESTBED_OverlayConnectMessage *msg,
1399 struct GNUNET_SERVICE_Client *client)
1401 struct ForwardedOperationContext *fopc;
1402 struct Route *route_to_peer2_host;
1403 struct Route *route_to_peer1_host;
1405 struct RegisteredHostContext *rhc;
1406 struct ForwardedOverlayConnectContext *focc;
1408 uint32_t peer2_host_id;
1412 p1 = ntohl (msg->peer1);
1413 p2 = ntohl (msg->peer2);
1414 op_id = GNUNET_ntohll (msg->operation_id);
1415 peer2_host_id = ntohl (msg->peer2_host_id);
1416 GNUNET_assert (VALID_PEER_ID (p1));
1417 GNUNET_assert (VALID_HOST_ID (peer2_host_id));
1418 peer = GST_peer_list[p1];
1419 GNUNET_assert (GNUNET_YES == peer->is_remote);
1420 LOG_DEBUG ("0x%llx: Forwarding overlay connect\n", op_id);
1421 route_to_peer2_host = GST_find_dest_route (peer2_host_id);
1422 route_to_peer1_host = GST_find_dest_route
1423 (peer->details.remote.remote_host_id);
1424 GNUNET_assert (NULL != route_to_peer1_host);
1425 if ((NULL != route_to_peer2_host) &&
1426 (route_to_peer1_host->dest == route_to_peer2_host->dest))
1428 /* Peer2 is either with us OR peer1 and peer2 can be reached through
1429 different subtrees OR peer2 is on a subtree unknown to us */
1430 if (NULL != (rhc = register_host (peer->details.remote.slave,
1431 GST_host_list[peer2_host_id])))
1433 LOG_DEBUG ("Queueing forwarding FOCC for connecting peers %u and %u\n", p1, p2);
1434 focc = GNUNET_new (struct ForwardedOverlayConnectContext);
1438 focc->peer2_host_id = peer2_host_id;
1439 focc->orig_msg = GNUNET_copy_message (&msg->header);
1440 focc->operation_id = op_id;
1441 focc->client = client;
1442 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
1449 LOG_DEBUG ("Forwarding without FOCC for connecting peers %u and %u\n", p1, p2);
1450 fopc = GNUNET_new (struct ForwardedOperationContext);
1451 fopc->client = client;
1452 fopc->operation_id = op_id;
1453 fopc->type = OP_OVERLAY_CONNECT;
1455 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1456 slave->controller, op_id,
1458 &GST_forwarded_operation_reply_relay,
1460 fopc->timeout_task =
1461 GNUNET_SCHEDULER_add_delayed (GST_timeout,
1462 &GST_forwarded_operation_timeout,
1464 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head,
1471 * Callback called when a connection to the controller of peer2 has been
1474 * @param cls the overlay connect contexts
1475 * @param c handle to the controller connection
1478 p2_controller_connect_cb (void *cls,
1479 struct GNUNET_TESTBED_Controller *c)
1481 struct OverlayConnectContext *occ = cls;
1482 struct RemotePeer2Context *rp2c;
1483 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
1485 GNUNET_assert (OCC_TYPE_LOCAL != occ->type);
1486 rp2c = &occ->p2ctx.remote;
1490 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
1492 htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_INFORMATION);
1493 cmsg.peer_id = htonl (occ->other_peer_id);
1494 cmsg.operation_id = GNUNET_htonll (occ->op_id);
1496 GNUNET_TESTBED_forward_operation_msg_ (rp2c->p2c,
1499 &overlay_connect_get_config,
1501 GNUNET_free_non_null (occ->emsg);
1502 GNUNET_asprintf (&occ->emsg,
1503 "0x%llx: Timeout while getting peer identity of peer "
1506 occ->other_peer_id);
1511 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT messages
1513 * @param cls identification of the client
1514 * @param msg the actual message
1517 handle_overlay_connect (void *cls,
1518 const struct GNUNET_TESTBED_OverlayConnectMessage *msg)
1520 struct GNUNET_SERVICE_Client *client = cls;
1523 struct OverlayConnectContext *occ;
1524 struct Neighbour *p2n;
1525 uint64_t operation_id;
1528 uint32_t peer2_host_id;
1530 p1 = ntohl (msg->peer1);
1531 p2 = ntohl (msg->peer2);
1532 if (! VALID_PEER_ID (p1))
1535 GNUNET_SERVICE_client_drop (client);
1538 peer = GST_peer_list[p1];
1539 operation_id = GNUNET_ntohll (msg->operation_id);
1541 ("Received overlay connect for peers %u and %u with op id: 0x%llx\n",
1545 peer2_host_id = ntohl (msg->peer2_host_id);
1546 if (GNUNET_YES == peer->is_remote)
1548 if (! VALID_HOST_ID (peer2_host_id))
1551 GNUNET_SERVICE_client_drop (client);
1554 forward_overlay_connect (msg, client);
1555 GNUNET_SERVICE_client_continue (client);
1559 occ = GNUNET_new (struct OverlayConnectContext);
1560 occ->type = OCC_TYPE_LOCAL;
1561 if (! VALID_PEER_ID (p2)) /* May be peer2 is on a another controller */
1563 if (NULL == (p2n = GST_get_neighbour (peer2_host_id)))
1565 if (! VALID_HOST_ID (peer2_host_id))
1568 LOG (GNUNET_ERROR_TYPE_WARNING,
1569 "0x%llx: Peer %u's host not in our neighbours list\n",
1571 GNUNET_SERVICE_client_drop (client);
1575 p2n = GST_create_neighbour (GST_host_list[peer2_host_id]);
1577 occ->type = OCC_TYPE_REMOTE_LATERAL;
1578 occ->p2ctx.remote.p2n = p2n;
1580 else if (GNUNET_YES == GST_peer_list[p2]->is_remote)
1582 occ->type = OCC_TYPE_REMOTE_SLAVE;
1583 occ->p2ctx.remote.p2c = GST_peer_list[p2]->details.remote.slave->controller;
1585 GNUNET_CONTAINER_DLL_insert_tail (occq_head,
1588 occ->client = client;
1589 occ->other_peer_id = p2;
1590 GST_peer_list[p1]->reference_cnt++;
1591 occ->peer = GST_peer_list[p1];
1592 occ->op_id = operation_id;
1593 GNUNET_assert (NULL == occ->timeout_task);
1595 GNUNET_SCHEDULER_add_delayed (GST_timeout,
1596 &timeout_overlay_connect,
1600 case OCC_TYPE_REMOTE_LATERAL:
1601 GNUNET_asprintf (&occ->emsg,
1602 "0x%llx: Timeout while acquiring connection to peer %u's "
1607 occ->p2ctx.remote.ncn
1608 = GST_neighbour_get_connection (p2n,
1609 &p2_controller_connect_cb,
1612 case OCC_TYPE_REMOTE_SLAVE:
1613 p2_controller_connect_cb (occ,
1614 occ->p2ctx.remote.p2c);
1616 case OCC_TYPE_LOCAL:
1617 peer2 = GST_peer_list[occ->other_peer_id];
1618 peer2->reference_cnt++;
1619 GNUNET_TESTING_peer_get_identity (peer2->details.local.peer,
1620 &occ->other_peer_identity);
1621 GNUNET_asprintf (&occ->emsg,
1622 "0x%llx: Timeout while connecting to CORE of peer with "
1627 GST_connection_pool_get_handle (occ->peer->id,
1628 occ->peer->details.local.cfg,
1629 GST_CONNECTIONPOOL_SERVICE_CORE,
1630 occ_cache_get_handle_core_cb, occ,
1631 &occ->other_peer_identity,
1632 &overlay_connect_notify, occ);
1635 GNUNET_SERVICE_client_continue (client);
1640 * Function to cleanup RemoteOverlayConnectCtx and any associated tasks
1643 * @param rocc the RemoteOverlayConnectCtx
1646 cleanup_rocc (struct RemoteOverlayConnectCtx *rocc)
1648 LOG_DEBUG ("0x%llx: Cleaning up rocc\n",
1650 if (NULL != rocc->attempt_connect_task_id)
1651 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
1652 if (NULL != rocc->timeout_rocc_task_id)
1653 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
1654 if (NULL != rocc->ohh)
1655 GNUNET_TRANSPORT_offer_hello_cancel (rocc->ohh);
1656 if (NULL != rocc->tcc.csh)
1657 GNUNET_ATS_connectivity_suggest_cancel (rocc->tcc.csh);
1658 GST_connection_pool_get_handle_done (rocc->tcc.cgh_p2_th);
1659 GST_connection_pool_get_handle_done (rocc->tcc.cgh_p2_ats);
1660 GNUNET_assert (rocc->peer->reference_cnt > 0);
1661 rocc->peer->reference_cnt--;
1662 if ((GNUNET_YES == rocc->peer->destroy_flag) &&
1663 (0 == rocc->peer->reference_cnt))
1664 GST_destroy_peer (rocc->peer);
1665 GNUNET_free_non_null (rocc->hello);
1666 GNUNET_CONTAINER_DLL_remove (roccq_head,
1674 * Task to timeout rocc and cleanit up
1676 * @param cls the RemoteOverlayConnectCtx
1679 timeout_rocc_task (void *cls)
1681 struct RemoteOverlayConnectCtx *rocc = cls;
1683 GNUNET_assert (rocc->timeout_rocc_task_id != NULL);
1684 rocc->timeout_rocc_task_id = NULL;
1685 LOG_DEBUG ("0x%llx: rocc timed out\n",
1687 cleanup_rocc (rocc);
1692 * Function called to notify transport users that another
1693 * peer connected to us.
1695 * @param cls the RemoteOverlayConnectContext
1696 * @param new_peer the peer that connected
1699 cache_transport_peer_connect_notify (void *cls,
1700 const struct GNUNET_PeerIdentity *new_peer)
1702 struct RemoteOverlayConnectCtx *rocc = cls;
1704 LOG_DEBUG ("0x%llx: Request Overlay connect notify\n",
1707 memcmp (new_peer, &rocc->a_id,
1708 sizeof (struct GNUNET_PeerIdentity)));
1709 LOG_DEBUG ("0x%llx: Peer %s connected\n",
1711 GNUNET_i2s (&rocc->a_id));
1712 cleanup_rocc (rocc);
1717 * Task to offer the HELLO message to the peer and ask it to connect to the peer
1718 * whose identity is in RemoteOverlayConnectCtx
1720 * @param cls the RemoteOverlayConnectCtx
1723 attempt_connect_task (void *cls);
1727 * Task that is run when hello has been sent If tc->reason =
1728 * #GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
1729 * #GNUNET_SCHEDULER_REASON_READ_READY is succeeded
1731 * @param cls the overlay connect context
1734 rocc_hello_sent_cb (void *cls)
1736 struct RemoteOverlayConnectCtx *rocc = cls;
1739 GNUNET_assert (NULL == rocc->attempt_connect_task_id);
1740 LOG_DEBUG ("0x%llx: HELLO of peer %s delivered to local peer with id: %u\n",
1742 GNUNET_i2s (&rocc->a_id),
1744 rocc->tcc.cgh_p2_ats =
1745 GST_connection_pool_get_handle (rocc->peer->id,
1746 rocc->peer->details.local.cfg,
1747 GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY,
1748 &occ_cache_get_handle_ats_rocc_cb,
1749 rocc, NULL, NULL, NULL);
1754 * Task to offer the HELLO message to the peer and ask it to connect to the peer
1755 * whose identity is in RemoteOverlayConnectCtx
1757 * @param cls the RemoteOverlayConnectCtx
1760 attempt_connect_task (void *cls)
1762 struct RemoteOverlayConnectCtx *rocc = cls;
1764 GNUNET_assert (NULL != rocc->attempt_connect_task_id);
1765 rocc->attempt_connect_task_id = NULL;
1766 LOG_DEBUG ("0x%llx: Offering HELLO of peer %s to remote peer with id: %u\n",
1768 GNUNET_i2s (&rocc->a_id),
1771 GNUNET_TRANSPORT_offer_hello (rocc->tcc.cfg,
1773 &rocc_hello_sent_cb,
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_CoreHandle *th,
1799 struct GNUNET_ATS_ConnectivityHandle *ac,
1800 const struct GNUNET_PeerIdentity *ignore_,
1801 const struct GNUNET_CONFIGURATION_Handle *cfg)
1803 struct RemoteOverlayConnectCtx *rocc = cls;
1807 rocc->timeout_rocc_task_id =
1808 GNUNET_SCHEDULER_add_now (&timeout_rocc_task, rocc);
1812 rocc->tcc.cfg = cfg;
1814 GNUNET_TRANSPORT_core_get_mq (rocc->tcc.th_,
1817 LOG_DEBUG ("0x%llx: Target peer %s already connected to local peer: %u\n",
1819 GNUNET_i2s (&rocc->a_id),
1821 cleanup_rocc (rocc);
1824 rocc->attempt_connect_task_id =
1825 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
1830 * Check #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
1832 * @param cls identification of the client
1833 * @param msg the actual message
1834 * @return #GNUNET_OK if @a msg is well-formed
1837 check_remote_overlay_connect (void *cls,
1838 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg)
1844 msize = ntohs (msg->header.size);
1845 if (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type))
1848 return GNUNET_SYSERR;
1850 hsize = ntohs (msg->hello->size);
1851 if ((sizeof (struct GNUNET_TESTBED_RemoteOverlayConnectMessage) + hsize) != msize)
1854 return GNUNET_SYSERR;
1856 peer_id = ntohl (msg->peer);
1857 if ((peer_id >= GST_peer_list_size) ||
1858 (NULL == GST_peer_list[peer_id]))
1860 GNUNET_break_op (0);
1861 return GNUNET_SYSERR;
1868 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
1870 * @param cls identification of the client
1871 * @param msg the actual message
1874 handle_remote_overlay_connect (void *cls,
1875 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg)
1877 struct GNUNET_SERVICE_Client *client = cls;
1878 struct RemoteOverlayConnectCtx *rocc;
1880 struct GNUNET_PeerIdentity pid;
1881 static char pid_str[16];
1885 hsize = ntohs (msg->hello->size);
1886 peer_id = ntohl (msg->peer);
1887 peer = GST_peer_list[peer_id];
1888 if (GNUNET_YES == peer->is_remote)
1890 struct GNUNET_MessageHeader *msg2;
1892 msg2 = GNUNET_copy_message (&msg->header);
1893 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller,
1895 GNUNET_SERVICE_client_continue (client);
1898 rocc = GNUNET_new (struct RemoteOverlayConnectCtx);
1899 rocc->op_id = GNUNET_ntohll (msg->operation_id);
1900 GNUNET_CONTAINER_DLL_insert_tail (roccq_head,
1903 rocc->a_id = msg->peer_identity;
1904 GNUNET_TESTING_peer_get_identity (peer->details.local.peer,
1906 (void) strncpy (pid_str,
1909 LOG_DEBUG ("0x%llx: Remote overlay connect %s to peer %s with hello size: %u\n",
1912 GNUNET_i2s (&rocc->a_id),
1915 rocc->peer->reference_cnt++;
1916 rocc->hello = GNUNET_malloc (hsize);
1917 GNUNET_memcpy (rocc->hello,
1920 rocc->tcc.cgh_p2_th =
1921 GST_connection_pool_get_handle (peer_id,
1922 rocc->peer->details.local.cfg,
1923 GST_CONNECTIONPOOL_SERVICE_TRANSPORT,
1924 &rocc_cache_get_handle_transport_cb,
1927 &cache_transport_peer_connect_notify,
1929 rocc->timeout_rocc_task_id =
1930 GNUNET_SCHEDULER_add_delayed (GST_timeout,
1933 GNUNET_SERVICE_client_continue (client);
1938 * Clears all pending overlay connect contexts in queue
1943 struct OverlayConnectContext *occ;
1945 while (NULL != (occ = occq_head))
1951 * Clears all pending remote overlay connect contexts in queue
1956 struct RemoteOverlayConnectCtx *rocc;
1958 while (NULL != (rocc = roccq_head))
1959 cleanup_rocc (rocc);