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 * Lookup a peer by ID.
218 * @param id global peer ID assigned to the peer
219 * @return handle to the host, NULL on error
221 struct GNUNET_TESTBED_Peer *
222 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
230 * Create the given peer at the specified host using the given
231 * controller. If the given controller is not running on the target
232 * host, it should find or create a controller at the target host and
233 * delegate creating the peer. Explicit delegation paths can be setup
234 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
235 * path exists, a direct link with a subordinate controller is setup
236 * for the first delegated peer to a particular host; the subordinate
237 * controller is then destroyed once the last peer that was delegated
238 * to the remote host is stopped. This function is used in particular
239 * if some other controller has already assigned a unique ID to the
242 * Creating the peer only creates the handle to manipulate and further
243 * configure the peer; use "GNUNET_TESTBED_peer_start" and
244 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
247 * Note that the given configuration will be adjusted by the
248 * controller to avoid port/path conflicts with other peers.
249 * The "final" configuration can be obtained using
250 * 'GNUNET_TESTBED_peer_get_information'.
252 * @param unique_id unique ID for this peer
253 * @param controller controller process to use
254 * @param host host to run the peer on
255 * @param cfg Template configuration to use for the peer. Should exist until
256 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
257 * @param cb the callback to call when the peer has been created
258 * @param cls the closure to the above callback
259 * @return the operation handle
261 struct GNUNET_TESTBED_Operation *
262 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
263 struct GNUNET_TESTBED_Controller *controller,
264 struct GNUNET_TESTBED_Host *host,
265 const struct GNUNET_CONFIGURATION_Handle *cfg,
266 GNUNET_TESTBED_PeerCreateCallback cb,
269 struct GNUNET_TESTBED_Peer *peer;
270 struct PeerCreateData *data;
271 struct OperationContext *opc;
273 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
274 peer->controller = controller;
276 peer->unique_id = unique_id;
277 peer->state = PS_INVALID;
278 data = GNUNET_malloc (sizeof (struct PeerCreateData));
284 opc = GNUNET_malloc (sizeof (struct OperationContext));
287 opc->id = controller->operation_counter++;
288 opc->type = OP_PEER_CREATE;
289 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
290 &oprelease_peer_create);
291 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_peer_create, opc->op);
297 * Create the given peer at the specified host using the given
298 * controller. If the given controller is not running on the target
299 * host, it should find or create a controller at the target host and
300 * delegate creating the peer. Explicit delegation paths can be setup
301 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
302 * path exists, a direct link with a subordinate controller is setup
303 * for the first delegated peer to a particular host; the subordinate
304 * controller is then destroyed once the last peer that was delegated
305 * to the remote host is stopped.
307 * Creating the peer only creates the handle to manipulate and further
308 * configure the peer; use "GNUNET_TESTBED_peer_start" and
309 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
312 * Note that the given configuration will be adjusted by the
313 * controller to avoid port/path conflicts with other peers.
314 * The "final" configuration can be obtained using
315 * 'GNUNET_TESTBED_peer_get_information'.
317 * @param controller controller process to use
318 * @param host host to run the peer on
319 * @param cfg Template configuration to use for the peer. Should exist until
320 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
321 * @param cb the callback to call when the peer has been created
322 * @param cls the closure to the above callback
323 * @return the operation handle
325 struct GNUNET_TESTBED_Operation *
326 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
327 struct GNUNET_TESTBED_Host *host,
328 const struct GNUNET_CONFIGURATION_Handle *cfg,
329 GNUNET_TESTBED_PeerCreateCallback cb,
332 static uint32_t id_gen;
334 return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
343 * Start the given peer.
345 * @param peer peer to start
346 * @return handle to the operation
348 struct GNUNET_TESTBED_Operation *
349 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
351 struct OperationContext *opc;
353 opc = GNUNET_malloc (sizeof (struct OperationContext));
354 opc->c = peer->controller;
356 opc->id = opc->c->operation_counter++;
357 opc->type = OP_PEER_START;
358 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
359 &oprelease_peer_start);
360 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
366 * Stop the given peer. The handle remains valid (use
367 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
368 * state of the peer).
370 * @param peer peer to stop
371 * @return handle to the operation
373 struct GNUNET_TESTBED_Operation *
374 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
376 struct OperationContext *opc;
378 opc = GNUNET_malloc (sizeof (struct OperationContext));
379 opc->c = peer->controller;
381 opc->id = opc->c->operation_counter++;
382 opc->type = OP_PEER_STOP;
383 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
384 &oprelease_peer_stop);
385 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
391 * Request information about a peer.
393 * @param peer peer to request information about
394 * @param pit desired information
395 * @return handle to the operation
397 struct GNUNET_TESTBED_Operation *
398 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
399 enum GNUNET_TESTBED_PeerInformationType pit)
401 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
402 struct GNUNET_TESTBED_Operation *op;
403 struct PeerInfoData *data;
405 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
406 data = GNUNET_malloc (sizeof (struct PeerInfoData));
409 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
410 op->type = OP_PEER_INFO;
411 op->operation_id = peer->controller->operation_counter++;
412 op->controller = peer->controller;
414 msg = GNUNET_malloc (sizeof (struct
415 GNUNET_TESTBED_PeerGetConfigurationMessage));
416 msg->header.size = htons
417 (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
418 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
419 msg->peer_id = htonl (peer->unique_id);
420 msg->operation_id = GNUNET_htonll (op->operation_id);
421 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
422 peer->controller->op_tail, op);
423 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
429 * Change peer configuration. Must only be called while the
430 * peer is stopped. Ports and paths cannot be changed this
433 * @param peer peer to change configuration for
434 * @param cfg new configuration (differences to existing
435 * configuration only)
436 * @return handle to the operation
438 struct GNUNET_TESTBED_Operation *
439 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
440 const struct GNUNET_CONFIGURATION_Handle *cfg)
442 // FIXME: handle locally or delegate...
449 * Destroy the given peer; the peer should have been
450 * stopped first (if it was started).
452 * @param peer peer to stop
453 * @return handle to the operation
455 struct GNUNET_TESTBED_Operation *
456 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
458 struct OperationContext *opc;
460 opc = GNUNET_malloc (sizeof (struct OperationContext));
462 opc->c = peer->controller;
463 opc->id = peer->controller->operation_counter++;
464 opc->type = OP_PEER_DESTROY;
465 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
466 &oprelease_peer_destroy);
467 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
474 * Manipulate the P2P underlay topology by configuring a link
477 * @param op_cls closure argument to give with the operation event
478 * @param p1 first peer
479 * @param p2 second peer
480 * @param co option to change
481 * @param ... option-specific values
482 * @return handle to the operation, NULL if configuring the link at this
483 * time is not allowed
485 struct GNUNET_TESTBED_Operation *
486 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
487 struct GNUNET_TESTBED_Peer *p1,
488 struct GNUNET_TESTBED_Peer *p2,
489 enum GNUNET_TESTBED_ConnectOption co, ...)
498 * Both peers must have been started before calling this function.
499 * This function then obtains a HELLO from 'p1', gives it to 'p2'
500 * and asks 'p2' to connect to 'p1'.
502 * @param op_cls closure argument to give with the operation event
503 * @param p1 first peer
504 * @param p2 second peer
505 * @return handle to the operation, NULL if connecting these two
506 * peers is fundamentally not possible at this time (peers
507 * not running or underlay disallows)
509 struct GNUNET_TESTBED_Operation *
510 GNUNET_TESTBED_overlay_connect (void *op_cls,
511 struct GNUNET_TESTBED_Peer *p1,
512 struct GNUNET_TESTBED_Peer *p2)
514 struct GNUNET_TESTBED_Operation *op;
515 struct OverlayConnectData *data;
516 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
518 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
519 GNUNET_assert (p1->controller == p2->controller);
520 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
523 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
524 op->controller = p1->controller;
525 op->operation_id = op->controller->operation_counter++;
526 op->type = OP_OVERLAY_CONNECT;
528 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
529 msg->header.size = htons (sizeof (struct
530 GNUNET_TESTBED_OverlayConnectMessage));
531 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
532 msg->peer1 = htonl (p1->unique_id);
533 msg->peer2 = htonl (p2->unique_id);
534 msg->operation_id = GNUNET_htonll (op->operation_id);
535 GNUNET_CONTAINER_DLL_insert_tail (op->controller->op_head,
536 op->controller->op_tail, op);
537 GNUNET_TESTBED_queue_message_ (op->controller,
538 (struct GNUNET_MessageHeader *) msg);
544 /* end of testbed_api_peers.c */