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,
119 opc->c->ocq_tail, opc);
120 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
125 * Callback which will be called when peer_create type operation is released
127 * @param cls the closure from GNUNET_TESTBED_operation_create_()
130 oprelease_peer_destroy (void *cls)
132 struct OperationContext *opc = cls;
134 if (OPC_STATE_FINISHED != opc->state)
135 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
141 * Function called when a peer start operation is ready
143 * @param cls the closure from GNUNET_TESTBED_operation_create_()
146 opstart_peer_start (void *cls)
148 struct OperationContext *opc = cls;
149 struct GNUNET_TESTBED_PeerStartMessage *msg;
150 struct GNUNET_TESTBED_Peer *peer;
152 GNUNET_assert (OP_PEER_START == opc->type);
153 GNUNET_assert (NULL != opc->data);
155 GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
156 opc->state = OPC_STATE_STARTED;
157 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
158 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
159 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
160 msg->peer_id = htonl (peer->unique_id);
161 msg->operation_id = GNUNET_htonll (opc->id);
162 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
163 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
168 * Callback which will be called when peer start type operation is released
170 * @param cls the closure from GNUNET_TESTBED_operation_create_()
173 oprelease_peer_start (void *cls)
175 struct OperationContext *opc = cls;
177 if (OPC_STATE_FINISHED != opc->state)
178 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
184 * Function called when a peer stop operation is ready
186 * @param cls the closure from GNUNET_TESTBED_operation_create_()
189 opstart_peer_stop (void *cls)
191 struct OperationContext *opc = cls;
192 struct GNUNET_TESTBED_PeerStopMessage *msg;
193 struct GNUNET_TESTBED_Peer *peer;
195 GNUNET_assert (NULL != opc->data);
197 GNUNET_assert (PS_STARTED == peer->state);
198 opc->state = OPC_STATE_STARTED;
199 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
200 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
201 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
202 msg->peer_id = htonl (peer->unique_id);
203 msg->operation_id = GNUNET_htonll (opc->id);
204 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
205 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
210 * Callback which will be called when peer stop type operation is released
212 * @param cls the closure from GNUNET_TESTBED_operation_create_()
215 oprelease_peer_stop (void *cls)
217 struct OperationContext *opc = cls;
219 if (OPC_STATE_FINISHED != opc->state)
220 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
226 * Generate PeerGetConfigurationMessage
228 * @param peer_id the id of the peer whose information we have to get
229 * @param operation_id the ip of the operation that should be represented in
232 * @return the PeerGetConfigurationMessage
234 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
235 GNUNET_TESTBED_generate_peergetconfig_msg_ (uint32_t peer_id,
236 uint64_t operation_id)
238 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
240 msg = GNUNET_malloc (sizeof (struct
241 GNUNET_TESTBED_PeerGetConfigurationMessage));
242 msg->header.size = htons
243 (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
244 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
245 msg->peer_id = htonl (peer_id);
246 msg->operation_id = GNUNET_htonll (operation_id);
252 * Function called when a peer get information operation is ready
254 * @param cls the closure from GNUNET_TESTBED_operation_create_()
257 opstart_peer_getinfo (void *cls)
259 struct OperationContext *opc = cls;
260 struct PeerInfoData *data;
261 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
264 GNUNET_assert (NULL != data);
265 opc->state = OPC_STATE_STARTED;
266 msg = GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
268 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
269 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
274 * Callback which will be called when peer stop type operation is released
276 * @param cls the closure from GNUNET_TESTBED_operation_create_()
279 oprelease_peer_getinfo (void *cls)
281 struct OperationContext *opc = cls;
282 struct PeerInfoData2 *data;
284 if (OPC_STATE_FINISHED != opc->state)
286 GNUNET_free_non_null (opc->data);
287 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
292 GNUNET_assert (NULL != data);
295 case GNUNET_TESTBED_PIT_CONFIGURATION:
296 GNUNET_CONFIGURATION_destroy (data->details.cfg);
298 case GNUNET_TESTBED_PIT_IDENTITY:
299 GNUNET_free (data->details.peer_identity);
302 GNUNET_assert (0); /* We should never reach here */
311 * Function called when a overlay connect operation is ready
313 * @param cls the closure from GNUNET_TESTBED_operation_create_()
316 opstart_overlay_connect (void *cls)
318 struct OperationContext *opc = cls;
319 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
320 struct OverlayConnectData *data;
322 opc->state = OPC_STATE_STARTED;
324 GNUNET_assert (NULL != data);
325 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
326 msg->header.size = htons (sizeof
327 (struct GNUNET_TESTBED_OverlayConnectMessage));
328 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
329 msg->peer1 = htonl (data->p1->unique_id);
330 msg->peer2 = htonl (data->p2->unique_id);
331 msg->operation_id = GNUNET_htonll (opc->id);
332 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
333 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
338 * Callback which will be called when overlay connect operation is released
340 * @param cls the closure from GNUNET_TESTBED_operation_create_()
343 oprelease_overlay_connect (void *cls)
345 struct OperationContext *opc = cls;
347 if (OPC_STATE_FINISHED != opc->state)
349 GNUNET_free (opc->data);
350 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
357 * Lookup a peer by ID.
359 * @param id global peer ID assigned to the peer
360 * @return handle to the host, NULL on error
362 struct GNUNET_TESTBED_Peer *
363 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
371 * Create the given peer at the specified host using the given
372 * controller. If the given controller is not running on the target
373 * host, it should find or create a controller at the target host and
374 * delegate creating the peer. Explicit delegation paths can be setup
375 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
376 * path exists, a direct link with a subordinate controller is setup
377 * for the first delegated peer to a particular host; the subordinate
378 * controller is then destroyed once the last peer that was delegated
379 * to the remote host is stopped. This function is used in particular
380 * if some other controller has already assigned a unique ID to the
383 * Creating the peer only creates the handle to manipulate and further
384 * configure the peer; use "GNUNET_TESTBED_peer_start" and
385 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
388 * Note that the given configuration will be adjusted by the
389 * controller to avoid port/path conflicts with other peers.
390 * The "final" configuration can be obtained using
391 * 'GNUNET_TESTBED_peer_get_information'.
393 * @param unique_id unique ID for this peer
394 * @param controller controller process to use
395 * @param host host to run the peer on
396 * @param cfg Template configuration to use for the peer. Should exist until
397 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
398 * @param cb the callback to call when the peer has been created
399 * @param cls the closure to the above callback
400 * @return the operation handle
402 struct GNUNET_TESTBED_Operation *
403 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
404 struct GNUNET_TESTBED_Controller *controller,
405 struct GNUNET_TESTBED_Host *host,
406 const struct GNUNET_CONFIGURATION_Handle *cfg,
407 GNUNET_TESTBED_PeerCreateCallback cb,
410 struct GNUNET_TESTBED_Peer *peer;
411 struct PeerCreateData *data;
412 struct OperationContext *opc;
414 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
415 peer->controller = controller;
417 peer->unique_id = unique_id;
418 peer->state = PS_INVALID;
419 data = GNUNET_malloc (sizeof (struct PeerCreateData));
425 opc = GNUNET_malloc (sizeof (struct OperationContext));
428 opc->id = controller->operation_counter++;
429 opc->type = OP_PEER_CREATE;
430 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
431 &oprelease_peer_create);
432 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
439 * Create the given peer at the specified host using the given
440 * controller. If the given controller is not running on the target
441 * host, it should find or create a controller at the target host and
442 * delegate creating the peer. Explicit delegation paths can be setup
443 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
444 * path exists, a direct link with a subordinate controller is setup
445 * for the first delegated peer to a particular host; the subordinate
446 * controller is then destroyed once the last peer that was delegated
447 * to the remote host is stopped.
449 * Creating the peer only creates the handle to manipulate and further
450 * configure the peer; use "GNUNET_TESTBED_peer_start" and
451 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
454 * Note that the given configuration will be adjusted by the
455 * controller to avoid port/path conflicts with other peers.
456 * The "final" configuration can be obtained using
457 * 'GNUNET_TESTBED_peer_get_information'.
459 * @param controller controller process to use
460 * @param host host to run the peer on
461 * @param cfg Template configuration to use for the peer. Should exist until
462 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
463 * @param cb the callback to call when the peer has been created
464 * @param cls the closure to the above callback
465 * @return the operation handle
467 struct GNUNET_TESTBED_Operation *
468 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
469 struct GNUNET_TESTBED_Host *host,
470 const struct GNUNET_CONFIGURATION_Handle *cfg,
471 GNUNET_TESTBED_PeerCreateCallback cb,
474 static uint32_t id_gen;
476 return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
485 * Start the given peer.
487 * @param peer peer to start
488 * @return handle to the operation
490 struct GNUNET_TESTBED_Operation *
491 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
493 struct OperationContext *opc;
495 opc = GNUNET_malloc (sizeof (struct OperationContext));
496 opc->c = peer->controller;
498 opc->id = opc->c->operation_counter++;
499 opc->type = OP_PEER_START;
500 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
501 &oprelease_peer_start);
502 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
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 * @return handle to the operation
516 struct GNUNET_TESTBED_Operation *
517 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
519 struct OperationContext *opc;
521 opc = GNUNET_malloc (sizeof (struct OperationContext));
522 opc->c = peer->controller;
524 opc->id = opc->c->operation_counter++;
525 opc->type = OP_PEER_STOP;
526 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
527 &oprelease_peer_stop);
528 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
535 * Request information about a peer.
537 * @param peer peer to request information about
538 * @param pit desired information
539 * @return handle to the operation
541 struct GNUNET_TESTBED_Operation *
542 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
543 enum GNUNET_TESTBED_PeerInformationType pit)
545 struct OperationContext *opc;
546 struct PeerInfoData *data;
548 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
549 data = GNUNET_malloc (sizeof (struct PeerInfoData));
552 opc = GNUNET_malloc (sizeof (struct OperationContext));
553 opc->c = peer->controller;
555 opc->type = OP_PEER_INFO;
556 opc->id = opc->c->operation_counter++;
557 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
558 &oprelease_peer_getinfo);
559 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
566 * Change peer configuration. Must only be called while the
567 * peer is stopped. Ports and paths cannot be changed this
570 * @param peer peer to change configuration for
571 * @param cfg new configuration (differences to existing
572 * configuration only)
573 * @return handle to the operation
575 struct GNUNET_TESTBED_Operation *
576 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
577 const struct GNUNET_CONFIGURATION_Handle *cfg)
579 // FIXME: handle locally or delegate...
586 * Destroy the given peer; the peer should have been
587 * stopped first (if it was started).
589 * @param peer peer to stop
590 * @return handle to the operation
592 struct GNUNET_TESTBED_Operation *
593 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
595 struct OperationContext *opc;
597 opc = GNUNET_malloc (sizeof (struct OperationContext));
599 opc->c = peer->controller;
600 opc->id = peer->controller->operation_counter++;
601 opc->type = OP_PEER_DESTROY;
602 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
603 &oprelease_peer_destroy);
604 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
611 * Manipulate the P2P underlay topology by configuring a link
614 * @param op_cls closure argument to give with the operation event
615 * @param p1 first peer
616 * @param p2 second peer
617 * @param co option to change
618 * @param ... option-specific values
619 * @return handle to the operation, NULL if configuring the link at this
620 * time is not allowed
622 struct GNUNET_TESTBED_Operation *
623 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
624 struct GNUNET_TESTBED_Peer *p1,
625 struct GNUNET_TESTBED_Peer *p2,
626 enum GNUNET_TESTBED_ConnectOption co, ...)
635 * Both peers must have been started before calling this function.
636 * This function then obtains a HELLO from 'p1', gives it to 'p2'
637 * and asks 'p2' to connect to 'p1'.
639 * @param op_cls closure argument to give with the operation event
640 * @param p1 first peer
641 * @param p2 second peer
642 * @return handle to the operation, NULL if connecting these two
643 * peers is fundamentally not possible at this time (peers
644 * not running or underlay disallows)
646 struct GNUNET_TESTBED_Operation *
647 GNUNET_TESTBED_overlay_connect (void *op_cls,
648 struct GNUNET_TESTBED_Peer *p1,
649 struct GNUNET_TESTBED_Peer *p2)
651 struct OperationContext *opc;
652 struct OverlayConnectData *data;
654 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
655 GNUNET_assert (p1->controller == p2->controller);
656 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
659 opc = GNUNET_malloc (sizeof (struct OperationContext));
661 opc->c = p1->controller;
662 opc->id = opc->c->operation_counter++;
663 opc->type = OP_OVERLAY_CONNECT;
664 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
665 &oprelease_overlay_connect);
666 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
673 /* end of testbed_api_peers.c */