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 data = GNUNET_malloc (sizeof (struct PeerCreateData));
105 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
106 op->controller = controller;
107 op->operation_id = controller->operation_counter++;
108 op->type = OP_PEER_CREATE;
110 config = GNUNET_CONFIGURATION_serialize (cfg, &c_size);
111 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
112 GNUNET_free (config);
113 msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
114 msg = GNUNET_realloc (xconfig, msize);
115 memmove (&msg[1], msg, xc_size); /* Move the compressed config */
116 msg->header.size = htons (msize);
117 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
118 msg->operation_id = GNUNET_htonll (op->operation_id);
119 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (peer->host));
120 msg->peer_id = htonl (peer->unique_id);
121 msg->config_size = htonl (c_size);
122 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
123 peer->controller->op_tail, op);
124 GNUNET_TESTBED_queue_message_ (controller,
125 (struct GNUNET_MessageHeader *) msg);
131 * Create the given peer at the specified host using the given
132 * controller. If the given controller is not running on the target
133 * host, it should find or create a controller at the target host and
134 * delegate creating the peer. Explicit delegation paths can be setup
135 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
136 * path exists, a direct link with a subordinate controller is setup
137 * for the first delegated peer to a particular host; the subordinate
138 * controller is then destroyed once the last peer that was delegated
139 * to the remote host is stopped.
141 * Creating the peer only creates the handle to manipulate and further
142 * configure the peer; use "GNUNET_TESTBED_peer_start" and
143 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
146 * Note that the given configuration will be adjusted by the
147 * controller to avoid port/path conflicts with other peers.
148 * The "final" configuration can be obtained using
149 * 'GNUNET_TESTBED_peer_get_information'.
151 * @param controller controller process to use
152 * @param host host to run the peer on
153 * @param cfg configuration to use for the peer
154 * @param cb the callback to call when the peer has been created
155 * @param cls the closure to the above callback
156 * @return the operation handle
158 struct GNUNET_TESTBED_Operation *
159 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
160 struct GNUNET_TESTBED_Host *host,
161 const struct GNUNET_CONFIGURATION_Handle *cfg,
162 GNUNET_TESTBED_PeerCreateCallback cb,
165 static uint32_t id_gen;
167 return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
176 * Start the given peer.
178 * @param peer peer to start
179 * @return handle to the operation
181 struct GNUNET_TESTBED_Operation *
182 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
184 struct GNUNET_TESTBED_Operation *op;
185 struct GNUNET_TESTBED_PeerStartMessage *msg;
187 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
188 op->operation_id = peer->controller->operation_counter++;
189 op->controller = peer->controller;
190 op->type = OP_PEER_START;
192 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
193 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
194 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
195 msg->peer_id = htonl (peer->unique_id);
196 msg->operation_id = GNUNET_htonll (op->operation_id);
197 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
198 peer->controller->op_tail, op);
199 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
205 * Stop the given peer. The handle remains valid (use
206 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
207 * state of the peer).
209 * @param peer peer to stop
210 * @return handle to the operation
212 struct GNUNET_TESTBED_Operation *
213 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
215 struct GNUNET_TESTBED_Operation *op;
216 struct GNUNET_TESTBED_PeerStopMessage *msg;
218 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
219 op->operation_id = peer->controller->operation_counter++;
220 op->controller = peer->controller;
221 op->type = OP_PEER_STOP;
223 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
224 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
225 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
226 msg->peer_id = htonl (peer->unique_id);
227 msg->operation_id = GNUNET_htonll (op->operation_id);
228 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
229 peer->controller->op_tail, op);
230 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
236 * Request information about a peer.
238 * @param peer peer to request information about
239 * @param pit desired information
240 * @return handle to the operation
242 struct GNUNET_TESTBED_Operation *
243 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
244 enum GNUNET_TESTBED_PeerInformationType pit)
246 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
247 struct GNUNET_TESTBED_Operation *op;
248 struct PeerInfoData *data;
250 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
251 data = GNUNET_malloc (sizeof (struct PeerInfoData));
254 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
255 op->type = OP_PEER_INFO;
256 op->operation_id = peer->controller->operation_counter++;
257 op->controller = peer->controller;
259 msg = GNUNET_malloc (sizeof (struct
260 GNUNET_TESTBED_PeerGetConfigurationMessage));
261 msg->header.size = htons
262 (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
263 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
264 msg->peer_id = htonl (peer->unique_id);
265 msg->operation_id = GNUNET_htonll (op->operation_id);
266 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
267 peer->controller->op_tail, op);
268 GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
274 * Change peer configuration. Must only be called while the
275 * peer is stopped. Ports and paths cannot be changed this
278 * @param peer peer to change configuration for
279 * @param cfg new configuration (differences to existing
280 * configuration only)
281 * @return handle to the operation
283 struct GNUNET_TESTBED_Operation *
284 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
285 const struct GNUNET_CONFIGURATION_Handle *cfg)
287 // FIXME: handle locally or delegate...
294 * Destroy the given peer; the peer should have been
295 * stopped first (if it was started).
297 * @param peer peer to stop
298 * @return handle to the operation
300 struct GNUNET_TESTBED_Operation *
301 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
303 struct GNUNET_TESTBED_Operation *op;
304 struct PeerDestroyData *data;
305 struct GNUNET_TESTBED_PeerDestroyMessage *msg;
307 data = GNUNET_malloc (sizeof (struct PeerDestroyData));
309 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
310 op->operation_id = peer->controller->operation_counter++;
311 op->controller = peer->controller;
312 op->type = OP_PEER_DESTROY;
314 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
315 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
316 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
317 msg->peer_id = htonl (peer->unique_id);
318 msg->operation_id = GNUNET_htonll (op->operation_id);
319 GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
320 peer->controller->op_tail, op);
321 GNUNET_TESTBED_queue_message_ (peer->controller,
322 (struct GNUNET_MessageHeader *) msg);
328 * Manipulate the P2P underlay topology by configuring a link
331 * @param op_cls closure argument to give with the operation event
332 * @param p1 first peer
333 * @param p2 second peer
334 * @param co option to change
335 * @param ... option-specific values
336 * @return handle to the operation, NULL if configuring the link at this
337 * time is not allowed
339 struct GNUNET_TESTBED_Operation *
340 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
341 struct GNUNET_TESTBED_Peer *p1,
342 struct GNUNET_TESTBED_Peer *p2,
343 enum GNUNET_TESTBED_ConnectOption co, ...)
352 * Both peers must have been started before calling this function.
353 * This function then obtains a HELLO from 'p1', gives it to 'p2'
354 * and asks 'p2' to connect to 'p1'.
356 * @param op_cls closure argument to give with the operation event
357 * @param p1 first peer
358 * @param p2 second peer
359 * @return handle to the operation, NULL if connecting these two
360 * peers is fundamentally not possible at this time (peers
361 * not running or underlay disallows)
363 struct GNUNET_TESTBED_Operation *
364 GNUNET_TESTBED_overlay_connect (void *op_cls,
365 struct GNUNET_TESTBED_Peer *p1,
366 struct GNUNET_TESTBED_Peer *p2)
374 /* end of testbed_api_peers.c */