2 This file is part of GNUnet.
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/gnunet-service-testbed_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
51 * The Context has been initialized; Nothing has been done on it
56 * Delegated host has been registered at the forwarding controller
58 DELEGATED_HOST_REGISTERED,
61 * The slave host has been registred at the forwarding controller
63 SLAVE_HOST_REGISTERED,
66 * The context has been finished (may have error)
73 * Link controllers request forwarding context
78 * The type of this data structure. Set this to CLOSURE_TYPE_LCF
80 enum ClosureType type;
83 * The gateway which will pass the link message to delegated host
85 struct Slave *gateway;
88 * The client which has asked to perform this operation
90 struct GNUNET_SERVER_Client *client;
93 * Handle for operations which are forwarded while linking controllers
95 struct GNUNET_TESTBED_Operation *op;
98 * The configuration which has to be either used as a template while starting
99 * the delegated controller or for connecting to the delegated controller
101 struct GNUNET_CONFIGURATION_Handle *cfg;
106 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
109 * The id of the operation which created this context
111 uint64_t operation_id;
114 * should the slave controller start the delegated controller?
119 * The state of this context
121 enum LCFContextState state;
126 uint32_t delegated_host_id;
131 uint32_t slave_host_id;
137 * Structure of a queue entry in LCFContext request queue
139 struct LCFContextQueue
144 struct LCFContext *lcf;
149 struct LCFContextQueue *next;
154 struct LCFContextQueue *prev;
157 struct NeighbourConnectNotification
159 struct NeighbourConnectNotification *next;
160 struct NeighbourConnectNotification *prev;
162 GST_NeigbourConnectNotifyCallback cb;
167 * A connected controller which is not our child
172 * The controller handle
174 struct GNUNET_TESTBED_Controller *controller;
177 * Operation handle for opening a lateral connection to another controller.
178 * Will be NULL if the slave controller is started by this controller
180 struct GNUNET_TESTBED_Operation *conn_op;
182 struct NeighbourConnectNotification *nl_head;
184 struct NeighbourConnectNotification *nl_tail;
186 GNUNET_SCHEDULER_TaskIdentifier notify_task;
188 unsigned int reference_cnt;
191 * The id of the host this controller is running on
198 static struct Neighbour **neighbour_list;
199 static unsigned int neighbour_list_size;
201 struct NeighbourConnectCtxt
203 struct NeighbourConnectCtxt *next;
204 struct NeighbourConnectCtxt *prev;
206 struct GNUNET_SERVER_Client *client;
207 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
208 struct NeighbourConnectNotification *nh;
212 struct NeighbourConnectCtxt *ncc_head;
213 struct NeighbourConnectCtxt *ncc_tail;
216 * A list of directly linked neighbours
218 struct Slave **GST_slave_list;
221 * The size of directly linked neighbours list
223 unsigned int GST_slave_list_size;
228 static struct Route **route_list;
231 * The head for the LCF queue
233 static struct LCFContextQueue *lcfq_head;
236 * The tail for the LCF queue
238 static struct LCFContextQueue *lcfq_tail;
241 * The lcf_task handle
243 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
246 * The size of the route list
248 static unsigned int route_list_size;
252 * Adds a slave to the slave array
254 * @param slave the slave controller to add
257 slave_list_add (struct Slave *slave)
259 if (slave->host_id >= GST_slave_list_size)
260 GST_array_grow_large_enough (GST_slave_list, GST_slave_list_size,
262 GNUNET_assert (NULL == GST_slave_list[slave->host_id]);
263 GST_slave_list[slave->host_id] = slave;
268 * Adds a route to the route list
270 * @param route the route to add
273 route_list_add (struct Route *route)
275 if (route->dest >= route_list_size)
276 GST_array_grow_large_enough (route_list, route_list_size, route->dest);
277 GNUNET_assert (NULL == route_list[route->dest]);
278 route_list[route->dest] = route;
282 neighbour_list_add (struct Neighbour *n)
284 if (n->host_id >= neighbour_list_size)
285 GST_array_grow_large_enough (neighbour_list, neighbour_list_size, n->host_id);
286 GNUNET_assert (NULL == neighbour_list[n->host_id]);
287 neighbour_list[n->host_id] = n;
292 * Cleans up the route list
295 GST_route_list_clear ()
299 for (id = 0; id < route_list_size; id++)
300 if (NULL != route_list[id])
301 GNUNET_free (route_list[id]);
302 GNUNET_free_non_null (route_list);
308 * Iterator for freeing hash map entries in a slave's reghost_map
310 * @param cls handle to the slave
311 * @param key current key code
312 * @param value value in the hash map
313 * @return GNUNET_YES if we should continue to
318 reghost_free_iterator (void *cls, const struct GNUNET_HashCode *key,
321 struct Slave *slave = cls;
322 struct RegisteredHostContext *rhc = value;
323 struct ForwardedOverlayConnectContext *focc;
325 GNUNET_assert (GNUNET_YES ==
326 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map, key,
328 while (NULL != (focc = rhc->focc_dll_head))
330 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
331 GST_cleanup_focc (focc);
333 if (NULL != rhc->sub_op)
334 GNUNET_TESTBED_operation_done (rhc->sub_op);
335 if (NULL != rhc->client)
336 GNUNET_SERVER_client_drop (rhc->client);
343 * Cleans up the slave list
346 GST_slave_list_clear ()
349 struct HostRegistration *hr_entry;
351 for (id = 0; id < GST_slave_list_size; id++)
352 if (NULL != GST_slave_list[id])
354 while (NULL != (hr_entry = GST_slave_list[id]->hr_dll_head))
356 GNUNET_CONTAINER_DLL_remove (GST_slave_list[id]->hr_dll_head,
357 GST_slave_list[id]->hr_dll_tail, hr_entry);
358 GNUNET_free (hr_entry);
360 if (NULL != GST_slave_list[id]->rhandle)
361 GNUNET_TESTBED_cancel_registration (GST_slave_list[id]->rhandle);
363 GNUNET_CONTAINER_multihashmap_iterate (GST_slave_list
365 reghost_free_iterator,
367 GNUNET_CONTAINER_multihashmap_destroy (GST_slave_list[id]->reghost_map);
368 if (NULL != GST_slave_list[id]->controller)
369 GNUNET_TESTBED_controller_disconnect (GST_slave_list[id]->controller);
370 if (NULL != GST_slave_list[id]->controller_proc)
371 GNUNET_TESTBED_controller_stop (GST_slave_list[id]->controller_proc);
372 GNUNET_free (GST_slave_list[id]);
374 GNUNET_free_non_null (GST_slave_list);
375 GST_slave_list = NULL;
380 * Finds the route with directly connected host as destination through which
381 * the destination host can be reached
383 * @param host_id the id of the destination host
384 * @return the route with directly connected destination host; NULL if no route
388 GST_find_dest_route (uint32_t host_id)
392 if (route_list_size <= host_id)
394 while (NULL != (route = route_list[host_id]))
396 if (route->thru == GST_context->host_id)
398 host_id = route->thru;
405 * Function to send a failure reponse for controller link operation
407 * @param client the client to send the message to
408 * @param operation_id the operation ID of the controller link request
409 * @param cfg the configuration with which the delegated controller is started.
410 * Can be NULL if the delegated controller is not started but just
412 * @param emsg set to an error message explaining why the controller link
413 * failed. Setting this to NULL signifies success. !This should be
414 * NULL if cfg is set!
417 send_controller_link_response (struct GNUNET_SERVER_Client *client,
418 uint64_t operation_id,
419 const struct GNUNET_CONFIGURATION_Handle
423 struct GNUNET_TESTBED_ControllerLinkResponse *msg;
429 GNUNET_assert ((NULL == cfg) || (NULL == emsg));
433 msize = sizeof (struct GNUNET_TESTBED_ControllerLinkResponse);
436 xconfig = GNUNET_TESTBED_compress_cfg_ (cfg,
439 msize += xconfig_size;
442 msize += strlen (emsg);
443 msg = GNUNET_malloc (msize);
444 msg->header.type = htons
445 (GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS_RESULT);
446 msg->header.size = htons (msize);
448 msg->success = htons (GNUNET_YES);
449 msg->operation_id = GNUNET_htonll (operation_id);
450 msg->config_size = htons ((uint16_t) config_size);
452 memcpy (&msg[1], xconfig, xconfig_size);
454 memcpy (&msg[1], emsg, strlen (emsg));
455 GST_queue_message (client, &msg->header);
460 * The Link Controller forwarding task
462 * @param cls the LCFContext
463 * @param tc the Task context from scheduler
466 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
470 * Completion callback for host registrations while forwarding Link Controller messages
472 * @param cls the LCFContext
473 * @param emsg the error message; NULL if host registration is successful
476 lcf_proc_cc (void *cls, const char *emsg)
478 struct LCFContext *lcf = cls;
480 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
485 goto registration_error;
486 lcf->state = DELEGATED_HOST_REGISTERED;
487 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
489 case DELEGATED_HOST_REGISTERED:
491 goto registration_error;
492 lcf->state = SLAVE_HOST_REGISTERED;
493 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
496 GNUNET_assert (0); /* Shouldn't reach here */
501 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
503 lcf->state = FINISHED;
504 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
509 * The Link Controller forwarding task
511 * @param cls the LCFContext
512 * @param tc the Task context from scheduler
515 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
519 * Task to free resources when forwarded link controllers has been timedout
521 * @param cls the LCFContext
522 * @param tc the task context from scheduler
525 lcf_forwarded_operation_timeout (void *cls,
526 const struct GNUNET_SCHEDULER_TaskContext *tc)
528 struct LCFContext *lcf = cls;
530 lcf->timeout_task = GNUNET_SCHEDULER_NO_TASK;
531 // GST_forwarded_operation_timeout (lcf->fopc, tc);
532 LOG (GNUNET_ERROR_TYPE_WARNING,
533 "A forwarded controller link operation has timed out\n");
534 send_controller_link_response (lcf->client, lcf->operation_id, NULL,
535 "A forwarded controller link operation has "
537 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
538 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
543 * The Link Controller forwarding task
545 * @param cls the LCFContext
546 * @param tc the Task context from scheduler
549 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
551 struct LCFContext *lcf = cls;
552 struct LCFContextQueue *lcfq;
554 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
559 GNUNET_TESTBED_is_host_registered_ (GST_host_list
560 [lcf->delegated_host_id],
561 lcf->gateway->controller))
563 GST_queue_host_registration (lcf->gateway, lcf_proc_cc, lcf,
564 GST_host_list[lcf->delegated_host_id]);
568 lcf->state = DELEGATED_HOST_REGISTERED;
569 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
572 case DELEGATED_HOST_REGISTERED:
574 GNUNET_TESTBED_is_host_registered_ (GST_host_list[lcf->slave_host_id],
575 lcf->gateway->controller))
577 GST_queue_host_registration (lcf->gateway, lcf_proc_cc, lcf,
578 GST_host_list[lcf->slave_host_id]);
582 lcf->state = SLAVE_HOST_REGISTERED;
583 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
586 case SLAVE_HOST_REGISTERED:
587 lcf->op = GNUNET_TESTBED_controller_link (lcf,
588 lcf->gateway->controller,
589 GST_host_list[lcf->delegated_host_id],
590 GST_host_list[lcf->slave_host_id],
592 lcf->is_subordinate);
594 GNUNET_SCHEDULER_add_delayed (GST_timeout, &lcf_forwarded_operation_timeout,
596 lcf->state = FINISHED;
600 GNUNET_assert (lcfq->lcf == lcf);
601 GNUNET_assert (NULL != lcf->cfg);
602 GNUNET_CONFIGURATION_destroy (lcf->cfg);
603 GNUNET_SERVER_client_drop (lcf->client);
604 GNUNET_TESTBED_operation_done (lcf->op);
606 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
608 if (NULL != lcfq_head)
610 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
616 * Callback for event from slave controllers
619 * @param event information about the event
622 slave_event_callback (void *cls,
623 const struct GNUNET_TESTBED_EventInformation *event)
625 struct RegisteredHostContext *rhc;
626 struct LCFContext *lcf;
627 struct GNUNET_CONFIGURATION_Handle *cfg;
628 struct GNUNET_TESTBED_Operation *old_op;
630 /* We currently only get here when working on RegisteredHostContexts and
632 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
634 if (CLOSURE_TYPE_RHC == rhc->type)
636 GNUNET_assert (rhc->sub_op == event->op);
640 cfg = event->details.operation_finished.generic;
641 old_op = rhc->sub_op;
642 rhc->state = RHC_LINK;
644 GNUNET_TESTBED_controller_link (rhc, rhc->gateway->controller,
645 rhc->reg_host, rhc->host, cfg,
647 GNUNET_TESTBED_operation_done (old_op);
650 LOG_DEBUG ("OL: Linking controllers successfull\n");
651 GNUNET_TESTBED_operation_done (rhc->sub_op);
653 rhc->state = RHC_OL_CONNECT;
654 GST_process_next_focc (rhc);
662 if (CLOSURE_TYPE_LCF == lcf->type)
664 GNUNET_assert (lcf->op == event->op);
665 GNUNET_assert (FINISHED == lcf->state);
666 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != lcf->timeout_task);
667 GNUNET_SCHEDULER_cancel (lcf->timeout_task);
668 if (NULL == event->details.operation_finished.emsg)
669 send_controller_link_response (lcf->client, lcf->operation_id,
670 GNUNET_TESTBED_host_get_cfg_
671 (GST_host_list[lcf->delegated_host_id]),
674 send_controller_link_response (lcf->client, lcf->operation_id,
676 event->details.operation_finished.emsg);
677 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
678 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
685 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
689 * Callback to signal successfull startup of the controller process
691 * @param cls the handle to the slave whose status is to be found here
692 * @param cfg the configuration with which the controller has been started;
693 * NULL if status is not GNUNET_OK
694 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
695 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
698 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
701 struct Slave *slave = cls;
702 struct LinkControllersContext *lcc;
705 if (GNUNET_SYSERR == status)
707 slave->controller_proc = NULL;
708 GST_slave_list[slave->host_id] = NULL;
711 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
712 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
716 GNUNET_TESTBED_controller_connect (GST_host_list[slave->host_id],
717 EVENT_MASK, &slave_event_callback,
719 if (NULL != slave->controller)
721 send_controller_link_response (lcc->client, lcc->operation_id, cfg, NULL);
725 send_controller_link_response (lcc->client, lcc->operation_id, NULL,
726 "Could not connect to delegated controller");
727 GNUNET_TESTBED_controller_stop (slave->controller_proc);
728 GST_slave_list[slave->host_id] = NULL;
736 if (NULL != lcc->client)
738 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
739 GNUNET_SERVER_client_drop (lcc->client);
749 neighbour_connect_notify_task (void *cls,
750 const struct GNUNET_SCHEDULER_TaskContext *tc);
753 trigger_notifications (struct Neighbour *n)
755 GNUNET_assert (NULL != n->conn_op);
756 if (NULL == n->nl_head)
758 if (NULL == n->controller)
760 if (GNUNET_SCHEDULER_NO_TASK != n->notify_task)
763 GNUNET_SCHEDULER_add_now (&neighbour_connect_notify_task, n->nl_head);
767 neighbour_connect_notify_task (void *cls,
768 const struct GNUNET_SCHEDULER_TaskContext *tc)
770 struct NeighbourConnectNotification *h = cls;
774 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->notify_task);
775 n->notify_task = GNUNET_SCHEDULER_NO_TASK;
776 GNUNET_assert (NULL != n->controller);
777 GNUNET_CONTAINER_DLL_remove (n->nl_head, n->nl_tail, h);
778 trigger_notifications (n);
779 if ((0 == n->reference_cnt) && (1 == n->inactive))
781 GNUNET_TESTBED_operation_activate_ (n->conn_op);
785 h->cb (h->cb_cls, n->controller);
789 opstart_neighbour_conn (void *cls)
791 struct Neighbour *n = cls;
793 GNUNET_assert (NULL != n->conn_op);
794 GNUNET_assert (NULL == n->controller);
795 LOG_DEBUG ("Opening connection to controller on host %u\n", n->host_id);
796 n->controller = GNUNET_TESTBED_controller_connect (GST_host_list[n->host_id],
798 &slave_event_callback,
800 trigger_notifications (n);
804 oprelease_neighbour_conn (void *cls)
806 struct Neighbour *n = cls;
808 GNUNET_assert (0 == n->reference_cnt);
809 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->notify_task);
810 GNUNET_assert (NULL == n->nl_head);
811 LOG_DEBUG ("Closing connection to controller on host %u\n", n->host_id);
812 GNUNET_TESTBED_controller_disconnect (n->controller);
813 n->controller = NULL;
817 struct NeighbourConnectNotification *
818 GST_neighbour_get_connection (struct Neighbour *n,
819 GST_NeigbourConnectNotifyCallback cb,
822 struct NeighbourConnectNotification *h;
824 GNUNET_assert (NULL != cb);
825 LOG_DEBUG ("Attempting to get connection to controller on host %u\n",
827 h = GNUNET_malloc (sizeof (struct NeighbourConnectNotification));
831 GNUNET_CONTAINER_DLL_insert_tail (n->nl_head, n->nl_tail, h);
832 if (NULL == n->conn_op)
834 GNUNET_assert (NULL == n->controller);
835 n->conn_op = GNUNET_TESTBED_operation_create_ (n, &opstart_neighbour_conn,
836 &oprelease_neighbour_conn);
837 GNUNET_TESTBED_operation_queue_insert_ (GST_opq_openfds, n->conn_op);
838 GNUNET_TESTBED_operation_begin_wait_ (n->conn_op);
841 trigger_notifications (n);
846 GST_neighbour_get_connection_cancel (struct NeighbourConnectNotification *h)
851 if ((h == n->nl_head) && (GNUNET_SCHEDULER_NO_TASK != n->notify_task))
853 GNUNET_SCHEDULER_cancel (n->notify_task);
854 n->notify_task = GNUNET_SCHEDULER_NO_TASK;
856 GNUNET_CONTAINER_DLL_remove (n->nl_head, n->nl_tail, h);
861 GST_neighbour_release_connection (struct Neighbour *n)
863 GNUNET_assert (0 == n->inactive);
864 GNUNET_assert (0 < n->reference_cnt);
866 if (0 == n->reference_cnt)
868 GNUNET_TESTBED_operation_inactivate_ (n->conn_op);
874 cleanup_ncc (struct NeighbourConnectCtxt *ncc)
877 GST_neighbour_get_connection_cancel (ncc->nh);
878 if (GNUNET_SCHEDULER_NO_TASK != ncc->timeout_task)
879 GNUNET_SCHEDULER_cancel (ncc->timeout_task);
880 GNUNET_SERVER_client_drop (ncc->client);
881 GNUNET_CONTAINER_DLL_remove (ncc_head, ncc_tail, ncc);
886 GST_neighbour_list_clean()
891 for (id = 0; id < neighbour_list_size; id++)
893 if (NULL == (n = neighbour_list[id]))
895 if (NULL != n->conn_op)
896 GNUNET_TESTBED_operation_release_ (n->conn_op);
898 neighbour_list[id] = NULL;
900 GNUNET_free_non_null (neighbour_list);
904 GST_get_neighbour (uint32_t id)
906 if (neighbour_list_size <= id)
909 return neighbour_list[id];
915 while (NULL != ncc_head)
916 cleanup_ncc (ncc_head);
920 timeout_neighbour_connect (void *cls,
921 const struct GNUNET_SCHEDULER_TaskContext *tc)
923 struct NeighbourConnectCtxt *ncc = cls;
925 ncc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
926 send_controller_link_response (ncc->client, ncc->op_id, NULL,
927 "Could not connect to delegated controller");
932 neighbour_connect_cb (void *cls, struct GNUNET_TESTBED_Controller *c)
934 struct NeighbourConnectCtxt *ncc = cls;
936 GNUNET_SCHEDULER_cancel (ncc->timeout_task);
937 ncc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
939 GST_neighbour_release_connection (ncc->n);
940 send_controller_link_response (ncc->client, ncc->op_id, NULL, NULL);
945 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
948 * @param client identification of the client
949 * @param message the actual message
952 GST_handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
953 const struct GNUNET_MessageHeader *message)
955 const struct GNUNET_TESTBED_ControllerLinkRequest *msg;
956 struct GNUNET_CONFIGURATION_Handle *cfg;
957 struct LCFContextQueue *lcfq;
959 struct Route *new_route;
961 uint32_t delegated_host_id;
962 uint32_t slave_host_id;
965 if (NULL == GST_context)
968 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
971 msize = ntohs (message->size);
972 if (sizeof (struct GNUNET_TESTBED_ControllerLinkRequest) >= msize)
975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
978 msg = (const struct GNUNET_TESTBED_ControllerLinkRequest *) message;
979 delegated_host_id = ntohl (msg->delegated_host_id);
980 if (delegated_host_id == GST_context->host_id)
983 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
987 if ((delegated_host_id >= GST_host_list_size) ||
988 (NULL == GST_host_list[delegated_host_id]))
990 LOG (GNUNET_ERROR_TYPE_WARNING,
991 "Delegated host %u not registered with us\n", delegated_host_id);
992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
995 slave_host_id = ntohl (msg->slave_host_id);
996 if ((slave_host_id >= GST_host_list_size) ||
997 (NULL == GST_host_list[slave_host_id]))
999 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1004 if (slave_host_id == delegated_host_id)
1006 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1010 cfg = GNUNET_TESTBED_extract_config_ (message); /* destroy cfg here or in lcfcontext */
1013 GNUNET_break (0); /* Configuration parsing error */
1014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1017 op_id = GNUNET_ntohll (msg->operation_id);
1018 if (slave_host_id == GST_context->host_id) /* Link from us */
1020 struct Slave *slave;
1021 struct LinkControllersContext *lcc;
1024 if (1 != msg->is_subordinate)
1026 struct Neighbour *n;
1027 struct NeighbourConnectCtxt *ncc;
1029 if ((delegated_host_id < neighbour_list_size) &&
1030 (NULL != neighbour_list[delegated_host_id]))
1033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1036 LOG_DEBUG ("Received request to establish a link to host %u\n",
1038 n = GNUNET_malloc (sizeof (struct Neighbour));
1039 n->host_id = delegated_host_id;
1040 neighbour_list_add (n); /* just add; connect on-demand */
1041 ncc = GNUNET_malloc (sizeof (struct NeighbourConnectCtxt));
1044 ncc->client = client;
1045 GNUNET_SERVER_client_keep (client);
1046 ncc->nh = GST_neighbour_get_connection (n, neighbour_connect_cb, ncc);
1047 ncc->timeout_task = GNUNET_SCHEDULER_add_delayed (GST_timeout,
1048 &timeout_neighbour_connect,
1050 GNUNET_CONTAINER_DLL_insert_tail (ncc_head, ncc_tail, ncc);
1051 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1054 if ((delegated_host_id < GST_slave_list_size) &&
1055 (NULL != GST_slave_list[delegated_host_id]))
1058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1061 LOG_DEBUG ("Received request to start and establish a link to host %u\n",
1063 slave = GNUNET_malloc (sizeof (struct Slave));
1064 slave->host_id = delegated_host_id;
1065 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
1066 slave_list_add (slave);
1067 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1068 lcc->operation_id = op_id;
1069 GNUNET_SERVER_client_keep (client);
1070 lcc->client = client;
1072 slave->controller_proc =
1073 GNUNET_TESTBED_controller_start (GST_context->master_ip,
1074 GST_host_list[slave->host_id],
1075 &slave_status_callback, slave);
1076 GNUNET_CONFIGURATION_destroy (cfg);
1077 new_route = GNUNET_malloc (sizeof (struct Route));
1078 new_route->dest = delegated_host_id;
1079 new_route->thru = GST_context->host_id;
1080 route_list_add (new_route);
1084 /* Route the request */
1085 if (slave_host_id >= route_list_size)
1087 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1088 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1091 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1092 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1093 lcfq->lcf->type = CLOSURE_TYPE_LCF;
1094 lcfq->lcf->delegated_host_id = delegated_host_id;
1095 lcfq->lcf->slave_host_id = slave_host_id;
1096 route = GST_find_dest_route (slave_host_id);
1097 GNUNET_assert (NULL != route); /* because we add routes carefully */
1098 GNUNET_assert (route->dest < GST_slave_list_size);
1099 GNUNET_assert (NULL != GST_slave_list[route->dest]);
1100 lcfq->lcf->cfg = cfg;
1101 lcfq->lcf->is_subordinate = msg->is_subordinate;
1102 lcfq->lcf->state = INIT;
1103 lcfq->lcf->operation_id = op_id;
1104 lcfq->lcf->gateway = GST_slave_list[route->dest];
1105 GNUNET_SERVER_client_keep (client);
1106 lcfq->lcf->client = client;
1107 if (NULL == lcfq_head)
1109 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1110 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1111 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1114 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1115 /* FIXME: Adding a new route should happen after the controllers are linked
1117 if (1 != msg->is_subordinate)
1119 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1122 if ((delegated_host_id < route_list_size) &&
1123 (NULL != route_list[delegated_host_id]))
1125 GNUNET_break_op (0); /* Are you trying to link delegated host twice
1126 * with is subordinate flag set to GNUNET_YES? */
1127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1130 new_route = GNUNET_malloc (sizeof (struct Route));
1131 new_route->dest = delegated_host_id;
1132 new_route->thru = route->dest;
1133 route_list_add (new_route);
1134 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1139 * Cleans up the queue used for forwarding link controllers requests
1144 struct LCFContextQueue *lcfq;
1146 if (NULL != lcfq_head)
1148 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1150 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1151 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1154 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1155 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1157 GNUNET_SERVER_client_drop (lcfq->lcf->client);
1158 GNUNET_assert (NULL != lcfq->lcf->cfg);
1159 GNUNET_CONFIGURATION_destroy (lcfq->lcf->cfg);
1160 GNUNET_free (lcfq->lcf);
1161 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);