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 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
345 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
350 * Callback which will be called when overlay connect operation is released
352 * @param cls the closure from GNUNET_TESTBED_operation_create_()
355 oprelease_overlay_connect (void *cls)
357 struct OperationContext *opc = cls;
359 if (OPC_STATE_FINISHED != opc->state)
361 GNUNET_free (opc->data);
362 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
369 * Lookup a peer by ID.
371 * @param id global peer ID assigned to the peer
372 * @return handle to the host, NULL on error
374 struct GNUNET_TESTBED_Peer *
375 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
383 * Create the given peer at the specified host using the given
384 * controller. If the given controller is not running on the target
385 * host, it should find or create a controller at the target host and
386 * delegate creating the peer. Explicit delegation paths can be setup
387 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
388 * path exists, a direct link with a subordinate controller is setup
389 * for the first delegated peer to a particular host; the subordinate
390 * controller is then destroyed once the last peer that was delegated
391 * to the remote host is stopped. This function is used in particular
392 * if some other controller has already assigned a unique ID to the
395 * Creating the peer only creates the handle to manipulate and further
396 * configure the peer; use "GNUNET_TESTBED_peer_start" and
397 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
400 * Note that the given configuration will be adjusted by the
401 * controller to avoid port/path conflicts with other peers.
402 * The "final" configuration can be obtained using
403 * 'GNUNET_TESTBED_peer_get_information'.
405 * @param unique_id unique ID for this peer
406 * @param controller controller process to use
407 * @param host host to run the peer on
408 * @param cfg Template configuration to use for the peer. Should exist until
409 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
410 * @param cb the callback to call when the peer has been created
411 * @param cls the closure to the above callback
412 * @return the operation handle
414 struct GNUNET_TESTBED_Operation *
415 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
416 struct GNUNET_TESTBED_Controller
418 struct GNUNET_TESTBED_Host *host,
419 const struct GNUNET_CONFIGURATION_Handle
420 *cfg, GNUNET_TESTBED_PeerCreateCallback cb,
423 struct GNUNET_TESTBED_Peer *peer;
424 struct PeerCreateData *data;
425 struct OperationContext *opc;
427 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
428 peer->controller = controller;
430 peer->unique_id = unique_id;
431 peer->state = PS_INVALID;
432 data = GNUNET_malloc (sizeof (struct PeerCreateData));
438 opc = GNUNET_malloc (sizeof (struct OperationContext));
441 opc->id = controller->operation_counter++;
442 opc->type = OP_PEER_CREATE;
444 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
445 &oprelease_peer_create);
446 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
453 * Create the given peer at the specified host using the given
454 * controller. If the given controller is not running on the target
455 * host, it should find or create a controller at the target host and
456 * delegate creating the peer. Explicit delegation paths can be setup
457 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
458 * path exists, a direct link with a subordinate controller is setup
459 * for the first delegated peer to a particular host; the subordinate
460 * controller is then destroyed once the last peer that was delegated
461 * to the remote host is stopped.
463 * Creating the peer only creates the handle to manipulate and further
464 * configure the peer; use "GNUNET_TESTBED_peer_start" and
465 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
468 * Note that the given configuration will be adjusted by the
469 * controller to avoid port/path conflicts with other peers.
470 * The "final" configuration can be obtained using
471 * 'GNUNET_TESTBED_peer_get_information'.
473 * @param controller controller process to use
474 * @param host host to run the peer on
475 * @param cfg Template configuration to use for the peer. Should exist until
476 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
477 * @param cb the callback to call when the peer has been created
478 * @param cls the closure to the above callback
479 * @return the operation handle
481 struct GNUNET_TESTBED_Operation *
482 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
483 struct GNUNET_TESTBED_Host *host,
484 const struct GNUNET_CONFIGURATION_Handle *cfg,
485 GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
487 static uint32_t id_gen;
489 return GNUNET_TESTBED_peer_create_with_id_ (id_gen++, controller, host, cfg,
495 * Start the given peer.
497 * @param peer peer to start
498 * @param pcc function to call upon completion
499 * @param pcc_cls closure for 'pcc'
500 * @return handle to the operation
502 struct GNUNET_TESTBED_Operation *
503 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer,
504 GNUNET_TESTBED_PeerChurnCallback pcc,
507 struct OperationContext *opc;
508 struct PeerEventData *data;
510 data = GNUNET_malloc (sizeof (struct PeerEventData));
513 data->pcc_cls = pcc_cls;
514 opc = GNUNET_malloc (sizeof (struct OperationContext));
515 opc->c = peer->controller;
517 opc->id = opc->c->operation_counter++;
518 opc->type = OP_PEER_START;
520 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
521 &oprelease_peer_start);
522 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
529 * Stop the given peer. The handle remains valid (use
530 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
531 * state of the peer).
533 * @param peer peer to stop
534 * @param pcc function to call upon completion
535 * @param pcc_cls closure for 'pcc'
536 * @return handle to the operation
538 struct GNUNET_TESTBED_Operation *
539 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer,
540 GNUNET_TESTBED_PeerChurnCallback pcc,
543 struct OperationContext *opc;
544 struct PeerEventData *data;
546 data = GNUNET_malloc (sizeof (struct PeerEventData));
549 data->pcc_cls = pcc_cls;
550 opc = GNUNET_malloc (sizeof (struct OperationContext));
551 opc->c = peer->controller;
553 opc->id = opc->c->operation_counter++;
554 opc->type = OP_PEER_STOP;
556 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
557 &oprelease_peer_stop);
558 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
565 * Request information about a peer. The controller callback will not be called
566 * with event type GNUNET_TESTBED_ET_OPERATION_FINISHED when result for this
567 * operation is available. Instead, the GNUNET_TESTBED_PeerInfoCallback() will
570 * @param peer peer to request information about
571 * @param pit desired information
572 * @param cb the convenience callback to be called when results for this
573 * operation are available
574 * @param cb_cls the closure for the above callback
575 * @return handle to the operation
577 struct GNUNET_TESTBED_Operation *
578 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
579 enum GNUNET_TESTBED_PeerInformationType
581 GNUNET_TESTBED_PeerInfoCallback cb,
584 struct OperationContext *opc;
585 struct PeerInfoData *data;
587 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
588 data = GNUNET_malloc (sizeof (struct PeerInfoData));
592 data->cb_cls = cb_cls;
593 opc = GNUNET_malloc (sizeof (struct OperationContext));
594 opc->c = peer->controller;
596 opc->type = OP_PEER_INFO;
597 opc->id = opc->c->operation_counter++;
599 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
600 &oprelease_peer_getinfo);
601 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
608 * Change peer configuration. Must only be called while the
609 * peer is stopped. Ports and paths cannot be changed this
612 * @param peer peer to change configuration for
613 * @param cfg new configuration (differences to existing
614 * configuration only)
615 * @return handle to the operation
617 struct GNUNET_TESTBED_Operation *
618 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
620 GNUNET_CONFIGURATION_Handle *cfg)
622 // FIXME: handle locally or delegate...
629 * Destroy the given peer; the peer should have been
630 * stopped first (if it was started).
632 * @param peer peer to stop
633 * @return handle to the operation
635 struct GNUNET_TESTBED_Operation *
636 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
638 struct OperationContext *opc;
640 opc = GNUNET_malloc (sizeof (struct OperationContext));
642 opc->c = peer->controller;
643 opc->id = peer->controller->operation_counter++;
644 opc->type = OP_PEER_DESTROY;
646 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
647 &oprelease_peer_destroy);
648 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
655 * Manipulate the P2P underlay topology by configuring a link
658 * @param op_cls closure argument to give with the operation event
659 * @param p1 first peer
660 * @param p2 second peer
661 * @param co option to change
662 * @param ... option-specific values
663 * @return handle to the operation, NULL if configuring the link at this
664 * time is not allowed
666 struct GNUNET_TESTBED_Operation *
667 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
668 struct GNUNET_TESTBED_Peer *p1,
669 struct GNUNET_TESTBED_Peer *p2,
670 enum GNUNET_TESTBED_ConnectOption co,
679 * Both peers must have been started before calling this function.
680 * This function then obtains a HELLO from 'p1', gives it to 'p2'
681 * and asks 'p2' to connect to 'p1'.
683 * @param op_cls closure argument to give with the operation event
684 * @param cb the callback to call when this operation has finished
685 * @param cb_cls the closure for the above callback
686 * @param p1 first peer
687 * @param p2 second peer
688 * @return handle to the operation, NULL if connecting these two
689 * peers is fundamentally not possible at this time (peers
690 * not running or underlay disallows)
692 struct GNUNET_TESTBED_Operation *
693 GNUNET_TESTBED_overlay_connect (void *op_cls,
694 GNUNET_TESTBED_OperationCompletionCallback cb,
696 struct GNUNET_TESTBED_Peer *p1,
697 struct GNUNET_TESTBED_Peer *p2)
699 struct OperationContext *opc;
700 struct OverlayConnectData *data;
702 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
703 GNUNET_assert (p1->controller == p2->controller);
704 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
708 data->cb_cls = cb_cls;
709 opc = GNUNET_malloc (sizeof (struct OperationContext));
711 opc->c = p1->controller;
712 opc->id = opc->c->operation_counter++;
713 opc->type = OP_OVERLAY_CONNECT;
715 GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
716 &oprelease_overlay_connect);
717 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
724 /* end of testbed_api_peers.c */