b30e2a92c4604736a93a62608308017f73e88c16
[oweals/gnunet.git] / src / testbed / testbed_api_peers.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--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_peers.c
23  * @brief management of the knowledge about peers in this library
24  *        (we know the peer ID, its host, pending operations, etc.)
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "testbed_api_peers.h"
29 #include "testbed_api.h"
30 #include "testbed.h"
31 #include "testbed_api_hosts.h"
32 #include "testbed_api_operations.h"
33
34 /**
35  * Function to call to start a peer_create type operation once all
36  * queues the operation is part of declare that the
37  * operation can be activated.
38  *
39  * @param cls the closure from GNUNET_TESTBED_operation_create_()
40  */
41 static void 
42 opstart_peer_create (void *cls)
43 {
44   struct OperationContext *opc = cls;
45   struct PeerCreateData *data;
46   struct GNUNET_TESTBED_PeerCreateMessage *msg;
47   char *config;
48   char *xconfig;
49   size_t c_size;
50   size_t xc_size;
51   uint16_t msize;
52
53   GNUNET_assert (OP_PEER_CREATE == opc->type);  
54   data = opc->data;
55   GNUNET_assert (NULL != data);
56   GNUNET_assert (NULL != data->peer);
57   config = GNUNET_CONFIGURATION_serialize (data->cfg, &c_size);
58   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
59   GNUNET_free (config);
60   msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
61   msg = GNUNET_realloc (xconfig, msize);
62   memmove (&msg[1], msg, xc_size);
63   msg->header.size = htons (msize);
64   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
65   msg->operation_id = GNUNET_htonll (opc->id);
66   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host));
67   msg->peer_id = htonl (data->peer->unique_id);
68   msg->config_size = htonl (c_size);
69   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
70                                     opc->c->ocq_tail, opc);
71   GNUNET_TESTBED_queue_message_ (opc->c,
72                                  (struct GNUNET_MessageHeader *) msg);
73 };
74
75
76 /**
77  * Callback which will be called when peer_create type operation is released
78  *
79  * @param cls the closure from GNUNET_TESTBED_operation_create_()
80  */
81 static void 
82 oprelease_peer_create (void *cls)
83 {
84   struct OperationContext *opc = cls;  
85
86   GNUNET_assert (NULL != opc->data);
87   GNUNET_free (opc->data);  
88   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
89   GNUNET_free (opc);
90 }
91
92
93 /**
94  * Function to called when a peer destroy operation is ready
95  *
96  * @param cls the closure from GNUNET_TESTBED_operation_create_()
97  */
98 static void 
99 opstart_peer_destroy (void *cls)
100 {
101   struct OperationContext *opc = cls;
102   struct GNUNET_TESTBED_Peer *peer;
103   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
104
105   GNUNET_assert (OP_PEER_DESTROY == opc->type);
106   peer = opc->data;
107   GNUNET_assert (NULL != peer);
108   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
109   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
110   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
111   msg->peer_id = htonl (peer->unique_id);
112   msg->operation_id = GNUNET_htonll (opc->id);
113   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
114                                     opc->c->ocq_tail, opc);
115   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
116 }
117
118
119 /**
120  * Callback which will be called when peer_create type operation is released
121  *
122  * @param cls the closure from GNUNET_TESTBED_operation_create_()
123  */
124 static void 
125 oprelease_peer_destroy (void *cls)
126 {
127   struct OperationContext *opc = cls;
128   
129   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
130   GNUNET_free (opc);
131 }
132
133
134 /**
135  * Function to called when a peer start operation is ready
136  *
137  * @param cls the closure from GNUNET_TESTBED_operation_create_()
138  */
139 static void 
140 opstart_peer_start (void *cls)
141 {
142   struct OperationContext *opc = cls;
143   struct GNUNET_TESTBED_PeerStartMessage *msg;
144   struct GNUNET_TESTBED_Peer *peer;
145
146   GNUNET_assert (OP_PEER_START == opc->type);
147   GNUNET_assert (NULL != opc->data);
148   peer = opc->data;
149   GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
150   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
151   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
152   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
153   msg->peer_id = htonl (peer->unique_id);
154   msg->operation_id = GNUNET_htonll (opc->id);
155   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
156   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
157 }
158
159
160 /**
161  * Callback which will be called when peer start type operation is released
162  *
163  * @param cls the closure from GNUNET_TESTBED_operation_create_()
164  */
165 static void 
166 oprelease_peer_start (void *cls)
167 {
168   struct OperationContext *opc = cls;
169   
170   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
171   GNUNET_free (opc);
172 }
173
174
175 /**
176  * Function to called when a peer stop operation is ready
177  *
178  * @param cls the closure from GNUNET_TESTBED_operation_create_()
179  */
180 static void 
181 opstart_peer_stop (void *cls)
182 {
183   struct OperationContext *opc = cls;
184   struct GNUNET_TESTBED_PeerStopMessage *msg;
185   struct GNUNET_TESTBED_Peer *peer;
186
187   GNUNET_assert (NULL != opc->data);
188   peer = opc->data;
189   GNUNET_assert (PS_STARTED == peer->state); 
190   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
191   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
192   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
193   msg->peer_id = htonl (peer->unique_id);
194   msg->operation_id = GNUNET_htonll (opc->id);
195   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
196   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
197 }
198
199
200 /**
201  * Callback which will be called when peer stop type operation is released
202  *
203  * @param cls the closure from GNUNET_TESTBED_operation_create_()
204  */
205 static void 
206 oprelease_peer_stop (void *cls)
207 {
208   struct OperationContext *opc = cls;
209   
210   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
211   GNUNET_free (opc);
212 }
213
214
215 /**
216  * Function to called when a peer get information operation is ready
217  *
218  * @param cls the closure from GNUNET_TESTBED_operation_create_()
219  */
220 static void 
221 opstart_peer_getinfo (void *cls)
222 {
223   struct OperationContext *opc = cls;
224   struct PeerInfoData *data;  
225   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
226
227   data = opc->data;
228   GNUNET_assert (NULL != data);  
229   msg = GNUNET_malloc (sizeof (struct
230                                GNUNET_TESTBED_PeerGetConfigurationMessage));
231   msg->header.size = htons
232     (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
233   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
234   msg->peer_id = htonl (data->peer->unique_id);
235   msg->operation_id = GNUNET_htonll (opc->id);
236   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
237   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
238 }
239
240
241 /**
242  * Callback which will be called when peer stop type operation is released
243  *
244  * @param cls the closure from GNUNET_TESTBED_operation_create_()
245  */
246 static void 
247 oprelease_peer_getinfo (void *cls)
248 {
249   struct OperationContext *opc = cls;
250   struct PeerInfoData2 *data;
251   
252   if (GNUNET_YES != opc->completed)
253     GNUNET_free_non_null (opc->data);
254   else
255   {
256     data = opc->data;
257     GNUNET_assert (NULL != data);
258     switch (data->pit)
259     {
260     case GNUNET_TESTBED_PIT_CONFIGURATION:
261       GNUNET_CONFIGURATION_destroy (data->details.cfg);
262       break;
263     case GNUNET_TESTBED_PIT_IDENTITY:
264       GNUNET_free (data->details.peer_identity);
265       break;
266     default:
267       GNUNET_assert (0);        /* We should never reach here */
268     }
269     GNUNET_free (data);
270   }
271   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
272   GNUNET_free (opc);
273 }
274
275
276
277 /**
278  * Lookup a peer by ID.
279  * 
280  * @param id global peer ID assigned to the peer
281  * @return handle to the host, NULL on error
282  */
283 struct GNUNET_TESTBED_Peer *
284 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
285 {
286   GNUNET_break (0);
287   return NULL;
288 }
289
290
291 /**
292  * Create the given peer at the specified host using the given
293  * controller.  If the given controller is not running on the target
294  * host, it should find or create a controller at the target host and
295  * delegate creating the peer.  Explicit delegation paths can be setup
296  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
297  * path exists, a direct link with a subordinate controller is setup
298  * for the first delegated peer to a particular host; the subordinate
299  * controller is then destroyed once the last peer that was delegated
300  * to the remote host is stopped.  This function is used in particular
301  * if some other controller has already assigned a unique ID to the
302  * peer.
303  *
304  * Creating the peer only creates the handle to manipulate and further
305  * configure the peer; use "GNUNET_TESTBED_peer_start" and
306  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
307  * processes.
308  *
309  * Note that the given configuration will be adjusted by the
310  * controller to avoid port/path conflicts with other peers.
311  * The "final" configuration can be obtained using
312  * 'GNUNET_TESTBED_peer_get_information'.
313  *
314  * @param unique_id unique ID for this peer
315  * @param controller controller process to use
316  * @param host host to run the peer on
317  * @param cfg Template configuration to use for the peer. Should exist until
318  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
319  * @param cb the callback to call when the peer has been created
320  * @param cls the closure to the above callback
321  * @return the operation handle
322  */
323 struct GNUNET_TESTBED_Operation *
324 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
325                                      struct GNUNET_TESTBED_Controller *controller,
326                                      struct GNUNET_TESTBED_Host *host,
327                                      const struct GNUNET_CONFIGURATION_Handle *cfg,
328                                      GNUNET_TESTBED_PeerCreateCallback cb,
329                                      void *cls)
330 {
331   struct GNUNET_TESTBED_Peer *peer;
332   struct PeerCreateData *data;
333   struct OperationContext *opc;
334
335   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
336   peer->controller = controller;
337   peer->host = host;
338   peer->unique_id = unique_id;
339   peer->state = PS_INVALID;
340   data = GNUNET_malloc (sizeof (struct PeerCreateData));
341   data->host = host;
342   data->cfg = cfg;
343   data->cb = cb;
344   data->cls = cls;
345   data->peer = peer;
346   opc = GNUNET_malloc (sizeof (struct OperationContext));
347   opc->c = controller;
348   opc->data = data;
349   opc->id = controller->operation_counter++;
350   opc->type = OP_PEER_CREATE;
351   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
352                                               &oprelease_peer_create);
353   GNUNET_TESTBED_operation_queue_insert_ (controller->opq_peer_create, opc->op);
354   return opc->op;
355 }
356
357
358 /**
359  * Create the given peer at the specified host using the given
360  * controller.  If the given controller is not running on the target
361  * host, it should find or create a controller at the target host and
362  * delegate creating the peer.  Explicit delegation paths can be setup
363  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
364  * path exists, a direct link with a subordinate controller is setup
365  * for the first delegated peer to a particular host; the subordinate
366  * controller is then destroyed once the last peer that was delegated
367  * to the remote host is stopped.
368  *
369  * Creating the peer only creates the handle to manipulate and further
370  * configure the peer; use "GNUNET_TESTBED_peer_start" and
371  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
372  * processes.
373  *
374  * Note that the given configuration will be adjusted by the
375  * controller to avoid port/path conflicts with other peers.
376  * The "final" configuration can be obtained using
377  * 'GNUNET_TESTBED_peer_get_information'.
378  *
379  * @param controller controller process to use
380  * @param host host to run the peer on
381  * @param cfg Template configuration to use for the peer. Should exist until
382  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
383  * @param cb the callback to call when the peer has been created
384  * @param cls the closure to the above callback
385  * @return the operation handle
386  */
387 struct GNUNET_TESTBED_Operation *
388 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
389                             struct GNUNET_TESTBED_Host *host,
390                             const struct GNUNET_CONFIGURATION_Handle *cfg,
391                             GNUNET_TESTBED_PeerCreateCallback cb,
392                             void *cls)
393 {
394   static uint32_t id_gen;
395
396   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
397                                               controller,
398                                               host,
399                                               cfg,
400                                               cb, cls);
401 }
402
403
404 /**
405  * Start the given peer.
406  *
407  * @param peer peer to start
408  * @return handle to the operation
409  */
410 struct GNUNET_TESTBED_Operation *
411 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
412 {
413   struct OperationContext *opc;
414
415   opc = GNUNET_malloc (sizeof (struct OperationContext));
416   opc->c = peer->controller;
417   opc->data = peer;
418   opc->id = opc->c->operation_counter++;
419   opc->type = OP_PEER_START;
420   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
421                                              &oprelease_peer_start);
422   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
423   return opc->op;  
424 }
425
426
427 /**
428  * Stop the given peer.  The handle remains valid (use
429  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
430  * state of the peer).
431  *
432  * @param peer peer to stop
433  * @return handle to the operation
434  */
435 struct GNUNET_TESTBED_Operation *
436 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
437 {
438   struct OperationContext *opc;
439
440   opc = GNUNET_malloc (sizeof (struct OperationContext));
441   opc->c = peer->controller;
442   opc->data = peer;
443   opc->id = opc->c->operation_counter++;
444   opc->type = OP_PEER_STOP;
445   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
446                                               &oprelease_peer_stop);
447   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
448   return opc->op;
449 }
450
451
452 /**
453  * Request information about a peer.
454  *
455  * @param peer peer to request information about
456  * @param pit desired information
457  * @return handle to the operation
458  */
459 struct GNUNET_TESTBED_Operation *
460 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
461                                      enum GNUNET_TESTBED_PeerInformationType pit)
462 {
463   struct OperationContext *opc;
464   struct PeerInfoData *data;
465
466   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
467   data = GNUNET_malloc (sizeof (struct PeerInfoData));
468   data->peer = peer;
469   data->pit = pit;
470   opc = GNUNET_malloc (sizeof (struct OperationContext));
471   opc->c = peer->controller;
472   opc->data = data;
473   opc->type = OP_PEER_INFO;
474   opc->id = opc->c->operation_counter++;
475   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
476                                              &oprelease_peer_getinfo);
477   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
478   return opc->op;
479 }
480
481
482 /**
483  * Change peer configuration.  Must only be called while the
484  * peer is stopped.  Ports and paths cannot be changed this
485  * way.
486  *
487  * @param peer peer to change configuration for
488  * @param cfg new configuration (differences to existing
489  *            configuration only)
490  * @return handle to the operation
491  */
492 struct GNUNET_TESTBED_Operation *
493 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
494                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
495 {
496   // FIXME: handle locally or delegate...
497   GNUNET_break (0);
498   return NULL;
499 }
500
501
502 /**
503  * Destroy the given peer; the peer should have been
504  * stopped first (if it was started).
505  *
506  * @param peer peer to stop
507  * @return handle to the operation
508  */
509 struct GNUNET_TESTBED_Operation *
510 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
511 {
512   struct OperationContext *opc;
513
514   opc = GNUNET_malloc (sizeof (struct OperationContext));
515   opc->data = peer;
516   opc->c = peer->controller;
517   opc->id = peer->controller->operation_counter++;
518   opc->type = OP_PEER_DESTROY;
519   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
520                                               &oprelease_peer_destroy);
521   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
522                                           opc->op);
523   return opc->op;
524 }
525
526
527 /**
528  * Manipulate the P2P underlay topology by configuring a link
529  * between two peers.  
530  *
531  * @param op_cls closure argument to give with the operation event
532  * @param p1 first peer
533  * @param p2 second peer
534  * @param co option to change
535  * @param ... option-specific values
536  * @return handle to the operation, NULL if configuring the link at this
537  *         time is not allowed
538  */
539 struct GNUNET_TESTBED_Operation *
540 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
541                                         struct GNUNET_TESTBED_Peer *p1,
542                                         struct GNUNET_TESTBED_Peer *p2,
543                                         enum GNUNET_TESTBED_ConnectOption co, ...)
544 {
545   GNUNET_break (0);
546   return NULL;
547 }
548
549
550
551 /**
552  * Both peers must have been started before calling this function.
553  * This function then obtains a HELLO from 'p1', gives it to 'p2'
554  * and asks 'p2' to connect to 'p1'.
555  *
556  * @param op_cls closure argument to give with the operation event
557  * @param p1 first peer
558  * @param p2 second peer
559  * @return handle to the operation, NULL if connecting these two
560  *         peers is fundamentally not possible at this time (peers
561  *         not running or underlay disallows)
562  */
563 struct GNUNET_TESTBED_Operation *
564 GNUNET_TESTBED_overlay_connect (void *op_cls,
565                                 struct GNUNET_TESTBED_Peer *p1,
566                                 struct GNUNET_TESTBED_Peer *p2)
567 {
568   struct GNUNET_TESTBED_Operation *op;
569   struct OverlayConnectData *data;
570   struct GNUNET_TESTBED_OverlayConnectMessage *msg;
571   
572   GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
573   GNUNET_assert (p1->controller == p2->controller);
574   data = GNUNET_malloc (sizeof (struct OverlayConnectData));
575   data->p1 = p1;
576   data->p2 = p2;  
577   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation)); 
578   op->controller = p1->controller;
579   op->operation_id = op->controller->operation_counter++;
580   op->type = OP_OVERLAY_CONNECT;
581   op->data = data;
582   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
583   msg->header.size = htons (sizeof (struct
584                                     GNUNET_TESTBED_OverlayConnectMessage));
585   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
586   msg->peer1 = htonl (p1->unique_id);
587   msg->peer2 = htonl (p2->unique_id);
588   msg->operation_id = GNUNET_htonll (op->operation_id);
589   GNUNET_CONTAINER_DLL_insert_tail (op->controller->op_head,
590                                     op->controller->op_tail, op);
591   GNUNET_TESTBED_queue_message_ (op->controller, 
592                                  (struct GNUNET_MessageHeader *) msg);
593   return NULL;
594 }
595
596
597
598 /* end of testbed_api_peers.c */