-helper service shutdown fixes; overlay connect message
[oweals/gnunet.git] / src / testbed / testbed_api_peers.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
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
26  */
27 #include "platform.h"
28 #include "testbed_api_peers.h"
29 #include "testbed_api.h"
30 #include "testbed.h"
31 #include "testbed_api_hosts.h"
32
33
34 /**
35  * Lookup a peer by ID.
36  * 
37  * @param id global peer ID assigned to the peer
38  * @return handle to the host, NULL on error
39  */
40 struct GNUNET_TESTBED_Peer *
41 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
42 {
43   GNUNET_break (0);
44   return NULL;
45 }
46
47
48 /**
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
59  * peer.
60  *
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
64  * processes.
65  *
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'.
70  *
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
78  */
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,
85                                      void *cls)
86 {
87   struct GNUNET_TESTBED_Peer *peer;
88   struct PeerCreateData *data;
89   struct GNUNET_TESTBED_Operation *op;
90   struct GNUNET_TESTBED_PeerCreateMessage *msg;
91   char *config;
92   char *xconfig;
93   size_t c_size;
94   size_t xc_size;
95   uint16_t msize;
96
97   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
98   peer->controller = controller;
99   peer->host = host;
100   peer->unique_id = unique_id;
101   peer->state = PS_INVALID;
102   data = GNUNET_malloc (sizeof (struct PeerCreateData));
103   data->cb = cb;
104   data->cls = cls;
105   data->peer = peer;
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;
110   op->data = data;
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);
127   return op;
128 }
129
130
131 /**
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.
141  *
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
145  * processes.
146  *
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'.
151  *
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
158  */
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,
164                             void *cls)
165 {
166   static uint32_t id_gen;
167
168   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
169                                               controller,
170                                               host,
171                                               cfg,
172                                               cb, cls);
173 }
174
175
176 /**
177  * Start the given peer.
178  *
179  * @param peer peer to start
180  * @return handle to the operation
181  */
182 struct GNUNET_TESTBED_Operation *
183 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
184 {
185   struct GNUNET_TESTBED_Operation *op;
186   struct GNUNET_TESTBED_PeerStartMessage *msg;
187
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;
193   op->data = peer;
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);
202   return op;
203 }
204
205
206 /**
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).
210  *
211  * @param peer peer to stop
212  * @return handle to the operation
213  */
214 struct GNUNET_TESTBED_Operation *
215 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
216 {
217   struct GNUNET_TESTBED_Operation *op;
218   struct GNUNET_TESTBED_PeerStopMessage *msg;
219
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;
225   op->data = peer;
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);
234   return op;
235 }
236
237
238 /**
239  * Request information about a peer.
240  *
241  * @param peer peer to request information about
242  * @param pit desired information
243  * @return handle to the operation
244  */
245 struct GNUNET_TESTBED_Operation *
246 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
247                                      enum GNUNET_TESTBED_PeerInformationType pit)
248 {
249   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
250   struct GNUNET_TESTBED_Operation *op;
251   struct PeerInfoData *data;
252   
253   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
254   data = GNUNET_malloc (sizeof (struct PeerInfoData));
255   data->peer = peer;
256   data->pit = pit;
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;
261   op->data = data;
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);
272   return op;
273 }
274
275
276 /**
277  * Change peer configuration.  Must only be called while the
278  * peer is stopped.  Ports and paths cannot be changed this
279  * way.
280  *
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
285  */
286 struct GNUNET_TESTBED_Operation *
287 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
288                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
289 {
290   // FIXME: handle locally or delegate...
291   GNUNET_break (0);
292   return NULL;
293 }
294
295
296 /**
297  * Destroy the given peer; the peer should have been
298  * stopped first (if it was started).
299  *
300  * @param peer peer to stop
301  * @return handle to the operation
302  */
303 struct GNUNET_TESTBED_Operation *
304 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
305 {
306   struct GNUNET_TESTBED_Operation *op;
307   struct PeerDestroyData *data;
308   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
309   
310   data = GNUNET_malloc (sizeof (struct PeerDestroyData));
311   data->peer = peer;
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;
316   op->data = data;
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);
326   return op;
327 }
328
329
330 /**
331  * Manipulate the P2P underlay topology by configuring a link
332  * between two peers.  
333  *
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
341  */
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, ...)
347 {
348   GNUNET_break (0);
349   return NULL;
350 }
351
352
353
354 /**
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'.
358  *
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)
365  */
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)
370 {
371   struct GNUNET_TESTBED_Operation *op;
372   struct OverlayConnectData *data;
373   struct GNUNET_TESTBED_OverlayConnectMessage *msg;
374   
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));
378   data->p1 = p1;
379   data->p2 = p2;  
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;
384   op->data = data;
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);
396   return NULL;
397 }
398
399
400
401 /* end of testbed_api_peers.c */