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,
73 opc->c->ocq_tail, opc);
74 GNUNET_TESTBED_queue_message_ (opc->c,
75 (struct GNUNET_MessageHeader *) msg);
80 * Callback which will be called when peer_create type operation is released
82 * @param cls the closure from GNUNET_TESTBED_operation_create_()
85 oprelease_peer_create (void *cls)
87 struct OperationContext *opc = cls;
89 if (OPC_STATE_FINISHED != opc->state)
91 GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
92 GNUNET_free (opc->data);
93 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
100 * Function called when a peer destroy operation is ready
102 * @param cls the closure from GNUNET_TESTBED_operation_create_()
105 opstart_peer_destroy (void *cls)
107 struct OperationContext *opc = cls;
108 struct GNUNET_TESTBED_Peer *peer;
109 struct GNUNET_TESTBED_PeerDestroyMessage *msg;
111 GNUNET_assert (OP_PEER_DESTROY == opc->type);
113 GNUNET_assert (NULL != peer);
114 opc->state = OPC_STATE_STARTED;
115 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
116 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
117 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
118 msg->peer_id = htonl (peer->unique_id);
119 msg->operation_id = GNUNET_htonll (opc->id);
120 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
121 opc->c->ocq_tail, opc);
122 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
127 * Callback which will be called when peer_create type operation is released
129 * @param cls the closure from GNUNET_TESTBED_operation_create_()
132 oprelease_peer_destroy (void *cls)
134 struct OperationContext *opc = cls;
136 if (OPC_STATE_FINISHED != opc->state)
137 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
143 * Function called when a peer start operation is ready
145 * @param cls the closure from GNUNET_TESTBED_operation_create_()
148 opstart_peer_start (void *cls)
150 struct OperationContext *opc = cls;
151 struct GNUNET_TESTBED_PeerStartMessage *msg;
152 struct GNUNET_TESTBED_Peer *peer;
154 GNUNET_assert (OP_PEER_START == opc->type);
155 GNUNET_assert (NULL != opc->data);
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)
180 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
186 * Function called when a peer stop operation is ready
188 * @param cls the closure from GNUNET_TESTBED_operation_create_()
191 opstart_peer_stop (void *cls)
193 struct OperationContext *opc = cls;
194 struct GNUNET_TESTBED_PeerStopMessage *msg;
195 struct GNUNET_TESTBED_Peer *peer;
197 GNUNET_assert (NULL != opc->data);
199 GNUNET_assert (PS_STARTED == peer->state);
200 opc->state = OPC_STATE_STARTED;
201 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
202 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
203 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
204 msg->peer_id = htonl (peer->unique_id);
205 msg->operation_id = GNUNET_htonll (opc->id);
206 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
207 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
212 * Callback which will be called when peer stop type operation is released
214 * @param cls the closure from GNUNET_TESTBED_operation_create_()
217 oprelease_peer_stop (void *cls)
219 struct OperationContext *opc = cls;
221 if (OPC_STATE_FINISHED != opc->state)
222 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
228 * Function called when a peer get information operation is ready
230 * @param cls the closure from GNUNET_TESTBED_operation_create_()
233 opstart_peer_getinfo (void *cls)
235 struct OperationContext *opc = cls;
236 struct PeerInfoData *data;
237 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
240 GNUNET_assert (NULL != data);
241 opc->state = OPC_STATE_STARTED;
242 msg = GNUNET_malloc (sizeof (struct
243 GNUNET_TESTBED_PeerGetConfigurationMessage));
244 msg->header.size = htons
245 (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
246 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
247 msg->peer_id = htonl (data->peer->unique_id);
248 msg->operation_id = GNUNET_htonll (opc->id);
249 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
250 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
255 * Callback which will be called when peer stop type operation is released
257 * @param cls the closure from GNUNET_TESTBED_operation_create_()
260 oprelease_peer_getinfo (void *cls)
262 struct OperationContext *opc = cls;
263 struct PeerInfoData2 *data;
265 if (OPC_STATE_FINISHED != opc->state)
267 GNUNET_free_non_null (opc->data);
268 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
273 GNUNET_assert (NULL != data);
276 case GNUNET_TESTBED_PIT_CONFIGURATION:
277 GNUNET_CONFIGURATION_destroy (data->details.cfg);
279 case GNUNET_TESTBED_PIT_IDENTITY:
280 GNUNET_free (data->details.peer_identity);
283 GNUNET_assert (0); /* We should never reach here */
292 * Function called when a overlay connect operation is ready
294 * @param cls the closure from GNUNET_TESTBED_operation_create_()
297 opstart_overlay_connect (void *cls)
299 struct OperationContext *opc = cls;
300 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
301 struct OverlayConnectData *data;
303 opc->state = OPC_STATE_STARTED;
305 GNUNET_assert (NULL != data);
306 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
307 msg->header.size = htons (sizeof
308 (struct GNUNET_TESTBED_OverlayConnectMessage));
309 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
310 msg->peer1 = htonl (data->p1->unique_id);
311 msg->peer2 = htonl (data->p2->unique_id);
312 msg->operation_id = GNUNET_htonll (opc->id);
313 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
314 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
319 * Callback which will be called when overlay connect operation is released
321 * @param cls the closure from GNUNET_TESTBED_operation_create_()
324 oprelease_overlay_connect (void *cls)
326 struct OperationContext *opc = cls;
328 if (OPC_STATE_FINISHED != opc->state)
330 GNUNET_free (opc->data);
331 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
338 * Lookup a peer by ID.
340 * @param id global peer ID assigned to the peer
341 * @return handle to the host, NULL on error
343 struct GNUNET_TESTBED_Peer *
344 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
352 * Create the given peer at the specified host using the given
353 * controller. If the given controller is not running on the target
354 * host, it should find or create a controller at the target host and
355 * delegate creating the peer. Explicit delegation paths can be setup
356 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
357 * path exists, a direct link with a subordinate controller is setup
358 * for the first delegated peer to a particular host; the subordinate
359 * controller is then destroyed once the last peer that was delegated
360 * to the remote host is stopped. This function is used in particular
361 * if some other controller has already assigned a unique ID to the
364 * Creating the peer only creates the handle to manipulate and further
365 * configure the peer; use "GNUNET_TESTBED_peer_start" and
366 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
369 * Note that the given configuration will be adjusted by the
370 * controller to avoid port/path conflicts with other peers.
371 * The "final" configuration can be obtained using
372 * 'GNUNET_TESTBED_peer_get_information'.
374 * @param unique_id unique ID for this peer
375 * @param controller controller process to use
376 * @param host host to run the peer on
377 * @param cfg Template configuration to use for the peer. Should exist until
378 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
379 * @param cb the callback to call when the peer has been created
380 * @param cls the closure to the above callback
381 * @return the operation handle
383 struct GNUNET_TESTBED_Operation *
384 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
385 struct GNUNET_TESTBED_Controller *controller,
386 struct GNUNET_TESTBED_Host *host,
387 const struct GNUNET_CONFIGURATION_Handle *cfg,
388 GNUNET_TESTBED_PeerCreateCallback cb,
391 struct GNUNET_TESTBED_Peer *peer;
392 struct PeerCreateData *data;
393 struct OperationContext *opc;
395 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
396 peer->controller = controller;
398 peer->unique_id = unique_id;
399 peer->state = PS_INVALID;
400 data = GNUNET_malloc (sizeof (struct PeerCreateData));
406 opc = GNUNET_malloc (sizeof (struct OperationContext));
409 opc->id = controller->operation_counter++;
410 opc->type = OP_PEER_CREATE;
411 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
412 &oprelease_peer_create);
413 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_peer_create, opc->op);
419 * Create the given peer at the specified host using the given
420 * controller. If the given controller is not running on the target
421 * host, it should find or create a controller at the target host and
422 * delegate creating the peer. Explicit delegation paths can be setup
423 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
424 * path exists, a direct link with a subordinate controller is setup
425 * for the first delegated peer to a particular host; the subordinate
426 * controller is then destroyed once the last peer that was delegated
427 * to the remote host is stopped.
429 * Creating the peer only creates the handle to manipulate and further
430 * configure the peer; use "GNUNET_TESTBED_peer_start" and
431 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
434 * Note that the given configuration will be adjusted by the
435 * controller to avoid port/path conflicts with other peers.
436 * The "final" configuration can be obtained using
437 * 'GNUNET_TESTBED_peer_get_information'.
439 * @param controller controller process to use
440 * @param host host to run the peer on
441 * @param cfg Template configuration to use for the peer. Should exist until
442 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
443 * @param cb the callback to call when the peer has been created
444 * @param cls the closure to the above callback
445 * @return the operation handle
447 struct GNUNET_TESTBED_Operation *
448 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
449 struct GNUNET_TESTBED_Host *host,
450 const struct GNUNET_CONFIGURATION_Handle *cfg,
451 GNUNET_TESTBED_PeerCreateCallback cb,
454 static uint32_t id_gen;
456 return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
465 * Start the given peer.
467 * @param peer peer to start
468 * @return handle to the operation
470 struct GNUNET_TESTBED_Operation *
471 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
473 struct OperationContext *opc;
475 opc = GNUNET_malloc (sizeof (struct OperationContext));
476 opc->c = peer->controller;
478 opc->id = opc->c->operation_counter++;
479 opc->type = OP_PEER_START;
480 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
481 &oprelease_peer_start);
482 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
488 * Stop the given peer. The handle remains valid (use
489 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
490 * state of the peer).
492 * @param peer peer to stop
493 * @return handle to the operation
495 struct GNUNET_TESTBED_Operation *
496 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
498 struct OperationContext *opc;
500 opc = GNUNET_malloc (sizeof (struct OperationContext));
501 opc->c = peer->controller;
503 opc->id = opc->c->operation_counter++;
504 opc->type = OP_PEER_STOP;
505 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
506 &oprelease_peer_stop);
507 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
513 * Request information about a peer.
515 * @param peer peer to request information about
516 * @param pit desired information
517 * @return handle to the operation
519 struct GNUNET_TESTBED_Operation *
520 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
521 enum GNUNET_TESTBED_PeerInformationType pit)
523 struct OperationContext *opc;
524 struct PeerInfoData *data;
526 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
527 data = GNUNET_malloc (sizeof (struct PeerInfoData));
530 opc = GNUNET_malloc (sizeof (struct OperationContext));
531 opc->c = peer->controller;
533 opc->type = OP_PEER_INFO;
534 opc->id = opc->c->operation_counter++;
535 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
536 &oprelease_peer_getinfo);
537 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
543 * Change peer configuration. Must only be called while the
544 * peer is stopped. Ports and paths cannot be changed this
547 * @param peer peer to change configuration for
548 * @param cfg new configuration (differences to existing
549 * configuration only)
550 * @return handle to the operation
552 struct GNUNET_TESTBED_Operation *
553 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
554 const struct GNUNET_CONFIGURATION_Handle *cfg)
556 // FIXME: handle locally or delegate...
563 * Destroy the given peer; the peer should have been
564 * stopped first (if it was started).
566 * @param peer peer to stop
567 * @return handle to the operation
569 struct GNUNET_TESTBED_Operation *
570 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
572 struct OperationContext *opc;
574 opc = GNUNET_malloc (sizeof (struct OperationContext));
576 opc->c = peer->controller;
577 opc->id = peer->controller->operation_counter++;
578 opc->type = OP_PEER_DESTROY;
579 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
580 &oprelease_peer_destroy);
581 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
588 * Manipulate the P2P underlay topology by configuring a link
591 * @param op_cls closure argument to give with the operation event
592 * @param p1 first peer
593 * @param p2 second peer
594 * @param co option to change
595 * @param ... option-specific values
596 * @return handle to the operation, NULL if configuring the link at this
597 * time is not allowed
599 struct GNUNET_TESTBED_Operation *
600 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
601 struct GNUNET_TESTBED_Peer *p1,
602 struct GNUNET_TESTBED_Peer *p2,
603 enum GNUNET_TESTBED_ConnectOption co, ...)
612 * Both peers must have been started before calling this function.
613 * This function then obtains a HELLO from 'p1', gives it to 'p2'
614 * and asks 'p2' to connect to 'p1'.
616 * @param op_cls closure argument to give with the operation event
617 * @param p1 first peer
618 * @param p2 second peer
619 * @return handle to the operation, NULL if connecting these two
620 * peers is fundamentally not possible at this time (peers
621 * not running or underlay disallows)
623 struct GNUNET_TESTBED_Operation *
624 GNUNET_TESTBED_overlay_connect (void *op_cls,
625 struct GNUNET_TESTBED_Peer *p1,
626 struct GNUNET_TESTBED_Peer *p2)
628 struct OperationContext *opc;
629 struct OverlayConnectData *data;
631 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
632 GNUNET_assert (p1->controller == p2->controller);
633 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
636 opc = GNUNET_malloc (sizeof (struct OperationContext));
638 opc->c = p1->controller;
639 opc->id = opc->c->operation_counter++;
640 opc->type = OP_OVERLAY_CONNECT;
641 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
642 &oprelease_overlay_connect);
643 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
650 /* end of testbed_api_peers.c */