aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-service-testbed.c.new
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-10-17 13:29:52 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-10-17 13:29:52 +0000
commitae6912492c5eca8119f118471e4d342f86bc1770 (patch)
tree949e17104b992919e32e284de4694a56e9621a9b /src/testbed/gnunet-service-testbed.c.new
parenta56471e1bbdaa1d133dfe02eb36bee7d6c6d8856 (diff)
downloadgnunet-ae6912492c5eca8119f118471e4d342f86bc1770.tar.gz
gnunet-ae6912492c5eca8119f118471e4d342f86bc1770.zip
a decent way to auto link controllers during overlay connects
Diffstat (limited to 'src/testbed/gnunet-service-testbed.c.new')
-rw-r--r--src/testbed/gnunet-service-testbed.c.new3608
1 files changed, 0 insertions, 3608 deletions
diff --git a/src/testbed/gnunet-service-testbed.c.new b/src/testbed/gnunet-service-testbed.c.new
deleted file mode 100644
index 135c8a29a..000000000
--- a/src/testbed/gnunet-service-testbed.c.new
+++ /dev/null
@@ -1,3608 +0,0 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19*/
20
21/**
22 * @file testbed/gnunet-service-testbed.c
23 * @brief implementation of the TESTBED service
24 * @author Sree Harsha Totakura
25 */
26
27#include "platform.h"
28#include "gnunet_service_lib.h"
29#include "gnunet_server_lib.h"
30#include "gnunet_transport_service.h"
31#include "gnunet_core_service.h"
32#include "gnunet_hello_lib.h"
33#include <zlib.h>
34
35#include "gnunet_testbed_service.h"
36#include "testbed.h"
37#include "testbed_api.h"
38#include "testbed_api_hosts.h"
39#include "gnunet_testing_lib-new.h"
40
41/**
42 * Generic logging
43 */
44#define LOG(kind,...) \
45 GNUNET_log (kind, __VA_ARGS__)
46
47/**
48 * Debug logging
49 */
50#define LOG_DEBUG(...) \
51 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
52
53/**
54 * By how much should the arrays lists grow
55 */
56#define LIST_GROW_STEP 10
57
58/**
59 * Default timeout for operations which may take some time
60 */
61#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
62
63/**
64 * The main context information associated with the client which started us
65 */
66struct Context
67{
68 /**
69 * The client handle associated with this context
70 */
71 struct GNUNET_SERVER_Client *client;
72
73 /**
74 * The network address of the master controller
75 */
76 char *master_ip;
77
78 /**
79 * The TESTING system handle for starting peers locally
80 */
81 struct GNUNET_TESTING_System *system;
82
83 /**
84 * Our host id according to this context
85 */
86 uint32_t host_id;
87};
88
89
90/**
91 * The message queue for sending messages to clients
92 */
93struct MessageQueue
94{
95 /**
96 * The message to be sent
97 */
98 struct GNUNET_MessageHeader *msg;
99
100 /**
101 * The client to send the message to
102 */
103 struct GNUNET_SERVER_Client *client;
104
105 /**
106 * next pointer for DLL
107 */
108 struct MessageQueue *next;
109
110 /**
111 * prev pointer for DLL
112 */
113 struct MessageQueue *prev;
114};
115
116
117/**
118 * The structure for identifying a shared service
119 */
120struct SharedService
121{
122 /**
123 * The name of the shared service
124 */
125 char *name;
126
127 /**
128 * Number of shared peers per instance of the shared service
129 */
130 uint32_t num_shared;
131
132 /**
133 * Number of peers currently sharing the service
134 */
135 uint32_t num_sharing;
136};
137
138
139/**
140 * A routing entry
141 */
142struct Route
143{
144 /**
145 * destination host
146 */
147 uint32_t dest;
148
149 /**
150 * The destination host is reachable thru
151 */
152 uint32_t thru;
153};
154
155
156/**
157 * Context information used while linking controllers
158 */
159struct LinkControllersContext;
160
161
162/**
163 * A DLL of host registrations to be made
164 */
165struct HostRegistration
166{
167 /**
168 * next registration in the DLL
169 */
170 struct HostRegistration *next;
171
172 /**
173 * previous registration in the DLL
174 */
175 struct HostRegistration *prev;
176
177 /**
178 * The callback to call after this registration's status is available
179 */
180 GNUNET_TESTBED_HostRegistrationCompletion cb;
181
182 /**
183 * The closure for the above callback
184 */
185 void *cb_cls;
186
187 /**
188 * The host that has to be registered
189 */
190 struct GNUNET_TESTBED_Host *host;
191};
192
193
194/**
195 * This context information will be created for each host that is registered at
196 * slave controllers during overlay connects.
197 */
198struct RegisteredHostContext
199{
200 /**
201 * The host which is being registered
202 */
203 struct GNUNET_TESTBED_Host *reg_host;
204
205 /**
206 * The host of the controller which has to connect to the above rhost
207 */
208 struct GNUNET_TESTBED_Host *host;
209
210 /**
211 * The gateway to which this operation is forwarded to
212 */
213 struct Slave *gateway;
214
215 /**
216 * The gateway through which peer2's controller can be reached
217 */
218 struct Slave *gateway2;
219
220 /**
221 * Handle for sub-operations
222 */
223 struct GNUNET_TESTBED_Operation *sub_op;
224
225 /**
226 * The client which initiated the link controller operation
227 */
228 struct GNUNET_SERVER_Client *client;
229
230 /**
231 * Head of the ForwardedOverlayConnectContext DLL
232 */
233 struct ForwardedOverlayConnectContext *focc_dll_head;
234
235 /**
236 * Tail of the ForwardedOverlayConnectContext DLL
237 */
238 struct ForwardedOverlayConnectContext *focc_dll_tail;
239
240 /**
241 * Enumeration of states for this context
242 */
243 enum RHCState {
244
245 /**
246 * The initial state
247 */
248 RHC_INIT = 0,
249
250 /**
251 * State where we attempt to register peer2's controller with peer1's controller
252 */
253 RHC_REGISTER,
254
255 /**
256 * State where we attempt to get peer2's controller configuration
257 */
258 RHC_GET_CFG,
259
260 /**
261 * State where we attempt to link the controller of peer 1 to the controller
262 * of peer2
263 */
264 RHC_LINK,
265
266 /**
267 * State where we attempt to do the overlay connection again
268 */
269 RHC_OL_CONNECT
270
271 } state;
272
273};
274
275
276/**
277 * Function to generate the hashcode corresponding to a RegisteredHostContext
278 *
279 * @param reg_host the host which is being registered in RegisteredHostContext
280 * @param host the host of the controller which has to connect to the above rhost
281 * @return the hashcode
282 */
283static struct GNUNET_HashCode
284hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
285 struct GNUNET_TESTBED_Host *host)
286{
287 struct GNUNET_HashCode hash;
288 uint32_t host_ids[2];
289
290 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
291 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
292 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
293 return hash;
294}
295
296
297/**
298 * Structure representing a connected(directly-linked) controller
299 */
300struct Slave
301{
302 /**
303 * The controller process handle if we had started the controller
304 */
305 struct GNUNET_TESTBED_ControllerProc *controller_proc;
306
307 /**
308 * The controller handle
309 */
310 struct GNUNET_TESTBED_Controller *controller;
311
312 /**
313 * The configuration of the slave. Cannot be NULL
314 */
315 struct GNUNET_CONFIGURATION_Handle *cfg;
316
317 /**
318 * handle to lcc which is associated with this slave startup. Should be set to
319 * NULL when the slave has successfully started up
320 */
321 struct LinkControllersContext *lcc;
322
323 /**
324 * Head of the host registration DLL
325 */
326 struct HostRegistration *hr_dll_head;
327
328 /**
329 * Tail of the host registration DLL
330 */
331 struct HostRegistration *hr_dll_tail;
332
333 /**
334 * The current host registration handle
335 */
336 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
337
338 /**
339 * Hashmap to hold Registered host contexts
340 */
341 struct GNUNET_CONTAINER_MultiHashMap *reghost_map;
342
343 /**
344 * The id of the host this controller is running on
345 */
346 uint32_t host_id;
347
348};
349
350
351/**
352 * States of LCFContext
353 */
354enum LCFContextState
355{
356 /**
357 * The Context has been initialized; Nothing has been done on it
358 */
359 INIT,
360
361 /**
362 * Delegated host has been registered at the forwarding controller
363 */
364 DELEGATED_HOST_REGISTERED,
365
366 /**
367 * The slave host has been registred at the forwarding controller
368 */
369 SLAVE_HOST_REGISTERED,
370
371 /**
372 * The context has been finished (may have error)
373 */
374 FINISHED
375};
376
377
378/**
379 * Link controllers request forwarding context
380 */
381struct LCFContext
382{
383 /**
384 * The gateway which will pass the link message to delegated host
385 */
386 struct Slave *gateway;
387
388 /**
389 * The controller link message that has to be forwarded to
390 */
391 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
392
393 /**
394 * The client which has asked to perform this operation
395 */
396 struct GNUNET_SERVER_Client *client;
397
398 /**
399 * The id of the operation which created this context
400 */
401 uint64_t operation_id;
402
403 /**
404 * The state of this context
405 */
406 enum LCFContextState state;
407
408 /**
409 * The delegated host
410 */
411 uint32_t delegated_host_id;
412
413 /**
414 * The slave host
415 */
416 uint32_t slave_host_id;
417
418};
419
420
421/**
422 * Structure of a queue entry in LCFContext request queue
423 */
424struct LCFContextQueue
425{
426 /**
427 * The LCFContext
428 */
429 struct LCFContext *lcf;
430
431 /**
432 * Head prt for DLL
433 */
434 struct LCFContextQueue *next;
435
436 /**
437 * Tail ptr for DLL
438 */
439 struct LCFContextQueue *prev;
440};
441
442
443/**
444 * A peer
445 */
446struct Peer
447{
448 union
449 {
450 struct
451 {
452 /**
453 * The peer handle from testing API
454 */
455 struct GNUNET_TESTING_Peer *peer;
456
457 /**
458 * The modified (by GNUNET_TESTING_peer_configure) configuration this
459 * peer is configured with
460 */
461 struct GNUNET_CONFIGURATION_Handle *cfg;
462
463 /**
464 * Is the peer running
465 */
466 int is_running;
467
468 } local;
469
470 struct
471 {
472 /**
473 * The slave this peer is started through
474 */
475 struct Slave *slave;
476
477 /**
478 * The id of the remote host this peer is running on
479 */
480 uint32_t remote_host_id;
481
482 } remote;
483
484 } details;
485
486 /**
487 * Is this peer locally created?
488 */
489 int is_remote;
490
491 /**
492 * Our local reference id for this peer
493 */
494 uint32_t id;
495
496};
497
498
499/**
500 * Context information for connecting 2 peers in overlay
501 */
502struct OverlayConnectContext
503{
504 /**
505 * The client which has requested for overlay connection
506 */
507 struct GNUNET_SERVER_Client *client;
508
509 /**
510 * the peer which has to connect to the other peer
511 */
512 struct Peer *peer;
513
514 /**
515 * Transport handle of the first peer to get its HELLO
516 */
517 struct GNUNET_TRANSPORT_Handle *p1th;
518
519 /**
520 * Transport handle of other peer to offer first peer's HELLO
521 */
522 struct GNUNET_TRANSPORT_Handle *p2th;
523
524 /**
525 * Core handles of the first peer; used to notify when second peer connects to it
526 */
527 struct GNUNET_CORE_Handle *ch;
528
529 /**
530 * HELLO of the other peer
531 */
532 struct GNUNET_MessageHeader *hello;
533
534 /**
535 * Get hello handle to acquire HELLO of first peer
536 */
537 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
538
539 /**
540 * The error message we send if this overlay connect operation has timed out
541 */
542 char *emsg;
543
544 /**
545 * Operation context for suboperations
546 */
547 struct OperationContext *opc;
548
549 /**
550 * Controller of peer 2; NULL if the peer is local
551 */
552 struct GNUNET_TESTBED_Controller *peer2_controller;
553
554 /**
555 * The peer identity of the first peer
556 */
557 struct GNUNET_PeerIdentity peer_identity;
558
559 /**
560 * The peer identity of the other peer
561 */
562 struct GNUNET_PeerIdentity other_peer_identity;
563
564 /**
565 * The id of the operation responsible for creating this context
566 */
567 uint64_t op_id;
568
569 /**
570 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
571 * connect to peer 2
572 */
573 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
574
575 /**
576 * The id of the overlay connect timeout task
577 */
578 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
579
580 /**
581 * The id of peer A
582 */
583 uint32_t peer_id;
584
585 /**
586 * The id of peer B
587 */
588 uint32_t other_peer_id;
589
590 /**
591 * Number of times we tried to send hello; used to increase delay in offering
592 * hellos
593 */
594 uint16_t retries;
595};
596
597
598/**
599 * Context information for RequestOverlayConnect
600 * operations. RequestOverlayConnect is used when peers A, B reside on different
601 * hosts and the host controller for peer B is asked by the host controller of
602 * peer A to make peer B connect to peer A
603 */
604struct RequestOverlayConnectContext
605{
606 /**
607 * The transport handle of peer B
608 */
609 struct GNUNET_TRANSPORT_Handle *th;
610
611 /**
612 * Peer A's HELLO
613 */
614 struct GNUNET_MessageHeader *hello;
615
616 /**
617 * The peer identity of peer A
618 */
619 struct GNUNET_PeerIdentity a_id;
620
621 /**
622 * Task for offering HELLO of A to B and doing try_connect
623 */
624 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
625
626 /**
627 * Task to timeout RequestOverlayConnect
628 */
629 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
630
631 /**
632 * Number of times we tried to send hello; used to increase delay in offering
633 * hellos
634 */
635 uint16_t retries;
636
637};
638
639
640/**
641 * Context information for operations forwarded to subcontrollers
642 */
643struct ForwardedOperationContext
644{
645 /**
646 * The generated operation context
647 */
648 struct OperationContext *opc;
649
650 /**
651 * The client to which we have to reply
652 */
653 struct GNUNET_SERVER_Client *client;
654
655 /**
656 * Closure pointer
657 */
658 void *cls;
659
660 /**
661 * Task ID for the timeout task
662 */
663 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
664
665 /**
666 * The id of the operation that has been forwarded
667 */
668 uint64_t operation_id;
669
670};
671
672
673/**
674 * Context information used while linking controllers
675 */
676struct LinkControllersContext
677{
678 /**
679 * The client which initiated the link controller operation
680 */
681 struct GNUNET_SERVER_Client *client;
682
683 /**
684 * The ID of the operation
685 */
686 uint64_t operation_id;
687
688};
689
690
691/**
692 * Context information to used during operations which forward the overlay
693 * connect message
694 */
695struct ForwardedOverlayConnectContext
696{
697 /**
698 * next ForwardedOverlayConnectContext in the DLL
699 */
700 struct ForwardedOverlayConnectContext *next;
701
702 /**
703 * previous ForwardedOverlayConnectContext in the DLL
704 */
705 struct ForwardedOverlayConnectContext *prev;
706
707 /**
708 * A copy of the original overlay connect message
709 */
710 struct GNUNET_MessageHeader *orig_msg;
711
712 /**
713 * The id of the operation which created this context information
714 */
715 uint64_t operation_id;
716
717 /**
718 * the id of peer 1
719 */
720 uint32_t peer1;
721
722 /**
723 * The id of peer 2
724 */
725 uint32_t peer2;
726
727 /**
728 * Id of the host where peer2 is running
729 */
730 uint32_t peer2_host_id;
731};
732
733
734
735/**
736 * The master context; generated with the first INIT message
737 */
738static struct Context *master_context;
739
740/**
741 * Our hostname; we give this to all the peers we start
742 */
743static char *hostname;
744
745
746/***********/
747/* Handles */
748/***********/
749
750/**
751 * Our configuration
752 */
753static struct GNUNET_CONFIGURATION_Handle *our_config;
754
755/**
756 * Current Transmit Handle; NULL if no notify transmit exists currently
757 */
758static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
759
760/****************/
761/* Lists & Maps */
762/****************/
763
764/**
765 * The head for the LCF queue
766 */
767static struct LCFContextQueue *lcfq_head;
768
769/**
770 * The tail for the LCF queue
771 */
772static struct LCFContextQueue *lcfq_tail;
773
774/**
775 * The message queue head
776 */
777static struct MessageQueue *mq_head;
778
779/**
780 * The message queue tail
781 */
782static struct MessageQueue *mq_tail;
783
784/**
785 * Array of hosts
786 */
787static struct GNUNET_TESTBED_Host **host_list;
788
789/**
790 * A list of routes
791 */
792static struct Route **route_list;
793
794/**
795 * A list of directly linked neighbours
796 */
797static struct Slave **slave_list;
798
799/**
800 * A list of peers we know about
801 */
802static struct Peer **peer_list;
803
804/**
805 * The hashmap of shared services
806 */
807static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
808
809/**
810 * The event mask for the events we listen from sub-controllers
811 */
812static uint64_t event_mask;
813
814/**
815 * The size of the host list
816 */
817static uint32_t host_list_size;
818
819/**
820 * The size of the route list
821 */
822static uint32_t route_list_size;
823
824/**
825 * The size of directly linked neighbours list
826 */
827static uint32_t slave_list_size;
828
829/**
830 * The size of the peer list
831 */
832static uint32_t peer_list_size;
833
834/*********/
835/* Tasks */
836/*********/
837
838/**
839 * The lcf_task handle
840 */
841static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
842
843/**
844 * The shutdown task handle
845 */
846static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
847
848
849/**
850 * Function called to notify a client about the connection begin ready to queue
851 * more data. "buf" will be NULL and "size" zero if the connection was closed
852 * for writing in the meantime.
853 *
854 * @param cls NULL
855 * @param size number of bytes available in buf
856 * @param buf where the callee should write the message
857 * @return number of bytes written to buf
858 */
859static size_t
860transmit_ready_notify (void *cls, size_t size, void *buf)
861{
862 struct MessageQueue *mq_entry;
863
864 transmit_handle = NULL;
865 mq_entry = mq_head;
866 GNUNET_assert (NULL != mq_entry);
867 if (0 == size)
868 return 0;
869 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
870 size = ntohs (mq_entry->msg->size);
871 memcpy (buf, mq_entry->msg, size);
872 GNUNET_free (mq_entry->msg);
873 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
874 GNUNET_free (mq_entry);
875 mq_entry = mq_head;
876 if (NULL != mq_entry)
877 transmit_handle =
878 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
879 ntohs (mq_entry->msg->size),
880 GNUNET_TIME_UNIT_FOREVER_REL,
881 &transmit_ready_notify, NULL);
882 return size;
883}
884
885
886/**
887 * Queues a message in send queue for sending to the service
888 *
889 * @param client the client to whom the queued message has to be sent
890 * @param msg the message to queue
891 */
892static void
893queue_message (struct GNUNET_SERVER_Client *client,
894 struct GNUNET_MessageHeader *msg)
895{
896 struct MessageQueue *mq_entry;
897 uint16_t type;
898 uint16_t size;
899
900 type = ntohs (msg->type);
901 size = ntohs (msg->size);
902 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
903 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
904 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
905 mq_entry->msg = msg;
906 mq_entry->client = client;
907 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
908 ntohs (msg->size));
909 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
910 if (NULL == transmit_handle)
911 transmit_handle =
912 GNUNET_SERVER_notify_transmit_ready (client, size,
913 GNUNET_TIME_UNIT_FOREVER_REL,
914 &transmit_ready_notify, NULL);
915}
916
917
918/**
919 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
920 *
921 * @param ptr the memory block to realloc
922 * @param size the size of ptr
923 * @param new_size the size to which ptr has to be realloc'ed
924 * @return the newly reallocated memory block
925 */
926static void *
927TESTBED_realloc (void *ptr, size_t size, size_t new_size)
928{
929 ptr = GNUNET_realloc (ptr, new_size);
930 if (new_size > size)
931 (void) memset (ptr + size, 0, new_size - size);
932 return ptr;
933}
934
935
936/**
937 * Function to add a host to the current list of known hosts
938 *
939 * @param host the host to add
940 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
941 * already in use
942 */
943static int
944host_list_add (struct GNUNET_TESTBED_Host *host)
945{
946 uint32_t host_id;
947 uint32_t orig_size;
948
949 host_id = GNUNET_TESTBED_host_get_id_ (host);
950 orig_size = host_list_size;
951 if (host_list_size <= host_id)
952 {
953 while (host_list_size <= host_id)
954 host_list_size += LIST_GROW_STEP;
955 host_list =
956 TESTBED_realloc (host_list,
957 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
958 sizeof (struct GNUNET_TESTBED_Host *)
959 * host_list_size);
960 }
961 if (NULL != host_list[host_id])
962 {
963 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
964 return GNUNET_SYSERR;
965 }
966 host_list[host_id] = host;
967 return GNUNET_OK;
968}
969
970
971/**
972 * Adds a route to the route list
973 *
974 * @param route the route to add
975 */
976static void
977route_list_add (struct Route *route)
978{
979 uint32_t orig_size;
980
981 orig_size = route_list_size;
982 if (route->dest >= route_list_size)
983 {
984 while (route->dest >= route_list_size)
985 route_list_size += LIST_GROW_STEP;
986 route_list =
987 TESTBED_realloc (route_list,
988 sizeof (struct Route *) * orig_size,
989 sizeof (struct Route *) * route_list_size);
990 }
991 GNUNET_assert (NULL == route_list[route->dest]);
992 route_list[route->dest] = route;
993}
994
995
996/**
997 * Adds a slave to the slave array
998 *
999 * @param slave the slave controller to add
1000 */
1001static void
1002slave_list_add (struct Slave *slave)
1003{
1004 if (slave->host_id >= slave_list_size)
1005 {
1006 slave_list =
1007 TESTBED_realloc (slave_list, sizeof (struct Slave *) * slave_list_size,
1008 sizeof (struct Slave *) * (slave_list_size +
1009 LIST_GROW_STEP));
1010 slave_list_size += LIST_GROW_STEP;
1011 }
1012 GNUNET_assert (NULL == slave_list[slave->host_id]);
1013 slave_list[slave->host_id] = slave;
1014}
1015
1016
1017/**
1018 * Adds a peer to the peer array
1019 *
1020 * @param peer the peer to add
1021 */
1022static void
1023peer_list_add (struct Peer *peer)
1024{
1025 uint32_t orig_size;
1026
1027 orig_size = peer_list_size;
1028 if (peer->id >= peer_list_size)
1029 {
1030 while (peer->id >= peer_list_size)
1031 peer_list_size += LIST_GROW_STEP;
1032 peer_list =
1033 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
1034 sizeof (struct Peer *) * peer_list_size);
1035 }
1036 GNUNET_assert (NULL == peer_list[peer->id]);
1037 peer_list[peer->id] = peer;
1038}
1039
1040
1041/**
1042 * Removes a the give peer from the peer array
1043 *
1044 * @param peer the peer to be removed
1045 */
1046static void
1047peer_list_remove (struct Peer *peer)
1048{
1049 uint32_t id;
1050 uint32_t orig_size;
1051
1052 peer_list[peer->id] = NULL;
1053 orig_size = peer_list_size;
1054 while (peer_list_size >= LIST_GROW_STEP)
1055 {
1056 for (id = peer_list_size - 1;
1057 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
1058 if (NULL != peer_list[id])
1059 break;
1060 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
1061 break;
1062 peer_list_size -= LIST_GROW_STEP;
1063 }
1064 if (orig_size == peer_list_size)
1065 return;
1066 peer_list =
1067 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
1068}
1069
1070
1071/**
1072 * Finds the route with directly connected host as destination through which
1073 * the destination host can be reached
1074 *
1075 * @param host_id the id of the destination host
1076 * @return the route with directly connected destination host; NULL if no route
1077 * is found
1078 */
1079static struct Route *
1080find_dest_route (uint32_t host_id)
1081{
1082 struct Route *route;
1083
1084 if (route_list_size <= host_id)
1085 return NULL;
1086 while (NULL != (route = route_list[host_id]))
1087 {
1088 if (route->thru == master_context->host_id)
1089 break;
1090 host_id = route->thru;
1091 }
1092 return route;
1093}
1094
1095
1096/**
1097 * Routes message to a host given its host_id
1098 *
1099 * @param host_id the id of the destination host
1100 * @param msg the message to be routed
1101 */
1102static void
1103route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
1104{
1105 GNUNET_break (0);
1106}
1107
1108
1109/**
1110 * Send operation failure message to client
1111 *
1112 * @param client the client to which the failure message has to be sent to
1113 * @param operation_id the id of the failed operation
1114 * @param emsg the error message; can be NULL
1115 */
1116static void
1117send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
1118 uint64_t operation_id, const char *emsg)
1119{
1120 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
1121 uint16_t msize;
1122 uint16_t emsg_len;
1123
1124 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1125 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
1126 msize += emsg_len;
1127 msg = GNUNET_malloc (msize);
1128 msg->header.size = htons (msize);
1129 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
1130 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1131 msg->operation_id = GNUNET_htonll (operation_id);
1132 if (0 != emsg_len)
1133 memcpy (&msg[1], emsg, emsg_len);
1134 queue_message (client, &msg->header);
1135}
1136
1137
1138/**
1139 * Function to send generic operation success message to given client
1140 *
1141 * @param client the client to send the message to
1142 * @param operation_id the id of the operation which was successful
1143 */
1144static void
1145send_operation_success_msg (struct GNUNET_SERVER_Client *client,
1146 uint64_t operation_id)
1147{
1148 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
1149 uint16_t msize;
1150
1151 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1152 msg = GNUNET_malloc (msize);
1153 msg->header.size = htons (msize);
1154 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1155 msg->operation_id = GNUNET_htonll (operation_id);
1156 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1157 queue_message (client, &msg->header);
1158}
1159
1160
1161/**
1162 * Callback which will be called to after a host registration succeeded or failed
1163 *
1164 * @param cls the handle to the slave at which the registration is completed
1165 * @param emsg the error message; NULL if host registration is successful
1166 */
1167static void
1168hr_completion (void *cls, const char *emsg);
1169
1170
1171/**
1172 * Attempts to register the next host in the host registration queue
1173 *
1174 * @param slave the slave controller whose host registration queue is checked
1175 * for host registrations
1176 */
1177static void
1178register_next_host (struct Slave *slave)
1179{
1180 struct HostRegistration *hr;
1181
1182 hr = slave->hr_dll_head;
1183 GNUNET_assert (NULL != hr);
1184 GNUNET_assert (NULL == slave->rhandle);
1185 slave->rhandle = GNUNET_TESTBED_register_host (slave->controller,
1186 hr->host,
1187 hr_completion,
1188 slave);
1189}
1190
1191
1192/**
1193 * Callback which will be called to after a host registration succeeded or failed
1194 *
1195 * @param cls the handle to the slave at which the registration is completed
1196 * @param emsg the error message; NULL if host registration is successful
1197 */
1198static void
1199hr_completion (void *cls, const char *emsg)
1200{
1201 struct Slave *slave = cls;
1202 struct HostRegistration *hr;
1203
1204 slave->rhandle = NULL;
1205 hr = slave->hr_dll_head;
1206 GNUNET_assert (NULL != hr);
1207 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head,
1208 slave->hr_dll_tail,
1209 hr);
1210 if (NULL != hr->cb)
1211 hr->cb (hr->cb_cls, emsg);
1212 GNUNET_free (hr);
1213 if ((NULL == slave->rhandle) && (NULL != slave->hr_dll_head))
1214 register_next_host (slave);
1215}
1216
1217
1218/**
1219 * Adds a host registration's request to a slave's registration queue
1220 *
1221 * @param slave the slave controller at which the given host has to be
1222 * registered
1223 * @param cb the host registration completion callback
1224 * @param cb_cls the closure for the host registration completion callback
1225 * @param host the host which has to be registered
1226 */
1227static void
1228queue_host_registration (struct Slave *slave,
1229 GNUNET_TESTBED_HostRegistrationCompletion cb,
1230 void *cb_cls,
1231 struct GNUNET_TESTBED_Host *host)
1232{
1233 struct HostRegistration *hr;
1234 int call_register;
1235
1236 hr = GNUNET_malloc (sizeof (struct HostRegistration));
1237 hr->cb = cb;
1238 hr->cb_cls = cb_cls;
1239 hr->host = host;
1240 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
1241 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head,
1242 slave->hr_dll_tail,
1243 hr);
1244 if (GNUNET_YES == call_register)
1245 register_next_host (slave);
1246}
1247
1248
1249/**
1250 * The Link Controller forwarding task
1251 *
1252 * @param cls the LCFContext
1253 * @param tc the Task context from scheduler
1254 */
1255static void
1256lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1257
1258
1259/**
1260 * Completion callback for host registrations while forwarding Link Controller messages
1261 *
1262 * @param cls the LCFContext
1263 * @param emsg the error message; NULL if host registration is successful
1264 */
1265static void
1266lcf_proc_cc (void *cls, const char *emsg)
1267{
1268 struct LCFContext *lcf = cls;
1269
1270 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1271 switch (lcf->state)
1272 {
1273 case INIT:
1274 if (NULL != emsg)
1275 goto registration_error;
1276 lcf->state = DELEGATED_HOST_REGISTERED;
1277 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1278 break;
1279 case DELEGATED_HOST_REGISTERED:
1280 if (NULL != emsg)
1281 goto registration_error;
1282 lcf->state = SLAVE_HOST_REGISTERED;
1283 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1284 break;
1285 default:
1286 GNUNET_assert (0); /* Shouldn't reach here */
1287 }
1288 return;
1289
1290 registration_error:
1291 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
1292 emsg);
1293 lcf->state = FINISHED;
1294 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1295}
1296
1297
1298/**
1299 * Callback to be called when forwarded link controllers operation is
1300 * successfull. We have to relay the reply msg back to the client
1301 *
1302 * @param cls ForwardedOperationContext
1303 * @param msg the peer create success message
1304 */
1305static void
1306forwarded_operation_reply_relay (void *cls,
1307 const struct GNUNET_MessageHeader *msg)
1308{
1309 struct ForwardedOperationContext *fopc = cls;
1310 struct GNUNET_MessageHeader *dup_msg;
1311 uint16_t msize;
1312
1313 msize = ntohs (msg->size);
1314 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1315 msize);
1316 dup_msg = GNUNET_malloc (msize);
1317 (void) memcpy (dup_msg, msg, msize);
1318 queue_message (fopc->client, dup_msg);
1319 GNUNET_SERVER_client_drop (fopc->client);
1320 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1321 GNUNET_free (fopc);
1322}
1323
1324
1325/**
1326 * Task to free resources when forwarded link controllers has been timedout
1327 *
1328 * @param cls the ForwardedOperationContext
1329 * @param tc the task context from scheduler
1330 */
1331static void
1332forwarded_operation_timeout (void *cls,
1333 const struct GNUNET_SCHEDULER_TaskContext *tc)
1334{
1335 struct ForwardedOperationContext *fopc = cls;
1336
1337 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1338 LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
1339 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1340 GNUNET_SERVER_client_drop (fopc->client);
1341 GNUNET_free (fopc);
1342}
1343
1344
1345/**
1346 * The Link Controller forwarding task
1347 *
1348 * @param cls the LCFContext
1349 * @param tc the Task context from scheduler
1350 */
1351static void
1352lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1353{
1354 struct LCFContext *lcf = cls;
1355 struct LCFContextQueue *lcfq;
1356 struct ForwardedOperationContext *fopc;
1357
1358 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1359 switch (lcf->state)
1360 {
1361 case INIT:
1362 if (GNUNET_NO ==
1363 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1364 lcf->gateway->controller))
1365 {
1366 queue_host_registration (lcf->gateway,
1367 lcf_proc_cc, lcf,
1368 host_list[lcf->delegated_host_id]);
1369 }
1370 else
1371 {
1372 lcf->state = DELEGATED_HOST_REGISTERED;
1373 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1374 }
1375 break;
1376 case DELEGATED_HOST_REGISTERED:
1377 if (GNUNET_NO ==
1378 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1379 lcf->gateway->controller))
1380 {
1381 queue_host_registration (lcf->gateway,
1382 lcf_proc_cc, lcf,
1383 host_list[lcf->slave_host_id]);
1384 }
1385 else
1386 {
1387 lcf->state = SLAVE_HOST_REGISTERED;
1388 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1389 }
1390 break;
1391 case SLAVE_HOST_REGISTERED:
1392 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1393 fopc->client = lcf->client;
1394 fopc->operation_id = lcf->operation_id;
1395 fopc->opc =
1396 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1397 lcf->operation_id,
1398 &lcf->msg->header,
1399 &forwarded_operation_reply_relay,
1400 fopc);
1401 fopc->timeout_task =
1402 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1403 fopc);
1404 lcf->state = FINISHED;
1405 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1406 break;
1407 case FINISHED:
1408 lcfq = lcfq_head;
1409 GNUNET_assert (lcfq->lcf == lcf);
1410 GNUNET_free (lcf->msg);
1411 GNUNET_free (lcf);
1412 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1413 GNUNET_free (lcfq);
1414 if (NULL != lcfq_head)
1415 lcf_proc_task_id =
1416 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1417 }
1418}
1419
1420
1421/**
1422 * Cleans up ForwardedOverlayConnectContext
1423 *
1424 * @param focc the ForwardedOverlayConnectContext to cleanup
1425 */
1426static void
1427cleanup_focc (struct ForwardedOverlayConnectContext *focc)
1428{
1429 GNUNET_free_non_null (focc->orig_msg);
1430 GNUNET_free (focc);
1431}
1432
1433
1434/**
1435 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1436 *
1437 * @param rhc the RegisteredHostContext
1438 */
1439static void
1440process_next_focc (struct RegisteredHostContext *rhc);
1441
1442
1443/**
1444 * Timeout task for cancelling a forwarded overlay connect connect
1445 *
1446 * @param cls the ForwardedOverlayConnectContext
1447 * @param tc the task context from the scheduler
1448 */
1449static void
1450forwarded_overlay_connect_timeout (void *cls,
1451 const struct GNUNET_SCHEDULER_TaskContext
1452 *tc)
1453{
1454 struct ForwardedOperationContext *fopc = cls;
1455 struct RegisteredHostContext *rhc;
1456 struct ForwardedOverlayConnectContext *focc;
1457
1458 rhc = fopc->cls;
1459 focc = rhc->focc_dll_head;
1460 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1461 cleanup_focc (focc);
1462 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n",
1463 focc->peer1, focc->peer2);
1464 forwarded_operation_timeout (cls, tc);
1465 if (NULL != rhc->focc_dll_head)
1466 process_next_focc (rhc);
1467}
1468
1469
1470/**
1471 * Callback to be called when forwarded overlay connection operation has a reply
1472 * from the sub-controller successfull. We have to relay the reply msg back to
1473 * the client
1474 *
1475 * @param cls ForwardedOperationContext
1476 * @param msg the peer create success message
1477 */
1478static void
1479forwarded_overlay_connect_listener (void *cls,
1480 const struct GNUNET_MessageHeader *msg)
1481{
1482 struct ForwardedOperationContext *fopc = cls;
1483 struct RegisteredHostContext *rhc;
1484 struct ForwardedOverlayConnectContext *focc;
1485
1486 rhc = fopc->cls;
1487 forwarded_operation_reply_relay (cls, msg);
1488 focc = rhc->focc_dll_head;
1489 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1490 cleanup_focc (focc);
1491 if (NULL != rhc->focc_dll_head)
1492 process_next_focc (rhc);
1493}
1494
1495
1496/**
1497 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1498 *
1499 * @param rhc the RegisteredHostContext
1500 */
1501static void
1502process_next_focc (struct RegisteredHostContext *rhc)
1503{
1504 struct ForwardedOperationContext *fopc;
1505 struct ForwardedOverlayConnectContext *focc;
1506
1507 focc = rhc->focc_dll_head;
1508 GNUNET_assert (NULL != focc);
1509 GNUNET_assert (RHC_OL_CONNECT == rhc->state);
1510 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1511 GNUNET_SERVER_client_keep (rhc->client);
1512 fopc->client = rhc->client;
1513 fopc->operation_id = focc->operation_id;
1514 fopc->cls = rhc;
1515 fopc->opc =
1516 GNUNET_TESTBED_forward_operation_msg_ (rhc->gateway->controller,
1517 focc->operation_id, focc->orig_msg,
1518 &forwarded_overlay_connect_listener,
1519 fopc);
1520 GNUNET_free (focc->orig_msg);
1521 focc->orig_msg = NULL;
1522 fopc->timeout_task =
1523 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_overlay_connect_timeout,
1524 fopc);
1525}
1526
1527
1528/**
1529 * Callback for event from slave controllers
1530 *
1531 * @param cls struct Slave *
1532 * @param event information about the event
1533 */
1534static void
1535slave_event_callback (void *cls,
1536 const struct GNUNET_TESTBED_EventInformation *event)
1537{
1538 struct RegisteredHostContext *rhc;
1539 struct GNUNET_CONFIGURATION_Handle *cfg;
1540 struct GNUNET_TESTBED_Operation *old_op;
1541
1542 /* We currently only get here when working on RegisteredHostContexts */
1543 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
1544 rhc = event->details.operation_finished.op_cls;
1545 GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
1546 switch (rhc->state)
1547 {
1548 case RHC_GET_CFG:
1549 cfg = event->details.operation_finished.generic;
1550 old_op = rhc->sub_op;
1551 rhc->state = RHC_LINK;
1552 rhc->sub_op =
1553 GNUNET_TESTBED_controller_link (rhc,
1554 rhc->gateway->controller,
1555 rhc->reg_host,
1556 rhc->host,
1557 cfg,
1558 GNUNET_NO);
1559 GNUNET_TESTBED_operation_done (old_op);
1560 break;
1561 case RHC_LINK:
1562 LOG_DEBUG ("OL: Linking controllers successfull\n");
1563 GNUNET_TESTBED_operation_done (rhc->sub_op);
1564 rhc->sub_op = NULL;
1565 rhc->state = RHC_OL_CONNECT;
1566 process_next_focc (rhc);
1567 break;
1568 default:
1569 GNUNET_assert (0);
1570 }
1571}
1572
1573
1574/**
1575 * Callback to signal successfull startup of the controller process
1576 *
1577 * @param cls the handle to the slave whose status is to be found here
1578 * @param cfg the configuration with which the controller has been started;
1579 * NULL if status is not GNUNET_OK
1580 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1581 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1582 */
1583static void
1584slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1585 int status)
1586{
1587 struct Slave *slave = cls;
1588 struct LinkControllersContext *lcc;
1589
1590 lcc = slave->lcc;
1591 if (GNUNET_SYSERR == status)
1592 {
1593 slave->controller_proc = NULL;
1594 slave_list[slave->host_id] = NULL;
1595 if (NULL != slave->cfg)
1596 GNUNET_CONFIGURATION_destroy (slave->cfg);
1597 GNUNET_free (slave);
1598 slave = NULL;
1599 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1600 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1601 goto clean_lcc;
1602 }
1603 slave->controller =
1604 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1605 event_mask,
1606 &slave_event_callback, slave);
1607 if (NULL != slave->controller)
1608 {
1609 send_operation_success_msg (lcc->client, lcc->operation_id);
1610 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1611 }
1612 else
1613 {
1614 send_operation_fail_msg (lcc->client, lcc->operation_id,
1615 "Could not connect to delegated controller");
1616 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1617 slave_list[slave->host_id] = NULL;
1618 GNUNET_free (slave);
1619 slave = NULL;
1620 }
1621
1622 clean_lcc:
1623 if (NULL != lcc)
1624 {
1625 if (NULL != lcc->client)
1626 {
1627 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1628 GNUNET_SERVER_client_drop (lcc->client);
1629 lcc->client = NULL;
1630 }
1631 GNUNET_free (lcc);
1632 }
1633 if (NULL != slave)
1634 slave->lcc = NULL;
1635}
1636
1637
1638/**
1639 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1640 *
1641 * @param cls NULL
1642 * @param client identification of the client
1643 * @param message the actual message
1644 */
1645static void
1646handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1647 const struct GNUNET_MessageHeader *message)
1648{
1649 const struct GNUNET_TESTBED_InitMessage *msg;
1650 struct GNUNET_TESTBED_Host *host;
1651 const char *controller_hostname;
1652 uint16_t msize;
1653
1654 if (NULL != master_context)
1655 {
1656 LOG_DEBUG ("We are being connected to laterally\n");
1657 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1658 return;
1659 }
1660 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1661 msize = ntohs (message->size);
1662 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1663 {
1664 GNUNET_break (0);
1665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1666 return;
1667 }
1668 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1669 controller_hostname = (const char *) &msg[1];
1670 if ('\0' != controller_hostname[msize - 1])
1671 {
1672 GNUNET_break (0);
1673 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1674 return;
1675 }
1676 master_context = GNUNET_malloc (sizeof (struct Context));
1677 master_context->client = client;
1678 master_context->host_id = ntohl (msg->host_id);
1679 master_context->master_ip = GNUNET_strdup (controller_hostname);
1680 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1681 master_context->system =
1682 GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1683 host =
1684 GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1685 master_context->master_ip,
1686 NULL,
1687 0);
1688 host_list_add (host);
1689 GNUNET_SERVER_client_keep (client);
1690 LOG_DEBUG ("Created master context with host ID: %u\n",
1691 master_context->host_id);
1692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1693}
1694
1695
1696/**
1697 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1698 *
1699 * @param cls NULL
1700 * @param client identification of the client
1701 * @param message the actual message
1702 */
1703static void
1704handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1705 const struct GNUNET_MessageHeader *message)
1706{
1707 struct GNUNET_TESTBED_Host *host;
1708 const struct GNUNET_TESTBED_AddHostMessage *msg;
1709 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1710 char *username;
1711 char *hostname;
1712 char *emsg;
1713 uint32_t host_id;
1714 uint16_t username_length;
1715 uint16_t hostname_length;
1716 uint16_t reply_size;
1717 uint16_t msize;
1718
1719 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1720 msize = ntohs (msg->header.size);
1721 username = (char *) &msg[1];
1722 username_length = ntohs (msg->user_name_length);
1723 if (0 != username_length)
1724 username_length++;
1725 /* msg must contain hostname */
1726 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1727 username_length + 1));
1728 if (0 != username_length)
1729 GNUNET_assert ('\0' == username[username_length - 1]);
1730 hostname = username + username_length;
1731 hostname_length =
1732 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1733 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1734 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1735 host_id = ntohl (msg->host_id);
1736 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1737 LOG_DEBUG ("-------host id: %u\n", host_id);
1738 LOG_DEBUG ("-------hostname: %s\n", hostname);
1739 if (0 != username_length)
1740 LOG_DEBUG ("-------username: %s\n", username);
1741 else
1742 {
1743 LOG_DEBUG ("-------username: NULL\n");
1744 username = NULL;
1745 }
1746 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1747 host =
1748 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1749 ntohs (msg->ssh_port));
1750 GNUNET_assert (NULL != host);
1751 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1752 if (GNUNET_OK != host_list_add (host))
1753 {
1754 /* We are unable to add a host */
1755 emsg = "A host exists with given host-id";
1756 LOG_DEBUG ("%s: %u", emsg, host_id);
1757 GNUNET_TESTBED_host_destroy (host);
1758 reply_size += strlen (emsg) + 1;
1759 reply = GNUNET_malloc (reply_size);
1760 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1761 }
1762 else
1763 {
1764 LOG_DEBUG ("Added host %u at %u\n",
1765 host_id, master_context->host_id);
1766 reply = GNUNET_malloc (reply_size);
1767 }
1768 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1769 reply->header.size = htons (reply_size);
1770 reply->host_id = htonl (host_id);
1771 queue_message (client, &reply->header);
1772 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1773}
1774
1775
1776/**
1777 * Iterator over hash map entries.
1778 *
1779 * @param cls closure
1780 * @param key current key code
1781 * @param value value in the hash map
1782 * @return GNUNET_YES if we should continue to
1783 * iterate,
1784 * GNUNET_NO if not.
1785 */
1786int
1787ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1788{
1789 struct SharedService *queried_ss = cls;
1790 struct SharedService *ss = value;
1791
1792 if (0 == strcmp (ss->name, queried_ss->name))
1793 return GNUNET_NO;
1794 else
1795 return GNUNET_YES;
1796}
1797
1798
1799/**
1800 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1801 *
1802 * @param cls NULL
1803 * @param client identification of the client
1804 * @param message the actual message
1805 */
1806static void
1807handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1808 const struct GNUNET_MessageHeader *message)
1809{
1810 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1811 struct SharedService *ss;
1812 char *service_name;
1813 struct GNUNET_HashCode hash;
1814 uint16_t msg_size;
1815 uint16_t service_name_size;
1816
1817 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1818 msg_size = ntohs (message->size);
1819 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1820 {
1821 GNUNET_break (0);
1822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1823 return;
1824 }
1825 service_name_size =
1826 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1827 service_name = (char *) &msg[1];
1828 if ('\0' != service_name[service_name_size - 1])
1829 {
1830 GNUNET_break (0);
1831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1832 return;
1833 }
1834 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1835 service_name, ntohl (msg->num_peers));
1836 if (ntohl (msg->host_id) != master_context->host_id)
1837 {
1838 route_message (ntohl (msg->host_id), message);
1839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1840 return;
1841 }
1842 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1843 ss = GNUNET_malloc (sizeof (struct SharedService));
1844 ss->name = strdup (service_name);
1845 ss->num_shared = ntohl (msg->num_peers);
1846 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1847 if (GNUNET_SYSERR ==
1848 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1849 &ss_exists_iterator, ss))
1850 {
1851 LOG (GNUNET_ERROR_TYPE_WARNING,
1852 "Service %s already configured as a shared service. "
1853 "Ignoring service sharing request \n", ss->name);
1854 GNUNET_free (ss->name);
1855 GNUNET_free (ss);
1856 return;
1857 }
1858 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1859 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1860}
1861
1862
1863/**
1864 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1865 *
1866 * @param cls NULL
1867 * @param client identification of the client
1868 * @param message the actual message
1869 */
1870static void
1871handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1872 const struct GNUNET_MessageHeader *message)
1873{
1874 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1875 struct GNUNET_CONFIGURATION_Handle *cfg;
1876 struct LCFContextQueue *lcfq;
1877 struct Route *route;
1878 struct Route *new_route;
1879 char *config;
1880 uLongf dest_size;
1881 size_t config_size;
1882 uint32_t delegated_host_id;
1883 uint32_t slave_host_id;
1884 uint16_t msize;
1885
1886 if (NULL == master_context)
1887 {
1888 GNUNET_break (0);
1889 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1890 return;
1891 }
1892 msize = ntohs (message->size);
1893 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1894 {
1895 GNUNET_break (0);
1896 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1897 return;
1898 }
1899 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1900 delegated_host_id = ntohl (msg->delegated_host_id);
1901 if (delegated_host_id == master_context->host_id)
1902 {
1903 GNUNET_break (0);
1904 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1906 return;
1907 }
1908 if ((delegated_host_id >= host_list_size) ||
1909 (NULL == host_list[delegated_host_id]))
1910 {
1911 LOG (GNUNET_ERROR_TYPE_WARNING,
1912 "Delegated host %u not registered with us\n", delegated_host_id);
1913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1914 return;
1915 }
1916 slave_host_id = ntohl (msg->slave_host_id);
1917 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1918 {
1919 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1920 slave_host_id);
1921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1922 return;
1923 }
1924 if (slave_host_id == delegated_host_id)
1925 {
1926 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1928 return;
1929 }
1930
1931 if (slave_host_id == master_context->host_id) /* Link from us */
1932 {
1933 struct Slave *slave;
1934 struct LinkControllersContext *lcc;
1935
1936 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1937 config_size = ntohs (msg->config_size);
1938 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1939 {
1940 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1941 delegated_host_id);
1942 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1943 return;
1944 }
1945 config = GNUNET_malloc (config_size);
1946 dest_size = (uLongf) config_size;
1947 if (Z_OK !=
1948 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1949 (uLong) msize))
1950 {
1951 GNUNET_break (0); /* Compression error */
1952 GNUNET_free (config);
1953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1954 return;
1955 }
1956 if (config_size != dest_size)
1957 {
1958 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1959 GNUNET_free (config);
1960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1961 return;
1962 }
1963 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1964 if (GNUNET_OK !=
1965 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1966 {
1967 GNUNET_break (0); /* Configuration parsing error */
1968 GNUNET_free (config);
1969 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1970 return;
1971 }
1972 GNUNET_free (config);
1973 if ((delegated_host_id < slave_list_size) &&
1974 (NULL != slave_list[delegated_host_id]))
1975 {
1976 GNUNET_break (0); /* Configuration parsing error */
1977 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1978 return;
1979 }
1980 slave = GNUNET_malloc (sizeof (struct Slave));
1981 slave->host_id = delegated_host_id;
1982 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
1983 slave_list_add (slave);
1984 if (1 != msg->is_subordinate)
1985 {
1986 slave->controller =
1987 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1988 event_mask,
1989 &slave_event_callback, slave);
1990 slave->cfg = cfg;
1991 if (NULL != slave->controller)
1992 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1993 else
1994 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1995 "Could not connect to delegated controller");
1996 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1997 return;
1998 }
1999 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
2000 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
2001 GNUNET_SERVER_client_keep (client);
2002 lcc->client = client;
2003 slave->lcc = lcc;
2004 slave->controller_proc =
2005 GNUNET_TESTBED_controller_start (master_context->master_ip,
2006 host_list[slave->host_id], cfg,
2007 &slave_status_callback, slave);
2008 GNUNET_CONFIGURATION_destroy (cfg);
2009 new_route = GNUNET_malloc (sizeof (struct Route));
2010 new_route->dest = delegated_host_id;
2011 new_route->thru = master_context->host_id;
2012 route_list_add (new_route);
2013 return;
2014 }
2015
2016 /* Route the request */
2017 if (slave_host_id >= route_list_size)
2018 {
2019 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
2020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2021 return;
2022 }
2023 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
2024 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
2025 lcfq->lcf->delegated_host_id = delegated_host_id;
2026 lcfq->lcf->slave_host_id = slave_host_id;
2027 route = find_dest_route (slave_host_id);
2028 GNUNET_assert (NULL != route); /* because we add routes carefully */
2029 GNUNET_assert (route->dest < slave_list_size);
2030 GNUNET_assert (NULL != slave_list[route->dest]);
2031 lcfq->lcf->state = INIT;
2032 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
2033 lcfq->lcf->gateway = slave_list[route->dest];
2034 lcfq->lcf->msg = GNUNET_malloc (msize);
2035 (void) memcpy (lcfq->lcf->msg, msg, msize);
2036 GNUNET_SERVER_client_keep (client);
2037 lcfq->lcf->client = client;
2038 if (NULL == lcfq_head)
2039 {
2040 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2041 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2042 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
2043 }
2044 else
2045 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2046 /* FIXME: Adding a new route should happen after the controllers are linked
2047 * successfully */
2048 if (1 != msg->is_subordinate)
2049 {
2050 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2051 return;
2052 }
2053 if ((delegated_host_id < route_list_size)
2054 && (NULL != route_list[delegated_host_id]))
2055 {
2056 GNUNET_break_op (0); /* Are you trying to link delegated host twice
2057 with is subordinate flag set to GNUNET_YES? */
2058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2059 return;
2060 }
2061 new_route = GNUNET_malloc (sizeof (struct Route));
2062 new_route->dest = delegated_host_id;
2063 new_route->thru = route->dest;
2064 route_list_add (new_route);
2065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2066}
2067
2068
2069/**
2070 * The task to be executed if the forwarded peer create operation has been
2071 * timed out
2072 *
2073 * @param cls the FowardedOperationContext
2074 * @param tc the TaskContext from the scheduler
2075 */
2076static void
2077peer_create_forward_timeout (void *cls,
2078 const struct GNUNET_SCHEDULER_TaskContext *tc)
2079{
2080 struct ForwardedOperationContext *fo_ctxt = cls;
2081
2082 /* send error msg to client */
2083 GNUNET_free (fo_ctxt->cls);
2084 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
2085 GNUNET_SERVER_client_drop (fo_ctxt->client);
2086 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
2087 GNUNET_free (fo_ctxt);
2088}
2089
2090
2091/**
2092 * Callback to be called when forwarded peer create operation is
2093 * successfull. We have to relay the reply msg back to the client
2094 *
2095 * @param cls ForwardedOperationContext
2096 * @param msg the peer create success message
2097 */
2098static void
2099peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2100{
2101 struct ForwardedOperationContext *fo_ctxt = cls;
2102 struct GNUNET_MessageHeader *dup_msg;
2103 struct Peer *remote_peer;
2104 uint16_t msize;
2105
2106 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
2107 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
2108 {
2109 GNUNET_assert (NULL != fo_ctxt->cls);
2110 remote_peer = fo_ctxt->cls;
2111 peer_list_add (remote_peer);
2112 }
2113 msize = ntohs (msg->size);
2114 dup_msg = GNUNET_malloc (msize);
2115 (void) memcpy (dup_msg, msg, msize);
2116 queue_message (fo_ctxt->client, dup_msg);
2117 GNUNET_SERVER_client_drop (fo_ctxt->client);
2118 GNUNET_free (fo_ctxt);
2119}
2120
2121
2122
2123/**
2124 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
2125 *
2126 * @param cls NULL
2127 * @param client identification of the client
2128 * @param message the actual message
2129 */
2130static void
2131handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
2132 const struct GNUNET_MessageHeader *message)
2133{
2134 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
2135 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
2136 struct GNUNET_CONFIGURATION_Handle *cfg;
2137 struct ForwardedOperationContext *fo_ctxt;
2138 struct Route *route;
2139 struct Peer *peer;
2140 char *config;
2141 size_t dest_size;
2142 int ret;
2143 uint32_t config_size;
2144 uint32_t host_id;
2145 uint32_t peer_id;
2146 uint16_t msize;
2147
2148
2149 msize = ntohs (message->size);
2150 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
2151 {
2152 GNUNET_break (0); /* We need configuration */
2153 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2154 return;
2155 }
2156 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
2157 host_id = ntohl (msg->host_id);
2158 peer_id = ntohl (msg->peer_id);
2159 if (UINT32_MAX == peer_id)
2160 {
2161 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2162 "Cannot create peer with given ID");
2163 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2164 return;
2165 }
2166 if (host_id == master_context->host_id)
2167 {
2168 char *emsg;
2169
2170 /* We are responsible for this peer */
2171 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
2172 config_size = ntohl (msg->config_size);
2173 config = GNUNET_malloc (config_size);
2174 dest_size = config_size;
2175 if (Z_OK !=
2176 (ret =
2177 uncompress ((Bytef *) config, (uLongf *) & dest_size,
2178 (const Bytef *) &msg[1], (uLong) msize)))
2179 {
2180 GNUNET_break (0); /* uncompression error */
2181 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2182 return;
2183 }
2184 if (config_size != dest_size)
2185 {
2186 GNUNET_break (0); /* Uncompressed config size mismatch */
2187 GNUNET_free (config);
2188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2189 return;
2190 }
2191 cfg = GNUNET_CONFIGURATION_create ();
2192 if (GNUNET_OK !=
2193 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2194 {
2195 GNUNET_break (0); /* Configuration parsing error */
2196 GNUNET_free (config);
2197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2198 return;
2199 }
2200 GNUNET_free (config);
2201 peer = GNUNET_malloc (sizeof (struct Peer));
2202 peer->is_remote = GNUNET_NO;
2203 peer->details.local.cfg = cfg;
2204 peer->id = peer_id;
2205 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
2206 peer->details.local.peer =
2207 GNUNET_TESTING_peer_configure (master_context->system,
2208 peer->details.local.cfg, peer->id,
2209 NULL /* Peer id */ ,
2210 &emsg);
2211 if (NULL == peer->details.local.peer)
2212 {
2213 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
2214 GNUNET_free (emsg);
2215 GNUNET_free (peer);
2216 GNUNET_break (0);
2217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2218 return;
2219 }
2220 peer->details.local.is_running = GNUNET_NO;
2221 peer_list_add (peer);
2222 reply =
2223 GNUNET_malloc (sizeof
2224 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2225 reply->header.size =
2226 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2227 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
2228 reply->peer_id = msg->peer_id;
2229 reply->operation_id = msg->operation_id;
2230 queue_message (client, &reply->header);
2231 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2232 return;
2233 }
2234
2235 /* Forward peer create request */
2236 route = find_dest_route (host_id);
2237 if (NULL == route)
2238 {
2239 GNUNET_break (0);
2240 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2241 return;
2242 }
2243
2244 peer = GNUNET_malloc (sizeof (struct Peer));
2245 peer->is_remote = GNUNET_YES;
2246 peer->id = peer_id;
2247 peer->details.remote.slave = slave_list[route->dest];
2248 peer->details.remote.remote_host_id = host_id;
2249 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2250 GNUNET_SERVER_client_keep (client);
2251 fo_ctxt->client = client;
2252 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
2253 fo_ctxt->cls = peer; //slave_list[route->dest]->controller;
2254 fo_ctxt->opc =
2255 GNUNET_TESTBED_forward_operation_msg_ (slave_list [route->dest]->controller,
2256 fo_ctxt->operation_id,
2257 &msg->header,
2258 peer_create_success_cb, fo_ctxt);
2259 fo_ctxt->timeout_task =
2260 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
2261 fo_ctxt);
2262 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2263}
2264
2265
2266/**
2267 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2268 *
2269 * @param cls NULL
2270 * @param client identification of the client
2271 * @param message the actual message
2272 */
2273static void
2274handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2275 const struct GNUNET_MessageHeader *message)
2276{
2277 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
2278 struct ForwardedOperationContext *fopc;
2279 struct Peer *peer;
2280 uint32_t peer_id;
2281
2282 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
2283 peer_id = ntohl (msg->peer_id);
2284 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
2285 peer_id, GNUNET_ntohll (msg->operation_id));
2286 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
2287 {
2288 LOG (GNUNET_ERROR_TYPE_ERROR,
2289 "Asked to destroy a non existent peer with id: %u\n", peer_id);
2290 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2291 "Peer doesn't exist");
2292 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2293 return;
2294 }
2295 peer = peer_list[peer_id];
2296 if (GNUNET_YES == peer->is_remote)
2297 {
2298 /* Forward the destory message to sub controller */
2299 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2300 GNUNET_SERVER_client_keep (client);
2301 fopc->client = client;
2302 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2303 fopc->opc =
2304 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2305 fopc->operation_id, &msg->header,
2306 &forwarded_operation_reply_relay,
2307 fopc);
2308 fopc->timeout_task =
2309 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2310 fopc);
2311 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2312 return;
2313 }
2314 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
2315 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
2316 peer_list_remove (peer);
2317 GNUNET_free (peer);
2318 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2319 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2320}
2321
2322
2323/**
2324 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2325 *
2326 * @param cls NULL
2327 * @param client identification of the client
2328 * @param message the actual message
2329 */
2330static void
2331handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
2332 const struct GNUNET_MessageHeader *message)
2333{
2334 const struct GNUNET_TESTBED_PeerStartMessage *msg;
2335 struct GNUNET_TESTBED_PeerEventMessage *reply;
2336 struct ForwardedOperationContext *fopc;
2337 struct Peer *peer;
2338 uint32_t peer_id;
2339
2340 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
2341 peer_id = ntohl (msg->peer_id);
2342 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2343 {
2344 GNUNET_break (0);
2345 LOG (GNUNET_ERROR_TYPE_ERROR,
2346 "Asked to start a non existent peer with id: %u\n", peer_id);
2347 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2348 return;
2349 }
2350 peer = peer_list[peer_id];
2351 if (GNUNET_YES == peer->is_remote)
2352 {
2353 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2354 GNUNET_SERVER_client_keep (client);
2355 fopc->client = client;
2356 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2357 fopc->opc =
2358 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2359 fopc->operation_id, &msg->header,
2360 &forwarded_operation_reply_relay,
2361 fopc);
2362 fopc->timeout_task =
2363 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2364 fopc);
2365 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2366 return;
2367 }
2368 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
2369 {
2370 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2371 "Failed to start");
2372 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2373 return;
2374 }
2375 peer->details.local.is_running = GNUNET_YES;
2376 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2377 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2378 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2379 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
2380 reply->host_id = htonl (master_context->host_id);
2381 reply->peer_id = msg->peer_id;
2382 reply->operation_id = msg->operation_id;
2383 queue_message (client, &reply->header);
2384 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2385}
2386
2387
2388/**
2389 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2390 *
2391 * @param cls NULL
2392 * @param client identification of the client
2393 * @param message the actual message
2394 */
2395static void
2396handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
2397 const struct GNUNET_MessageHeader *message)
2398{
2399 const struct GNUNET_TESTBED_PeerStopMessage *msg;
2400 struct GNUNET_TESTBED_PeerEventMessage *reply;
2401 struct ForwardedOperationContext *fopc;
2402 struct Peer *peer;
2403 uint32_t peer_id;
2404
2405 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
2406 peer_id = ntohl (msg->peer_id);
2407 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2408 {
2409 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2410 "Peer not found");
2411 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2412 return;
2413 }
2414 peer = peer_list[peer_id];
2415 if (GNUNET_YES == peer->is_remote)
2416 {
2417 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2418 GNUNET_SERVER_client_keep (client);
2419 fopc->client = client;
2420 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2421 fopc->opc =
2422 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2423 fopc->operation_id, &msg->header,
2424 &forwarded_operation_reply_relay,
2425 fopc);
2426 fopc->timeout_task =
2427 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2428 fopc);
2429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2430 return;
2431 }
2432 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
2433 {
2434 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2435 "Peer not running");
2436 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2437 return;
2438 }
2439 peer->details.local.is_running = GNUNET_NO;
2440 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2441 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2442 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2443 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2444 reply->host_id = htonl (master_context->host_id);
2445 reply->peer_id = msg->peer_id;
2446 reply->operation_id = msg->operation_id;
2447 queue_message (client, &reply->header);
2448 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2449}
2450
2451
2452/**
2453 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2454 *
2455 * @param cls NULL
2456 * @param client identification of the client
2457 * @param message the actual message
2458 */
2459static void
2460handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2461 const struct GNUNET_MessageHeader *message)
2462{
2463 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2464 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2465 struct Peer *peer;
2466 char *config;
2467 char *xconfig;
2468 size_t c_size;
2469 size_t xc_size;
2470 uint32_t peer_id;
2471 uint16_t msize;
2472
2473 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2474 peer_id = ntohl (msg->peer_id);
2475 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2476 {
2477 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2478 "Peer not found");
2479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2480 return;
2481 }
2482 peer = peer_list[peer_id];
2483 if (GNUNET_YES == peer->is_remote)
2484 {
2485 struct ForwardedOperationContext *fopc;
2486
2487 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2488 GNUNET_SERVER_client_keep (client);
2489 fopc->client = client;
2490 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2491 fopc->opc =
2492 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2493 fopc->operation_id, &msg->header,
2494 &forwarded_operation_reply_relay,
2495 fopc);
2496 fopc->timeout_task =
2497 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2498 fopc);
2499 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2500 return;
2501 }
2502 config =
2503 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2504 &c_size);
2505 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2506 GNUNET_free (config);
2507 msize =
2508 xc_size +
2509 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2510 reply = GNUNET_realloc (xconfig, msize);
2511 (void) memmove (&reply[1], reply, xc_size);
2512 reply->header.size = htons (msize);
2513 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2514 reply->peer_id = msg->peer_id;
2515 reply->operation_id = msg->operation_id;
2516 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2517 &reply->peer_identity);
2518 reply->config_size = htons ((uint16_t) c_size);
2519 queue_message (client, &reply->header);
2520 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2521}
2522
2523
2524/**
2525 * Task for cleaing up overlay connect context structure
2526 *
2527 * @param cls the overlay connect context
2528 * @param tc the task context
2529 */
2530static void
2531occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2532{
2533 struct OverlayConnectContext *occ = cls;
2534
2535 LOG_DEBUG ("Cleaning up occ\n");
2536 GNUNET_free_non_null (occ->emsg);
2537 GNUNET_free_non_null (occ->hello);
2538 GNUNET_SERVER_client_drop (occ->client);
2539 if (NULL != occ->opc)
2540 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2541 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2542 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2543 if (NULL != occ->ch)
2544 GNUNET_CORE_disconnect (occ->ch);
2545 if (NULL != occ->ghh)
2546 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2547 if (NULL != occ->p1th)
2548 GNUNET_TRANSPORT_disconnect (occ->p1th);
2549 if (NULL != occ->p2th)
2550 GNUNET_TRANSPORT_disconnect (occ->p2th);
2551 GNUNET_free (occ);
2552}
2553
2554
2555/**
2556 * Task which will be run when overlay connect request has been timed out
2557 *
2558 * @param cls the OverlayConnectContext
2559 * @param tc the TaskContext
2560 */
2561static void
2562timeout_overlay_connect (void *cls,
2563 const struct GNUNET_SCHEDULER_TaskContext *tc)
2564{
2565 struct OverlayConnectContext *occ = cls;
2566
2567 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2568 LOG (GNUNET_ERROR_TYPE_WARNING,
2569 "Timeout while connecting peers %u and %u\n",
2570 occ->peer_id, occ->other_peer_id);
2571 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2572 occ_cleanup (occ, tc);
2573}
2574
2575
2576
2577/**
2578 * Function called to notify transport users that another
2579 * peer connected to us.
2580 *
2581 * @param cls closure
2582 * @param new_peer the peer that connected
2583 * @param ats performance data
2584 * @param ats_count number of entries in ats (excluding 0-termination)
2585 */
2586static void
2587overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2588 const struct GNUNET_ATS_Information *ats,
2589 unsigned int ats_count)
2590{
2591 struct OverlayConnectContext *occ = cls;
2592 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2593 char *new_peer_str;
2594 char *other_peer_str;
2595
2596 LOG_DEBUG ("Overlay connect notify\n");
2597 if (0 ==
2598 memcmp (new_peer, &occ->peer_identity,
2599 sizeof (struct GNUNET_PeerIdentity)))
2600 return;
2601 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2602 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2603 if (0 !=
2604 memcmp (new_peer, &occ->other_peer_identity,
2605 sizeof (struct GNUNET_PeerIdentity)))
2606 {
2607 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2608 new_peer_str, other_peer_str);
2609 GNUNET_free (new_peer_str);
2610 GNUNET_free (other_peer_str);
2611 return;
2612 }
2613 GNUNET_free (new_peer_str);
2614 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2615 GNUNET_i2s (&occ->peer_identity));
2616 GNUNET_free (other_peer_str);
2617 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2618 {
2619 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2620 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2621 }
2622 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2623 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2624 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2625 GNUNET_free_non_null (occ->emsg);
2626 occ->emsg = NULL;
2627 if (NULL != occ->p2th)
2628 GNUNET_TRANSPORT_disconnect (occ->p2th);
2629 occ->p2th = NULL;
2630 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2631 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2632 msg->header.size =
2633 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2634 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2635 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2636 msg->peer1 = htonl (occ->peer_id);
2637 msg->peer2 = htonl (occ->other_peer_id);
2638 msg->operation_id = GNUNET_htonll (occ->op_id);
2639 queue_message (occ->client, &msg->header);
2640 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2641}
2642
2643
2644/**
2645 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2646 * peer 1.
2647 *
2648 * @param cls the OverlayConnectContext
2649 * @param tc the TaskContext from scheduler
2650 */
2651static void
2652send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2653{
2654 struct OverlayConnectContext *occ = cls;
2655 char *other_peer_str;
2656
2657 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2658 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2659 return;
2660 GNUNET_assert (NULL != occ->hello);
2661 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2662 if (NULL != occ->peer2_controller)
2663 {
2664 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2665 uint16_t msize;
2666 uint16_t hello_size;
2667
2668 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2669 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2670 hello_size = ntohs (occ->hello->size);
2671 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2672 msg = GNUNET_malloc (msize);
2673 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2674 msg->header.size = htons (msize);
2675 msg->peer = htonl (occ->other_peer_id);
2676 msg->operation_id = GNUNET_htonll (occ->op_id);
2677 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2678 sizeof (struct GNUNET_PeerIdentity));
2679 memcpy (msg->hello, occ->hello, hello_size);
2680 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2681 }
2682 else
2683 {
2684 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2685 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2686 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2687 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2688 occ->send_hello_task =
2689 GNUNET_SCHEDULER_add_delayed
2690 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2691 100 * (pow (2, occ->retries++))),
2692 &send_hello, occ);
2693 }
2694 GNUNET_free (other_peer_str);
2695}
2696
2697/**
2698 * Test for checking whether HELLO message is empty
2699 *
2700 * @param cls empty flag to set
2701 * @param address the HELLO
2702 * @param expiration expiration of the HELLO
2703 * @return
2704 */
2705static int
2706test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2707 struct GNUNET_TIME_Absolute expiration)
2708{
2709 int *empty = cls;
2710
2711 *empty = GNUNET_NO;
2712 return GNUNET_OK;
2713}
2714
2715
2716/**
2717 * Function called whenever there is an update to the HELLO of peers in the
2718 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2719 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2720 *
2721 * @param cls closure
2722 * @param hello our updated HELLO
2723 */
2724static void
2725hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2726{
2727 struct OverlayConnectContext *occ = cls;
2728 int empty;
2729 uint16_t msize;
2730
2731 msize = ntohs (hello->size);
2732 empty = GNUNET_YES;
2733 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2734 hello, GNUNET_NO, &test_address,
2735 &empty);
2736 if (GNUNET_YES == empty)
2737 {
2738 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2739 return;
2740 }
2741 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2742 occ->hello = GNUNET_malloc (msize);
2743 memcpy (occ->hello, hello, msize);
2744 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2745 occ->ghh = NULL;
2746 GNUNET_TRANSPORT_disconnect (occ->p1th);
2747 occ->p1th = NULL;
2748 GNUNET_free_non_null (occ->emsg);
2749 if (NULL == occ->peer2_controller)
2750 {
2751 occ->p2th =
2752 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
2753 &occ->other_peer_identity, NULL, NULL, NULL,
2754 NULL);
2755 if (NULL == occ->p2th)
2756 {
2757 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2758 GNUNET_i2s (&occ->other_peer_identity));
2759 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2760 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2761 return;
2762 }
2763 }
2764 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2765 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2766}
2767
2768
2769/**
2770 * Function called after GNUNET_CORE_connect has succeeded (or failed
2771 * for good). Note that the private key of the peer is intentionally
2772 * not exposed here; if you need it, your process should try to read
2773 * the private key file directly (which should work if you are
2774 * authorized...).
2775 *
2776 * @param cls closure
2777 * @param server handle to the server, NULL if we failed
2778 * @param my_identity ID of this peer, NULL if we failed
2779 */
2780static void
2781core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2782 const struct GNUNET_PeerIdentity *my_identity)
2783{
2784 struct OverlayConnectContext *occ = cls;
2785
2786 GNUNET_free_non_null (occ->emsg);
2787 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2788 if ((NULL == server) || (NULL == my_identity))
2789 goto error_return;
2790 GNUNET_free (occ->emsg);
2791 occ->ch = server;
2792 occ->emsg = NULL;
2793 memcpy (&occ->peer_identity, my_identity,
2794 sizeof (struct GNUNET_PeerIdentity));
2795 occ->p1th =
2796 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2797 &occ->peer_identity, NULL, NULL, NULL, NULL);
2798 if (NULL == occ->p1th)
2799 {
2800 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2801 GNUNET_i2s (&occ->peer_identity));
2802 goto error_return;
2803 }
2804 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2805 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2806 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2807 return;
2808
2809 error_return:
2810 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2811 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2812 return;
2813}
2814
2815
2816/**
2817 * Callback to be called when forwarded get peer config operation as part of
2818 * overlay connect is successfull. Connection to Peer 1's core is made and is
2819 * checked for new connection from peer 2
2820 *
2821 * @param cls ForwardedOperationContext
2822 * @param msg the peer create success message
2823 */
2824static void
2825overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2826{
2827 struct OverlayConnectContext *occ = cls;
2828 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2829 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2830 {NULL, 0, 0}
2831 };
2832
2833 occ->opc = NULL;
2834 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2835 goto error_return;
2836 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2837 msg;
2838 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2839 sizeof (struct GNUNET_PeerIdentity));
2840 GNUNET_free_non_null (occ->emsg);
2841 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2842 occ->ch =
2843 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2844 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2845 GNUNET_NO, no_handlers);
2846 if (NULL == occ->ch)
2847 goto error_return;
2848 return;
2849
2850 error_return:
2851 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2852 occ->timeout_task =
2853 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2854}
2855
2856
2857/**
2858 * Callback which will be called to after a host registration succeeded or failed
2859 *
2860 * @param cls the RegisteredHostContext
2861 * @param emsg the error message; NULL if host registration is successful
2862 */
2863static void
2864registeredhost_registration_completion (void *cls, const char *emsg)
2865{
2866 struct RegisteredHostContext *rhc = cls;
2867 struct GNUNET_CONFIGURATION_Handle *cfg;
2868 uint32_t peer2_host_id;
2869
2870 /* if (NULL != rhc->focc_dll_head) */
2871 /* process_next_focc (rhc); */
2872 peer2_host_id = GNUNET_TESTBED_host_get_id_ (rhc->reg_host);
2873 GNUNET_assert (RHC_INIT == rhc->state);
2874 GNUNET_assert (NULL == rhc->sub_op);
2875 if ((NULL == rhc->gateway2)
2876 || ((peer2_host_id < slave_list_size) /* Check if we have the needed config */
2877 && (NULL != slave_list[peer2_host_id])))
2878 {
2879 rhc->state = RHC_LINK;
2880 cfg = (NULL == rhc->gateway2) ? our_config : slave_list[peer2_host_id]->cfg;
2881 rhc->sub_op =
2882 GNUNET_TESTBED_controller_link (rhc,
2883 rhc->gateway->controller,
2884 rhc->reg_host,
2885 rhc->host,
2886 cfg,
2887 GNUNET_NO);
2888 return;
2889 }
2890 rhc->state = RHC_GET_CFG;
2891 rhc->sub_op = GNUNET_TESTBED_get_slave_config (rhc,
2892 rhc->gateway2->controller,
2893 rhc->reg_host);
2894}
2895
2896
2897/**
2898 * Iterator to match a registered host context
2899 *
2900 * @param cls pointer 2 pointer of RegisteredHostContext
2901 * @param key current key code
2902 * @param value value in the hash map
2903 * @return GNUNET_YES if we should continue to
2904 * iterate,
2905 * GNUNET_NO if not.
2906 */
2907static int
2908reghost_match_iterator (void *cls,
2909 const struct GNUNET_HashCode * key,
2910 void *value)
2911{
2912 struct RegisteredHostContext **rh = cls;
2913 struct RegisteredHostContext *rh_val = value;
2914
2915 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
2916 {
2917 GNUNET_free (*rh);
2918 *rh = rh_val;
2919 return GNUNET_NO;
2920 }
2921 return GNUNET_YES;
2922}
2923
2924
2925/**
2926 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2927 *
2928 * @param cls NULL
2929 * @param client identification of the client
2930 * @param message the actual message
2931 */
2932static void
2933handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2934 const struct GNUNET_MessageHeader *message)
2935{
2936 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2937 struct OverlayConnectContext *occ;
2938 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2939 {NULL, 0, 0}
2940 };
2941 struct Peer *peer;
2942 uint64_t operation_id;
2943 uint32_t p1;
2944 uint32_t p2;
2945 uint32_t peer2_host_id;
2946
2947 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2948 p1 = ntohl (msg->peer1);
2949 p2 = ntohl (msg->peer2);
2950 peer2_host_id = ntohl (msg->peer2_host_id);
2951 GNUNET_assert (p1 < peer_list_size);
2952 GNUNET_assert (NULL != peer_list[p1]);
2953 peer = peer_list[p1];
2954 operation_id = GNUNET_ntohll (msg->operation_id);
2955 LOG_DEBUG ("Received overlay connect for peers %u and %u with op id: 0x%lx\n",
2956 p1, p2, operation_id);
2957 if (GNUNET_YES == peer->is_remote)
2958 {
2959 struct ForwardedOperationContext *fopc;
2960 struct Route *route_to_peer2_host;
2961 struct Route *route_to_peer1_host;
2962
2963 LOG_DEBUG ("Forwarding overlay connect\n");
2964 GNUNET_SERVER_client_keep (client);
2965 route_to_peer2_host = NULL;
2966 route_to_peer1_host = NULL;
2967 route_to_peer2_host = find_dest_route (peer2_host_id);
2968 if ((NULL != route_to_peer2_host)
2969 || (peer2_host_id == master_context->host_id))
2970 {
2971 route_to_peer1_host =
2972 find_dest_route (peer_list[p1]->details.remote.remote_host_id);
2973 GNUNET_assert (NULL != route_to_peer1_host);
2974 if ((peer2_host_id == master_context->host_id)
2975 || (route_to_peer2_host->dest != route_to_peer1_host->dest))
2976 {
2977 struct GNUNET_HashCode hash;
2978 struct RegisteredHostContext *rhc;
2979 int skip_focc;
2980
2981 rhc = GNUNET_malloc (sizeof (struct RegisteredHostContext));
2982 rhc->reg_host = host_list[route_to_peer2_host->dest];
2983 rhc->host = host_list[route_to_peer1_host->dest];
2984 GNUNET_assert (NULL != rhc->reg_host);
2985 GNUNET_assert (NULL != rhc->host);
2986 rhc->gateway = peer->details.remote.slave;
2987 rhc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
2988 slave_list[route_to_peer2_host->dest];
2989 rhc->state = RHC_INIT;
2990 GNUNET_SERVER_client_keep (client);
2991 rhc->client = client;
2992 hash = hash_hosts (rhc->reg_host, rhc->host);
2993 skip_focc = GNUNET_NO;
2994 if ((GNUNET_NO ==
2995 GNUNET_CONTAINER_multihashmap_contains
2996 (peer->details.remote.slave->reghost_map, &hash))
2997 || (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple
2998 (peer->details.remote.slave->reghost_map, &hash,
2999 reghost_match_iterator, &rhc)))
3000 {
3001 /* create and add a new registerd host context */
3002 /* add the focc to its queue */
3003 GNUNET_CONTAINER_multihashmap_put
3004 (peer->details.remote.slave->reghost_map,
3005 &hash, rhc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3006 GNUNET_assert (NULL != host_list[peer2_host_id]);
3007 queue_host_registration (peer->details.remote.slave,
3008 registeredhost_registration_completion, rhc,
3009 host_list[peer2_host_id]);
3010 }
3011 else {
3012 /* rhc is now set to the existing one from the hash map by
3013 reghost_match_iterator */
3014 /* if queue is empty then ignore creating focc and proceed with
3015 normal forwarding */
3016 if (NULL == rhc->focc_dll_head)
3017 skip_focc = GNUNET_YES;
3018 }
3019 if (GNUNET_NO == skip_focc)
3020 {
3021 struct ForwardedOverlayConnectContext *focc;
3022 uint16_t msize;
3023
3024 msize = sizeof (struct GNUNET_TESTBED_OverlayConnectMessage);
3025 focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
3026 focc->peer1 = p1;
3027 focc->peer2 = p2;
3028 focc->peer2_host_id = peer2_host_id;
3029 focc->orig_msg = GNUNET_malloc (msize);
3030 (void) memcpy (focc->orig_msg, message, msize);
3031 focc->operation_id = operation_id;
3032 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
3033 rhc->focc_dll_tail,
3034 focc);
3035 return;
3036 }
3037 }
3038 }
3039 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
3040 fopc->client = client;
3041 fopc->operation_id = operation_id;
3042 fopc->opc =
3043 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
3044 operation_id, message,
3045 &forwarded_operation_reply_relay,
3046 fopc);
3047 fopc->timeout_task =
3048 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
3049 fopc);
3050 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3051 return;
3052 }
3053 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
3054 GNUNET_SERVER_client_keep (client);
3055 occ->client = client;
3056 occ->peer_id = p1;
3057 occ->other_peer_id = p2;
3058 occ->peer = peer_list[p1];
3059 occ->op_id = GNUNET_ntohll (msg->operation_id);
3060 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
3061 {
3062 if ((peer2_host_id >= slave_list_size)
3063 || (NULL ==slave_list[peer2_host_id]))
3064 {
3065 struct GNUNET_TESTBED_NeedControllerConfig *reply;
3066
3067 LOG_DEBUG ("Need controller configuration for connecting peers %u and %u\n",
3068 occ->peer_id, occ->other_peer_id);
3069 GNUNET_free (occ);
3070 reply = GNUNET_malloc (sizeof (struct
3071 GNUNET_TESTBED_NeedControllerConfig));
3072 reply->header.size = htons (sizeof (struct
3073 GNUNET_TESTBED_NeedControllerConfig));
3074 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG);
3075 reply->controller_host_id = msg->peer2_host_id;
3076 reply->operation_id = msg->operation_id;
3077 queue_message (client, &reply->header);
3078 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3079 return;
3080 }
3081 else
3082 {
3083 occ->peer2_controller = slave_list[peer2_host_id]->controller;
3084 if (NULL == occ->peer2_controller)
3085 {
3086 GNUNET_break (0); /* What's going on? */
3087 GNUNET_SERVER_client_drop (client);
3088 GNUNET_free (occ);
3089 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3090 return;
3091 }
3092 }
3093 }
3094 else
3095 {
3096 if (GNUNET_YES == peer_list[occ->other_peer_id]->is_remote)
3097 occ->peer2_controller = peer_list[occ->other_peer_id]->details.remote.slave->controller;
3098 }
3099 /* Get the identity of the second peer */
3100 if (NULL != occ->peer2_controller)
3101 {
3102 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
3103
3104 cmsg.header.size =
3105 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
3106 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
3107 cmsg.peer_id = msg->peer2;
3108 cmsg.operation_id = msg->operation_id;
3109 occ->opc =
3110 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
3111 occ->op_id, &cmsg.header,
3112 &overlay_connect_get_config,
3113 occ);
3114 occ->emsg =
3115 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
3116 occ->timeout_task =
3117 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3118 (GNUNET_TIME_UNIT_SECONDS, 30),
3119 &timeout_overlay_connect, occ);
3120 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3121 return;
3122 }
3123 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
3124 &occ->other_peer_identity);
3125 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
3126 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3127 occ->ch =
3128 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3129 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3130 GNUNET_NO, no_handlers);
3131 if (NULL == occ->ch)
3132 occ->timeout_task =
3133 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3134 else
3135 occ->timeout_task =
3136 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3137 (GNUNET_TIME_UNIT_SECONDS, 30),
3138 &timeout_overlay_connect, occ);
3139 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3140}
3141
3142
3143/**
3144 * Function to cleanup RequestOverlayConnectContext and any associated tasks
3145 * with it
3146 *
3147 * @param rocc the RequestOverlayConnectContext
3148 */
3149static void
3150cleanup_rocc (struct RequestOverlayConnectContext *rocc)
3151{
3152 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
3153 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
3154 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
3155 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
3156 GNUNET_TRANSPORT_disconnect (rocc->th);
3157 GNUNET_free_non_null (rocc->hello);
3158 GNUNET_free (rocc);
3159}
3160
3161
3162/**
3163 * Task to timeout rocc and cleanit up
3164 *
3165 * @param cls the RequestOverlayConnectContext
3166 * @param tc the TaskContext from scheduler
3167 */
3168static void
3169timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3170{
3171 struct RequestOverlayConnectContext *rocc = cls;
3172
3173 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
3174 cleanup_rocc (rocc);
3175}
3176
3177
3178/**
3179 * Function called to notify transport users that another
3180 * peer connected to us.
3181 *
3182 * @param cls closure
3183 * @param new_peer the peer that connected
3184 * @param ats performance data
3185 * @param ats_count number of entries in ats (excluding 0-termination)
3186 */
3187static void
3188transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
3189 const struct GNUNET_ATS_Information * ats,
3190 uint32_t ats_count)
3191{
3192 struct RequestOverlayConnectContext *rocc = cls;
3193
3194 LOG_DEBUG ("Request Overlay connect notify\n");
3195 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
3196 return;
3197 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
3198 cleanup_rocc (rocc);
3199}
3200
3201
3202/**
3203 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3204 * whose identity is in RequestOverlayConnectContext
3205 *
3206 * @param cls the RequestOverlayConnectContext
3207 * @param tc the TaskContext from scheduler
3208 */
3209static void
3210attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3211{
3212 struct RequestOverlayConnectContext *rocc = cls;
3213
3214 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
3215 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
3216 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
3217 rocc->attempt_connect_task_id =
3218 GNUNET_SCHEDULER_add_delayed
3219 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3220 100 * (pow (2, rocc->retries++))),
3221 &attempt_connect_task, rocc);
3222}
3223
3224
3225/**
3226 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
3227 *
3228 * @param cls NULL
3229 * @param client identification of the client
3230 * @param message the actual message
3231 */
3232static void
3233handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
3234 const struct GNUNET_MessageHeader *message)
3235{
3236 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
3237 struct RequestOverlayConnectContext *rocc;
3238 struct Peer *peer;
3239 uint32_t peer_id;
3240 uint16_t msize;
3241 uint16_t hsize;
3242
3243 msize = ntohs (message->size);
3244 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
3245 {
3246 GNUNET_break (0);
3247 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3248 return;
3249 }
3250 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
3251 if ((NULL == msg->hello) ||
3252 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
3253 {
3254 GNUNET_break (0);
3255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3256 return;
3257 }
3258 hsize = ntohs (msg->hello->size);
3259 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
3260 {
3261 GNUNET_break (0);
3262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3263 return;
3264 }
3265 peer_id = ntohl (msg->peer);
3266 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
3267 {
3268 GNUNET_break_op (0);
3269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3270 return;
3271 }
3272 if (GNUNET_YES == peer->is_remote)
3273 {
3274 struct GNUNET_MessageHeader *msg2;
3275
3276 msg2 = GNUNET_malloc (msize);
3277 (void) memcpy (msg2, message, msize);
3278 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller, msg2);
3279 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3280 return;
3281 }
3282 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
3283 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
3284 NULL, &transport_connect_notify, NULL);
3285 if (NULL == rocc->th)
3286 {
3287 GNUNET_break (0);
3288 GNUNET_free (rocc);
3289 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3290 return;
3291 }
3292 memcpy (&rocc->a_id, &msg->peer_identity,
3293 sizeof (struct GNUNET_PeerIdentity));
3294 rocc->hello = GNUNET_malloc (hsize);
3295 memcpy (rocc->hello, msg->hello, hsize);
3296 rocc->attempt_connect_task_id =
3297 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
3298 rocc->timeout_rocc_task_id =
3299 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
3300 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3301}
3302
3303
3304/**
3305 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
3306 *
3307 * @param cls NULL
3308 * @param client identification of the client
3309 * @param message the actual message
3310 */
3311static void
3312handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
3313 const struct GNUNET_MessageHeader *message)
3314{
3315 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
3316 struct Slave *slave;
3317 struct GNUNET_TESTBED_SlaveConfiguration *reply;
3318 char *config;
3319 char *xconfig;
3320 size_t config_size;
3321 size_t xconfig_size;
3322 size_t reply_size;
3323 uint64_t op_id;
3324 uint32_t slave_id;
3325
3326 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
3327 slave_id = ntohl (msg->slave_id);
3328 op_id = GNUNET_ntohll (msg->operation_id);
3329 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
3330 {
3331 send_operation_fail_msg (client, op_id, "Slave not found");
3332 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3333 return;
3334 }
3335 slave = slave_list[slave_id];
3336 if (NULL == slave->cfg)
3337 {
3338 send_operation_fail_msg (client, op_id,
3339 "Configuration not found (slave not started by me)");
3340 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3341 return;
3342 }
3343 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
3344 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
3345 &xconfig);
3346 GNUNET_free (config);
3347 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
3348 GNUNET_break (reply_size <= UINT16_MAX);
3349 GNUNET_break (config_size <= UINT16_MAX);
3350 reply = GNUNET_realloc (xconfig, reply_size);
3351 (void) memmove (&reply[1], reply, xconfig_size);
3352 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
3353 reply->header.size = htons ((uint16_t) reply_size);
3354 reply->slave_id = msg->slave_id;
3355 reply->operation_id = msg->operation_id;
3356 reply->config_size = htons ((uint16_t) config_size);
3357 queue_message (client, &reply->header);
3358 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3359}
3360
3361
3362/**
3363 * Iterator over hash map entries.
3364 *
3365 * @param cls closure
3366 * @param key current key code
3367 * @param value value in the hash map
3368 * @return GNUNET_YES if we should continue to
3369 * iterate,
3370 * GNUNET_NO if not.
3371 */
3372static int
3373ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
3374{
3375 struct SharedService *ss = value;
3376
3377 GNUNET_assert (GNUNET_YES ==
3378 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
3379 GNUNET_free (ss->name);
3380 GNUNET_free (ss);
3381 return GNUNET_YES;
3382}
3383
3384
3385/**
3386 * Iterator for freeing hash map entries in a slave's reghost_map
3387 *
3388 * @param cls handle to the slave
3389 * @param key current key code
3390 * @param value value in the hash map
3391 * @return GNUNET_YES if we should continue to
3392 * iterate,
3393 * GNUNET_NO if not.
3394 */
3395static int
3396reghost_free_iterator (void *cls,
3397 const struct GNUNET_HashCode * key,
3398 void *value)
3399{
3400 struct Slave *slave = cls;
3401 struct RegisteredHostContext *rhc = value;
3402 struct ForwardedOverlayConnectContext *focc;
3403
3404 GNUNET_assert (GNUNET_YES ==
3405 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map,
3406 key, value));
3407 while (NULL != (focc = rhc->focc_dll_head))
3408 {
3409 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head,
3410 rhc->focc_dll_tail,
3411 focc);
3412 cleanup_focc (focc);
3413 }
3414 if (NULL != rhc->sub_op)
3415 GNUNET_TESTBED_operation_cancel (rhc->sub_op);
3416 if (NULL != rhc->client)
3417 GNUNET_SERVER_client_drop (rhc->client);
3418 GNUNET_free (value);
3419 return GNUNET_YES;
3420}
3421
3422
3423/**
3424 * Task to clean up and shutdown nicely
3425 *
3426 * @param cls NULL
3427 * @param tc the TaskContext from scheduler
3428 */
3429static void
3430shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3431{
3432 struct LCFContextQueue *lcfq;
3433 uint32_t id;
3434
3435 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
3436 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
3437 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
3438 NULL);
3439 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
3440 if (NULL != lcfq_head)
3441 {
3442 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
3443 {
3444 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
3445 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
3446 }
3447 }
3448 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
3449 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
3450 {
3451 GNUNET_free (lcfq->lcf->msg);
3452 GNUNET_free (lcfq->lcf);
3453 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
3454 GNUNET_free (lcfq);
3455 }
3456 /* Clear peer list */
3457 for (id = 0; id < peer_list_size; id++)
3458 if (NULL != peer_list[id])
3459 {
3460 if (GNUNET_NO == peer_list[id]->is_remote)
3461 {
3462 if (GNUNET_YES == peer_list[id]->details.local.is_running)
3463 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
3464 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
3465 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
3466 }
3467 GNUNET_free (peer_list[id]);
3468 }
3469 GNUNET_free_non_null (peer_list);
3470 /* Clear host list */
3471 for (id = 0; id < host_list_size; id++)
3472 if (NULL != host_list[id])
3473 GNUNET_TESTBED_host_destroy (host_list[id]);
3474 GNUNET_free_non_null (host_list);
3475 /* Clear route list */
3476 for (id = 0; id < route_list_size; id++)
3477 if (NULL != route_list[id])
3478 GNUNET_free (route_list[id]);
3479 GNUNET_free_non_null (route_list);
3480 /* Clear slave_list */
3481 for (id = 0; id < slave_list_size; id++)
3482 if (NULL != slave_list[id])
3483 {
3484 struct HostRegistration *hr_entry;
3485
3486 while (NULL != (hr_entry = slave_list[id]->hr_dll_head))
3487 {
3488 GNUNET_CONTAINER_DLL_remove (slave_list[id]->hr_dll_head,
3489 slave_list[id]->hr_dll_tail,
3490 hr_entry);
3491 GNUNET_free (hr_entry);
3492 }
3493 if (NULL != slave_list[id]->rhandle)
3494 GNUNET_TESTBED_cancel_registration (slave_list[id]->rhandle);
3495 (void) GNUNET_CONTAINER_multihashmap_iterate (slave_list[id]->reghost_map,
3496 reghost_free_iterator,
3497 slave_list[id]);
3498 GNUNET_CONTAINER_multihashmap_destroy (slave_list[id]->reghost_map);
3499 if (NULL != slave_list[id]->cfg)
3500 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
3501 if (NULL != slave_list[id]->controller)
3502 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
3503 if (NULL != slave_list[id]->controller_proc)
3504 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
3505 GNUNET_free (slave_list[id]);
3506 }
3507 GNUNET_free_non_null (slave_list);
3508 if (NULL != master_context)
3509 {
3510 GNUNET_free_non_null (master_context->master_ip);
3511 if (NULL != master_context->system)
3512 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
3513 GNUNET_free (master_context);
3514 master_context = NULL;
3515 }
3516 GNUNET_free_non_null (hostname);
3517 GNUNET_CONFIGURATION_destroy (our_config);
3518}
3519
3520
3521/**
3522 * Callback for client disconnect
3523 *
3524 * @param cls NULL
3525 * @param client the client which has disconnected
3526 */
3527static void
3528client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
3529{
3530 if (NULL == master_context)
3531 return;
3532 if (client == master_context->client)
3533 {
3534 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
3535 GNUNET_SERVER_client_drop (client);
3536 /* should not be needed as we're terminated by failure to read
3537 * from stdin, but if stdin fails for some reason, this shouldn't
3538 * hurt for now --- might need to revise this later if we ever
3539 * decide that master connections might be temporarily down
3540 * for some reason */
3541 //GNUNET_SCHEDULER_shutdown ();
3542 }
3543}
3544
3545
3546/**
3547 * Testbed setup
3548 *
3549 * @param cls closure
3550 * @param server the initialized server
3551 * @param cfg configuration to use
3552 */
3553static void
3554testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
3555 const struct GNUNET_CONFIGURATION_Handle *cfg)
3556{
3557 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
3558 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
3559 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
3560 {&handle_configure_shared_service, NULL,
3561 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
3562 {&handle_link_controllers, NULL,
3563 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
3564 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
3565 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
3566 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
3567 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
3568 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
3569 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
3570 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
3571 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
3572 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
3573 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
3574 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
3575 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
3576 0},
3577 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
3578 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
3579 {NULL}
3580 };
3581
3582 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
3583 (cfg, "testbed", "HOSTNAME", &hostname));
3584 our_config = GNUNET_CONFIGURATION_dup (cfg);
3585 GNUNET_SERVER_add_handlers (server, message_handlers);
3586 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
3587 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
3588 shutdown_task_id =
3589 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3590 &shutdown_task, NULL);
3591 LOG_DEBUG ("Testbed startup complete\n");
3592 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
3593}
3594
3595
3596/**
3597 * The starting point of execution
3598 */
3599int
3600main (int argc, char *const *argv)
3601{
3602 //sleep (15); /* Debugging */
3603 return (GNUNET_OK ==
3604 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
3605 &testbed_run, NULL)) ? 0 : 1;
3606}
3607
3608/* end of gnunet-service-testbed.c */