- separate overlay connect message handling as module
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.h
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.h
23  * @brief data structures shared amongst components of TESTBED service
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_core_service.h"
32
33 #include "testbed.h"
34 #include "testbed_api.h"
35 #include "testbed_api_operations.h"
36 #include "testbed_api_hosts.h"
37 #include "gnunet_testing_lib.h"
38
39
40 /**
41  * Generic logging
42  */
43 #define LOG(kind,...)                           \
44   GNUNET_log (kind, __VA_ARGS__)
45
46 /**
47  * Debug logging
48  */
49 #define LOG_DEBUG(...)                          \
50   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
51
52 /**
53  * By how much should the arrays lists grow
54  */
55 #define LIST_GROW_STEP 10
56
57 /**
58  * Default timeout for operations which may take some time
59  */
60 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
61
62
63 /**
64  * A routing entry
65  */
66 struct Route
67 {
68   /**
69    * destination host
70    */
71   uint32_t dest;
72
73   /**
74    * The destination host is reachable thru
75    */
76   uint32_t thru;
77 };
78
79
80 /**
81  * Context information for operations forwarded to subcontrollers
82  */
83 struct ForwardedOperationContext
84 {
85   /**
86    * The next pointer for DLL
87    */
88   struct ForwardedOperationContext *next;
89
90   /**
91    * The prev pointer for DLL
92    */
93   struct ForwardedOperationContext *prev;
94   
95   /**
96    * The generated operation context
97    */
98   struct OperationContext *opc;
99
100   /**
101    * The client to which we have to reply
102    */
103   struct GNUNET_SERVER_Client *client;
104
105   /**
106    * Closure pointer
107    */
108   void *cls;
109
110   /**
111    * Task ID for the timeout task
112    */
113   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
114
115   /**
116    * The id of the operation that has been forwarded
117    */
118   uint64_t operation_id;
119
120   /**
121    * The type of the operation which is forwarded
122    */
123   enum OperationType type;
124
125 };
126
127
128 /**
129  * A DLL of host registrations to be made
130  */
131 struct HostRegistration
132 {
133   /**
134    * next registration in the DLL
135    */
136   struct HostRegistration *next;
137
138   /**
139    * previous registration in the DLL
140    */
141   struct HostRegistration *prev;
142
143   /**
144    * The callback to call after this registration's status is available
145    */
146   GNUNET_TESTBED_HostRegistrationCompletion cb;
147
148   /**
149    * The closure for the above callback
150    */
151   void *cb_cls;
152
153   /**
154    * The host that has to be registered
155    */
156   struct GNUNET_TESTBED_Host *host;
157 };
158
159
160 /**
161  * Context information used while linking controllers
162  */
163 struct LinkControllersContext
164 {
165   /**
166    * The client which initiated the link controller operation
167    */
168   struct GNUNET_SERVER_Client *client;
169
170   /**
171    * The ID of the operation
172    */
173   uint64_t operation_id;
174
175 };
176
177
178 /**
179  * Structure representing a connected(directly-linked) controller
180  */
181 struct Slave
182 {
183   /**
184    * The controller process handle if we had started the controller
185    */
186   struct GNUNET_TESTBED_ControllerProc *controller_proc;
187
188   /**
189    * The controller handle
190    */
191   struct GNUNET_TESTBED_Controller *controller;
192
193   /**
194    * The configuration of the slave. Cannot be NULL
195    */
196   struct GNUNET_CONFIGURATION_Handle *cfg;
197
198   /**
199    * handle to lcc which is associated with this slave startup. Should be set to
200    * NULL when the slave has successfully started up
201    */
202   struct LinkControllersContext *lcc;
203
204   /**
205    * Head of the host registration DLL
206    */
207   struct HostRegistration *hr_dll_head;
208
209   /**
210    * Tail of the host registration DLL
211    */
212   struct HostRegistration *hr_dll_tail;
213
214   /**
215    * The current host registration handle
216    */
217   struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
218
219   /**
220    * Hashmap to hold Registered host contexts
221    */
222   struct GNUNET_CONTAINER_MultiHashMap *reghost_map;
223
224   /**
225    * The id of the host this controller is running on
226    */
227   uint32_t host_id;
228
229 };
230
231
232 /**
233  * A peer
234  */
235
236 struct Peer
237 {
238   
239   union
240   {
241     struct
242     {
243       /**
244        * The peer handle from testing API
245        */
246       struct GNUNET_TESTING_Peer *peer;
247
248       /**
249        * The modified (by GNUNET_TESTING_peer_configure) configuration this
250        * peer is configured with
251        */
252       struct GNUNET_CONFIGURATION_Handle *cfg;
253       
254       /**
255        * Is the peer running
256        */
257       int is_running;
258
259     } local;
260
261     struct
262     {
263       /**
264        * The slave this peer is started through
265        */
266       struct Slave *slave;
267
268       /**
269        * The id of the remote host this peer is running on
270        */
271       uint32_t remote_host_id;
272
273     } remote;
274
275   } details;
276
277   /**
278    * Is this peer locally created?
279    */
280   int is_remote;
281
282   /**
283    * Our local reference id for this peer
284    */
285   uint32_t id;
286
287   /**
288    * References to peers are using in forwarded overlay contexts and remote
289    * overlay connect contexts. A peer can only be destroyed after all such
290    * contexts are destroyed. For this, we maintain a reference counter. When we
291    * use a peer in any such context, we increment this counter. We decrement it
292    * when we are destroying these contexts
293    */
294   uint32_t reference_cnt;
295
296   /**
297    * While destroying a peer, due to the fact that there could be references to
298    * this peer, we delay the peer destroy to a further time. We do this by using
299    * this flag to destroy the peer while destroying a context in which this peer
300    * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
301    * the peer
302    */
303   uint32_t destroy_flag;
304
305 };
306
307
308 /**
309  * The main context information associated with the client which started us
310  */
311 struct Context
312 {
313   /**
314    * The client handle associated with this context
315    */
316   struct GNUNET_SERVER_Client *client;
317
318   /**
319    * The network address of the master controller
320    */
321   char *master_ip;
322
323   /**
324    * The TESTING system handle for starting peers locally
325    */
326   struct GNUNET_TESTING_System *system;
327   
328   /**
329    * Our host id according to this context
330    */
331   uint32_t host_id;
332 };
333
334
335 /**
336  * The structure for identifying a shared service
337  */
338 struct SharedService
339 {
340   /**
341    * The name of the shared service
342    */
343   char *name;
344
345   /**
346    * Number of shared peers per instance of the shared service
347    */
348   uint32_t num_shared;
349
350   /**
351    * Number of peers currently sharing the service
352    */
353   uint32_t num_sharing;
354 };
355
356
357 /**
358  * Context information to used during operations which forward the overlay
359  * connect message
360  */
361 struct ForwardedOverlayConnectContext
362 {
363   /**
364    * next ForwardedOverlayConnectContext in the DLL
365    */
366   struct ForwardedOverlayConnectContext *next;
367
368   /**
369    * previous ForwardedOverlayConnectContext in the DLL
370    */
371   struct ForwardedOverlayConnectContext *prev;
372
373   /**
374    * A copy of the original overlay connect message
375    */
376   struct GNUNET_MessageHeader *orig_msg;
377
378   /**
379    * The id of the operation which created this context information
380    */
381   uint64_t operation_id;
382
383   /**
384    * the id of peer 1
385    */
386   uint32_t peer1;
387   
388   /**
389    * The id of peer 2
390    */
391   uint32_t peer2;
392   
393   /**
394    * Id of the host where peer2 is running
395    */
396   uint32_t peer2_host_id;
397 };
398
399
400 /**
401  * This context information will be created for each host that is registered at
402  * slave controllers during overlay connects.
403  */
404 struct RegisteredHostContext
405 {
406   /**
407    * The host which is being registered
408    */
409   struct GNUNET_TESTBED_Host *reg_host;
410
411   /**
412    * The host of the controller which has to connect to the above rhost
413    */
414   struct GNUNET_TESTBED_Host *host;
415
416   /**
417    * The gateway to which this operation is forwarded to
418    */
419   struct Slave *gateway;
420
421   /**
422    * The gateway through which peer2's controller can be reached
423    */
424   struct Slave *gateway2;
425
426   /**
427    * Handle for sub-operations
428    */
429   struct GNUNET_TESTBED_Operation *sub_op;
430
431   /**
432    * The client which initiated the link controller operation
433    */
434   struct GNUNET_SERVER_Client *client;
435
436   /**
437    * Head of the ForwardedOverlayConnectContext DLL
438    */
439   struct ForwardedOverlayConnectContext *focc_dll_head;
440
441   /**
442    * Tail of the ForwardedOverlayConnectContext DLL
443    */
444   struct ForwardedOverlayConnectContext *focc_dll_tail;
445   
446   /**
447    * Enumeration of states for this context
448    */
449   enum RHCState {
450
451     /**
452      * The initial state
453      */
454     RHC_INIT = 0,
455
456     /**
457      * State where we attempt to get peer2's controller configuration
458      */
459     RHC_GET_CFG,
460
461     /**
462      * State where we attempt to link the controller of peer 1 to the controller
463      * of peer2
464      */
465     RHC_LINK,
466
467     /**
468      * State where we attempt to do the overlay connection again
469      */
470     RHC_OL_CONNECT
471     
472   } state;
473
474 };
475
476
477 /**
478  * States of LCFContext
479  */
480 enum LCFContextState
481 {
482   /**
483    * The Context has been initialized; Nothing has been done on it
484    */
485   INIT,
486
487   /**
488    * Delegated host has been registered at the forwarding controller
489    */
490   DELEGATED_HOST_REGISTERED,
491   
492   /**
493    * The slave host has been registred at the forwarding controller
494    */
495   SLAVE_HOST_REGISTERED,
496   
497   /**
498    * The context has been finished (may have error)
499    */
500   FINISHED
501 };
502
503
504 /**
505  * Link controllers request forwarding context
506  */
507 struct LCFContext
508 {
509   /**
510    * The gateway which will pass the link message to delegated host
511    */
512   struct Slave *gateway;
513
514   /**
515    * The controller link message that has to be forwarded to
516    */
517   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
518
519   /**
520    * The client which has asked to perform this operation
521    */
522   struct GNUNET_SERVER_Client *client;
523
524   /**
525    * Handle for operations which are forwarded while linking controllers
526    */
527   struct ForwardedOperationContext *fopc;
528
529   /**
530    * The id of the operation which created this context
531    */
532   uint64_t operation_id;
533
534   /**
535    * The state of this context
536    */
537   enum LCFContextState state;
538
539   /**
540    * The delegated host
541    */
542   uint32_t delegated_host_id;
543
544   /**
545    * The slave host
546    */
547   uint32_t slave_host_id;
548
549 };
550
551
552 /**
553  * Structure of a queue entry in LCFContext request queue
554  */
555 struct LCFContextQueue
556 {
557   /**
558    * The LCFContext
559    */
560   struct LCFContext *lcf;
561
562   /**
563    * Head prt for DLL
564    */
565   struct LCFContextQueue *next;
566
567   /**
568    * Tail ptr for DLL
569    */
570   struct LCFContextQueue *prev;
571 };
572
573 /**
574  * Our configuration
575  */
576 struct GNUNET_CONFIGURATION_Handle *our_config;
577
578 /**
579  * The master context; generated with the first INIT message
580  */
581 extern struct Context *TESTBED_context;
582
583 /**
584  * DLL head for forwarded operation contexts
585  */
586 extern struct ForwardedOperationContext *fopcq_head;
587
588 /**
589  * DLL tail for forwarded operation contexts
590  */
591 extern struct ForwardedOperationContext *fopcq_tail;
592
593 /**
594  * A list of peers we know about
595  */
596 extern struct Peer **TESTBED_peer_list;
597
598 /**
599  * Array of hosts
600  */
601 extern struct GNUNET_TESTBED_Host **TESTBED_host_list;
602
603 /**
604  * A list of directly linked neighbours
605  */
606 extern struct Slave **TESTBED_slave_list;
607
608 /**
609  * The size of the peer list
610  */
611 extern unsigned int TESTBED_peer_list_size;
612
613 /**
614  * The size of the host list
615  */
616 extern unsigned int TESTBED_host_list_size;
617
618 /**
619  * The size of directly linked neighbours list
620  */
621 extern unsigned int TESTBED_slave_list_size;
622
623
624 /**
625  * Queues a message in send queue for sending to the service
626  *
627  * @param client the client to whom the queued message has to be sent
628  * @param msg the message to queue
629  */
630 void
631 TESTBED_queue_message (struct GNUNET_SERVER_Client *client,
632                        struct GNUNET_MessageHeader *msg);
633
634
635 /**
636  * Function to destroy a peer
637  *
638  * @param peer the peer structure to destroy
639  */
640 void
641 TESTBED_destroy_peer (struct Peer *peer);
642
643
644 /**
645  * Looks up in the hello cache and returns the HELLO of the given peer
646  *
647  * @param id the peer identity of the peer whose HELLO has to be looked up
648  * @return the HELLO message; NULL if not found
649  */
650 const struct GNUNET_MessageHeader *
651 TESTBED_hello_cache_lookup (const struct GNUNET_PeerIdentity *id);
652
653 /**
654  * Caches the HELLO of the given peer. Updates the HELLO if it was already
655  * cached before
656  *
657  * @param id the peer identity of the peer whose HELLO has to be cached
658  * @param hello the HELLO message
659  */
660 void
661 TESTBED_hello_cache_add (const struct GNUNET_PeerIdentity *id,
662                          const struct GNUNET_MessageHeader *hello);
663
664
665 /**
666  * Initializes the cache
667  *
668  * @param size the size of the cache
669  */
670 void
671 TESTBED_cache_init (unsigned int size);
672
673
674 /**
675  * Clear cache
676  */
677 void
678 TESTBED_cache_clear ();
679
680
681 /**
682  * Finds the route with directly connected host as destination through which
683  * the destination host can be reached
684  *
685  * @param host_id the id of the destination host
686  * @return the route with directly connected destination host; NULL if no route
687  *           is found
688  */
689 struct Route *
690 TESTBED_find_dest_route (uint32_t host_id);
691
692
693 /**
694  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
695  *
696  * @param cls NULL
697  * @param client identification of the client
698  * @param message the actual message
699  */
700 void
701 TESTBED_handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
702                                 const struct GNUNET_MessageHeader *message);
703
704
705 /**
706  * Adds a host registration's request to a slave's registration queue
707  *
708  * @param slave the slave controller at which the given host has to be
709  *          registered 
710  * @param cb the host registration completion callback
711  * @param cb_cls the closure for the host registration completion callback
712  * @param host the host which has to be registered
713  */
714 void
715 TESTBED_queue_host_registration (struct Slave *slave,
716                                  GNUNET_TESTBED_HostRegistrationCompletion cb,
717                                  void *cb_cls,
718                                  struct GNUNET_TESTBED_Host *host);
719
720
721 /**
722  * Callback to relay the reply msg of a forwarded operation back to the client
723  *
724  * @param cls ForwardedOperationContext
725  * @param msg the message to relay
726  */
727 void
728 TESTBED_forwarded_operation_reply_relay (void *cls,
729                                          const struct GNUNET_MessageHeader *msg);
730
731
732 /**
733  * Task to free resources when forwarded operation has been timedout
734  *
735  * @param cls the ForwardedOperationContext
736  * @param tc the task context from scheduler
737  */
738 void
739 TESTBED_forwarded_operation_timeout (void *cls,
740                                      const struct GNUNET_SCHEDULER_TaskContext *tc);
741
742
743 /**
744  * Send operation failure message to client
745  *
746  * @param client the client to which the failure message has to be sent to
747  * @param operation_id the id of the failed operation
748  * @param emsg the error message; can be NULL
749  */
750 void
751 TESTBED_send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
752                                  uint64_t operation_id, const char *emsg);
753
754
755 /**
756  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
757  *
758  * @param cls NULL
759  * @param client identification of the client
760  * @param message the actual message
761  */
762 void
763 TESTBED_handle_overlay_request_connect (void *cls,
764                                         struct GNUNET_SERVER_Client *client, 
765                                         const struct GNUNET_MessageHeader
766                                         *message);
767
768
769 /**
770  * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
771  *
772  * @param rhc the RegisteredHostContext
773  */
774 void
775 TESTBED_process_next_focc (struct RegisteredHostContext *rhc);
776
777
778 /**
779  * Cleans up ForwardedOverlayConnectContext
780  *
781  * @param focc the ForwardedOverlayConnectContext to cleanup
782  */
783 void
784 TESTBED_cleanup_focc (struct ForwardedOverlayConnectContext *focc);
785
786
787 /**
788  * Clears all pending overlay connect contexts in queue
789  */
790 void
791 TESTBED_free_occq ();
792
793
794 /**
795  * Clears all pending remote overlay connect contexts in queue
796  */
797 void
798 TESTBED_free_roccq ();
799
800
801
802 /* End of gnunet-service-testbed.h */