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
28 #include "testbed_api_peers.h"
29 #include "testbed_api.h"
31 #include "testbed_api_hosts.h"
32 #include "testbed_api_operations.h"
35 * Function to call to start a peer_create type operation once all
36 * queues the operation is part of declare that the
37 * operation can be activated.
39 * @param cls the closure from GNUNET_TESTBED_operation_create_()
42 opstart_peer_create (void *cls)
44 struct OperationContext *opc = cls;
45 struct PeerCreateData *data;
46 struct GNUNET_TESTBED_PeerCreateMessage *msg;
53 GNUNET_assert (OP_PEER_CREATE == opc->type);
55 GNUNET_assert (NULL != data);
56 GNUNET_assert (NULL != data->peer);
57 config = GNUNET_CONFIGURATION_serialize (data->cfg, &c_size);
58 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
60 msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
61 msg = GNUNET_realloc (xconfig, msize);
62 memmove (&msg[1], msg, xc_size);
63 msg->header.size = htons (msize);
64 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
65 msg->operation_id = GNUNET_htonll (opc->id);
66 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host));
67 msg->peer_id = htonl (data->peer->unique_id);
68 msg->config_size = htonl (c_size);
69 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
70 opc->c->ocq_tail, opc);
71 GNUNET_TESTBED_queue_message_ (opc->c,
72 (struct GNUNET_MessageHeader *) msg);
77 * Callback which will be called when peer_create type operation is released
79 * @param cls the closure from GNUNET_TESTBED_operation_create_()
82 oprelease_peer_create (void *cls)
84 struct OperationContext *opc = cls;
86 GNUNET_assert (NULL != opc->data);
87 GNUNET_free (opc->data);
88 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
94 * Function to called when a peer destroy operation is ready
96 * @param cls the closure from GNUNET_TESTBED_operation_create_()
99 opstart_peer_destroy (void *cls)
101 struct OperationContext *opc = cls;
102 struct GNUNET_TESTBED_Peer *peer;
103 struct GNUNET_TESTBED_PeerDestroyMessage *msg;
105 GNUNET_assert (OP_PEER_DESTROY == opc->type);
107 GNUNET_assert (NULL != peer);
108 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
109 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
110 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
111 msg->peer_id = htonl (peer->unique_id);
112 msg->operation_id = GNUNET_htonll (opc->id);
113 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
114 opc->c->ocq_tail, opc);
115 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
120 * Callback which will be called when peer_create type operation is released
122 * @param cls the closure from GNUNET_TESTBED_operation_create_()
125 oprelease_peer_destroy (void *cls)
127 struct OperationContext *opc = cls;
129 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
135 * Function to called when a peer start operation is ready
137 * @param cls the closure from GNUNET_TESTBED_operation_create_()
140 opstart_peer_start (void *cls)
142 struct OperationContext *opc = cls;
143 struct GNUNET_TESTBED_PeerStartMessage *msg;
144 struct GNUNET_TESTBED_Peer *peer;
146 GNUNET_assert (OP_PEER_START == opc->type);
147 GNUNET_assert (NULL != opc->data);
149 GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
150 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
151 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
152 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
153 msg->peer_id = htonl (peer->unique_id);
154 msg->operation_id = GNUNET_htonll (opc->id);
155 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
156 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
161 * Callback which will be called when peer start type operation is released
163 * @param cls the closure from GNUNET_TESTBED_operation_create_()
166 oprelease_peer_start (void *cls)
168 struct OperationContext *opc = cls;
170 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
176 * Function to called when a peer stop operation is ready
178 * @param cls the closure from GNUNET_TESTBED_operation_create_()
181 opstart_peer_stop (void *cls)
183 struct OperationContext *opc = cls;
184 struct GNUNET_TESTBED_PeerStopMessage *msg;
185 struct GNUNET_TESTBED_Peer *peer;
187 GNUNET_assert (NULL != opc->data);
189 GNUNET_assert (PS_STARTED == peer->state);
190 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
191 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
192 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
193 msg->peer_id = htonl (peer->unique_id);
194 msg->operation_id = GNUNET_htonll (opc->id);
195 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
196 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
201 * Callback which will be called when peer stop type operation is released
203 * @param cls the closure from GNUNET_TESTBED_operation_create_()
206 oprelease_peer_stop (void *cls)
208 struct OperationContext *opc = cls;
210 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
216 * Function to called when a peer get information operation is ready
218 * @param cls the closure from GNUNET_TESTBED_operation_create_()
221 opstart_peer_getinfo (void *cls)
223 struct OperationContext *opc = cls;
224 struct PeerInfoData *data;
225 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
228 GNUNET_assert (NULL != data);
229 msg = GNUNET_malloc (sizeof (struct
230 GNUNET_TESTBED_PeerGetConfigurationMessage));
231 msg->header.size = htons
232 (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
233 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
234 msg->peer_id = htonl (data->peer->unique_id);
235 msg->operation_id = GNUNET_htonll (opc->id);
236 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
237 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
242 * Callback which will be called when peer stop type operation is released
244 * @param cls the closure from GNUNET_TESTBED_operation_create_()
247 oprelease_peer_getinfo (void *cls)
249 struct OperationContext *opc = cls;
250 struct PeerInfoData2 *data;
252 if (GNUNET_YES != opc->completed)
253 GNUNET_free_non_null (opc->data);
257 GNUNET_assert (NULL != data);
260 case GNUNET_TESTBED_PIT_CONFIGURATION:
261 GNUNET_CONFIGURATION_destroy (data->details.cfg);
263 case GNUNET_TESTBED_PIT_IDENTITY:
264 GNUNET_free (data->details.peer_identity);
267 GNUNET_assert (0); /* We should never reach here */
271 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
278 * Lookup a peer by ID.
280 * @param id global peer ID assigned to the peer
281 * @return handle to the host, NULL on error
283 struct GNUNET_TESTBED_Peer *
284 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
292 * Create the given peer at the specified host using the given
293 * controller. If the given controller is not running on the target
294 * host, it should find or create a controller at the target host and
295 * delegate creating the peer. Explicit delegation paths can be setup
296 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
297 * path exists, a direct link with a subordinate controller is setup
298 * for the first delegated peer to a particular host; the subordinate
299 * controller is then destroyed once the last peer that was delegated
300 * to the remote host is stopped. This function is used in particular
301 * if some other controller has already assigned a unique ID to the
304 * Creating the peer only creates the handle to manipulate and further
305 * configure the peer; use "GNUNET_TESTBED_peer_start" and
306 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
309 * Note that the given configuration will be adjusted by the
310 * controller to avoid port/path conflicts with other peers.
311 * The "final" configuration can be obtained using
312 * 'GNUNET_TESTBED_peer_get_information'.
314 * @param unique_id unique ID for this peer
315 * @param controller controller process to use
316 * @param host host to run the peer on
317 * @param cfg Template configuration to use for the peer. Should exist until
318 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
319 * @param cb the callback to call when the peer has been created
320 * @param cls the closure to the above callback
321 * @return the operation handle
323 struct GNUNET_TESTBED_Operation *
324 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
325 struct GNUNET_TESTBED_Controller *controller,
326 struct GNUNET_TESTBED_Host *host,
327 const struct GNUNET_CONFIGURATION_Handle *cfg,
328 GNUNET_TESTBED_PeerCreateCallback cb,
331 struct GNUNET_TESTBED_Peer *peer;
332 struct PeerCreateData *data;
333 struct OperationContext *opc;
335 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
336 peer->controller = controller;
338 peer->unique_id = unique_id;
339 peer->state = PS_INVALID;
340 data = GNUNET_malloc (sizeof (struct PeerCreateData));
346 opc = GNUNET_malloc (sizeof (struct OperationContext));
349 opc->id = controller->operation_counter++;
350 opc->type = OP_PEER_CREATE;
351 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
352 &oprelease_peer_create);
353 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_peer_create, opc->op);
359 * Create the given peer at the specified host using the given
360 * controller. If the given controller is not running on the target
361 * host, it should find or create a controller at the target host and
362 * delegate creating the peer. Explicit delegation paths can be setup
363 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
364 * path exists, a direct link with a subordinate controller is setup
365 * for the first delegated peer to a particular host; the subordinate
366 * controller is then destroyed once the last peer that was delegated
367 * to the remote host is stopped.
369 * Creating the peer only creates the handle to manipulate and further
370 * configure the peer; use "GNUNET_TESTBED_peer_start" and
371 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
374 * Note that the given configuration will be adjusted by the
375 * controller to avoid port/path conflicts with other peers.
376 * The "final" configuration can be obtained using
377 * 'GNUNET_TESTBED_peer_get_information'.
379 * @param controller controller process to use
380 * @param host host to run the peer on
381 * @param cfg Template configuration to use for the peer. Should exist until
382 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
383 * @param cb the callback to call when the peer has been created
384 * @param cls the closure to the above callback
385 * @return the operation handle
387 struct GNUNET_TESTBED_Operation *
388 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
389 struct GNUNET_TESTBED_Host *host,
390 const struct GNUNET_CONFIGURATION_Handle *cfg,
391 GNUNET_TESTBED_PeerCreateCallback cb,
394 static uint32_t id_gen;
396 return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
405 * Start the given peer.
407 * @param peer peer to start
408 * @return handle to the operation
410 struct GNUNET_TESTBED_Operation *
411 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
413 struct OperationContext *opc;
415 opc = GNUNET_malloc (sizeof (struct OperationContext));
416 opc->c = peer->controller;
418 opc->id = opc->c->operation_counter++;
419 opc->type = OP_PEER_START;
420 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
421 &oprelease_peer_start);
422 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
428 * Stop the given peer. The handle remains valid (use
429 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
430 * state of the peer).
432 * @param peer peer to stop
433 * @return handle to the operation
435 struct GNUNET_TESTBED_Operation *
436 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
438 struct OperationContext *opc;
440 opc = GNUNET_malloc (sizeof (struct OperationContext));
441 opc->c = peer->controller;
443 opc->id = opc->c->operation_counter++;
444 opc->type = OP_PEER_STOP;
445 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
446 &oprelease_peer_stop);
447 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
453 * Request information about a peer.
455 * @param peer peer to request information about
456 * @param pit desired information
457 * @return handle to the operation
459 struct GNUNET_TESTBED_Operation *
460 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
461 enum GNUNET_TESTBED_PeerInformationType pit)
463 struct OperationContext *opc;
464 struct PeerInfoData *data;
466 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
467 data = GNUNET_malloc (sizeof (struct PeerInfoData));
470 opc = GNUNET_malloc (sizeof (struct OperationContext));
471 opc->c = peer->controller;
473 opc->type = OP_PEER_INFO;
474 opc->id = opc->c->operation_counter++;
475 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
476 &oprelease_peer_getinfo);
477 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
483 * Change peer configuration. Must only be called while the
484 * peer is stopped. Ports and paths cannot be changed this
487 * @param peer peer to change configuration for
488 * @param cfg new configuration (differences to existing
489 * configuration only)
490 * @return handle to the operation
492 struct GNUNET_TESTBED_Operation *
493 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
494 const struct GNUNET_CONFIGURATION_Handle *cfg)
496 // FIXME: handle locally or delegate...
503 * Destroy the given peer; the peer should have been
504 * stopped first (if it was started).
506 * @param peer peer to stop
507 * @return handle to the operation
509 struct GNUNET_TESTBED_Operation *
510 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
512 struct OperationContext *opc;
514 opc = GNUNET_malloc (sizeof (struct OperationContext));
516 opc->c = peer->controller;
517 opc->id = peer->controller->operation_counter++;
518 opc->type = OP_PEER_DESTROY;
519 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
520 &oprelease_peer_destroy);
521 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
528 * Manipulate the P2P underlay topology by configuring a link
531 * @param op_cls closure argument to give with the operation event
532 * @param p1 first peer
533 * @param p2 second peer
534 * @param co option to change
535 * @param ... option-specific values
536 * @return handle to the operation, NULL if configuring the link at this
537 * time is not allowed
539 struct GNUNET_TESTBED_Operation *
540 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
541 struct GNUNET_TESTBED_Peer *p1,
542 struct GNUNET_TESTBED_Peer *p2,
543 enum GNUNET_TESTBED_ConnectOption co, ...)
552 * Both peers must have been started before calling this function.
553 * This function then obtains a HELLO from 'p1', gives it to 'p2'
554 * and asks 'p2' to connect to 'p1'.
556 * @param op_cls closure argument to give with the operation event
557 * @param p1 first peer
558 * @param p2 second peer
559 * @return handle to the operation, NULL if connecting these two
560 * peers is fundamentally not possible at this time (peers
561 * not running or underlay disallows)
563 struct GNUNET_TESTBED_Operation *
564 GNUNET_TESTBED_overlay_connect (void *op_cls,
565 struct GNUNET_TESTBED_Peer *p1,
566 struct GNUNET_TESTBED_Peer *p2)
568 struct GNUNET_TESTBED_Operation *op;
569 struct OverlayConnectData *data;
570 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
572 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
573 GNUNET_assert (p1->controller == p2->controller);
574 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
577 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
578 op->controller = p1->controller;
579 op->operation_id = op->controller->operation_counter++;
580 op->type = OP_OVERLAY_CONNECT;
582 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
583 msg->header.size = htons (sizeof (struct
584 GNUNET_TESTBED_OverlayConnectMessage));
585 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
586 msg->peer1 = htonl (p1->unique_id);
587 msg->peer2 = htonl (p2->unique_id);
588 msg->operation_id = GNUNET_htonll (op->operation_id);
589 GNUNET_CONTAINER_DLL_insert_tail (op->controller->op_head,
590 op->controller->op_tail, op);
591 GNUNET_TESTBED_queue_message_ (op->controller,
592 (struct GNUNET_MessageHeader *) msg);
598 /* end of testbed_api_peers.c */