glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / testbed / testbed_api_peers.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14  */
15
16 /**
17  * @file testbed/testbed_api_peers.c
18  * @brief management of the knowledge about peers in this library
19  *        (we know the peer ID, its host, pending operations, etc.)
20  * @author Christian Grothoff
21  * @author Sree Harsha Totakura
22  */
23
24 #include "platform.h"
25 #include "testbed_api_peers.h"
26 #include "testbed_api.h"
27 #include "testbed.h"
28 #include "testbed_api_hosts.h"
29 #include "testbed_api_operations.h"
30
31
32 /**
33  * Peer list DLL head
34  */
35 static struct GNUNET_TESTBED_Peer *peer_list_head;
36
37 /**
38  * Peer list DLL tail
39  */
40 static struct GNUNET_TESTBED_Peer *peer_list_tail;
41
42
43 /**
44  * Adds a peer to the peer list
45  *
46  * @param peer the peer to add to the peer list
47  */
48 void
49 GNUNET_TESTBED_peer_register_ (struct GNUNET_TESTBED_Peer *peer)
50 {
51   GNUNET_CONTAINER_DLL_insert_tail (peer_list_head, peer_list_tail, peer);
52 }
53
54
55 /**
56  * Removes a peer from the peer list
57  *
58  * @param peer the peer to remove
59  */
60 void
61 GNUNET_TESTBED_peer_deregister_ (struct GNUNET_TESTBED_Peer *peer)
62 {
63   GNUNET_CONTAINER_DLL_remove (peer_list_head, peer_list_tail, peer);
64 }
65
66
67 /**
68  * Frees all peers
69  */
70 void
71 GNUNET_TESTBED_cleanup_peers_ (void)
72 {
73   struct GNUNET_TESTBED_Peer *peer;
74
75   while (NULL != (peer = peer_list_head))
76   {
77     GNUNET_TESTBED_peer_deregister_ (peer);
78     GNUNET_free (peer);
79   }
80 }
81
82
83
84 /**
85  * Function to call to start a peer_create type operation once all
86  * queues the operation is part of declare that the
87  * operation can be activated.
88  *
89  * @param cls the closure from GNUNET_TESTBED_operation_create_()
90  */
91 static void
92 opstart_peer_create (void *cls)
93 {
94   struct OperationContext *opc = cls;
95   struct PeerCreateData *data = opc->data;
96   struct GNUNET_TESTBED_PeerCreateMessage *msg;
97   struct GNUNET_MQ_Envelope *env;
98   char *config;
99   char *xconfig;
100   size_t c_size;
101   size_t xc_size;
102
103   GNUNET_assert (OP_PEER_CREATE == opc->type);
104   GNUNET_assert (NULL != data);
105   GNUNET_assert (NULL != data->peer);
106   opc->state = OPC_STATE_STARTED;
107   config = GNUNET_CONFIGURATION_serialize (data->cfg,
108                                            &c_size);
109   xc_size = GNUNET_TESTBED_compress_config_ (config,
110                                              c_size,
111                                              &xconfig);
112   GNUNET_free (config);
113   env = GNUNET_MQ_msg_extra (msg,
114                              xc_size,
115                              GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER);
116   msg->operation_id = GNUNET_htonll (opc->id);
117   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host));
118   msg->peer_id = htonl (data->peer->unique_id);
119   msg->config_size = htons ((uint16_t) c_size);
120   GNUNET_memcpy (&msg[1],
121           xconfig,
122           xc_size);
123   GNUNET_MQ_send (opc->c->mq,
124                   env);
125   GNUNET_free (xconfig);
126   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
127 }
128
129
130 /**
131  * Callback which will be called when peer_create type operation is released
132  *
133  * @param cls the closure from GNUNET_TESTBED_operation_create_()
134  */
135 static void
136 oprelease_peer_create (void *cls)
137 {
138   struct OperationContext *opc = cls;
139
140   switch (opc->state)
141   {
142   case OPC_STATE_STARTED:
143     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
144     /* No break we continue flow */
145   case OPC_STATE_INIT:
146     GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
147     GNUNET_free (opc->data);
148     break;
149   case OPC_STATE_FINISHED:
150     break;
151   }
152   GNUNET_free (opc);
153 }
154
155
156 /**
157  * Function called when a peer destroy operation is ready
158  *
159  * @param cls the closure from GNUNET_TESTBED_operation_create_()
160  */
161 static void
162 opstart_peer_destroy (void *cls)
163 {
164   struct OperationContext *opc = cls;
165   struct GNUNET_TESTBED_Peer *peer = opc->data;
166   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
167   struct GNUNET_MQ_Envelope *env;
168
169   GNUNET_assert (OP_PEER_DESTROY == opc->type);
170   GNUNET_assert (NULL != peer);
171   opc->state = OPC_STATE_STARTED;
172   env = GNUNET_MQ_msg (msg,
173                        GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER);
174   msg->peer_id = htonl (peer->unique_id);
175   msg->operation_id = GNUNET_htonll (opc->id);
176   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
177   GNUNET_MQ_send (peer->controller->mq,
178                   env);
179 }
180
181
182 /**
183  * Callback which will be called when peer_create type operation is released
184  *
185  * @param cls the closure from GNUNET_TESTBED_operation_create_()
186  */
187 static void
188 oprelease_peer_destroy (void *cls)
189 {
190   struct OperationContext *opc = cls;
191
192   switch (opc->state)
193   {
194   case OPC_STATE_STARTED:
195     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
196     /* no break; continue */
197   case OPC_STATE_INIT:
198     break;
199   case OPC_STATE_FINISHED:
200     break;
201   }
202   GNUNET_free (opc);
203 }
204
205
206 /**
207  * Function called when a peer start operation is ready
208  *
209  * @param cls the closure from GNUNET_TESTBED_operation_create_()
210  */
211 static void
212 opstart_peer_start (void *cls)
213 {
214   struct OperationContext *opc = cls;
215   struct GNUNET_TESTBED_PeerStartMessage *msg;
216   struct GNUNET_MQ_Envelope *env;
217   struct PeerEventData *data;
218   struct GNUNET_TESTBED_Peer *peer;
219
220   GNUNET_assert (OP_PEER_START == opc->type);
221   GNUNET_assert (NULL != (data = opc->data));
222   GNUNET_assert (NULL != (peer = data->peer));
223   GNUNET_assert ((TESTBED_PS_CREATED == peer->state) || (TESTBED_PS_STOPPED == peer->state));
224   opc->state = OPC_STATE_STARTED;
225   env = GNUNET_MQ_msg (msg,
226                        GNUNET_MESSAGE_TYPE_TESTBED_START_PEER);
227   msg->peer_id = htonl (peer->unique_id);
228   msg->operation_id = GNUNET_htonll (opc->id);
229   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
230   GNUNET_MQ_send (peer->controller->mq,
231                   env);
232 }
233
234
235 /**
236  * Callback which will be called when peer start type operation is released
237  *
238  * @param cls the closure from GNUNET_TESTBED_operation_create_()
239  */
240 static void
241 oprelease_peer_start (void *cls)
242 {
243   struct OperationContext *opc = cls;
244
245   switch (opc->state)
246   {
247   case OPC_STATE_STARTED:
248     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
249     /* no break; continue */
250   case OPC_STATE_INIT:
251     GNUNET_free (opc->data);
252     break;
253   case OPC_STATE_FINISHED:
254     break;
255   }
256   GNUNET_free (opc);
257 }
258
259
260 /**
261  * Function called when a peer stop operation is ready
262  *
263  * @param cls the closure from GNUNET_TESTBED_operation_create_()
264  */
265 static void
266 opstart_peer_stop (void *cls)
267 {
268   struct OperationContext *opc = cls;
269   struct GNUNET_TESTBED_PeerStopMessage *msg;
270   struct PeerEventData *data;
271   struct GNUNET_TESTBED_Peer *peer;
272   struct GNUNET_MQ_Envelope *env;
273
274   GNUNET_assert (NULL != (data = opc->data));
275   GNUNET_assert (NULL != (peer = data->peer));
276   GNUNET_assert (TESTBED_PS_STARTED == peer->state);
277   opc->state = OPC_STATE_STARTED;
278   env = GNUNET_MQ_msg (msg,
279                        GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER);
280   msg->peer_id = htonl (peer->unique_id);
281   msg->operation_id = GNUNET_htonll (opc->id);
282   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
283   GNUNET_MQ_send (peer->controller->mq,
284                   env);
285 }
286
287
288 /**
289  * Callback which will be called when peer stop type operation is released
290  *
291  * @param cls the closure from GNUNET_TESTBED_operation_create_()
292  */
293 static void
294 oprelease_peer_stop (void *cls)
295 {
296   struct OperationContext *opc = cls;
297
298   switch (opc->state)
299   {
300   case OPC_STATE_STARTED:
301     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
302     /* no break; continue */
303   case OPC_STATE_INIT:
304     GNUNET_free (opc->data);
305     break;
306   case OPC_STATE_FINISHED:
307     break;
308   }
309   GNUNET_free (opc);
310 }
311
312
313 /**
314  * Generate PeerGetConfigurationMessage
315  *
316  * @param peer_id the id of the peer whose information we have to get
317  * @param operation_id the ip of the operation that should be represented in the
318  *          message
319  * @return the PeerGetConfigurationMessage
320  */
321 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
322 GNUNET_TESTBED_generate_peergetconfig_msg_ (uint32_t peer_id,
323                                             uint64_t operation_id)
324 {
325   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
326
327   msg =
328       GNUNET_malloc (sizeof
329                      (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
330   msg->header.size =
331       htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
332   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_INFORMATION);
333   msg->peer_id = htonl (peer_id);
334   msg->operation_id = GNUNET_htonll (operation_id);
335   return msg;
336 }
337
338
339 /**
340  * Function called when a peer get information operation is ready
341  *
342  * @param cls the closure from GNUNET_TESTBED_operation_create_()
343  */
344 static void
345 opstart_peer_getinfo (void *cls)
346 {
347   struct OperationContext *opc = cls;
348   struct PeerInfoData *data = opc->data;
349   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
350
351   GNUNET_assert (NULL != data);
352   opc->state = OPC_STATE_STARTED;
353   msg =
354       GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
355                                                   opc->id);
356   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
357   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
358 }
359
360
361 /**
362  * Callback which will be called when peer stop type operation is released
363  *
364  * @param cls the closure from GNUNET_TESTBED_operation_create_()
365  */
366 static void
367 oprelease_peer_getinfo (void *cls)
368 {
369   struct OperationContext *opc = cls;
370   struct GNUNET_TESTBED_PeerInformation *data;
371
372   switch (opc->state)
373   {
374   case OPC_STATE_STARTED:
375     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
376     /* no break; continue */
377   case OPC_STATE_INIT:
378     GNUNET_free (opc->data);
379     break;
380   case OPC_STATE_FINISHED:
381     data = opc->data;
382     GNUNET_assert (NULL != data);
383     switch (data->pit)
384     {
385     case GNUNET_TESTBED_PIT_CONFIGURATION:
386       if (NULL != data->result.cfg)
387         GNUNET_CONFIGURATION_destroy (data->result.cfg);
388       break;
389     case GNUNET_TESTBED_PIT_IDENTITY:
390       GNUNET_free (data->result.id);
391       break;
392     default:
393       GNUNET_assert (0);        /* We should never reach here */
394     }
395     GNUNET_free (data);
396     break;
397   }
398   GNUNET_free (opc);
399 }
400
401
402 /**
403  * Function called when a overlay connect operation is ready
404  *
405  * @param cls the closure from GNUNET_TESTBED_operation_create_()
406  */
407 static void
408 opstart_overlay_connect (void *cls)
409 {
410   struct OperationContext *opc = cls;
411   struct GNUNET_MQ_Envelope *env;
412   struct GNUNET_TESTBED_OverlayConnectMessage *msg;
413   struct OverlayConnectData *data;
414
415   opc->state = OPC_STATE_STARTED;
416   data = opc->data;
417   GNUNET_assert (NULL != data);
418   env = GNUNET_MQ_msg (msg,
419                        GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT);
420   msg->peer1 = htonl (data->p1->unique_id);
421   msg->peer2 = htonl (data->p2->unique_id);
422   msg->operation_id = GNUNET_htonll (opc->id);
423   msg->peer2_host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->p2->host));
424   GNUNET_TESTBED_insert_opc_ (opc->c,
425                               opc);
426   GNUNET_MQ_send (opc->c->mq,
427                   env);
428 }
429
430
431 /**
432  * Callback which will be called when overlay connect operation is released
433  *
434  * @param cls the closure from GNUNET_TESTBED_operation_create_()
435  */
436 static void
437 oprelease_overlay_connect (void *cls)
438 {
439   struct OperationContext *opc = cls;
440   struct OverlayConnectData *data;
441
442   data = opc->data;
443   switch (opc->state)
444   {
445   case OPC_STATE_INIT:
446     break;
447   case OPC_STATE_STARTED:
448     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
449     break;
450   case OPC_STATE_FINISHED:
451     break;
452   }
453   GNUNET_free (data);
454   GNUNET_free (opc);
455 }
456
457
458 /**
459  * Function called when a peer reconfigure operation is ready
460  *
461  * @param cls the closure from GNUNET_TESTBED_operation_create_()
462  */
463 static void
464 opstart_peer_reconfigure (void *cls)
465 {
466   struct OperationContext *opc = cls;
467   struct PeerReconfigureData *data = opc->data;
468   struct GNUNET_MQ_Envelope *env;
469   struct GNUNET_TESTBED_PeerReconfigureMessage *msg;
470   char *xconfig;
471   size_t xc_size;
472
473   opc->state = OPC_STATE_STARTED;
474   GNUNET_assert (NULL != data);
475   xc_size = GNUNET_TESTBED_compress_config_ (data->config,
476                                              data->cfg_size,
477                                              &xconfig);
478   GNUNET_free (data->config);
479   data->config = NULL;
480   GNUNET_assert (xc_size < UINT16_MAX - sizeof (*msg));
481   env = GNUNET_MQ_msg_extra (msg,
482                              xc_size,
483                              GNUNET_MESSAGE_TYPE_TESTBED_RECONFIGURE_PEER);
484   msg->peer_id = htonl (data->peer->unique_id);
485   msg->operation_id = GNUNET_htonll (opc->id);
486   msg->config_size = htons (data->cfg_size);
487   GNUNET_memcpy (&msg[1],
488           xconfig,
489           xc_size);
490   GNUNET_free (xconfig);
491   GNUNET_free (data);
492   opc->data = NULL;
493   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
494   GNUNET_MQ_send (opc->c->mq,
495                   env);
496 }
497
498
499 /**
500  * Callback which will be called when a peer reconfigure operation is released
501  *
502  * @param cls the closure from GNUNET_TESTBED_operation_create_()
503  */
504 static void
505 oprelease_peer_reconfigure (void *cls)
506 {
507   struct OperationContext *opc = cls;
508   struct PeerReconfigureData *data = opc->data;
509
510   switch (opc->state)
511   {
512   case OPC_STATE_INIT:
513     GNUNET_free (data->config);
514     GNUNET_free (data);
515     break;
516   case OPC_STATE_STARTED:
517     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
518     break;
519   case OPC_STATE_FINISHED:
520     break;
521   }
522   GNUNET_free (opc);
523 }
524
525
526 /**
527  * Lookup a peer by ID.
528  *
529  * @param id global peer ID assigned to the peer
530  * @return handle to the host, NULL on error
531  */
532 struct GNUNET_TESTBED_Peer *
533 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
534 {
535   GNUNET_break (0);
536   return NULL;
537 }
538
539
540 /**
541  * Create the given peer at the specified host using the given
542  * controller.  If the given controller is not running on the target
543  * host, it should find or create a controller at the target host and
544  * delegate creating the peer.  Explicit delegation paths can be setup
545  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
546  * path exists, a direct link with a subordinate controller is setup
547  * for the first delegated peer to a particular host; the subordinate
548  * controller is then destroyed once the last peer that was delegated
549  * to the remote host is stopped.
550  *
551  * Creating the peer only creates the handle to manipulate and further
552  * configure the peer; use "GNUNET_TESTBED_peer_start" and
553  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
554  * processes.
555  *
556  * Note that the given configuration will be adjusted by the
557  * controller to avoid port/path conflicts with other peers.
558  * The "final" configuration can be obtained using
559  * 'GNUNET_TESTBED_peer_get_information'.
560  *
561  * @param controller controller process to use
562  * @param host host to run the peer on; cannot be NULL
563  * @param cfg Template configuration to use for the peer. Should exist until
564  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
565  * @param cb the callback to call when the peer has been created
566  * @param cls the closure to the above callback
567  * @return the operation handle
568  */
569 struct GNUNET_TESTBED_Operation *
570 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
571                             struct GNUNET_TESTBED_Host *host,
572                             const struct GNUNET_CONFIGURATION_Handle *cfg,
573                             GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
574 {
575
576   struct GNUNET_TESTBED_Peer *peer;
577   struct PeerCreateData *data;
578   struct OperationContext *opc;
579   static uint32_t id_gen;
580
581   peer = GNUNET_new (struct GNUNET_TESTBED_Peer);
582   peer->controller = controller;
583   peer->host = host;
584   peer->unique_id = id_gen++;
585   peer->state = TESTBED_PS_INVALID;
586   data = GNUNET_new (struct PeerCreateData);
587   data->host = host;
588   data->cfg = cfg;
589   data->cb = cb;
590   data->cls = cls;
591   data->peer = peer;
592   opc = GNUNET_new (struct OperationContext);
593   opc->c = controller;
594   opc->data = data;
595   opc->id = GNUNET_TESTBED_get_next_op_id (controller);
596   opc->type = OP_PEER_CREATE;
597   opc->op =
598       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
599                                         &oprelease_peer_create);
600   GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
601                                           opc->op);
602   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
603   return opc->op;
604 }
605
606
607 /**
608  * Start the given peer.
609  *
610  * @param op_cls the closure for this operation; will be set in
611  *          event->details.operation_finished.op_cls when this operation fails.
612  * @param peer peer to start
613  * @param pcc function to call upon completion
614  * @param pcc_cls closure for 'pcc'
615  * @return handle to the operation
616  */
617 struct GNUNET_TESTBED_Operation *
618 GNUNET_TESTBED_peer_start (void *op_cls, struct GNUNET_TESTBED_Peer *peer,
619                            GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
620 {
621   struct OperationContext *opc;
622   struct PeerEventData *data;
623
624   data = GNUNET_new (struct PeerEventData);
625   data->peer = peer;
626   data->pcc = pcc;
627   data->pcc_cls = pcc_cls;
628   opc = GNUNET_new (struct OperationContext);
629   opc->c = peer->controller;
630   opc->data = data;
631   opc->op_cls = op_cls;
632   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
633   opc->type = OP_PEER_START;
634   opc->op =
635       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
636                                         &oprelease_peer_start);
637   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
638                                           opc->op);
639   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
640   return opc->op;
641 }
642
643
644 /**
645  * Stop the given peer.  The handle remains valid (use
646  * "GNUNET_TESTBED_peer_destroy" to fully clean up the
647  * state of the peer).
648  *
649  * @param op_cls the closure for this operation; will be set in the event
650  *          information
651  * @param peer peer to stop
652  * @param pcc function to call upon completion
653  * @param pcc_cls closure for 'pcc'
654  * @return handle to the operation
655  */
656 struct GNUNET_TESTBED_Operation *
657 GNUNET_TESTBED_peer_stop (void *op_cls,
658                           struct GNUNET_TESTBED_Peer *peer,
659                           GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
660 {
661   struct OperationContext *opc;
662   struct PeerEventData *data;
663
664   data = GNUNET_new (struct PeerEventData);
665   data->peer = peer;
666   data->pcc = pcc;
667   data->pcc_cls = pcc_cls;
668   opc = GNUNET_new (struct OperationContext);
669   opc->c = peer->controller;
670   opc->data = data;
671   opc->op_cls = op_cls;
672   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
673   opc->type = OP_PEER_STOP;
674   opc->op =
675       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
676                                         &oprelease_peer_stop);
677   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
678                                           opc->op);
679   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
680   return opc->op;
681 }
682
683
684 /**
685  * Request information about a peer. The controller callback will not be called
686  * with event type GNUNET_TESTBED_ET_OPERATION_FINISHED when result for this
687  * operation is available. Instead, the GNUNET_TESTBED_PeerInfoCallback() will
688  * be called.
689  * The peer information in the callback is valid until the operation is canceled.
690  *
691  * @param peer peer to request information about
692  * @param pit desired information
693  * @param cb the convenience callback to be called when results for this
694  *          operation are available
695  * @param cb_cls the closure for the above callback
696  * @return handle to the operation
697  */
698 struct GNUNET_TESTBED_Operation *
699 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
700                                      enum GNUNET_TESTBED_PeerInformationType
701                                      pit, GNUNET_TESTBED_PeerInfoCallback cb,
702                                      void *cb_cls)
703 {
704   struct OperationContext *opc;
705   struct PeerInfoData *data;
706
707   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
708   GNUNET_assert (NULL != cb);
709   data = GNUNET_new (struct PeerInfoData);
710   data->peer = peer;
711   data->pit = pit;
712   data->cb = cb;
713   data->cb_cls = cb_cls;
714   opc = GNUNET_new (struct OperationContext);
715   opc->c = peer->controller;
716   opc->data = data;
717   opc->type = OP_PEER_INFO;
718   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
719   opc->op =
720       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
721                                         &oprelease_peer_getinfo);
722   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
723                                           opc->op);
724   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
725   return opc->op;
726 }
727
728
729 /**
730  * Change peer configuration.  Must only be called while the
731  * peer is stopped.  Ports and paths cannot be changed this
732  * way.
733  *
734  * @param peer peer to change configuration for
735  * @param cfg new configuration (differences to existing
736  *            configuration only)
737  * @return handle to the operation
738  */
739 struct GNUNET_TESTBED_Operation *
740 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
741                                           const struct
742                                           GNUNET_CONFIGURATION_Handle *cfg)
743 {
744   struct OperationContext *opc;
745   struct PeerReconfigureData *data;
746   size_t csize;
747
748   data = GNUNET_new (struct PeerReconfigureData);
749   data->peer = peer;
750   data->config = GNUNET_CONFIGURATION_serialize (cfg, &csize);
751   if (NULL == data->config)
752   {
753     GNUNET_free (data);
754     return NULL;
755   }
756   if (csize > UINT16_MAX)
757   {
758     GNUNET_break (0);
759     GNUNET_free (data->config);
760     GNUNET_free (data);
761     return NULL;
762   }
763   data->cfg_size = (uint16_t) csize;
764   opc = GNUNET_new (struct OperationContext);
765   opc->c = peer->controller;
766   opc->data = data;
767   opc->type = OP_PEER_RECONFIGURE;
768   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
769   opc->op =
770       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_reconfigure,
771                                         &oprelease_peer_reconfigure);
772   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
773                                           opc->op);
774   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
775   return opc->op;
776 }
777
778
779 /**
780  * Destroy the given peer; the peer should have been
781  * stopped first (if it was started).
782  *
783  * @param peer peer to stop
784  * @return handle to the operation
785  */
786 struct GNUNET_TESTBED_Operation *
787 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
788 {
789   struct OperationContext *opc;
790
791   opc = GNUNET_new (struct OperationContext);
792   opc->data = peer;
793   opc->c = peer->controller;
794   opc->id = GNUNET_TESTBED_get_next_op_id (peer->controller);
795   opc->type = OP_PEER_DESTROY;
796   opc->op =
797       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
798                                         &oprelease_peer_destroy);
799   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
800                                           opc->op);
801   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
802   return opc->op;
803 }
804
805
806 /**
807  * Manipulate the P2P underlay topology by configuring a link
808  * between two peers.
809  *
810  * @param op_cls closure argument to give with the operation event
811  * @param p1 first peer
812  * @param p2 second peer
813  * @param co option to change
814  * @param ... option-specific values
815  * @return handle to the operation, NULL if configuring the link at this
816  *         time is not allowed
817  */
818 struct GNUNET_TESTBED_Operation *
819 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
820                                         struct GNUNET_TESTBED_Peer *p1,
821                                         struct GNUNET_TESTBED_Peer *p2,
822                                         enum GNUNET_TESTBED_ConnectOption co,
823                                         ...)
824 {
825   GNUNET_break (0);
826   return NULL;
827 }
828
829
830 /**
831  * Both peers must have been started before calling this function.
832  * This function then obtains a HELLO from 'p1', gives it to 'p2'
833  * and asks 'p2' to connect to 'p1'.
834  *
835  * @param op_cls closure argument to give with the operation event
836  * @param cb the callback to call when this operation has finished
837  * @param cb_cls the closure for the above callback
838  * @param p1 first peer
839  * @param p2 second peer
840  * @return handle to the operation, NULL if connecting these two
841  *         peers is fundamentally not possible at this time (peers
842  *         not running or underlay disallows)
843  */
844 struct GNUNET_TESTBED_Operation *
845 GNUNET_TESTBED_overlay_connect (void *op_cls,
846                                 GNUNET_TESTBED_OperationCompletionCallback cb,
847                                 void *cb_cls, struct GNUNET_TESTBED_Peer *p1,
848                                 struct GNUNET_TESTBED_Peer *p2)
849 {
850   struct OperationContext *opc;
851   struct OverlayConnectData *data;
852
853   GNUNET_assert ((TESTBED_PS_STARTED == p1->state) && (TESTBED_PS_STARTED == p2->state));
854   data = GNUNET_new (struct OverlayConnectData);
855   data->p1 = p1;
856   data->p2 = p2;
857   data->cb = cb;
858   data->cb_cls = cb_cls;
859   opc = GNUNET_new (struct OperationContext);
860   opc->data = data;
861   opc->c = p1->controller;
862   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
863   opc->type = OP_OVERLAY_CONNECT;
864   opc->op_cls = op_cls;
865   opc->op =
866       GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
867                                         &oprelease_overlay_connect);
868   GNUNET_TESTBED_host_queue_oc_ (p1->host, opc->op);
869   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
870   return opc->op;
871 }
872
873
874 /**
875  * Function called when a peer manage service operation is ready
876  *
877  * @param cls the closure from GNUNET_TESTBED_operation_create_()
878  */
879 static void
880 opstart_manage_service (void *cls)
881 {
882   struct OperationContext *opc = cls;
883   struct ManageServiceData *data = opc->data;
884   struct GNUNET_MQ_Envelope *env;
885   struct GNUNET_TESTBED_ManagePeerServiceMessage *msg;
886   size_t xlen;
887
888   GNUNET_assert (NULL != data);
889   xlen = data->msize - sizeof (struct GNUNET_TESTBED_ManagePeerServiceMessage);
890   env = GNUNET_MQ_msg_extra (msg,
891                              xlen,
892                              GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE);
893   msg->peer_id = htonl (data->peer->unique_id);
894   msg->operation_id = GNUNET_htonll (opc->id);
895   msg->start = (uint8_t) data->start;
896   GNUNET_memcpy (&msg[1],
897           data->service_name,
898           xlen);
899   GNUNET_free (data->service_name);
900   data->service_name = NULL;
901   opc->state = OPC_STATE_STARTED;
902   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
903   GNUNET_MQ_send (opc->c->mq,
904                   env);
905 }
906
907
908 /**
909  * Callback which will be called when peer manage server operation is released
910  *
911  * @param cls the closure from GNUNET_TESTBED_operation_create_()
912  */
913 static void
914 oprelease_manage_service (void *cls)
915 {
916   struct OperationContext *opc = cls;
917   struct ManageServiceData *data;
918
919   data = opc->data;
920   switch (opc->state)
921   {
922   case OPC_STATE_STARTED:
923     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
924     break;
925   case OPC_STATE_INIT:
926     GNUNET_assert (NULL != data);
927     GNUNET_free (data->service_name);
928     break;
929   case OPC_STATE_FINISHED:
930     break;
931   }
932   GNUNET_free_non_null (data);
933   GNUNET_free (opc);
934 }
935
936
937 /**
938  * Start or stop given service at a peer.  This should not be called to
939  * start/stop the peer's ARM service.  Use GNUNET_TESTBED_peer_start(),
940  * GNUNET_TESTBED_peer_stop() for starting/stopping peer's ARM service.  Success
941  * or failure of the generated operation is signalled through the controller
942  * event callback and/or operation completion callback.
943  *
944  * @param op_cls the closure for the operation
945  * @param peer the peer whose service is to be started/stopped
946  * @param service_name the name of the service
947  * @param cb the operation completion callback
948  * @param cb_cls the closure for the operation completion callback
949  * @param start 1 to start the service; 0 to stop the service
950  * @return an operation handle; NULL upon error (peer not running)
951  */
952 struct GNUNET_TESTBED_Operation *
953 GNUNET_TESTBED_peer_manage_service (void *op_cls,
954                                     struct GNUNET_TESTBED_Peer *peer,
955                                     const char *service_name,
956                                     GNUNET_TESTBED_OperationCompletionCallback cb,
957                                     void *cb_cls,
958                                     unsigned int start)
959 {
960   struct ManageServiceData *data;
961   struct OperationContext *opc;
962   size_t msize;
963
964   GNUNET_assert (TESTBED_PS_STARTED == peer->state); /* peer is not running? */
965   msize = strlen (service_name) + 1;
966   msize += sizeof (struct GNUNET_TESTBED_ManagePeerServiceMessage);
967   if (GNUNET_MAX_MESSAGE_SIZE < msize)
968     return NULL;
969   data = GNUNET_new (struct ManageServiceData);
970   data->cb = cb;
971   data->cb_cls = cb_cls;
972   data->peer = peer;
973   data->service_name = GNUNET_strdup (service_name);
974   data->start = start;
975   data->msize = (uint16_t) msize;
976   opc = GNUNET_new (struct OperationContext);
977   opc->data = data;
978   opc->c = peer->controller;
979   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
980   opc->type = OP_MANAGE_SERVICE;
981   opc->op_cls = op_cls;
982   opc->op =
983       GNUNET_TESTBED_operation_create_ (opc, &opstart_manage_service,
984                                         &oprelease_manage_service);
985   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
986                                           opc->op);
987   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
988   return opc->op;
989 }
990
991
992
993 /* end of testbed_api_peers.c */