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"
35 * Lookup a peer by ID.
37 * @param id global peer ID assigned to the peer
38 * @return handle to the host, NULL on error
40 struct GNUNET_TESTBED_Peer *
41 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
49 * Create the given peer at the specified host using the given
50 * controller. If the given controller is not running on the target
51 * host, it should find or create a controller at the target host and
52 * delegate creating the peer. Explicit delegation paths can be setup
53 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
54 * path exists, a direct link with a subordinate controller is setup
55 * for the first delegated peer to a particular host; the subordinate
56 * controller is then destroyed once the last peer that was delegated
57 * to the remote host is stopped. This function is used in particular
58 * if some other controller has already assigned a unique ID to the
61 * Creating the peer only creates the handle to manipulate and further
62 * configure the peer; use "GNUNET_TESTBED_peer_start" and
63 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
66 * Note that the given configuration will be adjusted by the
67 * controller to avoid port/path conflicts with other peers.
68 * The "final" configuration can be obtained using
69 * 'GNUNET_TESTBED_peer_get_information'.
71 * @param unique_id unique ID for this peer
72 * @param controller controller process to use
73 * @param host host to run the peer on
74 * @param cfg configuration to use for the peer
75 * @param cb the callback to call when the peer has been created
76 * @param cls the closure to the above callback
77 * @return the operation handle
79 struct GNUNET_TESTBED_Operation *
80 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
81 struct GNUNET_TESTBED_Controller *controller,
82 struct GNUNET_TESTBED_Host *host,
83 const struct GNUNET_CONFIGURATION_Handle *cfg,
84 GNUNET_TESTBED_PeerCreateCallback cb,
87 struct GNUNET_TESTBED_Peer *peer;
88 struct PeerCreateData *data;
89 struct GNUNET_TESTBED_Operation *op;
90 struct GNUNET_TESTBED_PeerCreateMessage *msg;
97 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
98 peer->controller = controller;
100 peer->unique_id = unique_id;
101 peer->state = PS_INVALID;
102 data = GNUNET_malloc (sizeof (struct PeerCreateData));
106 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
107 op->controller = controller;
108 op->operation_id = controller->operation_counter++;
109 op->type = OP_PEER_CREATE;
111 config = GNUNET_CONFIGURATION_serialize (cfg, &c_size);
112 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
113 GNUNET_free (config);
114 msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
115 msg = GNUNET_realloc (xconfig, msize);
116 memmove (&msg[1], msg, xc_size); /* Move the compressed config */
117 msg->header.size = htons (msize);
118 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
119 msg->operation_id = GNUNET_htonll (op->operation_id);
120 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (peer->host));
121 msg->peer_id = htonl (peer->unique_id);
122 msg->config_size = htonl (c_size);
123 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
124 peer->controller->op_tail, op);
125 GNUNET_TESTBED_queue_message_ (controller,
126 (struct GNUNET_MessageHeader *) msg);
132 * Create the given peer at the specified host using the given
133 * controller. If the given controller is not running on the target
134 * host, it should find or create a controller at the target host and
135 * delegate creating the peer. Explicit delegation paths can be setup
136 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
137 * path exists, a direct link with a subordinate controller is setup
138 * for the first delegated peer to a particular host; the subordinate
139 * controller is then destroyed once the last peer that was delegated
140 * to the remote host is stopped.
142 * Creating the peer only creates the handle to manipulate and further
143 * configure the peer; use "GNUNET_TESTBED_peer_start" and
144 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
147 * Note that the given configuration will be adjusted by the
148 * controller to avoid port/path conflicts with other peers.
149 * The "final" configuration can be obtained using
150 * 'GNUNET_TESTBED_peer_get_information'.
152 * @param controller controller process to use
153 * @param host host to run the peer on
154 * @param cfg configuration to use for the peer
155 * @param cb the callback to call when the peer has been created
156 * @param cls the closure to the above callback
157 * @return the operation handle
159 struct GNUNET_TESTBED_Operation *
160 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
161 struct GNUNET_TESTBED_Host *host,
162 const struct GNUNET_CONFIGURATION_Handle *cfg,
163 GNUNET_TESTBED_PeerCreateCallback cb,
166 static uint32_t id_gen;
168 return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
177 * Start the given peer.
179 * @param peer peer to start
180 * @return handle to the operation
182 struct GNUNET_TESTBED_Operation *
183 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
185 struct GNUNET_TESTBED_Operation *op;
186 struct GNUNET_TESTBED_PeerStartMessage *msg;
188 GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
189 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
190 op->operation_id = peer->controller->operation_counter++;
191 op->controller = peer->controller;
192 op->type = OP_PEER_START;
194 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
195 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
196 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
197 msg->peer_id = htonl (peer->unique_id);
198 msg->operation_id = GNUNET_htonll (op->operation_id);
199 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
200 peer->controller->op_tail, op);
201 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
207 * Stop the given peer. The handle remains valid (use
208 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
209 * state of the peer).
211 * @param peer peer to stop
212 * @return handle to the operation
214 struct GNUNET_TESTBED_Operation *
215 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
217 struct GNUNET_TESTBED_Operation *op;
218 struct GNUNET_TESTBED_PeerStopMessage *msg;
220 GNUNET_assert (PS_STARTED == peer->state);
221 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
222 op->operation_id = peer->controller->operation_counter++;
223 op->controller = peer->controller;
224 op->type = OP_PEER_STOP;
226 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
227 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
228 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
229 msg->peer_id = htonl (peer->unique_id);
230 msg->operation_id = GNUNET_htonll (op->operation_id);
231 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
232 peer->controller->op_tail, op);
233 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
239 * Request information about a peer.
241 * @param peer peer to request information about
242 * @param pit desired information
243 * @return handle to the operation
245 struct GNUNET_TESTBED_Operation *
246 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
247 enum GNUNET_TESTBED_PeerInformationType pit)
249 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
250 struct GNUNET_TESTBED_Operation *op;
251 struct PeerInfoData *data;
253 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
254 data = GNUNET_malloc (sizeof (struct PeerInfoData));
257 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
258 op->type = OP_PEER_INFO;
259 op->operation_id = peer->controller->operation_counter++;
260 op->controller = peer->controller;
262 msg = GNUNET_malloc (sizeof (struct
263 GNUNET_TESTBED_PeerGetConfigurationMessage));
264 msg->header.size = htons
265 (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
266 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
267 msg->peer_id = htonl (peer->unique_id);
268 msg->operation_id = GNUNET_htonll (op->operation_id);
269 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
270 peer->controller->op_tail, op);
271 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
277 * Change peer configuration. Must only be called while the
278 * peer is stopped. Ports and paths cannot be changed this
281 * @param peer peer to change configuration for
282 * @param cfg new configuration (differences to existing
283 * configuration only)
284 * @return handle to the operation
286 struct GNUNET_TESTBED_Operation *
287 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
288 const struct GNUNET_CONFIGURATION_Handle *cfg)
290 // FIXME: handle locally or delegate...
297 * Destroy the given peer; the peer should have been
298 * stopped first (if it was started).
300 * @param peer peer to stop
301 * @return handle to the operation
303 struct GNUNET_TESTBED_Operation *
304 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
306 struct GNUNET_TESTBED_Operation *op;
307 struct PeerDestroyData *data;
308 struct GNUNET_TESTBED_PeerDestroyMessage *msg;
310 data = GNUNET_malloc (sizeof (struct PeerDestroyData));
312 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
313 op->operation_id = peer->controller->operation_counter++;
314 op->controller = peer->controller;
315 op->type = OP_PEER_DESTROY;
317 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
318 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
319 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
320 msg->peer_id = htonl (peer->unique_id);
321 msg->operation_id = GNUNET_htonll (op->operation_id);
322 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
323 peer->controller->op_tail, op);
324 GNUNET_TESTBED_queue_message_ (peer->controller,
325 (struct GNUNET_MessageHeader *) msg);
331 * Manipulate the P2P underlay topology by configuring a link
334 * @param op_cls closure argument to give with the operation event
335 * @param p1 first peer
336 * @param p2 second peer
337 * @param co option to change
338 * @param ... option-specific values
339 * @return handle to the operation, NULL if configuring the link at this
340 * time is not allowed
342 struct GNUNET_TESTBED_Operation *
343 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
344 struct GNUNET_TESTBED_Peer *p1,
345 struct GNUNET_TESTBED_Peer *p2,
346 enum GNUNET_TESTBED_ConnectOption co, ...)
355 * Both peers must have been started before calling this function.
356 * This function then obtains a HELLO from 'p1', gives it to 'p2'
357 * and asks 'p2' to connect to 'p1'.
359 * @param op_cls closure argument to give with the operation event
360 * @param p1 first peer
361 * @param p2 second peer
362 * @return handle to the operation, NULL if connecting these two
363 * peers is fundamentally not possible at this time (peers
364 * not running or underlay disallows)
366 struct GNUNET_TESTBED_Operation *
367 GNUNET_TESTBED_overlay_connect (void *op_cls,
368 struct GNUNET_TESTBED_Peer *p1,
369 struct GNUNET_TESTBED_Peer *p2)
371 struct GNUNET_TESTBED_Operation *op;
372 struct OverlayConnectData *data;
373 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
375 GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
376 GNUNET_assert (p1->controller == p2->controller);
377 data = GNUNET_malloc (sizeof (struct OverlayConnectData));
380 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
381 op->controller = p1->controller;
382 op->operation_id = op->controller->operation_counter++;
383 op->type = OP_OVERLAY_CONNECT;
385 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
386 msg->header.size = htons (sizeof (struct
387 GNUNET_TESTBED_OverlayConnectMessage));
388 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
389 msg->peer1 = htonl (p1->unique_id);
390 msg->peer2 = htonl (p2->unique_id);
391 msg->operation_id = GNUNET_htonll (op->operation_id);
392 GNUNET_CONTAINER_DLL_insert_tail (op->controller->op_head,
393 op->controller->op_tail, op);
394 GNUNET_TESTBED_queue_message_ (op->controller,
395 (struct GNUNET_MessageHeader *) msg);
401 /* end of testbed_api_peers.c */