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,
116 (struct GNUNET_MessageHeader *) msg);
121 * Callback which will be called when peer_create type operation is released
123 * @param cls the closure from GNUNET_TESTBED_operation_create_()
126 oprelease_peer_destroy (void *cls)
128 struct OperationContext *opc = cls;
130 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
136 * Lookup a peer by ID.
138 * @param id global peer ID assigned to the peer
139 * @return handle to the host, NULL on error
141 struct GNUNET_TESTBED_Peer *
142 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
150 * Create the given peer at the specified host using the given
151 * controller. If the given controller is not running on the target
152 * host, it should find or create a controller at the target host and
153 * delegate creating the peer. Explicit delegation paths can be setup
154 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
155 * path exists, a direct link with a subordinate controller is setup
156 * for the first delegated peer to a particular host; the subordinate
157 * controller is then destroyed once the last peer that was delegated
158 * to the remote host is stopped. This function is used in particular
159 * if some other controller has already assigned a unique ID to the
162 * Creating the peer only creates the handle to manipulate and further
163 * configure the peer; use "GNUNET_TESTBED_peer_start" and
164 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
167 * Note that the given configuration will be adjusted by the
168 * controller to avoid port/path conflicts with other peers.
169 * The "final" configuration can be obtained using
170 * 'GNUNET_TESTBED_peer_get_information'.
172 * @param unique_id unique ID for this peer
173 * @param controller controller process to use
174 * @param host host to run the peer on
175 * @param cfg Template configuration to use for the peer. Should exist until
176 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
177 * @param cb the callback to call when the peer has been created
178 * @param cls the closure to the above callback
179 * @return the operation handle
181 struct GNUNET_TESTBED_Operation *
182 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
183 struct GNUNET_TESTBED_Controller *controller,
184 struct GNUNET_TESTBED_Host *host,
185 const struct GNUNET_CONFIGURATION_Handle *cfg,
186 GNUNET_TESTBED_PeerCreateCallback cb,
189 struct GNUNET_TESTBED_Peer *peer;
190 struct PeerCreateData *data;
191 struct OperationContext *opc;
193 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
194 peer->controller = controller;
196 peer->unique_id = unique_id;
197 peer->state = PS_INVALID;
198 data = GNUNET_malloc (sizeof (struct PeerCreateData));
204 opc = GNUNET_malloc (sizeof (struct OperationContext));
207 opc->id = controller->operation_counter++;
208 opc->type = OP_PEER_CREATE;
209 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
210 &oprelease_peer_create);
211 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_peer_create, opc->op);
217 * Create the given peer at the specified host using the given
218 * controller. If the given controller is not running on the target
219 * host, it should find or create a controller at the target host and
220 * delegate creating the peer. Explicit delegation paths can be setup
221 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
222 * path exists, a direct link with a subordinate controller is setup
223 * for the first delegated peer to a particular host; the subordinate
224 * controller is then destroyed once the last peer that was delegated
225 * to the remote host is stopped.
227 * Creating the peer only creates the handle to manipulate and further
228 * configure the peer; use "GNUNET_TESTBED_peer_start" and
229 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
232 * Note that the given configuration will be adjusted by the
233 * controller to avoid port/path conflicts with other peers.
234 * The "final" configuration can be obtained using
235 * 'GNUNET_TESTBED_peer_get_information'.
237 * @param controller controller process to use
238 * @param host host to run the peer on
239 * @param cfg Template configuration to use for the peer. Should exist until
240 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
241 * @param cb the callback to call when the peer has been created
242 * @param cls the closure to the above callback
243 * @return the operation handle
245 struct GNUNET_TESTBED_Operation *
246 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
247 struct GNUNET_TESTBED_Host *host,
248 const struct GNUNET_CONFIGURATION_Handle *cfg,
249 GNUNET_TESTBED_PeerCreateCallback cb,
252 static uint32_t id_gen;
254 return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
263 * Start the given peer.
265 * @param peer peer to start
266 * @return handle to the operation
268 struct GNUNET_TESTBED_Operation *
269 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
271 struct GNUNET_TESTBED_Operation *op;
272 struct GNUNET_TESTBED_PeerStartMessage *msg;
274 GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
275 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
276 op->operation_id = peer->controller->operation_counter++;
277 op->controller = peer->controller;
278 op->type = OP_PEER_START;
280 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
281 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
282 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
283 msg->peer_id = htonl (peer->unique_id);
284 msg->operation_id = GNUNET_htonll (op->operation_id);
285 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
286 peer->controller->op_tail, op);
287 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
293 * Stop the given peer. The handle remains valid (use
294 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
295 * state of the peer).
297 * @param peer peer to stop
298 * @return handle to the operation
300 struct GNUNET_TESTBED_Operation *
301 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
303 struct GNUNET_TESTBED_Operation *op;
304 struct GNUNET_TESTBED_PeerStopMessage *msg;
306 GNUNET_assert (PS_STARTED == peer->state);
307 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
308 op->operation_id = peer->controller->operation_counter++;
309 op->controller = peer->controller;
310 op->type = OP_PEER_STOP;
312 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
313 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
314 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
315 msg->peer_id = htonl (peer->unique_id);
316 msg->operation_id = GNUNET_htonll (op->operation_id);
317 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
318 peer->controller->op_tail, op);
319 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
325 * Request information about a peer.
327 * @param peer peer to request information about
328 * @param pit desired information
329 * @return handle to the operation
331 struct GNUNET_TESTBED_Operation *
332 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
333 enum GNUNET_TESTBED_PeerInformationType pit)
335 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
336 struct GNUNET_TESTBED_Operation *op;
337 struct PeerInfoData *data;
339 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
340 data = GNUNET_malloc (sizeof (struct PeerInfoData));
343 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
344 op->type = OP_PEER_INFO;
345 op->operation_id = peer->controller->operation_counter++;
346 op->controller = peer->controller;
348 msg = GNUNET_malloc (sizeof (struct
349 GNUNET_TESTBED_PeerGetConfigurationMessage));
350 msg->header.size = htons
351 (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
352 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
353 msg->peer_id = htonl (peer->unique_id);
354 msg->operation_id = GNUNET_htonll (op->operation_id);
355 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
356 peer->controller->op_tail, op);
357 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
363 * Change peer configuration. Must only be called while the
364 * peer is stopped. Ports and paths cannot be changed this
367 * @param peer peer to change configuration for
368 * @param cfg new configuration (differences to existing
369 * configuration only)
370 * @return handle to the operation
372 struct GNUNET_TESTBED_Operation *
373 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
374 const struct GNUNET_CONFIGURATION_Handle *cfg)
376 // FIXME: handle locally or delegate...
383 * Destroy the given peer; the peer should have been
384 * stopped first (if it was started).
386 * @param peer peer to stop
387 * @return handle to the operation
389 struct GNUNET_TESTBED_Operation *
390 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
392 struct OperationContext *opc;
394 opc = GNUNET_malloc (sizeof (struct OperationContext));
396 opc->c = peer->controller;
397 opc->id = peer->controller->operation_counter++;
398 opc->type = OP_PEER_DESTROY;
399 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
400 &oprelease_peer_destroy);
401 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
408 * Manipulate the P2P underlay topology by configuring a link
411 * @param op_cls closure argument to give with the operation event
412 * @param p1 first peer
413 * @param p2 second peer
414 * @param co option to change
415 * @param ... option-specific values
416 * @return handle to the operation, NULL if configuring the link at this
417 * time is not allowed
419 struct GNUNET_TESTBED_Operation *
420 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
421 struct GNUNET_TESTBED_Peer *p1,
422 struct GNUNET_TESTBED_Peer *p2,
423 enum GNUNET_TESTBED_ConnectOption co, ...)
432 * Both peers must have been started before calling this function.
433 * This function then obtains a HELLO from 'p1', gives it to 'p2'
434 * and asks 'p2' to connect to 'p1'.
436 * @param op_cls closure argument to give with the operation event
437 * @param p1 first peer
438 * @param p2 second peer
439 * @return handle to the operation, NULL if connecting these two
440 * peers is fundamentally not possible at this time (peers
441 * not running or underlay disallows)
443 struct GNUNET_TESTBED_Operation *
444 GNUNET_TESTBED_overlay_connect (void *op_cls,
445 struct GNUNET_TESTBED_Peer *p1,
446 struct GNUNET_TESTBED_Peer *p2)
448 struct GNUNET_TESTBED_Operation *op;
449 struct OverlayConnectData *data;
450 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
452 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
453 GNUNET_assert (p1->controller == p2->controller);
454 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
457 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
458 op->controller = p1->controller;
459 op->operation_id = op->controller->operation_counter++;
460 op->type = OP_OVERLAY_CONNECT;
462 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
463 msg->header.size = htons (sizeof (struct
464 GNUNET_TESTBED_OverlayConnectMessage));
465 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
466 msg->peer1 = htonl (p1->unique_id);
467 msg->peer2 = htonl (p2->unique_id);
468 msg->operation_id = GNUNET_htonll (op->operation_id);
469 GNUNET_CONTAINER_DLL_insert_tail (op->controller->op_head,
470 op->controller->op_tail, op);
471 GNUNET_TESTBED_queue_message_ (op->controller,
472 (struct GNUNET_MessageHeader *) msg);
478 /* end of testbed_api_peers.c */