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_CREATE_PEER);
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;
89 case OPC_STATE_STARTED:
90 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
91 /* No break we continue flow */
93 GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
94 GNUNET_free (opc->data);
96 case OPC_STATE_FINISHED:
104 * Function called when a peer destroy operation is ready
106 * @param cls the closure from GNUNET_TESTBED_operation_create_()
109 opstart_peer_destroy (void *cls)
111 struct OperationContext *opc = cls;
112 struct GNUNET_TESTBED_Peer *peer;
113 struct GNUNET_TESTBED_PeerDestroyMessage *msg;
115 GNUNET_assert (OP_PEER_DESTROY == opc->type);
117 GNUNET_assert (NULL != peer);
118 opc->state = OPC_STATE_STARTED;
119 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
120 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
121 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER);
122 msg->peer_id = htonl (peer->unique_id);
123 msg->operation_id = GNUNET_htonll (opc->id);
124 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
125 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
130 * Callback which will be called when peer_create type operation is released
132 * @param cls the closure from GNUNET_TESTBED_operation_create_()
135 oprelease_peer_destroy (void *cls)
137 struct OperationContext *opc = cls;
139 if (OPC_STATE_FINISHED != opc->state)
140 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
146 * Function called when a peer start operation is ready
148 * @param cls the closure from GNUNET_TESTBED_operation_create_()
151 opstart_peer_start (void *cls)
153 struct OperationContext *opc = cls;
154 struct GNUNET_TESTBED_PeerStartMessage *msg;
155 struct PeerEventData *data;
156 struct GNUNET_TESTBED_Peer *peer;
158 GNUNET_assert (OP_PEER_START == opc->type);
159 GNUNET_assert (NULL != opc->data);
161 GNUNET_assert (NULL != data->peer);
163 GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
164 opc->state = OPC_STATE_STARTED;
165 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
166 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
167 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_START_PEER);
168 msg->peer_id = htonl (peer->unique_id);
169 msg->operation_id = GNUNET_htonll (opc->id);
170 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
171 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
176 * Callback which will be called when peer start type operation is released
178 * @param cls the closure from GNUNET_TESTBED_operation_create_()
181 oprelease_peer_start (void *cls)
183 struct OperationContext *opc = cls;
185 if (OPC_STATE_FINISHED != opc->state)
187 GNUNET_free (opc->data);
188 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
195 * Function called when a peer stop operation is ready
197 * @param cls the closure from GNUNET_TESTBED_operation_create_()
200 opstart_peer_stop (void *cls)
202 struct OperationContext *opc = cls;
203 struct GNUNET_TESTBED_PeerStopMessage *msg;
204 struct PeerEventData *data;
205 struct GNUNET_TESTBED_Peer *peer;
207 GNUNET_assert (NULL != opc->data);
209 GNUNET_assert (NULL != data->peer);
211 GNUNET_assert (PS_STARTED == peer->state);
212 opc->state = OPC_STATE_STARTED;
213 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
214 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER);
215 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
216 msg->peer_id = htonl (peer->unique_id);
217 msg->operation_id = GNUNET_htonll (opc->id);
218 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
219 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
224 * Callback which will be called when peer stop type operation is released
226 * @param cls the closure from GNUNET_TESTBED_operation_create_()
229 oprelease_peer_stop (void *cls)
231 struct OperationContext *opc = cls;
233 if (OPC_STATE_FINISHED != opc->state)
235 GNUNET_free (opc->data);
236 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
243 * Generate PeerGetConfigurationMessage
245 * @param peer_id the id of the peer whose information we have to get
246 * @param operation_id the ip of the operation that should be represented in the
248 * @return the PeerGetConfigurationMessage
250 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
251 GNUNET_TESTBED_generate_peergetconfig_msg_ (uint32_t peer_id,
252 uint64_t operation_id)
254 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
257 GNUNET_malloc (sizeof
258 (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
260 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
261 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_CONFIGURATION);
262 msg->peer_id = htonl (peer_id);
263 msg->operation_id = GNUNET_htonll (operation_id);
269 * Function called when a peer get information operation is ready
271 * @param cls the closure from GNUNET_TESTBED_operation_create_()
274 opstart_peer_getinfo (void *cls)
276 struct OperationContext *opc = cls;
277 struct PeerInfoData *data;
278 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
281 GNUNET_assert (NULL != data);
282 opc->state = OPC_STATE_STARTED;
284 GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
286 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
287 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
292 * Callback which will be called when peer stop type operation is released
294 * @param cls the closure from GNUNET_TESTBED_operation_create_()
297 oprelease_peer_getinfo (void *cls)
299 struct OperationContext *opc = cls;
300 struct GNUNET_TESTBED_PeerInformation *data;
302 if (OPC_STATE_FINISHED != opc->state)
304 GNUNET_free_non_null (opc->data);
305 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
310 GNUNET_assert (NULL != data);
313 case GNUNET_TESTBED_PIT_CONFIGURATION:
314 GNUNET_CONFIGURATION_destroy (data->result.cfg);
316 case GNUNET_TESTBED_PIT_IDENTITY:
317 GNUNET_free (data->result.id);
320 GNUNET_assert (0); /* We should never reach here */
329 * Function called when a overlay connect operation is ready
331 * @param cls the closure from GNUNET_TESTBED_operation_create_()
334 opstart_overlay_connect (void *cls)
336 struct OperationContext *opc = cls;
337 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
338 struct OverlayConnectData *data;
340 opc->state = OPC_STATE_STARTED;
342 GNUNET_assert (NULL != data);
343 data->tslot_index = GNUNET_TESTBED_get_tslot_ (data->p1->host, data);
344 data->tstart = GNUNET_TIME_absolute_get ();
345 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
347 htons (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
348 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT);
349 msg->peer1 = htonl (data->p1->unique_id);
350 msg->peer2 = htonl (data->p2->unique_id);
351 msg->operation_id = GNUNET_htonll (opc->id);
352 msg->peer2_host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->p2->host));
353 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
354 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
359 * Callback which will be called when overlay connect operation is released
361 * @param cls the closure from GNUNET_TESTBED_operation_create_()
364 oprelease_overlay_connect (void *cls)
366 struct OperationContext *opc = cls;
367 struct GNUNET_TIME_Relative duration;
368 struct OverlayConnectData *data;
375 case OPC_STATE_STARTED:
376 (void) GNUNET_TESTBED_release_time_slot_ (data->p1->host, data->tslot_index,
378 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
380 case OPC_STATE_FINISHED:
381 duration = GNUNET_TIME_absolute_get_duration (data->tstart);
382 GNUNET_TESTBED_update_time_slot_ (data->p1->host, data->tslot_index, data,
383 duration, data->failed);
391 * Lookup a peer by ID.
393 * @param id global peer ID assigned to the peer
394 * @return handle to the host, NULL on error
396 struct GNUNET_TESTBED_Peer *
397 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
405 * Create the given peer at the specified host using the given
406 * controller. If the given controller is not running on the target
407 * host, it should find or create a controller at the target host and
408 * delegate creating the peer. Explicit delegation paths can be setup
409 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
410 * path exists, a direct link with a subordinate controller is setup
411 * for the first delegated peer to a particular host; the subordinate
412 * controller is then destroyed once the last peer that was delegated
413 * to the remote host is stopped.
415 * Creating the peer only creates the handle to manipulate and further
416 * configure the peer; use "GNUNET_TESTBED_peer_start" and
417 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
420 * Note that the given configuration will be adjusted by the
421 * controller to avoid port/path conflicts with other peers.
422 * The "final" configuration can be obtained using
423 * 'GNUNET_TESTBED_peer_get_information'.
425 * @param controller controller process to use
426 * @param host host to run the peer on; cannot be NULL
427 * @param cfg Template configuration to use for the peer. Should exist until
428 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
429 * @param cb the callback to call when the peer has been created
430 * @param cls the closure to the above callback
431 * @return the operation handle
433 struct GNUNET_TESTBED_Operation *
434 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
435 struct GNUNET_TESTBED_Host *host,
436 const struct GNUNET_CONFIGURATION_Handle *cfg,
437 GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
440 struct GNUNET_TESTBED_Peer *peer;
441 struct PeerCreateData *data;
442 struct OperationContext *opc;
443 static uint32_t id_gen;
445 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
446 peer->controller = controller;
448 peer->unique_id = id_gen++;
449 peer->state = PS_INVALID;
450 data = GNUNET_malloc (sizeof (struct PeerCreateData));
456 opc = GNUNET_malloc (sizeof (struct OperationContext));
459 opc->id = GNUNET_TESTBED_get_next_op_id (controller);
460 opc->type = OP_PEER_CREATE;
462 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
463 &oprelease_peer_create);
464 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
466 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
472 * Start the given peer.
474 * @param op_cls the closure for this operation; will be set in
475 * event->details.operation_finished.op_cls when this operation fails.
476 * @param peer peer to start
477 * @param pcc function to call upon completion
478 * @param pcc_cls closure for 'pcc'
479 * @return handle to the operation
481 struct GNUNET_TESTBED_Operation *
482 GNUNET_TESTBED_peer_start (void *op_cls, struct GNUNET_TESTBED_Peer *peer,
483 GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
485 struct OperationContext *opc;
486 struct PeerEventData *data;
488 data = GNUNET_malloc (sizeof (struct PeerEventData));
491 data->pcc_cls = pcc_cls;
492 opc = GNUNET_malloc (sizeof (struct OperationContext));
493 opc->c = peer->controller;
495 opc->op_cls = op_cls;
496 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
497 opc->type = OP_PEER_START;
499 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
500 &oprelease_peer_start);
501 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
503 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
509 * Stop the given peer. The handle remains valid (use
510 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
511 * state of the peer).
513 * @param peer peer to stop
514 * @param pcc function to call upon completion
515 * @param pcc_cls closure for 'pcc'
516 * @return handle to the operation
518 struct GNUNET_TESTBED_Operation *
519 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer,
520 GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
522 struct OperationContext *opc;
523 struct PeerEventData *data;
525 data = GNUNET_malloc (sizeof (struct PeerEventData));
528 data->pcc_cls = pcc_cls;
529 opc = GNUNET_malloc (sizeof (struct OperationContext));
530 opc->c = peer->controller;
532 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
533 opc->type = OP_PEER_STOP;
535 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
536 &oprelease_peer_stop);
537 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
539 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
545 * Request information about a peer. The controller callback will not be called
546 * with event type GNUNET_TESTBED_ET_OPERATION_FINISHED when result for this
547 * operation is available. Instead, the GNUNET_TESTBED_PeerInfoCallback() will
550 * @param peer peer to request information about
551 * @param pit desired information
552 * @param cb the convenience callback to be called when results for this
553 * operation are available
554 * @param cb_cls the closure for the above callback
555 * @return handle to the operation
557 struct GNUNET_TESTBED_Operation *
558 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
559 enum GNUNET_TESTBED_PeerInformationType
560 pit, GNUNET_TESTBED_PeerInfoCallback cb,
563 struct OperationContext *opc;
564 struct PeerInfoData *data;
566 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
567 data = GNUNET_malloc (sizeof (struct PeerInfoData));
571 data->cb_cls = cb_cls;
572 opc = GNUNET_malloc (sizeof (struct OperationContext));
573 opc->c = peer->controller;
575 opc->type = OP_PEER_INFO;
576 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
578 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
579 &oprelease_peer_getinfo);
580 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
582 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
588 * Change peer configuration. Must only be called while the
589 * peer is stopped. Ports and paths cannot be changed this
592 * @param peer peer to change configuration for
593 * @param cfg new configuration (differences to existing
594 * configuration only)
595 * @return handle to the operation
597 struct GNUNET_TESTBED_Operation *
598 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
600 GNUNET_CONFIGURATION_Handle *cfg)
602 // FIXME: handle locally or delegate...
609 * Destroy the given peer; the peer should have been
610 * stopped first (if it was started).
612 * @param peer peer to stop
613 * @return handle to the operation
615 struct GNUNET_TESTBED_Operation *
616 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
618 struct OperationContext *opc;
620 opc = GNUNET_malloc (sizeof (struct OperationContext));
622 opc->c = peer->controller;
623 opc->id = GNUNET_TESTBED_get_next_op_id (peer->controller);
624 opc->type = OP_PEER_DESTROY;
626 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
627 &oprelease_peer_destroy);
628 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
630 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
636 * Manipulate the P2P underlay topology by configuring a link
639 * @param op_cls closure argument to give with the operation event
640 * @param p1 first peer
641 * @param p2 second peer
642 * @param co option to change
643 * @param ... option-specific values
644 * @return handle to the operation, NULL if configuring the link at this
645 * time is not allowed
647 struct GNUNET_TESTBED_Operation *
648 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
649 struct GNUNET_TESTBED_Peer *p1,
650 struct GNUNET_TESTBED_Peer *p2,
651 enum GNUNET_TESTBED_ConnectOption co,
660 * Both peers must have been started before calling this function.
661 * This function then obtains a HELLO from 'p1', gives it to 'p2'
662 * and asks 'p2' to connect to 'p1'.
664 * @param op_cls closure argument to give with the operation event
665 * @param cb the callback to call when this operation has finished
666 * @param cb_cls the closure for the above callback
667 * @param p1 first peer
668 * @param p2 second peer
669 * @return handle to the operation, NULL if connecting these two
670 * peers is fundamentally not possible at this time (peers
671 * not running or underlay disallows)
673 struct GNUNET_TESTBED_Operation *
674 GNUNET_TESTBED_overlay_connect (void *op_cls,
675 GNUNET_TESTBED_OperationCompletionCallback cb,
676 void *cb_cls, struct GNUNET_TESTBED_Peer *p1,
677 struct GNUNET_TESTBED_Peer *p2)
679 struct OperationContext *opc;
680 struct OverlayConnectData *data;
682 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
683 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
687 data->cb_cls = cb_cls;
688 opc = GNUNET_malloc (sizeof (struct OperationContext));
690 opc->c = p1->controller;
691 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
692 opc->type = OP_OVERLAY_CONNECT;
693 opc->op_cls = op_cls;
695 GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
696 &oprelease_overlay_connect);
697 GNUNET_TESTBED_host_queue_oc (p1->host, opc->op);
698 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
704 /* end of testbed_api_peers.c */