- restructure
[oweals/gnunet.git] / src / testbed / testbed_api.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 3, 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/testbed_api.h
23  * @brief Interface for functions internally exported from testbed_api.c
24  * @author Sree Harsha Totakura
25  */
26
27 #ifndef TESTBED_API_H
28 #define TESTBED_API_H
29
30 #include "gnunet_testbed_service.h"
31 #include "testbed.h"
32 #include "testbed_helper.h"
33
34 /**
35  * Testbed Helper binary name
36  */
37 #define HELPER_TESTBED_BINARY "gnunet-helper-testbed"
38
39
40 /**
41  * Enumeration of operations
42  */
43 enum OperationType
44 {
45     /**
46      * Peer create operation
47      */
48   OP_PEER_CREATE,
49
50     /**
51      * Peer start operation
52      */
53   OP_PEER_START,
54
55     /**
56      * Peer stop operation
57      */
58   OP_PEER_STOP,
59
60     /**
61      * Peer destroy operation
62      */
63   OP_PEER_DESTROY,
64
65     /**
66      * Get peer information operation
67      */
68   OP_PEER_INFO,
69
70     /**
71      * Overlay connection operation
72      */
73   OP_OVERLAY_CONNECT,
74
75     /**
76      * Forwarded operation
77      */
78   OP_FORWARDED,
79
80     /**
81      * Link controllers operation
82      */
83   OP_LINK_CONTROLLERS,
84
85   /**
86    * Get slave config operation
87    */
88   OP_GET_SLAVE_CONFIG,
89
90   /**
91    * Stop and destroy all peers  
92    */
93   OP_SHUTDOWN_PEERS,
94
95   /**
96    * Start/stop service at a peer
97    */
98   OP_MANAGE_SERVICE
99 };
100
101
102 /**
103  * The message queue for sending messages to the controller service
104  */
105 struct MessageQueue;
106
107
108 /**
109  * Enumeration of states of OperationContext
110  */
111 enum OperationContextState
112 {
113     /**
114      * The initial state where the associated operation has just been created
115      * and is waiting in the operation queues to be started
116      */
117   OPC_STATE_INIT = 0,
118
119     /**
120      * The operation has been started. It may occupy some resources which are to
121      * be freed if cancelled.
122      */
123   OPC_STATE_STARTED,
124
125     /**
126      * The operation has finished. The end results of this operation may occupy
127      * some resources which are to be freed by operation_done
128      */
129   OPC_STATE_FINISHED
130 };
131
132
133 /**
134  * Context information for GNUNET_TESTBED_Operation
135  */
136 struct OperationContext
137 {
138   /**
139    * next ptr for DLL
140    */
141   struct OperationContext *next;
142
143   /**
144    * prev ptr for DLL
145    */
146   struct OperationContext *prev;
147
148   /**
149    * The controller to which this operation context belongs to
150    */
151   struct GNUNET_TESTBED_Controller *c;
152
153   /**
154    * The operation
155    */
156   struct GNUNET_TESTBED_Operation *op;
157
158   /**
159    * The operation closure
160    */
161   void *op_cls;
162
163   /**
164    * Data relevant to the operation
165    */
166   void *data;
167
168   /**
169    * The id of the opearation
170    */
171   uint64_t id;
172
173   /**
174    * The type of operation
175    */
176   enum OperationType type;
177
178   /**
179    * The state of the operation
180    */
181   enum OperationContextState state;
182 };
183
184
185 /**
186  * Handle to interact with a GNUnet testbed controller.  Each
187  * controller has at least one master handle which is created when the
188  * controller is created; this master handle interacts with the
189  * controller process, destroying it destroys the controller (by
190  * closing stdin of the controller process).  Additionally,
191  * controllers can interact with each other (in a P2P fashion); those
192  * links are established via TCP/IP on the controller's service port.
193  */
194 struct GNUNET_TESTBED_Controller
195 {
196   /**
197    * The host where the controller is running
198    */
199   struct GNUNET_TESTBED_Host *host;
200
201   /**
202    * The controller callback
203    */
204   GNUNET_TESTBED_ControllerCallback cc;
205
206   /**
207    * The closure for controller callback
208    */
209   void *cc_cls;
210
211   /**
212    * The configuration to use while connecting to controller
213    */
214   struct GNUNET_CONFIGURATION_Handle *cfg;
215
216   /**
217    * The client connection handle to the controller service
218    */
219   struct GNUNET_CLIENT_Connection *client;
220
221   /**
222    * The head of the message queue
223    */
224   struct MessageQueue *mq_head;
225
226   /**
227    * The tail of the message queue
228    */
229   struct MessageQueue *mq_tail;
230
231   /**
232    * The client transmit handle
233    */
234   struct GNUNET_CLIENT_TransmitHandle *th;
235
236   /**
237    * The host registration handle; NULL if no current registration requests are
238    * present
239    */
240   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
241
242   /**
243    * The head of the opeartion context queue
244    */
245   struct OperationContext *ocq_head;
246
247   /**
248    * The tail of the operation context queue
249    */
250   struct OperationContext *ocq_tail;
251
252   /**
253    * Operation queue for simultaneous operations
254    */
255   struct OperationQueue *opq_parallel_operations;
256
257   /**
258    * Operation queue for simultaneous service connections
259    */
260   struct OperationQueue *opq_parallel_service_connections;
261
262   /**
263    * Operation queue for simultaneous topology configuration operations
264    */
265   struct OperationQueue *opq_parallel_topology_config_operations;
266
267   /**
268    * The controller event mask
269    */
270   uint64_t event_mask;
271
272   /**
273    * Did we start the receive loop yet?
274    */
275   int in_receive;
276
277   /**
278    * Did we create the host for this?
279    */
280   int aux_host;
281
282   /**
283    * The operation id counter. use current value and increment
284    */
285   uint32_t operation_counter;
286
287 };
288
289
290 /**
291  * Queues a message in send queue for sending to the service
292  *
293  * @param controller the handle to the controller
294  * @param msg the message to queue
295  */
296 void
297 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
298                                struct GNUNET_MessageHeader *msg);
299
300
301 /**
302  * Compresses given configuration using zlib compress
303  *
304  * @param config the serialized configuration
305  * @param size the size of config
306  * @param xconfig will be set to the compressed configuration (memory is fresly
307  *          allocated)
308  * @return the size of the xconfig
309  */
310 size_t
311 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
312                                  char **xconfig);
313
314
315 /**
316  * Function to serialize and compress using zlib a configuration through a
317  * configuration handle
318  *
319  * @param cfg the configuration
320  * @param size the size of configuration when serialize.  Will be set on success.
321  * @param xsize the sizeo of the compressed configuration.  Will be set on success.
322  * @return the serialized and compressed configuration
323  */
324 char *
325 GNUNET_TESTBED_compress_cfg_ (const struct GNUNET_CONFIGURATION_Handle *cfg,
326                               size_t *size, size_t *xsize);
327
328
329 /**
330  * Adds an operation to the queue of operations
331  *
332  * @param op the operation to add
333  */
334 void
335 GNUNET_TESTBED_operation_add_ (struct GNUNET_TESTBED_Operation *op);
336
337
338 /**
339  * Creates a helper initialization message. This function is here because we
340  * want to use this in testing
341  *
342  * @param trusted_ip the ip address of the controller which will be set as TRUSTED
343  *          HOST(all connections form this ip are permitted by the testbed) when
344  *          starting testbed controller at host. This can either be a single ip
345  *          address or a network address in CIDR notation.
346  * @param hostname the hostname of the destination this message is intended for
347  * @param cfg the configuration that has to used to start the testbed service
348  *          thru helper
349  * @return the initialization message
350  */
351 struct GNUNET_TESTBED_HelperInit *
352 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname, const char *hostname,
353                                         const struct GNUNET_CONFIGURATION_Handle
354                                         *cfg);
355
356
357 /**
358  * Sends the given message as an operation. The given callback is called when a
359  * reply for the operation is available.  Call
360  * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
361  * operation context if the cc hasn't been called
362  *
363  * @param controller the controller to which the message has to be sent
364  * @param operation_id the operation id of the message
365  * @param msg the message to send
366  * @param cc the callback to call when reply is available
367  * @param cc_cls the closure for the above callback
368  * @return the operation context which can be used to cancel the forwarded
369  *           operation
370  */
371 struct OperationContext *
372 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
373                                        *controller, uint64_t operation_id,
374                                        const struct GNUNET_MessageHeader *msg,
375                                        GNUNET_CLIENT_MessageHandler cc,
376                                        void *cc_cls);
377
378 /**
379  * Function to cancel an operation created by simply forwarding an operation
380  * message.
381  *
382  * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
383  */
384 void
385 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc);
386
387
388 /**
389  * Generates configuration by uncompressing configuration in given message. The
390  * given message should be of the following types:
391  * GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
392  * GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
393  *
394  * @param msg the message containing compressed configuration
395  * @return handle to the parsed configuration
396  */
397 struct GNUNET_CONFIGURATION_Handle *
398 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg);
399
400
401 /**
402  * Checks the integrity of the OpeationFailureEventMessage and if good returns
403  * the error message it contains.
404  *
405  * @param msg the OperationFailureEventMessage
406  * @return the error message
407  */
408 const char *
409 GNUNET_TESTBED_parse_error_string_ (const struct
410                                     GNUNET_TESTBED_OperationFailureEventMessage
411                                     *msg);
412
413
414 /**
415  * Function to return the operation id for a controller. The operation id is
416  * created from the controllers host id and its internal operation counter.
417  *
418  * @param controller the handle to the controller whose operation id has to be incremented
419  * @return the incremented operation id.
420  */
421 uint64_t
422 GNUNET_TESTBED_get_next_op_id (struct GNUNET_TESTBED_Controller *controller);
423
424
425 /**
426  * Like GNUNET_TESTBED_get_slave_config(), however without the host registration
427  * check. Another difference is that this function takes the id of the slave
428  * host.
429  *
430  * @param op_cls the closure for the operation
431  * @param master the handle to master controller
432  * @param slave_host_id id of the host where the slave controller is running to
433  *          the slave_host should remain valid until this operation is cancelled
434  *          or marked as finished
435  * @return the operation handle;
436  */
437 struct GNUNET_TESTBED_Operation *
438 GNUNET_TESTBED_get_slave_config_ (void *op_cls,
439                                   struct GNUNET_TESTBED_Controller *master,
440                                   uint32_t slave_host_id);
441
442
443 /**
444  * Same as the GNUNET_TESTBED_controller_link_2, but with ids for delegated host
445  * and slave host
446  *
447  * @param op_cls the operation closure for the event which is generated to
448  *          signal success or failure of this operation
449  * @param master handle to the master controller who creates the association
450  * @param delegated_host_id id of the host to which requests should be delegated
451  * @param slave_host_id id of the host which is used to run the slave controller
452  * @param sxcfg serialized and compressed configuration
453  * @param sxcfg_size the size sxcfg
454  * @param scfg_size the size of uncompressed serialized configuration
455  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
456  *          be started by the slave controller; GNUNET_NO if the slave
457  *          controller has to connect to the already started delegated
458  *          controller via TCP/IP
459  * @return the operation handle
460  */
461 struct GNUNET_TESTBED_Operation *
462 GNUNET_TESTBED_controller_link_2_ (void *op_cls,
463                                    struct GNUNET_TESTBED_Controller *master,
464                                    uint32_t delegated_host_id,
465                                    uint32_t slave_host_id, const char *sxcfg,
466                                    size_t sxcfg_size, size_t scfg_size,
467                                    int is_subordinate);
468
469
470 /**
471  * Same as the GNUNET_TESTBED_controller_link, but with ids for delegated host
472  * and slave host
473  *
474  * @param op_cls the operation closure for the event which is generated to
475  *          signal success or failure of this operation
476  * @param master handle to the master controller who creates the association
477  * @param delegated_host_id id of the host to which requests should be
478  *          delegated; cannot be NULL
479  * @param slave_host_id id of the host which should connect to controller
480  *          running on delegated host ; use NULL to make the master controller
481  *          connect to the delegated host
482  * @param slave_cfg configuration to use for the slave controller
483  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
484  *          be started by the slave controller; GNUNET_NO if the slave
485  *          controller has to connect to the already started delegated
486  *          controller via TCP/IP
487  * @return the operation handle
488  */
489 struct GNUNET_TESTBED_Operation *
490 GNUNET_TESTBED_controller_link_ (void *op_cls,
491                                  struct GNUNET_TESTBED_Controller *master,
492                                  uint32_t delegated_host_id,
493                                  uint32_t slave_host_id,
494                                  const struct GNUNET_CONFIGURATION_Handle
495                                  *slave_cfg, int is_subordinate);
496
497
498
499
500 #endif
501 /* end of testbed_api.h */