2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
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 * @author Sree Harsha Totakura
30 #include "testbed_api_peers.h"
31 #include "testbed_api.h"
33 #include "testbed_api_hosts.h"
34 #include "testbed_api_operations.h"
37 * Function to call to start a peer_create type operation once all
38 * queues the operation is part of declare that the
39 * operation can be activated.
41 * @param cls the closure from GNUNET_TESTBED_operation_create_()
44 opstart_peer_create (void *cls)
46 struct OperationContext *opc = cls;
47 struct PeerCreateData *data;
48 struct GNUNET_TESTBED_PeerCreateMessage *msg;
55 GNUNET_assert (OP_PEER_CREATE == opc->type);
57 GNUNET_assert (NULL != data);
58 GNUNET_assert (NULL != data->peer);
59 opc->state = OPC_STATE_STARTED;
60 config = GNUNET_CONFIGURATION_serialize (data->cfg, &c_size);
61 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
63 msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
64 msg = GNUNET_realloc (xconfig, msize);
65 memmove (&msg[1], msg, xc_size);
66 msg->header.size = htons (msize);
67 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
68 msg->operation_id = GNUNET_htonll (opc->id);
69 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host));
70 msg->peer_id = htonl (data->peer->unique_id);
71 msg->config_size = htonl (c_size);
72 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
73 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
78 * Callback which will be called when peer_create type operation is released
80 * @param cls the closure from GNUNET_TESTBED_operation_create_()
83 oprelease_peer_create (void *cls)
85 struct OperationContext *opc = cls;
87 if (OPC_STATE_FINISHED != opc->state)
89 GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
90 GNUNET_free (opc->data);
91 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
98 * Function called when a peer destroy operation is ready
100 * @param cls the closure from GNUNET_TESTBED_operation_create_()
103 opstart_peer_destroy (void *cls)
105 struct OperationContext *opc = cls;
106 struct GNUNET_TESTBED_Peer *peer;
107 struct GNUNET_TESTBED_PeerDestroyMessage *msg;
109 GNUNET_assert (OP_PEER_DESTROY == opc->type);
111 GNUNET_assert (NULL != peer);
112 opc->state = OPC_STATE_STARTED;
113 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
114 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
115 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
116 msg->peer_id = htonl (peer->unique_id);
117 msg->operation_id = GNUNET_htonll (opc->id);
118 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
119 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
124 * Callback which will be called when peer_create type operation is released
126 * @param cls the closure from GNUNET_TESTBED_operation_create_()
129 oprelease_peer_destroy (void *cls)
131 struct OperationContext *opc = cls;
133 if (OPC_STATE_FINISHED != opc->state)
134 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
140 * Function called when a peer start operation is ready
142 * @param cls the closure from GNUNET_TESTBED_operation_create_()
145 opstart_peer_start (void *cls)
147 struct OperationContext *opc = cls;
148 struct GNUNET_TESTBED_PeerStartMessage *msg;
149 struct PeerEventData *data;
150 struct GNUNET_TESTBED_Peer *peer;
152 GNUNET_assert (OP_PEER_START == opc->type);
153 GNUNET_assert (NULL != opc->data);
155 GNUNET_assert (NULL != data->peer);
157 GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
158 opc->state = OPC_STATE_STARTED;
159 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
160 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
161 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
162 msg->peer_id = htonl (peer->unique_id);
163 msg->operation_id = GNUNET_htonll (opc->id);
164 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
165 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
170 * Callback which will be called when peer start type operation is released
172 * @param cls the closure from GNUNET_TESTBED_operation_create_()
175 oprelease_peer_start (void *cls)
177 struct OperationContext *opc = cls;
179 if (OPC_STATE_FINISHED != opc->state)
181 GNUNET_free (opc->data);
182 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
189 * Function called when a peer stop operation is ready
191 * @param cls the closure from GNUNET_TESTBED_operation_create_()
194 opstart_peer_stop (void *cls)
196 struct OperationContext *opc = cls;
197 struct GNUNET_TESTBED_PeerStopMessage *msg;
198 struct PeerEventData *data;
199 struct GNUNET_TESTBED_Peer *peer;
201 GNUNET_assert (NULL != opc->data);
203 GNUNET_assert (NULL != data->peer);
205 GNUNET_assert (PS_STARTED == peer->state);
206 opc->state = OPC_STATE_STARTED;
207 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
208 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
209 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
210 msg->peer_id = htonl (peer->unique_id);
211 msg->operation_id = GNUNET_htonll (opc->id);
212 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
213 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
218 * Callback which will be called when peer stop type operation is released
220 * @param cls the closure from GNUNET_TESTBED_operation_create_()
223 oprelease_peer_stop (void *cls)
225 struct OperationContext *opc = cls;
227 if (OPC_STATE_FINISHED != opc->state)
229 GNUNET_free (opc->data);
230 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
237 * Generate PeerGetConfigurationMessage
239 * @param peer_id the id of the peer whose information we have to get
240 * @param operation_id the ip of the operation that should be represented in the
242 * @return the PeerGetConfigurationMessage
244 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
245 GNUNET_TESTBED_generate_peergetconfig_msg_ (uint32_t peer_id,
246 uint64_t operation_id)
248 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
251 GNUNET_malloc (sizeof
252 (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
254 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
255 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
256 msg->peer_id = htonl (peer_id);
257 msg->operation_id = GNUNET_htonll (operation_id);
263 * Function called when a peer get information operation is ready
265 * @param cls the closure from GNUNET_TESTBED_operation_create_()
268 opstart_peer_getinfo (void *cls)
270 struct OperationContext *opc = cls;
271 struct PeerInfoData *data;
272 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
275 GNUNET_assert (NULL != data);
276 opc->state = OPC_STATE_STARTED;
278 GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
280 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
281 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
286 * Callback which will be called when peer stop type operation is released
288 * @param cls the closure from GNUNET_TESTBED_operation_create_()
291 oprelease_peer_getinfo (void *cls)
293 struct OperationContext *opc = cls;
294 struct GNUNET_TESTBED_PeerInformation *data;
296 if (OPC_STATE_FINISHED != opc->state)
298 GNUNET_free_non_null (opc->data);
299 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
304 GNUNET_assert (NULL != data);
307 case GNUNET_TESTBED_PIT_CONFIGURATION:
308 GNUNET_CONFIGURATION_destroy (data->result.cfg);
310 case GNUNET_TESTBED_PIT_IDENTITY:
311 GNUNET_free (data->result.id);
314 GNUNET_assert (0); /* We should never reach here */
323 * Function called when a overlay connect operation is ready
325 * @param cls the closure from GNUNET_TESTBED_operation_create_()
328 opstart_overlay_connect (void *cls)
330 struct OperationContext *opc = cls;
331 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
332 struct OverlayConnectData *data;
334 opc->state = OPC_STATE_STARTED;
336 GNUNET_assert (NULL != data);
337 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
339 htons (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
340 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
341 msg->peer1 = htonl (data->p1->unique_id);
342 msg->peer2 = htonl (data->p2->unique_id);
343 msg->operation_id = GNUNET_htonll (opc->id);
344 msg->peer2_host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->p2->host));
345 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
346 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
351 * Callback which will be called when overlay connect operation is released
353 * @param cls the closure from GNUNET_TESTBED_operation_create_()
356 oprelease_overlay_connect (void *cls)
358 struct OperationContext *opc = cls;
360 if (OPC_STATE_FINISHED != opc->state)
362 GNUNET_free (opc->data);
363 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
370 * Lookup a peer by ID.
372 * @param id global peer ID assigned to the peer
373 * @return handle to the host, NULL on error
375 struct GNUNET_TESTBED_Peer *
376 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
384 * Create the given peer at the specified host using the given
385 * controller. If the given controller is not running on the target
386 * host, it should find or create a controller at the target host and
387 * delegate creating the peer. Explicit delegation paths can be setup
388 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
389 * path exists, a direct link with a subordinate controller is setup
390 * for the first delegated peer to a particular host; the subordinate
391 * controller is then destroyed once the last peer that was delegated
392 * to the remote host is stopped. This function is used in particular
393 * if some other controller has already assigned a unique ID to the
396 * Creating the peer only creates the handle to manipulate and further
397 * configure the peer; use "GNUNET_TESTBED_peer_start" and
398 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
401 * Note that the given configuration will be adjusted by the
402 * controller to avoid port/path conflicts with other peers.
403 * The "final" configuration can be obtained using
404 * 'GNUNET_TESTBED_peer_get_information'.
406 * @param unique_id unique ID for this peer
407 * @param controller controller process to use
408 * @param host host to run the peer on
409 * @param cfg Template configuration to use for the peer. Should exist until
410 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
411 * @param cb the callback to call when the peer has been created
412 * @param cls the closure to the above callback
413 * @return the operation handle
415 struct GNUNET_TESTBED_Operation *
416 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
417 struct GNUNET_TESTBED_Controller
419 struct GNUNET_TESTBED_Host *host,
420 const struct GNUNET_CONFIGURATION_Handle
421 *cfg, GNUNET_TESTBED_PeerCreateCallback cb,
424 struct GNUNET_TESTBED_Peer *peer;
425 struct PeerCreateData *data;
426 struct OperationContext *opc;
428 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
429 peer->controller = controller;
431 peer->unique_id = unique_id;
432 peer->state = PS_INVALID;
433 data = GNUNET_malloc (sizeof (struct PeerCreateData));
439 opc = GNUNET_malloc (sizeof (struct OperationContext));
442 opc->id = GNUNET_TESTBED_get_next_op_id (controller);
443 opc->type = OP_PEER_CREATE;
445 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
446 &oprelease_peer_create);
447 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
454 * Create the given peer at the specified host using the given
455 * controller. If the given controller is not running on the target
456 * host, it should find or create a controller at the target host and
457 * delegate creating the peer. Explicit delegation paths can be setup
458 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
459 * path exists, a direct link with a subordinate controller is setup
460 * for the first delegated peer to a particular host; the subordinate
461 * controller is then destroyed once the last peer that was delegated
462 * to the remote host is stopped.
464 * Creating the peer only creates the handle to manipulate and further
465 * configure the peer; use "GNUNET_TESTBED_peer_start" and
466 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
469 * Note that the given configuration will be adjusted by the
470 * controller to avoid port/path conflicts with other peers.
471 * The "final" configuration can be obtained using
472 * 'GNUNET_TESTBED_peer_get_information'.
474 * @param controller controller process to use
475 * @param host host to run the peer on
476 * @param cfg Template configuration to use for the peer. Should exist until
477 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
478 * @param cb the callback to call when the peer has been created
479 * @param cls the closure to the above callback
480 * @return the operation handle
482 struct GNUNET_TESTBED_Operation *
483 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
484 struct GNUNET_TESTBED_Host *host,
485 const struct GNUNET_CONFIGURATION_Handle *cfg,
486 GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
488 static uint32_t id_gen;
490 return GNUNET_TESTBED_peer_create_with_id_ (id_gen++, controller, host, cfg,
496 * Start the given peer.
498 * @param op_cls the closure for this operation; will be set in
499 * event->details.operation_finished.op_cls when this operation fails.
500 * @param peer peer to start
501 * @param pcc function to call upon completion
502 * @param pcc_cls closure for 'pcc'
503 * @return handle to the operation
505 struct GNUNET_TESTBED_Operation *
506 GNUNET_TESTBED_peer_start (void *op_cls,
507 struct GNUNET_TESTBED_Peer *peer,
508 GNUNET_TESTBED_PeerChurnCallback pcc,
511 struct OperationContext *opc;
512 struct PeerEventData *data;
514 data = GNUNET_malloc (sizeof (struct PeerEventData));
517 data->pcc_cls = pcc_cls;
518 opc = GNUNET_malloc (sizeof (struct OperationContext));
519 opc->c = peer->controller;
521 opc->op_cls = op_cls;
522 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
523 opc->type = OP_PEER_START;
525 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
526 &oprelease_peer_start);
527 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
534 * Stop the given peer. The handle remains valid (use
535 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
536 * state of the peer).
538 * @param peer peer to stop
539 * @param pcc function to call upon completion
540 * @param pcc_cls closure for 'pcc'
541 * @return handle to the operation
543 struct GNUNET_TESTBED_Operation *
544 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer,
545 GNUNET_TESTBED_PeerChurnCallback pcc,
548 struct OperationContext *opc;
549 struct PeerEventData *data;
551 data = GNUNET_malloc (sizeof (struct PeerEventData));
554 data->pcc_cls = pcc_cls;
555 opc = GNUNET_malloc (sizeof (struct OperationContext));
556 opc->c = peer->controller;
558 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
559 opc->type = OP_PEER_STOP;
561 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
562 &oprelease_peer_stop);
563 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
570 * Request information about a peer. The controller callback will not be called
571 * with event type GNUNET_TESTBED_ET_OPERATION_FINISHED when result for this
572 * operation is available. Instead, the GNUNET_TESTBED_PeerInfoCallback() will
575 * @param peer peer to request information about
576 * @param pit desired information
577 * @param cb the convenience callback to be called when results for this
578 * operation are available
579 * @param cb_cls the closure for the above callback
580 * @return handle to the operation
582 struct GNUNET_TESTBED_Operation *
583 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
584 enum GNUNET_TESTBED_PeerInformationType
586 GNUNET_TESTBED_PeerInfoCallback cb,
589 struct OperationContext *opc;
590 struct PeerInfoData *data;
592 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
593 data = GNUNET_malloc (sizeof (struct PeerInfoData));
597 data->cb_cls = cb_cls;
598 opc = GNUNET_malloc (sizeof (struct OperationContext));
599 opc->c = peer->controller;
601 opc->type = OP_PEER_INFO;
602 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
604 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
605 &oprelease_peer_getinfo);
606 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
613 * Change peer configuration. Must only be called while the
614 * peer is stopped. Ports and paths cannot be changed this
617 * @param peer peer to change configuration for
618 * @param cfg new configuration (differences to existing
619 * configuration only)
620 * @return handle to the operation
622 struct GNUNET_TESTBED_Operation *
623 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
625 GNUNET_CONFIGURATION_Handle *cfg)
627 // FIXME: handle locally or delegate...
634 * Destroy the given peer; the peer should have been
635 * stopped first (if it was started).
637 * @param peer peer to stop
638 * @return handle to the operation
640 struct GNUNET_TESTBED_Operation *
641 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
643 struct OperationContext *opc;
645 opc = GNUNET_malloc (sizeof (struct OperationContext));
647 opc->c = peer->controller;
648 opc->id = GNUNET_TESTBED_get_next_op_id (peer->controller);
649 opc->type = OP_PEER_DESTROY;
651 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
652 &oprelease_peer_destroy);
653 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
660 * Manipulate the P2P underlay topology by configuring a link
663 * @param op_cls closure argument to give with the operation event
664 * @param p1 first peer
665 * @param p2 second peer
666 * @param co option to change
667 * @param ... option-specific values
668 * @return handle to the operation, NULL if configuring the link at this
669 * time is not allowed
671 struct GNUNET_TESTBED_Operation *
672 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
673 struct GNUNET_TESTBED_Peer *p1,
674 struct GNUNET_TESTBED_Peer *p2,
675 enum GNUNET_TESTBED_ConnectOption co,
684 * Both peers must have been started before calling this function.
685 * This function then obtains a HELLO from 'p1', gives it to 'p2'
686 * and asks 'p2' to connect to 'p1'.
688 * @param op_cls closure argument to give with the operation event
689 * @param cb the callback to call when this operation has finished
690 * @param cb_cls the closure for the above callback
691 * @param p1 first peer
692 * @param p2 second peer
693 * @return handle to the operation, NULL if connecting these two
694 * peers is fundamentally not possible at this time (peers
695 * not running or underlay disallows)
697 struct GNUNET_TESTBED_Operation *
698 GNUNET_TESTBED_overlay_connect (void *op_cls,
699 GNUNET_TESTBED_OperationCompletionCallback cb,
701 struct GNUNET_TESTBED_Peer *p1,
702 struct GNUNET_TESTBED_Peer *p2)
704 struct OperationContext *opc;
705 struct OverlayConnectData *data;
707 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
708 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
712 data->cb_cls = cb_cls;
713 data->state = OCD_INIT;
714 opc = GNUNET_malloc (sizeof (struct OperationContext));
716 opc->c = p1->controller;
717 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
718 opc->type = OP_OVERLAY_CONNECT;
719 opc->op_cls = op_cls;
721 GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
722 &oprelease_overlay_connect);
723 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
730 /* end of testbed_api_peers.c */