2 This file is part of GNUnet.
3 Copyright (C) 2008--2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file testbed/gnunet-service-testbed_links.c
23 * @brief TESTBED service components that deals with starting slave controllers
24 * and establishing lateral links between controllers
25 * @author Sree Harsha Totakura
28 #include "gnunet-service-testbed.h"
31 * Redefine LOG with a changed log component string
36 #define LOG(kind, ...) \
37 GNUNET_log_from(kind, "testbed-links", __VA_ARGS__)
40 * The event mask for the events we listen from sub-controllers
42 #define EVENT_MASK (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED)
46 * States of LCFContext
48 enum LCFContextState {
50 * The Context has been initialized; Nothing has been done on it
55 * Delegated host has been registered at the forwarding controller
57 DELEGATED_HOST_REGISTERED,
60 * The slave host has been registred at the forwarding controller
62 SLAVE_HOST_REGISTERED,
65 * The context has been finished (may have error)
72 * Link controllers request forwarding context
78 struct LCFContext *next;
83 struct LCFContext *prev;
86 * The gateway which will pass the link message to delegated host
88 struct Slave *gateway;
91 * The client which has asked to perform this operation
93 struct GNUNET_SERVICE_Client *client;
96 * Handle for operations which are forwarded while linking controllers
98 struct GNUNET_TESTBED_Operation *op;
103 struct GNUNET_SCHEDULER_Task *timeout_task;
106 * The id of the operation which created this context
108 uint64_t operation_id;
111 * should the slave controller start the delegated controller?
116 * The state of this context
118 enum LCFContextState state;
123 uint32_t delegated_host_id;
128 uint32_t slave_host_id;
133 * Notification context to be used to notify when connection to the neighbour's
134 * controller is opened
136 struct NeighbourConnectNotification {
138 * DLL next for inclusion in neighbour's list of notification requests
140 struct NeighbourConnectNotification *next;
145 struct NeighbourConnectNotification *prev;
153 * The notification callback to call when we are connect to neighbour
155 GST_NeigbourConnectNotifyCallback cb;
158 * The closure for the above callback
165 * A connected controller which is not our child
169 * The controller handle
171 struct GNUNET_TESTBED_Controller *controller;
174 * Operation handle for opening a lateral connection to another controller.
175 * Will be NULL if the slave controller is started by this controller
177 struct GNUNET_TESTBED_Operation *conn_op;
180 * DLL head for the list of notification requests
182 struct NeighbourConnectNotification *nl_head;
185 * DLL tail for the list of notification requests
187 struct NeighbourConnectNotification *nl_tail;
190 * Task id for the task to call notifications from the notification list
192 struct GNUNET_SCHEDULER_Task * notify_task;
195 * How many references are present currently to this neighbour's connection
197 unsigned int reference_cnt;
200 * Is the conn_op inactivated?
202 unsigned int inactive;
205 * The id of the host this controller is running on
214 static struct Neighbour **neighbour_list;
217 * The size of the neighbour list
219 static unsigned int neighbour_list_size;
223 * Context information for establishing a link to neighbour (Used is
224 * GST_handle_link_controllers()
226 struct NeighbourConnectCtxt {
228 * DLL next for inclusion in the corresponding context list
230 struct NeighbourConnectCtxt *next;
235 struct NeighbourConnectCtxt *prev;
238 * The neighbour to whom connection should be made
243 * The client requesting the connection
245 struct GNUNET_SERVICE_Client *client;
248 * Task to be run upon timeout
250 struct GNUNET_SCHEDULER_Task *timeout_task;
253 * The notification handle associated with the neighbour's connection request
255 struct NeighbourConnectNotification *nh;
258 * The id of the link-controllers operation responsible for creating this
265 * DLL head for the list of neighbour connect contexts
267 struct NeighbourConnectCtxt *ncc_head;
270 * DLL tail for the list of neighbour connect contexts
272 struct NeighbourConnectCtxt *ncc_tail;
275 * A list of directly linked neighbours
277 struct Slave **GST_slave_list;
280 * The size of directly linked neighbours list
282 unsigned int GST_slave_list_size;
287 static struct Route **route_list;
292 static struct LCFContext *lcf_head;
295 * The tail for the LCF queue
297 static struct LCFContext *lcf_tail;
300 * The lcf_task handle
302 static struct GNUNET_SCHEDULER_Task * lcf_proc_task_id;
305 * The size of the route list
307 static unsigned int route_list_size;
311 * Adds a slave to the slave array
313 * @param slave the slave controller to add
316 slave_list_add(struct Slave *slave)
318 if (slave->host_id >= GST_slave_list_size)
319 GST_array_grow_large_enough(GST_slave_list,
322 GNUNET_assert(NULL == GST_slave_list[slave->host_id]);
323 GST_slave_list[slave->host_id] = slave;
328 * Clean up all forwarded operation overlay context matching the
329 * client given in @a cls.
331 * @param cls a `struct GNUNET_SERVICE_Client *` to match
333 * @param value the `struct RegisteredHostContext` to search for @a cls
334 * @return #GNUNET_OK (continue iterating)
337 drop_client_entries(void *cls,
338 const struct GNUNET_HashCode *key,
341 struct GNUNET_SERVICE_Client *client = cls;
342 struct RegisteredHostContext *rhc = value;
343 struct ForwardedOverlayConnectContext *focc;
344 struct ForwardedOverlayConnectContext *foccn;
346 for (focc = rhc->focc_dll_head; NULL != focc; focc = foccn)
349 if (focc->client == client)
350 GST_cleanup_focc(focc);
357 * Adds a route to the route list
359 * @param route the route to add
362 route_list_add(struct Route *route)
364 if (route->dest >= route_list_size)
365 GST_array_grow_large_enough(route_list, route_list_size, route->dest);
366 GNUNET_assert(NULL == route_list[route->dest]);
367 route_list[route->dest] = route;
372 * Add a neighbour to the neighbour list. Grows the neighbour list
375 * @param n the neighbour to add
378 neighbour_list_add(struct Neighbour *n)
380 if (n->host_id >= neighbour_list_size)
381 GST_array_grow_large_enough(neighbour_list, neighbour_list_size, n->host_id);
382 GNUNET_assert(NULL == neighbour_list[n->host_id]);
383 neighbour_list[n->host_id] = n;
388 * Cleans up the route list
391 GST_route_list_clear()
395 for (id = 0; id < route_list_size; id++)
396 if (NULL != route_list[id])
397 GNUNET_free(route_list[id]);
398 GNUNET_free_non_null(route_list);
404 * Iterator for freeing hash map entries in a slave's reghost_map
406 * @param cls handle to the slave
407 * @param key current key code
408 * @param value value in the hash map
409 * @return #GNUNET_YES if we should continue to iterate,
413 reghost_free_iterator(void *cls,
414 const struct GNUNET_HashCode *key,
417 struct Slave *slave = cls;
418 struct RegisteredHostContext *rhc = value;
419 struct ForwardedOverlayConnectContext *focc;
421 GNUNET_assert(GNUNET_YES ==
422 GNUNET_CONTAINER_multihashmap_remove(slave->reghost_map, key,
424 while (NULL != (focc = rhc->focc_dll_head))
425 GST_cleanup_focc(focc);
432 * Kill a #Slave object
434 * @param slave the #Slave object
437 kill_slave(struct Slave *slave)
439 struct HostRegistration *hr_entry;
441 while (NULL != (hr_entry = slave->hr_dll_head))
443 GNUNET_CONTAINER_DLL_remove(slave->hr_dll_head, slave->hr_dll_tail,
445 GNUNET_free(hr_entry);
447 if (NULL != slave->rhandle)
448 GNUNET_TESTBED_cancel_registration(slave->rhandle);
449 GNUNET_assert(GNUNET_SYSERR !=
450 GNUNET_CONTAINER_multihashmap_iterate(slave->reghost_map,
451 reghost_free_iterator,
453 GNUNET_CONTAINER_multihashmap_destroy(slave->reghost_map);
454 if (NULL != slave->controller)
455 GNUNET_TESTBED_controller_disconnect(slave->controller);
456 if (NULL != slave->controller_proc)
458 LOG_DEBUG("Stopping a slave\n");
459 GNUNET_TESTBED_controller_kill_(slave->controller_proc);
465 * Destroy a #Slave object
467 * @param slave the #Slave object
470 destroy_slave(struct Slave *slave)
472 if (NULL != slave->controller_proc)
474 GNUNET_TESTBED_controller_destroy_(slave->controller_proc);
475 LOG_DEBUG("Slave stopped\n");
477 GST_slave_list[slave->host_id] = NULL;
483 * Cleans up the slave list
486 GST_slave_list_clear()
491 for (id = 0; id < GST_slave_list_size; id++)
493 slave = GST_slave_list[id];
498 for (id = 0; id < GST_slave_list_size; id++)
500 slave = GST_slave_list[id];
503 destroy_slave(slave);
505 GNUNET_free_non_null(GST_slave_list);
506 GST_slave_list = NULL;
511 * Finds the route with directly connected host as destination through which
512 * the destination host can be reached
514 * @param host_id the id of the destination host
515 * @return the route with directly connected destination host; NULL if no route
519 GST_find_dest_route(uint32_t host_id)
523 if (route_list_size <= host_id)
525 while (NULL != (route = route_list[host_id]))
527 if (route->thru == GST_context->host_id)
529 host_id = route->thru;
536 * Function to send a failure reponse for controller link operation
538 * @param client the client to send the message to
539 * @param operation_id the operation ID of the controller link request
540 * @param cfg the configuration with which the delegated controller is started.
541 * Can be NULL if the delegated controller is not started but just
543 * @param emsg set to an error message explaining why the controller link
544 * failed. Setting this to NULL signifies success. !This should be
545 * NULL if cfg is set!
548 send_controller_link_response(struct GNUNET_SERVICE_Client *client,
549 uint64_t operation_id,
550 const struct GNUNET_CONFIGURATION_Handle *cfg,
553 struct GNUNET_MQ_Envelope *env;
554 struct GNUNET_TESTBED_ControllerLinkResponse *msg;
560 GNUNET_assert((NULL == cfg) || (NULL == emsg));
567 xconfig = GNUNET_TESTBED_compress_cfg_(cfg,
570 msize += xconfig_size;
573 msize += strlen(emsg);
574 env = GNUNET_MQ_msg_extra(msg,
576 GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS_RESULT);
578 msg->success = htons(GNUNET_YES);
579 msg->operation_id = GNUNET_htonll(operation_id);
580 msg->config_size = htons((uint16_t)config_size);
583 GNUNET_memcpy(&msg[1],
586 GNUNET_free(xconfig);
589 GNUNET_memcpy(&msg[1],
592 GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client),
598 * The Link Controller forwarding task
600 * @param cls the LCFContext
603 lcf_proc_task(void *cls);
607 * Completion callback for host registrations while forwarding Link Controller messages
609 * @param cls the LCFContext
610 * @param emsg the error message; NULL if host registration is successful
613 lcf_proc_cc(void *cls,
616 struct LCFContext *lcf = cls;
618 GNUNET_assert(NULL == lcf_proc_task_id);
623 goto registration_error;
624 lcf->state = DELEGATED_HOST_REGISTERED;
625 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task, lcf);
628 case DELEGATED_HOST_REGISTERED:
630 goto registration_error;
631 lcf->state = SLAVE_HOST_REGISTERED;
632 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task, lcf);
636 GNUNET_assert(0); /* Shouldn't reach here */
641 LOG(GNUNET_ERROR_TYPE_WARNING,
642 "Host registration failed with message: %s\n",
644 lcf->state = FINISHED;
645 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task,
651 * The Link Controller forwarding task
653 * @param cls the LCFContext
656 lcf_proc_task(void *cls);
660 * Task to free resources when forwarded link controllers has been timedout
662 * @param cls the LCFContext
665 lcf_forwarded_operation_timeout(void *cls)
667 struct LCFContext *lcf = cls;
669 lcf->timeout_task = NULL;
670 // GST_forwarded_operation_timeout (lcf->fopc, tc);
671 LOG(GNUNET_ERROR_TYPE_WARNING,
672 "A forwarded controller link operation has timed out\n");
673 send_controller_link_response(lcf->client,
676 "A forwarded controller link operation has timed out\n");
677 GNUNET_assert(NULL == lcf_proc_task_id);
678 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task,
684 * The Link Controller forwarding task
686 * @param cls the LCFContext
689 lcf_proc_task(void *cls)
691 struct LCFContext *lcf = cls;
693 lcf_proc_task_id = NULL;
698 GNUNET_TESTBED_is_host_registered_(GST_host_list
699 [lcf->delegated_host_id],
700 lcf->gateway->controller))
702 GST_queue_host_registration(lcf->gateway, lcf_proc_cc, lcf,
703 GST_host_list[lcf->delegated_host_id]);
707 lcf->state = DELEGATED_HOST_REGISTERED;
708 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task, lcf);
712 case DELEGATED_HOST_REGISTERED:
714 GNUNET_TESTBED_is_host_registered_(GST_host_list[lcf->slave_host_id],
715 lcf->gateway->controller))
717 GST_queue_host_registration(lcf->gateway, lcf_proc_cc, lcf,
718 GST_host_list[lcf->slave_host_id]);
722 lcf->state = SLAVE_HOST_REGISTERED;
723 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task, lcf);
727 case SLAVE_HOST_REGISTERED:
728 lcf->op = GNUNET_TESTBED_controller_link(lcf,
729 lcf->gateway->controller,
730 GST_host_list[lcf->delegated_host_id],
731 GST_host_list[lcf->slave_host_id],
732 lcf->is_subordinate);
734 GNUNET_SCHEDULER_add_delayed(GST_timeout,
735 &lcf_forwarded_operation_timeout,
737 lcf->state = FINISHED;
742 GNUNET_TESTBED_operation_done(lcf->op);
743 GNUNET_CONTAINER_DLL_remove(lcf_head,
747 if (NULL != lcf_head)
748 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task,
755 * Callback for event from slave controllers
758 * @param event information about the event
761 slave_event_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
763 struct LCFContext *lcf;
765 /* We currently only get here when working on LCFContexts */
766 GNUNET_assert(GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
768 GNUNET_assert(lcf->op == event->op);
769 GNUNET_TESTBED_operation_done(lcf->op);
771 GNUNET_assert(FINISHED == lcf->state);
772 GNUNET_assert(NULL != lcf->timeout_task);
773 GNUNET_SCHEDULER_cancel(lcf->timeout_task);
774 if (NULL == event->details.operation_finished.emsg)
775 send_controller_link_response(lcf->client, lcf->operation_id,
776 GNUNET_TESTBED_host_get_cfg_
777 (GST_host_list[lcf->delegated_host_id]),
780 send_controller_link_response(lcf->client, lcf->operation_id,
782 event->details.operation_finished.emsg);
783 GNUNET_assert(NULL == lcf_proc_task_id);
784 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task, lcf);
790 * Callback to signal successfull startup of the controller process
792 * @param cls the handle to the slave whose status is to be found here
793 * @param cfg the configuration with which the controller has been started;
794 * NULL if status is not #GNUNET_OK
795 * @param status #GNUNET_OK if the startup is successfull; #GNUNET_SYSERR if not,
796 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
799 slave_status_cb(void *cls,
800 const struct GNUNET_CONFIGURATION_Handle *cfg,
803 struct Slave *slave = cls;
804 struct LinkControllersContext *lcc;
807 if (GNUNET_SYSERR == status)
809 slave->controller_proc = NULL;
810 /* Stop all link controller forwarding tasks since we shutdown here anyway
811 and as these tasks they depend on the operation queues which are created
812 through GNUNET_TESTBED_controller_connect() and in kill_slave() we call
813 the destructor function GNUNET_TESTBED_controller_disconnect() */
816 destroy_slave(slave);
818 LOG(GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
819 GNUNET_SCHEDULER_shutdown(); /* We too shutdown */
823 GNUNET_TESTBED_controller_connect(GST_host_list[slave->host_id],
824 EVENT_MASK, &slave_event_cb,
826 if (NULL != slave->controller)
828 send_controller_link_response(lcc->client, lcc->operation_id, cfg, NULL);
832 send_controller_link_response(lcc->client, lcc->operation_id, NULL,
833 "Could not connect to delegated controller");
835 destroy_slave(slave);
842 if (NULL != lcc->client)
844 GNUNET_SERVICE_client_continue(lcc->client);
855 * Trigger notification task if there are notification requests currently
856 * waiting in the given neighbour. Also activates the neighbour connect operation
857 * if it was previously inactivated so that the connection to the neighbour can
860 * @param n the neighbour
863 trigger_notifications(struct Neighbour *n);
867 * Task to call the notification queued in the notifications list of the given
870 * @param cls the neighbour
873 neighbour_connect_notify_task(void *cls)
875 struct Neighbour *n = cls;
876 struct NeighbourConnectNotification *h;
878 GNUNET_assert(NULL != (h = n->nl_head));
879 GNUNET_assert(NULL != n->notify_task);
880 n->notify_task = NULL;
881 GNUNET_assert(NULL != n->controller);
882 GNUNET_CONTAINER_DLL_remove(n->nl_head, n->nl_tail, h);
883 trigger_notifications(n);
884 h->cb(h->cb_cls, n->controller);
890 * Trigger notification task if there are notification requests currently
891 * waiting in the given neighbour. Also activates the neighbour connect operation
892 * if it was previously inactivated so that the connection to the neighbour can
895 * @param n the neighbour
898 trigger_notifications(struct Neighbour *n)
900 GNUNET_assert(NULL != n->conn_op);
901 if (NULL == n->nl_head)
903 if (NULL == n->controller)
905 if (NULL != n->notify_task)
907 if (1 == n->inactive)
909 GNUNET_assert(0 == n->reference_cnt);
910 GNUNET_TESTBED_operation_activate_(n->conn_op);
915 GNUNET_SCHEDULER_add_now(&neighbour_connect_notify_task, n);
920 * Callback to be called when the neighbour connect operation is started. The
921 * connection to the neigbour is opened here and any pending notifications are
924 * @param cls the neighbour
927 opstart_neighbour_conn(void *cls)
929 struct Neighbour *n = cls;
931 GNUNET_assert(NULL != n->conn_op);
932 GNUNET_assert(NULL == n->controller);
933 LOG_DEBUG("Opening connection to controller on host %u\n", n->host_id);
934 n->controller = GNUNET_TESTBED_controller_connect(GST_host_list[n->host_id],
938 trigger_notifications(n);
943 * Callback to be called when the neighbour connect operation is released
945 * @param cls the neighbour
948 oprelease_neighbour_conn(void *cls)
950 struct Neighbour *n = cls;
952 GNUNET_assert(0 == n->reference_cnt);
953 GNUNET_assert(NULL == n->notify_task);
954 GNUNET_assert(NULL == n->nl_head);
955 if (NULL != n->controller)
957 LOG_DEBUG("Closing connection to controller on host %u\n", n->host_id);
958 GNUNET_TESTBED_controller_disconnect(n->controller);
959 n->controller = NULL;
967 * Try to open a connection to the given neigbour. If the connection is open
968 * already, then it is re-used. If not, the request is queued in the operation
969 * queues responsible for bounding the total number of file descriptors. The
970 * actual connection will happen when the operation queue marks the
971 * corresponding operation as active.
973 * @param n the neighbour to open a connection to
974 * @param cb the notification callback to call when the connection is opened
975 * @param cb_cls the closure for the above callback
977 struct NeighbourConnectNotification *
978 GST_neighbour_get_connection(struct Neighbour *n,
979 GST_NeigbourConnectNotifyCallback cb,
982 struct NeighbourConnectNotification *h;
984 GNUNET_assert(NULL != cb);
985 LOG_DEBUG("Attempting to get connection to controller on host %u\n",
987 h = GNUNET_new(struct NeighbourConnectNotification);
991 GNUNET_CONTAINER_DLL_insert_tail(n->nl_head, n->nl_tail, h);
992 if (NULL == n->conn_op)
994 GNUNET_assert(NULL == n->controller);
995 n->conn_op = GNUNET_TESTBED_operation_create_(n, &opstart_neighbour_conn,
996 &oprelease_neighbour_conn);
997 GNUNET_TESTBED_operation_queue_insert_(GST_opq_openfds, n->conn_op);
998 GNUNET_TESTBED_operation_begin_wait_(n->conn_op);
1001 trigger_notifications(n);
1007 * Cancel the request for opening a connection to the neighbour
1009 * @param h the notification handle
1012 GST_neighbour_get_connection_cancel(struct NeighbourConnectNotification *h)
1014 struct Neighbour *n;
1018 cleanup_task = (h == n->nl_head) ? GNUNET_YES : GNUNET_NO;
1019 GNUNET_CONTAINER_DLL_remove(n->nl_head, n->nl_tail, h);
1021 if (GNUNET_NO == cleanup_task)
1023 if (NULL == n->notify_task)
1025 GNUNET_assert(0 < n->reference_cnt);
1027 GNUNET_SCHEDULER_cancel(n->notify_task);
1028 n->notify_task = NULL;
1029 if (NULL == n->nl_head)
1031 if ((0 == n->reference_cnt) && (0 == n->inactive))
1034 GNUNET_TESTBED_operation_inactivate_(n->conn_op);
1038 trigger_notifications(n);
1043 * Release the connection to the neighbour. The actual connection will be
1044 * closed if connections to other neighbour are waiting (to maintain a bound on
1045 * the total number of connections that are open).
1047 * @param n the neighbour whose connection can be closed
1050 GST_neighbour_release_connection(struct Neighbour *n)
1052 GNUNET_assert(0 == n->inactive);
1053 GNUNET_assert(0 < n->reference_cnt);
1055 if (0 == n->reference_cnt)
1058 GNUNET_TESTBED_operation_inactivate_(n->conn_op);
1064 * Cleanup neighbour connect contexts
1066 * @param ncc the neighbour connect context to cleanup
1069 cleanup_ncc(struct NeighbourConnectCtxt *ncc)
1071 if (NULL != ncc->nh)
1072 GST_neighbour_get_connection_cancel(ncc->nh);
1073 if (NULL != ncc->timeout_task)
1074 GNUNET_SCHEDULER_cancel(ncc->timeout_task);
1075 GNUNET_CONTAINER_DLL_remove(ncc_head,
1083 * Cleans up the neighbour list
1086 GST_neighbour_list_clean()
1088 struct Neighbour *n;
1091 for (id = 0; id < neighbour_list_size; id++)
1093 if (NULL == (n = neighbour_list[id]))
1095 if (NULL != n->conn_op)
1096 GNUNET_TESTBED_operation_release_(n->conn_op);
1098 neighbour_list[id] = NULL;
1100 GNUNET_free_non_null(neighbour_list);
1105 * Get a neighbour from the neighbour list
1107 * @param id the index of the neighbour in the neighbour list
1108 * @return the Neighbour; NULL if the given index in invalid (index greater than
1109 * the list size or neighbour at that index is NULL)
1112 GST_get_neighbour(uint32_t id)
1114 if (neighbour_list_size <= id)
1116 return neighbour_list[id];
1121 * Function to cleanup the neighbour connect contexts
1126 while (NULL != ncc_head)
1127 cleanup_ncc(ncc_head);
1132 * Task to be run upon timeout while attempting to connect to the neighbour
1134 * @param cls the NeighbourConnectCtxt created in GST_handle_link_controllers()
1137 timeout_neighbour_connect(void *cls)
1139 struct NeighbourConnectCtxt *ncc = cls;
1141 ncc->timeout_task = NULL;
1142 send_controller_link_response(ncc->client,
1145 "Could not connect to delegated controller");
1151 * Callback called when a connection to the neighbour is made
1153 * @param cls the NeighbourConnectCtxt created in GST_handle_link_controllers()
1154 * @param c the handle the neighbour's controller
1157 neighbour_connect_cb(void *cls,
1158 struct GNUNET_TESTBED_Controller *c)
1160 struct NeighbourConnectCtxt *ncc = cls;
1162 GNUNET_SCHEDULER_cancel(ncc->timeout_task);
1163 ncc->timeout_task = NULL;
1165 GST_neighbour_release_connection(ncc->n);
1166 send_controller_link_response(ncc->client,
1175 * Function to create a neigbour and add it into the neighbour list
1177 * @param host the host of the neighbour
1180 GST_create_neighbour(struct GNUNET_TESTBED_Host *host)
1182 struct Neighbour *n;
1184 n = GNUNET_new(struct Neighbour);
1185 n->host_id = GNUNET_TESTBED_host_get_id_(host);
1186 neighbour_list_add(n); /* just add; connect on-demand */
1192 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1194 * @param cls identification of the client
1195 * @param msg the actual message
1198 handle_link_controllers(void *cls,
1199 const struct GNUNET_TESTBED_ControllerLinkRequest *msg)
1201 struct GNUNET_SERVICE_Client *client = cls;
1202 struct LCFContext *lcf;
1203 struct Route *route;
1204 struct Route *new_route;
1206 uint32_t delegated_host_id;
1207 uint32_t slave_host_id;
1209 if (NULL == GST_context)
1212 GNUNET_SERVICE_client_drop(client);
1215 delegated_host_id = ntohl(msg->delegated_host_id);
1216 if (delegated_host_id == GST_context->host_id)
1219 LOG(GNUNET_ERROR_TYPE_WARNING,
1220 "Trying to link ourselves\n");
1221 GNUNET_SERVICE_client_drop(client);
1224 if ((delegated_host_id >= GST_host_list_size) ||
1225 (NULL == GST_host_list[delegated_host_id]))
1227 LOG(GNUNET_ERROR_TYPE_WARNING,
1228 "Delegated host %u not registered with us\n",
1230 GNUNET_SERVICE_client_drop(client);
1233 slave_host_id = ntohl(msg->slave_host_id);
1234 if ((slave_host_id >= GST_host_list_size) ||
1235 (NULL == GST_host_list[slave_host_id]))
1237 LOG(GNUNET_ERROR_TYPE_WARNING,
1238 "Slave host %u not registered with us\n",
1240 GNUNET_SERVICE_client_drop(client);
1243 if (slave_host_id == delegated_host_id)
1245 LOG(GNUNET_ERROR_TYPE_WARNING,
1246 "Slave and delegated host are same\n");
1247 GNUNET_SERVICE_client_drop(client);
1250 op_id = GNUNET_ntohll(msg->operation_id);
1251 if (slave_host_id == GST_context->host_id) /* Link from us */
1253 struct Slave *slave;
1254 struct LinkControllersContext *lcc;
1256 if (1 != msg->is_subordinate)
1258 struct Neighbour *n;
1259 struct NeighbourConnectCtxt *ncc;
1261 if ((delegated_host_id < neighbour_list_size) &&
1262 (NULL != neighbour_list[delegated_host_id]))
1265 GNUNET_SERVICE_client_drop(client);
1268 LOG_DEBUG("Received request to establish a link to host %u\n",
1270 n = GST_create_neighbour(GST_host_list[delegated_host_id]);
1271 ncc = GNUNET_new(struct NeighbourConnectCtxt);
1274 ncc->client = client;
1275 ncc->nh = GST_neighbour_get_connection(n,
1276 &neighbour_connect_cb,
1279 = GNUNET_SCHEDULER_add_delayed(GST_timeout,
1280 &timeout_neighbour_connect,
1282 GNUNET_CONTAINER_DLL_insert_tail(ncc_head,
1285 GNUNET_SERVICE_client_continue(client);
1288 if ((delegated_host_id < GST_slave_list_size) &&
1289 (NULL != GST_slave_list[delegated_host_id]))
1292 GNUNET_SERVICE_client_drop(client);
1295 LOG_DEBUG("Received request to start and establish a link to host %u\n",
1297 slave = GNUNET_new(struct Slave);
1298 slave->host_id = delegated_host_id;
1299 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create(100,
1301 slave_list_add(slave);
1302 lcc = GNUNET_new(struct LinkControllersContext);
1303 lcc->operation_id = op_id;
1304 lcc->client = client;
1306 slave->controller_proc
1307 = GNUNET_TESTBED_controller_start(GST_context->master_ip,
1308 GST_host_list[slave->host_id],
1311 new_route = GNUNET_new(struct Route);
1312 new_route->dest = delegated_host_id;
1313 new_route->thru = GST_context->host_id;
1314 route_list_add(new_route);
1318 /* Route the request */
1319 if (slave_host_id >= route_list_size)
1321 LOG(GNUNET_ERROR_TYPE_WARNING,
1322 "No route towards slave host");
1323 GNUNET_SERVICE_client_drop(client);
1326 lcf = GNUNET_new(struct LCFContext);
1327 lcf->delegated_host_id = delegated_host_id;
1328 lcf->slave_host_id = slave_host_id;
1329 route = GST_find_dest_route(slave_host_id);
1330 GNUNET_assert(NULL != route); /* because we add routes carefully */
1331 GNUNET_assert(route->dest < GST_slave_list_size);
1332 GNUNET_assert(NULL != GST_slave_list[route->dest]);
1333 lcf->is_subordinate = msg->is_subordinate;
1335 lcf->operation_id = op_id;
1336 lcf->gateway = GST_slave_list[route->dest];
1337 lcf->client = client;
1338 if (NULL == lcf_head)
1340 GNUNET_assert(NULL == lcf_proc_task_id);
1341 GNUNET_CONTAINER_DLL_insert_tail(lcf_head,
1344 lcf_proc_task_id = GNUNET_SCHEDULER_add_now(&lcf_proc_task,
1349 GNUNET_CONTAINER_DLL_insert_tail(lcf_head,
1353 /* FIXME: Adding a new route should happen after the controllers are linked
1355 if (1 != msg->is_subordinate)
1357 GNUNET_SERVICE_client_continue(client);
1360 if ((delegated_host_id < route_list_size) &&
1361 (NULL != route_list[delegated_host_id]))
1363 GNUNET_break_op(0); /* Are you trying to link delegated host twice
1364 * with is subordinate flag set to GNUNET_YES? */
1365 GNUNET_SERVICE_client_drop(client);
1368 new_route = GNUNET_new(struct Route);
1369 new_route->dest = delegated_host_id;
1370 new_route->thru = route->dest;
1371 route_list_add(new_route);
1372 GNUNET_SERVICE_client_continue(client);
1377 * Clean up @a client handle if we stored any via #handle_link_controllers(),
1378 * the given client disconnected.
1380 * @param client the client that is history
1383 GST_link_notify_disconnect(struct GNUNET_SERVICE_Client *client)
1385 struct NeighbourConnectCtxt *ncc;
1386 struct NeighbourConnectCtxt *nccn;
1387 struct LCFContext *lcf;
1388 struct LCFContext *lcfn;
1390 for (ncc = ncc_head; NULL != ncc; ncc = nccn)
1393 if (ncc->client == client)
1396 for (unsigned int i = 0; i < GST_slave_list_size; i++)
1398 struct Slave *slave = GST_slave_list[i];
1399 struct LinkControllersContext *lcc;
1403 GNUNET_CONTAINER_multihashmap_iterate(slave->reghost_map,
1404 &drop_client_entries,
1409 if (lcc->client == client)
1415 for (lcf = lcf_head; NULL != lcf; lcf = lcfn)
1418 if ((NULL != lcf) &&
1419 (client == lcf->client))
1421 if (NULL != lcf->op)
1422 GNUNET_TESTBED_operation_done(lcf->op);
1423 GNUNET_CONTAINER_DLL_remove(lcf_head,
1433 * Cleans up the queue used for forwarding link controllers requests
1438 struct LCFContext *lcf;
1440 if (NULL != lcf_head)
1442 if (NULL != lcf_proc_task_id)
1444 GNUNET_SCHEDULER_cancel(lcf_proc_task_id);
1445 lcf_proc_task_id = NULL;
1448 GNUNET_assert(NULL == lcf_proc_task_id);
1449 for (lcf = lcf_head; NULL != lcf; lcf = lcf_head)
1451 if (NULL != lcf->op)
1452 GNUNET_TESTBED_operation_done(lcf->op);
1453 if (NULL != lcf->timeout_task)
1454 GNUNET_SCHEDULER_cancel(lcf->timeout_task);
1455 GNUNET_CONTAINER_DLL_remove(lcf_head,