- unify testbed operation handlers, avoid pointer arithmetic
[oweals/gnunet.git] / src / testbed / testbed_api.h
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 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    * The controller to which this operation context belongs to
140    */
141   struct GNUNET_TESTBED_Controller *c;
142
143   /**
144    * The operation
145    */
146   struct GNUNET_TESTBED_Operation *op;
147
148   /**
149    * The operation closure
150    */
151   void *op_cls;
152
153   /**
154    * Data relevant to the operation
155    */
156   void *data;
157
158   /**
159    * The id of the opearation
160    */
161   uint64_t id;
162
163   /**
164    * The type of operation
165    */
166   enum OperationType type;
167
168   /**
169    * The state of the operation
170    */
171   enum OperationContextState state;
172 };
173
174
175 /**
176  * Handle to interact with a GNUnet testbed controller.  Each
177  * controller has at least one master handle which is created when the
178  * controller is created; this master handle interacts with the
179  * controller process, destroying it destroys the controller (by
180  * closing stdin of the controller process).  Additionally,
181  * controllers can interact with each other (in a P2P fashion); those
182  * links are established via TCP/IP on the controller's service port.
183  */
184 struct GNUNET_TESTBED_Controller
185 {
186   /**
187    * The host where the controller is running
188    */
189   struct GNUNET_TESTBED_Host *host;
190
191   /**
192    * The controller callback
193    */
194   GNUNET_TESTBED_ControllerCallback cc;
195
196   /**
197    * The closure for controller callback
198    */
199   void *cc_cls;
200
201   /**
202    * The configuration to use while connecting to controller
203    */
204   struct GNUNET_CONFIGURATION_Handle *cfg;
205
206   /**
207    * The client connection handle to the controller service
208    */
209   struct GNUNET_CLIENT_Connection *client;
210
211   /**
212    * The head of the message queue
213    */
214   struct MessageQueue *mq_head;
215
216   /**
217    * The tail of the message queue
218    */
219   struct MessageQueue *mq_tail;
220
221   /**
222    * The client transmit handle
223    */
224   struct GNUNET_CLIENT_TransmitHandle *th;
225
226   /**
227    * The host registration handle; NULL if no current registration requests are
228    * present
229    */
230   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
231
232   /**
233    * The map of active operation contexts
234    */
235   struct GNUNET_CONTAINER_MultiHashMap32 *opc_map;
236
237   /**
238    * Operation queue for simultaneous operations
239    */
240   struct OperationQueue *opq_parallel_operations;
241
242   /**
243    * Operation queue for simultaneous service connections
244    */
245   struct OperationQueue *opq_parallel_service_connections;
246
247   /**
248    * Operation queue for simultaneous topology configuration operations
249    */
250   struct OperationQueue *opq_parallel_topology_config_operations;
251
252   /**
253    * The controller event mask
254    */
255   uint64_t event_mask;
256
257   /**
258    * Did we start the receive loop yet?
259    */
260   int in_receive;
261
262   /**
263    * The operation id counter. use current value and increment
264    */
265   uint32_t operation_counter;
266
267 };
268
269
270 /**
271  * Queues a message in send queue for sending to the service
272  *
273  * @param controller the handle to the controller
274  * @param msg the message to queue
275  */
276 void
277 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
278                                struct GNUNET_MessageHeader *msg);
279
280
281 /**
282  * Inserts the given operation context into the operation context map of the
283  * given controller.  Creates the operation context map if one does not exist
284  * for the controller
285  *
286  * @param c the controller
287  * @param opc the operation context to be inserted
288  */
289 void
290 GNUNET_TESTBED_insert_opc_ (struct GNUNET_TESTBED_Controller *c,
291                             struct OperationContext *opc);
292
293
294 /**
295  * Removes the given operation context from the operation context map of the
296  * given controller
297  *
298  * @param c the controller
299  * @param opc the operation context to remove 
300  */
301 void
302 GNUNET_TESTBED_remove_opc_ (const struct GNUNET_TESTBED_Controller *c,
303                             struct OperationContext *opc);
304
305
306 /**
307  * Compresses given configuration using zlib compress
308  *
309  * @param config the serialized configuration
310  * @param size the size of config
311  * @param xconfig will be set to the compressed configuration (memory is fresly
312  *          allocated)
313  * @return the size of the xconfig
314  */
315 size_t
316 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
317                                  char **xconfig);
318
319
320 /**
321  * Function to serialize and compress using zlib a configuration through a
322  * configuration handle
323  *
324  * @param cfg the configuration
325  * @param size the size of configuration when serialize.  Will be set on success.
326  * @param xsize the sizeo of the compressed configuration.  Will be set on success.
327  * @return the serialized and compressed configuration
328  */
329 char *
330 GNUNET_TESTBED_compress_cfg_ (const struct GNUNET_CONFIGURATION_Handle *cfg,
331                               size_t *size, size_t *xsize);
332
333
334 /**
335  * Creates a helper initialization message. This function is here because we
336  * want to use this in testing
337  *
338  * @param trusted_ip the ip address of the controller which will be set as TRUSTED
339  *          HOST(all connections form this ip are permitted by the testbed) when
340  *          starting testbed controller at host. This can either be a single ip
341  *          address or a network address in CIDR notation.
342  * @param hostname the hostname of the destination this message is intended for
343  * @param cfg the configuration that has to used to start the testbed service
344  *          thru helper
345  * @return the initialization message
346  */
347 struct GNUNET_TESTBED_HelperInit *
348 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname, const char *hostname,
349                                         const struct GNUNET_CONFIGURATION_Handle
350                                         *cfg);
351
352
353 /**
354  * Sends the given message as an operation. The given callback is called when a
355  * reply for the operation is available.  Call
356  * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
357  * operation context if the cc hasn't been called
358  *
359  * @param controller the controller to which the message has to be sent
360  * @param operation_id the operation id of the message
361  * @param msg the message to send
362  * @param cc the callback to call when reply is available
363  * @param cc_cls the closure for the above callback
364  * @return the operation context which can be used to cancel the forwarded
365  *           operation
366  */
367 struct OperationContext *
368 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
369                                        *controller, uint64_t operation_id,
370                                        const struct GNUNET_MessageHeader *msg,
371                                        GNUNET_CLIENT_MessageHandler cc,
372                                        void *cc_cls);
373
374 /**
375  * Function to cancel an operation created by simply forwarding an operation
376  * message.
377  *
378  * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
379  */
380 void
381 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc);
382
383
384 /**
385  * Generates configuration by uncompressing configuration in given message. The
386  * given message should be of the following types:
387  * GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
388  * GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
389  *
390  * @param msg the message containing compressed configuration
391  * @return handle to the parsed configuration
392  */
393 struct GNUNET_CONFIGURATION_Handle *
394 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg);
395
396
397 /**
398  * Checks the integrity of the OpeationFailureEventMessage and if good returns
399  * the error message it contains.
400  *
401  * @param msg the OperationFailureEventMessage
402  * @return the error message
403  */
404 const char *
405 GNUNET_TESTBED_parse_error_string_ (const struct
406                                     GNUNET_TESTBED_OperationFailureEventMessage
407                                     *msg);
408
409
410 /**
411  * Function to return the operation id for a controller. The operation id is
412  * created from the controllers host id and its internal operation counter.
413  *
414  * @param controller the handle to the controller whose operation id has to be incremented
415  * @return the incremented operation id.
416  */
417 uint64_t
418 GNUNET_TESTBED_get_next_op_id (struct GNUNET_TESTBED_Controller *controller);
419
420
421 /**
422  * Like GNUNET_TESTBED_get_slave_config(), however without the host registration
423  * check. Another difference is that this function takes the id of the slave
424  * host.
425  *
426  * @param op_cls the closure for the operation
427  * @param master the handle to master controller
428  * @param slave_host_id id of the host where the slave controller is running to
429  *          the slave_host should remain valid until this operation is cancelled
430  *          or marked as finished
431  * @return the operation handle;
432  */
433 struct GNUNET_TESTBED_Operation *
434 GNUNET_TESTBED_get_slave_config_ (void *op_cls,
435                                   struct GNUNET_TESTBED_Controller *master,
436                                   uint32_t slave_host_id);
437
438
439 /**
440  * Same as the GNUNET_TESTBED_controller_link_2, but with ids for delegated host
441  * and slave host
442  *
443  * @param op_cls the operation closure for the event which is generated to
444  *          signal success or failure of this operation
445  * @param master handle to the master controller who creates the association
446  * @param delegated_host_id id of the host to which requests should be delegated
447  * @param slave_host_id id of the host which is used to run the slave controller
448  * @param sxcfg serialized and compressed configuration
449  * @param sxcfg_size the size sxcfg
450  * @param scfg_size the size of uncompressed serialized configuration
451  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
452  *          be started by the slave controller; GNUNET_NO if the slave
453  *          controller has to connect to the already started delegated
454  *          controller via TCP/IP
455  * @return the operation handle
456  */
457 struct GNUNET_TESTBED_Operation *
458 GNUNET_TESTBED_controller_link_2_ (void *op_cls,
459                                    struct GNUNET_TESTBED_Controller *master,
460                                    uint32_t delegated_host_id,
461                                    uint32_t slave_host_id, const char *sxcfg,
462                                    size_t sxcfg_size, size_t scfg_size,
463                                    int is_subordinate);
464
465
466 /**
467  * Same as the GNUNET_TESTBED_controller_link, but with ids for delegated host
468  * and slave host
469  *
470  * @param op_cls the operation closure for the event which is generated to
471  *          signal success or failure of this operation
472  * @param master handle to the master controller who creates the association
473  * @param delegated_host_id id of the host to which requests should be
474  *          delegated; cannot be NULL
475  * @param slave_host_id id of the host which should connect to controller
476  *          running on delegated host ; use NULL to make the master controller
477  *          connect to the delegated host
478  * @param slave_cfg configuration to use for the slave controller
479  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
480  *          be started by the slave controller; GNUNET_NO if the slave
481  *          controller has to connect to the already started delegated
482  *          controller via TCP/IP
483  * @return the operation handle
484  */
485 struct GNUNET_TESTBED_Operation *
486 GNUNET_TESTBED_controller_link_ (void *op_cls,
487                                  struct GNUNET_TESTBED_Controller *master,
488                                  uint32_t delegated_host_id,
489                                  uint32_t slave_host_id,
490                                  const struct GNUNET_CONFIGURATION_Handle
491                                  *slave_cfg, int is_subordinate);
492
493
494
495
496 #endif
497 /* end of testbed_api.h */