65217ebdd0f36d0a015c9e3cab7dda4f65927327
[oweals/gnunet.git] / src / testbed / testbed_api_hosts.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_hosts.c
23  * @brief API for manipulating 'hosts' controlled by the GNUnet testing service;
24  *        allows parsing hosts files, starting, stopping and communicating (via
25  *        SSH/stdin/stdout) with the remote (or local) processes
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_transport_service.h"
33
34 #include "testbed_api.h"
35 #include "testbed_api_hosts.h"
36 #include "testbed_helper.h"
37 #include "testbed_api_operations.h"
38 #include "testbed_api_sd.h"
39
40 #include <zlib.h>
41
42 /**
43  * Generic logging shorthand
44  */
45 #define LOG(kind, ...)                          \
46   GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
47
48 /**
49  * Debug logging shorthand
50  */
51 #define LOG_DEBUG(...)                          \
52   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
53
54 /**
55  * Prints API violation message
56  */
57 #define API_VIOLATION(cond,errstr)              \
58   do {                                          \
59     if (cond)                                   \
60       break;                                    \
61     LOG (GNUNET_ERROR_TYPE_ERROR, "API violation detected: %s\n", errstr); \
62     GNUNET_assert (0);                                                  \
63   } while (0)
64
65 /**
66  * Number of extra elements we create space for when we grow host list
67  */
68 #define HOST_LIST_GROW_STEP 10
69
70
71 /**
72  * A list entry for registered controllers list
73  */
74 struct RegisteredController
75 {
76   /**
77    * The controller at which this host is registered
78    */
79   const struct GNUNET_TESTBED_Controller *controller;
80
81   /**
82    * The next ptr for DLL
83    */
84   struct RegisteredController *next;
85
86   /**
87    * The prev ptr for DLL
88    */
89   struct RegisteredController *prev;
90 };
91
92
93 /**
94  * A slot to record time taken by an overlay connect operation
95  */
96 struct TimeSlot
97 {
98   /**
99    * A key to identify this timeslot
100    */
101   void *key;
102
103   /**
104    * Time
105    */
106   struct GNUNET_TIME_Relative time;
107
108   /**
109    * Number of timing values accumulated
110    */
111   unsigned int nvals;
112 };
113
114
115 /**
116  * Opaque handle to a host running experiments managed by the testing framework.
117  * The master process must be able to SSH to this host without password (via
118  * ssh-agent).
119  */
120 struct GNUNET_TESTBED_Host
121 {
122
123   /**
124    * The next pointer for DLL
125    */
126   struct GNUNET_TESTBED_Host *next;
127
128   /**
129    * The prev pointer for DLL
130    */
131   struct GNUNET_TESTBED_Host *prev;
132
133   /**
134    * The hostname of the host; NULL for localhost
135    */
136   const char *hostname;
137
138   /**
139    * The username to be used for SSH login
140    */
141   const char *username;
142
143   /**
144    * the configuration to use as a template while starting a controller on this
145    * host.  Operation queue size specific to a host are also read from this
146    * configuration handle.  After starting the controller, it points to the actual
147    * configuration with which the controller is running
148    */
149   struct GNUNET_CONFIGURATION_Handle *cfg;
150
151   /**
152    * The head for the list of controllers where this host is registered
153    */
154   struct RegisteredController *rc_head;
155
156   /**
157    * The tail for the list of controllers where this host is registered
158    */
159   struct RegisteredController *rc_tail;
160
161   /**
162    * Operation queue for simultaneous overlay connect operations target at this
163    * host
164    */
165   struct OperationQueue *opq_parallel_overlay_connect_operations;
166
167   /**
168    * An array of timing slots; size should be equal to the current number of parallel
169    * overlay connects
170    */
171   struct TimeSlot *tslots;
172
173   /**
174    * Handle for SD calculations amount parallel overlay connect operation finish
175    * times
176    */
177   struct SDHandle *poc_sd;  
178
179   /**
180    * The number of parallel overlay connects we do currently
181    */
182   unsigned int num_parallel_connects;
183
184   /**
185    * Counter to indicate when all the available time slots are filled
186    */
187   unsigned int tslots_filled;
188
189   /**
190    * Is a controller started on this host? FIXME: Is this needed?
191    */
192   int controller_started;
193
194   /**
195    * Is this host locked by GNUNET_TESTBED_controller_start()?
196    */
197   int locked;
198
199   /**
200    * Global ID we use to refer to a host on the network
201    */
202   uint32_t id;
203
204   /**
205    * The port which is to be used for SSH
206    */
207   uint16_t port;
208
209 };
210
211
212 /**
213  * Array of available hosts
214  */
215 static struct GNUNET_TESTBED_Host **host_list;
216
217 /**
218  * The size of the available hosts list
219  */
220 static unsigned int host_list_size;
221
222
223 /**
224  * Lookup a host by ID.
225  *
226  * @param id global host ID assigned to the host; 0 is
227  *        reserved to always mean 'localhost'
228  * @return handle to the host, NULL if host not found
229  */
230 struct GNUNET_TESTBED_Host *
231 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
232 {
233   if (host_list_size <= id)
234     return NULL;
235   return host_list[id];
236 }
237
238
239 /**
240  * Create a host by ID; given this host handle, we could not
241  * run peers at the host, but we can talk about the host
242  * internally.
243  *
244  * @param id global host ID assigned to the host; 0 is
245  *        reserved to always mean 'localhost'
246  * @param cfg the configuration to use as a template while starting a controller
247  *          on this host.  Operation queue sizes specific to a host are also
248  *          read from this configuration handle
249  * @return handle to the host, NULL on error
250  */
251 struct GNUNET_TESTBED_Host *
252 GNUNET_TESTBED_host_create_by_id_ (uint32_t id,
253                                    const struct GNUNET_CONFIGURATION_Handle
254                                    *cfg)
255 {
256   return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, cfg, 0);
257 }
258
259
260 /**
261  * Obtain the host's unique global ID.
262  *
263  * @param host handle to the host, NULL means 'localhost'
264  * @return id global host ID assigned to the host (0 is
265  *         'localhost', but then obviously not globally unique)
266  */
267 uint32_t
268 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
269 {
270   return host->id;
271 }
272
273
274 /**
275  * Obtain the host's hostname.
276  *
277  * @param host handle to the host, NULL means 'localhost'
278  * @return hostname of the host
279  */
280 const char *
281 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
282 {
283   return host->hostname;
284 }
285
286
287 /**
288  * Obtain the host's username
289  *
290  * @param host handle to the host, NULL means 'localhost'
291  * @return username to login to the host
292  */
293 const char *
294 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
295 {
296   return host->username;
297 }
298
299
300 /**
301  * Obtain the host's ssh port
302  *
303  * @param host handle to the host, NULL means 'localhost'
304  * @return username to login to the host
305  */
306 uint16_t
307 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
308 {
309   return host->port;
310 }
311
312
313 /**
314  * Check whether a controller is already started on the given host
315  *
316  * @param host the handle to the host
317  * @return GNUNET_YES if the controller is already started; GNUNET_NO if not
318  */
319 int
320 GNUNET_TESTBED_host_controller_started (const struct GNUNET_TESTBED_Host *host)
321 {
322   return host->controller_started;
323 }
324
325
326 /**
327  * Obtain the host's configuration template
328  *
329  * @param host handle to the host
330  * @return the host's configuration template
331  */
332 const struct GNUNET_CONFIGURATION_Handle *
333 GNUNET_TESTBED_host_get_cfg_ (const struct GNUNET_TESTBED_Host *host)
334 {
335   return host->cfg;
336 }
337
338
339 /**
340  * Create a host to run peers and controllers on.
341  *
342  * @param id global host ID assigned to the host; 0 is
343  *        reserved to always mean 'localhost'
344  * @param hostname name of the host, use "NULL" for localhost
345  * @param username username to use for the login; may be NULL
346  * @param cfg the configuration to use as a template while starting a controller
347  *          on this host.  Operation queue sizes specific to a host are also
348  *          read from this configuration handle
349  * @param port port number to use for ssh; use 0 to let ssh decide
350  * @return handle to the host, NULL on error
351  */
352 struct GNUNET_TESTBED_Host *
353 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
354                                     const char *username, 
355                                     const struct GNUNET_CONFIGURATION_Handle
356                                     *cfg,
357                                     uint16_t port)
358 {
359   struct GNUNET_TESTBED_Host *host;
360   unsigned int new_size;
361
362   if ((id < host_list_size) && (NULL != host_list[id]))
363   {
364     LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
365     return NULL;
366   }
367   host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
368   host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
369   host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
370   host->id = id;
371   host->port = (0 == port) ? 22 : port;
372   host->cfg = GNUNET_CONFIGURATION_dup (cfg);
373   host->opq_parallel_overlay_connect_operations =
374       GNUNET_TESTBED_operation_queue_create_ (0);
375   GNUNET_TESTBED_set_num_parallel_overlay_connects_ (host, 1);
376   host->poc_sd = GNUNET_TESTBED_SD_init_ (10);
377   new_size = host_list_size;
378   while (id >= new_size)
379     new_size += HOST_LIST_GROW_STEP;
380   if (new_size != host_list_size)
381     GNUNET_array_grow (host_list, host_list_size, new_size);
382   GNUNET_assert (id < host_list_size);
383   LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
384   host_list[id] = host;
385   return host;
386 }
387
388
389 /**
390  * Create a host to run peers and controllers on.
391  *
392  * @param hostname name of the host, use "NULL" for localhost
393  * @param username username to use for the login; may be NULL
394  * @param cfg the configuration to use as a template while starting a controller
395  *          on this host.  Operation queue sizes specific to a host are also
396  *          read from this configuration handle
397  * @param port port number to use for ssh; use 0 to let ssh decide
398  * @return handle to the host, NULL on error
399  */
400 struct GNUNET_TESTBED_Host *
401 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
402                             const struct GNUNET_CONFIGURATION_Handle *cfg,
403                             uint16_t port)
404 {
405   static uint32_t uid_generator;
406
407   if (NULL == hostname)
408     return GNUNET_TESTBED_host_create_with_id (0, hostname, username, 
409                                                cfg, port);
410   return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
411                                              username, cfg, port);
412 }
413
414
415 /**
416  * Load a set of hosts from a configuration file.
417  *
418  * @param filename file with the host specification
419  * @param cfg the configuration to use as a template while starting a controller
420  *          on any of the loaded hosts.  Operation queue sizes specific to a host
421  *          are also read from this configuration handle
422  * @param hosts set to the hosts found in the file; caller must free this if
423  *          number of hosts returned is greater than 0
424  * @return number of hosts returned in 'hosts', 0 on error
425  */
426 unsigned int
427 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
428                                      const struct GNUNET_CONFIGURATION_Handle
429                                      *cfg,
430                                      struct GNUNET_TESTBED_Host ***hosts)
431 {
432   //struct GNUNET_TESTBED_Host **host_array;
433   struct GNUNET_TESTBED_Host *starting_host;
434   char *data;
435   char *buf;
436   char username[256];
437   char hostname[256];
438   uint64_t fs;
439   short int port;
440   int ret;
441   unsigned int offset;
442   unsigned int count;
443
444
445   GNUNET_assert (NULL != filename);
446   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
447   {
448     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
449     return 0;
450   }
451   if (GNUNET_OK !=
452       GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
453     fs = 0;
454   if (0 == fs)
455   {
456     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
457     return 0;
458   }
459   data = GNUNET_malloc (fs);
460   if (fs != GNUNET_DISK_fn_read (filename, data, fs))
461   {
462     GNUNET_free (data);
463     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
464          filename);
465     return 0;
466   }
467   buf = data;
468   offset = 0;
469   starting_host = NULL;
470   count = 0;
471   while (offset < (fs - 1))
472   {
473     offset++;
474     if (((data[offset] == '\n')) && (buf != &data[offset]))
475     {
476       data[offset] = '\0';
477       ret =
478           SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
479                   hostname, &port);
480       if (3 == ret)
481       {
482         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483                     "Successfully read host %s, port %d and user %s from file\n",
484                     hostname, port, username);
485         /* We store hosts in a static list; hence we only require the starting
486          * host pointer in that list to access the newly created list of hosts */
487         if (NULL == starting_host)
488           starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
489                                                       port);
490         else
491           (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
492         count++;
493       }
494       else
495         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
496                     "Error reading line `%s' in hostfile\n", buf);
497       buf = &data[offset + 1];
498     }
499     else if ((data[offset] == '\n') || (data[offset] == '\0'))
500       buf = &data[offset + 1];
501   }
502   GNUNET_free (data);
503   if (NULL == starting_host)
504     return 0;
505   *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
506   memcpy (*hosts, &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
507           sizeof (struct GNUNET_TESTBED_Host *) * count);
508   return count;
509 }
510
511
512 /**
513  * Destroy a host handle.  Must only be called once everything
514  * running on that host has been stopped.
515  *
516  * @param host handle to destroy
517  */
518 void
519 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
520 {
521   struct RegisteredController *rc;
522   uint32_t id;
523
524   GNUNET_assert (host->id < host_list_size);
525   GNUNET_assert (host_list[host->id] == host);
526   host_list[host->id] = NULL;
527   /* clear registered controllers list */
528   for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
529   {
530     GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
531     GNUNET_free (rc);
532   }
533   GNUNET_free_non_null ((char *) host->username);
534   GNUNET_free_non_null ((char *) host->hostname);
535   GNUNET_TESTBED_operation_queue_destroy_
536       (host->opq_parallel_overlay_connect_operations);
537   GNUNET_TESTBED_SD_destroy_ (host->poc_sd);
538   GNUNET_free_non_null (host->tslots);
539   GNUNET_CONFIGURATION_destroy (host->cfg);
540   GNUNET_free (host);
541   while (host_list_size >= HOST_LIST_GROW_STEP)
542   {
543     for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
544          id--)
545       if (NULL != host_list[id])
546         break;
547     if (id != host_list_size - HOST_LIST_GROW_STEP)
548       break;
549     if (NULL != host_list[id])
550       break;
551     host_list_size -= HOST_LIST_GROW_STEP;
552   }
553   host_list =
554       GNUNET_realloc (host_list,
555                       sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
556 }
557
558
559 /**
560  * Marks a host as registered with a controller
561  *
562  * @param host the host to mark
563  * @param controller the controller at which this host is registered
564  */
565 void
566 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
567                                          const struct GNUNET_TESTBED_Controller
568                                          *const controller)
569 {
570   struct RegisteredController *rc;
571
572   for (rc = host->rc_head; NULL != rc; rc = rc->next)
573   {
574     if (controller == rc->controller)   /* already registered at controller */
575     {
576       GNUNET_break (0);
577       return;
578     }
579   }
580   rc = GNUNET_malloc (sizeof (struct RegisteredController));
581   rc->controller = controller;
582   GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
583 }
584
585
586 /**
587  * Checks whether a host has been registered
588  *
589  * @param host the host to check
590  * @param controller the controller at which host's registration is checked
591  * @return GNUNET_YES if registered; GNUNET_NO if not
592  */
593 int
594 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
595                                     const struct GNUNET_TESTBED_Controller
596                                     *const controller)
597 {
598   struct RegisteredController *rc;
599
600   for (rc = host->rc_head; NULL != rc; rc = rc->next)
601   {
602     if (controller == rc->controller)   /* already registered at controller */
603     {
604       return GNUNET_YES;
605     }
606   }
607   return GNUNET_NO;
608 }
609
610
611 /**
612  * Handle for controller process
613  */
614 struct GNUNET_TESTBED_ControllerProc
615 {
616   /**
617    * The process handle
618    */
619   struct GNUNET_HELPER_Handle *helper;
620
621   /**
622    * The arguments used to start the helper
623    */
624   char **helper_argv;
625
626   /**
627    * The host where the helper is run
628    */
629   struct GNUNET_TESTBED_Host *host;
630
631   /**
632    * The controller error callback
633    */
634   GNUNET_TESTBED_ControllerStatusCallback cb;
635
636   /**
637    * The closure for the above callback
638    */
639   void *cls;
640
641   /**
642    * The send handle for the helper
643    */
644   struct GNUNET_HELPER_SendHandle *shandle;
645
646   /**
647    * The message corresponding to send handle
648    */
649   struct GNUNET_MessageHeader *msg;
650
651 };
652
653
654 /**
655  * Function to copy NULL terminated list of arguments
656  *
657  * @param argv the NULL terminated list of arguments. Cannot be NULL.
658  * @return the copied NULL terminated arguments
659  */
660 static char **
661 copy_argv (const char *const *argv)
662 {
663   char **argv_dup;
664   unsigned int argp;
665
666   GNUNET_assert (NULL != argv);
667   for (argp = 0; NULL != argv[argp]; argp++) ;
668   argv_dup = GNUNET_malloc (sizeof (char *) * (argp + 1));
669   for (argp = 0; NULL != argv[argp]; argp++)
670     argv_dup[argp] = strdup (argv[argp]);
671   return argv_dup;
672 }
673
674
675 /**
676  * Function to join NULL terminated list of arguments
677  *
678  * @param argv1 the NULL terminated list of arguments. Cannot be NULL.
679  * @param argv2 the NULL terminated list of arguments. Cannot be NULL.
680  * @return the joined NULL terminated arguments
681  */
682 static char **
683 join_argv (const char *const *argv1, const char *const *argv2)
684 {
685   char **argvj;
686   char *argv;
687   unsigned int carg;
688   unsigned int cnt;
689
690   carg = 0;
691   argvj = NULL;
692   for (cnt = 0; NULL != argv1[cnt]; cnt++)
693   {
694     argv = GNUNET_strdup (argv1[cnt]);
695     GNUNET_array_append (argvj, carg, argv);
696   }
697   for (cnt = 0; NULL != argv2[cnt]; cnt++)
698   {
699     argv = GNUNET_strdup (argv2[cnt]);
700     GNUNET_array_append (argvj, carg, argv);
701   }
702   GNUNET_array_append (argvj, carg, NULL);
703   return argvj;
704 }
705
706
707 /**
708  * Frees the given NULL terminated arguments
709  *
710  * @param argv the NULL terminated list of arguments
711  */
712 static void
713 free_argv (char **argv)
714 {
715   unsigned int argp;
716
717   for (argp = 0; NULL != argv[argp]; argp++)
718     GNUNET_free (argv[argp]);
719   GNUNET_free (argv);
720 }
721
722
723 /**
724  * Generates arguments for opening a remote shell. Builds up the arguments
725  * from the environment variable GNUNET_TESTBED_RSH_CMD. The variable
726  * should not mention `-p' (port) option and destination address as these will
727  * be set locally in the function from its parameteres. If the environmental
728  * variable is not found then it defaults to `ssh -o BatchMode=yes -o
729  * NoHostAuthenticationForLocalhost=yes'
730  *
731  * @param port the destination port number
732  * @param dst the destination address
733  * @return NULL terminated list of arguments
734  */
735 static char **
736 gen_rsh_args (const char *port, const char *dst)
737 {
738   static const char *default_ssh_args[] = {
739     "ssh",
740     "-o",
741     "BatchMode=yes",
742     "-o",
743     "NoHostAuthenticationForLocalhost=yes",
744     NULL
745   };
746   char **ssh_args;
747   char *ssh_cmd;
748   char *ssh_cmd_cp;
749   char *arg;
750   unsigned int cnt;
751
752   ssh_args = NULL;
753   if (NULL != (ssh_cmd = getenv ("GNUNET_TESTBED_RSH_CMD")))
754   {
755     ssh_cmd = GNUNET_strdup (ssh_cmd);
756     ssh_cmd_cp = ssh_cmd;
757     for (cnt = 0; NULL != (arg = strtok (ssh_cmd, " ")); ssh_cmd = NULL)
758       GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (arg));
759     GNUNET_free (ssh_cmd_cp);
760   }
761   else
762   {
763     ssh_args = copy_argv (default_ssh_args);
764     cnt = (sizeof (default_ssh_args)) / (sizeof (const char *));
765     GNUNET_array_grow (ssh_args, cnt, cnt - 1);
766   }
767   GNUNET_array_append (ssh_args, cnt, GNUNET_strdup ("-p"));
768   GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (port));
769   GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (dst));
770   GNUNET_array_append (ssh_args, cnt, NULL);
771   return ssh_args;
772 }
773
774
775 /**
776  * Generates the arguments needed for executing the given binary in a remote
777  * shell. Builds the arguments from the environmental variable
778  * GNUNET_TETSBED_RSH_CMD_SUFFIX. If the environmental variable is not found,
779  * only the given binary name will be present in the returned arguments
780  *
781  * @param append_args the arguments to append after generating the suffix
782  *          arguments. Can be NULL; if not must be NULL terminated 'char *' array
783  * @return NULL-terminated args
784  */
785 static char **
786 gen_rsh_suffix_args (const char * const *append_args)
787 {
788   char **rshell_args;
789   char *rshell_cmd;
790   char *rshell_cmd_cp;
791   char *arg;
792   unsigned int cnt;
793   unsigned int append_cnt;
794
795   rshell_args = NULL;
796   cnt = 0;
797   if (NULL != (rshell_cmd = getenv ("GNUNET_TESTBED_RSH_CMD_SUFFIX")))
798   {
799     rshell_cmd = GNUNET_strdup (rshell_cmd);
800     rshell_cmd_cp = rshell_cmd;
801     for (; NULL != (arg = strtok (rshell_cmd, " ")); rshell_cmd = NULL)
802       GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (arg));
803     GNUNET_free (rshell_cmd_cp);
804   }
805   if (NULL != append_args)
806   {
807     for (append_cnt = 0; NULL != append_args[append_cnt]; append_cnt++)      
808       GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (append_args[append_cnt]));
809   }
810   GNUNET_array_append (rshell_args, cnt, NULL);
811   return rshell_args;
812 }
813
814
815 /**
816  * Functions with this signature are called whenever a
817  * complete message is received by the tokenizer.
818  *
819  * Do not call GNUNET_SERVER_mst_destroy in callback
820  *
821  * @param cls closure
822  * @param client identification of the client
823  * @param message the actual message
824  *
825  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
826  */
827 static int
828 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
829 {
830   struct GNUNET_TESTBED_ControllerProc *cp = cls;
831   const struct GNUNET_TESTBED_HelperReply *msg;
832   const char *hostname;
833   char *config;
834   uLongf config_size;
835   uLongf xconfig_size;
836
837   msg = (const struct GNUNET_TESTBED_HelperReply *) message;
838   GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
839                  ntohs (msg->header.size));
840   GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
841                  ntohs (msg->header.type));
842   config_size = (uLongf) ntohs (msg->config_size);
843   xconfig_size =
844       (uLongf) (ntohs (msg->header.size) -
845                 sizeof (struct GNUNET_TESTBED_HelperReply));
846   config = GNUNET_malloc (config_size);
847   GNUNET_assert (Z_OK ==
848                  uncompress ((Bytef *) config, &config_size,
849                              (const Bytef *) &msg[1], xconfig_size));
850   /* Replace the configuration template present in the host with the
851      controller's running configuration */
852   GNUNET_CONFIGURATION_destroy (cp->host->cfg);
853   cp->host->cfg = GNUNET_CONFIGURATION_create ();
854   GNUNET_assert (GNUNET_CONFIGURATION_deserialize
855                  (cp->host->cfg, config, config_size, GNUNET_NO));
856   GNUNET_free (config);
857   if ((NULL == cp->host) ||
858       (NULL == (hostname = GNUNET_TESTBED_host_get_hostname (cp->host))))
859     hostname = "localhost";
860   /* Change the hostname so that we can connect to it */
861   GNUNET_CONFIGURATION_set_value_string (cp->host->cfg, "testbed", "hostname",
862                                          hostname);
863   cp->host->locked = GNUNET_NO;
864   cp->host->controller_started = GNUNET_YES;
865   cp->cb (cp->cls, cp->host->cfg, GNUNET_OK);
866   return GNUNET_OK;
867 }
868
869
870 /**
871  * Continuation function from GNUNET_HELPER_send()
872  *
873  * @param cls closure
874  * @param result GNUNET_OK on success,
875  *               GNUNET_NO if helper process died
876  *               GNUNET_SYSERR during GNUNET_HELPER_stop
877  */
878 static void
879 clear_msg (void *cls, int result)
880 {
881   struct GNUNET_TESTBED_ControllerProc *cp = cls;
882
883   GNUNET_assert (NULL != cp->shandle);
884   cp->shandle = NULL;
885   GNUNET_free (cp->msg);
886 }
887
888
889 /**
890  * Callback that will be called when the helper process dies. This is not called
891  * when the helper process is stoped using GNUNET_HELPER_stop()
892  *
893  * @param cls the closure from GNUNET_HELPER_start()
894  */
895 static void
896 helper_exp_cb (void *cls)
897 {
898   struct GNUNET_TESTBED_ControllerProc *cp = cls;
899   GNUNET_TESTBED_ControllerStatusCallback cb;
900   void *cb_cls;
901
902   cb = cp->cb;
903   cb_cls = cp->cls;
904   cp->helper = NULL;
905   GNUNET_TESTBED_controller_stop (cp);
906   if (NULL != cb)
907     cb (cb_cls, NULL, GNUNET_SYSERR);
908 }
909
910
911 /**
912  * Starts a controller process at the given host
913  *
914  * @param trusted_ip the ip address of the controller which will be set as TRUSTED
915  *          HOST(all connections form this ip are permitted by the testbed) when
916  *          starting testbed controller at host. This can either be a single ip
917  *          address or a network address in CIDR notation.
918  * @param host the host where the controller has to be started; NULL for
919  *          localhost
920  * @param cfg template configuration to use for the remote controller; the
921  *          remote controller will be started with a slightly modified
922  *          configuration (port numbers, unix domain sockets and service home
923  *          values are changed as per TESTING library on the remote host)
924  * @param cb function called when the controller is successfully started or
925  *          dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
926  *          called if cb is called with GNUNET_SYSERR as status. Will never be
927  *          called in the same task as 'GNUNET_TESTBED_controller_start'
928  *          (synchronous errors will be signalled by returning NULL). This
929  *          parameter cannot be NULL.
930  * @param cls closure for above callbacks
931  * @return the controller process handle, NULL on errors
932  */
933 struct GNUNET_TESTBED_ControllerProc *
934 GNUNET_TESTBED_controller_start (const char *trusted_ip,
935                                  struct GNUNET_TESTBED_Host *host,
936                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
937                                  GNUNET_TESTBED_ControllerStatusCallback cb,
938                                  void *cls)
939 {
940   struct GNUNET_TESTBED_ControllerProc *cp;
941   struct GNUNET_TESTBED_HelperInit *msg;
942   const char *hostname;
943
944   static char *const binary_argv[] = {
945     HELPER_TESTBED_BINARY, NULL
946   };
947
948   hostname = NULL;
949   API_VIOLATION (GNUNET_NO == host->locked,
950                  "Host is already locked by a previous call to GNUNET_TESTBED_controller_start()");
951   host->locked = GNUNET_YES;
952   API_VIOLATION (GNUNET_NO == host->controller_started,
953                  "Attempting to start a controller on a host which is already started a controller");
954   cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
955   if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
956   {
957     cp->helper =
958         GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
959                              &helper_mst, &helper_exp_cb, cp);
960   }
961   else
962   {
963     char *helper_binary_path_args[2];
964     char **rsh_args;
965     char **rsh_suffix_args;
966     const char *username;
967     char *port;
968     char *dst;
969
970     username = GNUNET_TESTBED_host_get_username_ (host);
971     hostname = GNUNET_TESTBED_host_get_hostname (host);
972     GNUNET_asprintf (&port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
973     if (NULL == username)
974       GNUNET_asprintf (&dst, "%s", hostname);
975     else
976       GNUNET_asprintf (&dst, "%s@%s", username, hostname);
977     LOG_DEBUG ("Starting SSH to destination %s\n", dst);
978
979     if (GNUNET_OK !=
980         GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
981                                                "HELPER_BINARY_PATH",
982                                                &helper_binary_path_args[0]))
983       helper_binary_path_args[0] =
984           GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
985     helper_binary_path_args[1] = NULL;
986     rsh_args = gen_rsh_args (port, dst);
987     rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args);
988     cp->helper_argv =
989         join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args);
990     free_argv (rsh_args);
991     free_argv (rsh_suffix_args);
992     GNUNET_free (port);
993     GNUNET_free (dst);
994     cp->helper =
995         GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst,
996                              &helper_exp_cb, cp);
997     GNUNET_free (helper_binary_path_args[0]);
998   }
999   if (NULL == cp->helper)
1000   {
1001     if (NULL != cp->helper_argv)
1002       free_argv (cp->helper_argv);
1003     GNUNET_free (cp);
1004     return NULL;
1005   }
1006   cp->host = host;
1007   cp->cb = cb;
1008   cp->cls = cls;
1009   msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, hostname, cfg);
1010   cp->msg = &msg->header;
1011   cp->shandle =
1012       GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1013   if (NULL == cp->shandle)
1014   {
1015     GNUNET_free (msg);
1016     GNUNET_TESTBED_controller_stop (cp);
1017     return NULL;
1018   }
1019   return cp;
1020 }
1021
1022
1023 /**
1024  * Stop the controller process (also will terminate all peers and controllers
1025  * dependent on this controller).  This function blocks until the testbed has
1026  * been fully terminated (!). The controller status cb from
1027  * GNUNET_TESTBED_controller_start() will not be called.
1028  *
1029  * @param cproc the controller process handle
1030  */
1031 void
1032 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1033 {
1034   if (NULL != cproc->shandle)
1035     GNUNET_HELPER_send_cancel (cproc->shandle);
1036   if (NULL != cproc->helper)
1037     GNUNET_HELPER_soft_stop (cproc->helper);
1038   if (NULL != cproc->helper_argv)
1039     free_argv (cproc->helper_argv);
1040   cproc->host->controller_started = GNUNET_NO;
1041   cproc->host->locked = GNUNET_NO;
1042   GNUNET_free (cproc);
1043 }
1044
1045
1046 /**
1047  * The handle for whether a host is habitable or not
1048  */
1049 struct GNUNET_TESTBED_HostHabitableCheckHandle
1050 {
1051   /**
1052    * The host to check
1053    */
1054   const struct GNUNET_TESTBED_Host *host;
1055
1056   /**
1057    * The callback to call once we have the status
1058    */
1059   GNUNET_TESTBED_HostHabitableCallback cb;
1060
1061   /**
1062    * The callback closure
1063    */
1064   void *cb_cls;
1065
1066   /**
1067    * The process handle for the SSH process
1068    */
1069   struct GNUNET_OS_Process *auxp;
1070
1071   /**
1072    * The arguments used to start the helper
1073    */
1074   char **helper_argv;
1075
1076   /**
1077    * Task id for the habitability check task
1078    */
1079   GNUNET_SCHEDULER_TaskIdentifier habitability_check_task;
1080
1081   /**
1082    * How long we wait before checking the process status. Should grow
1083    * exponentially
1084    */
1085   struct GNUNET_TIME_Relative wait_time;
1086
1087 };
1088
1089
1090 /**
1091  * Task for checking whether a host is habitable or not
1092  *
1093  * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
1094  * @param tc the scheduler task context
1095  */
1096 static void
1097 habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1098 {
1099   struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
1100   void *cb_cls;
1101   GNUNET_TESTBED_HostHabitableCallback cb;
1102   const struct GNUNET_TESTBED_Host *host;
1103   unsigned long code;
1104   enum GNUNET_OS_ProcessStatusType type;
1105   int ret;
1106
1107   h->habitability_check_task = GNUNET_SCHEDULER_NO_TASK;
1108   ret = GNUNET_OS_process_status (h->auxp, &type, &code);
1109   if (GNUNET_SYSERR == ret)
1110   {
1111     GNUNET_break (0);
1112     ret = GNUNET_NO;
1113     goto call_cb;
1114   }
1115   if (GNUNET_NO == ret)
1116   {
1117     h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1118     h->habitability_check_task =
1119         GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1120     return;
1121   }
1122   GNUNET_OS_process_destroy (h->auxp);
1123   h->auxp = NULL;
1124   ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
1125
1126 call_cb:
1127   if (NULL != h->auxp)
1128     GNUNET_OS_process_destroy (h->auxp);
1129   cb = h->cb;
1130   cb_cls = h->cb_cls;
1131   host = h->host;
1132   GNUNET_free (h);
1133   if (NULL != cb)
1134     cb (cb_cls, host, ret);
1135 }
1136
1137
1138 /**
1139  * Checks whether a host can be used to start testbed service
1140  *
1141  * @param host the host to check
1142  * @param config the configuration handle to lookup the path of the testbed
1143  *          helper
1144  * @param cb the callback to call to inform about habitability of the given host
1145  * @param cb_cls the closure for the callback
1146  * @return NULL upon any error or a handle which can be passed to
1147  *           GNUNET_TESTBED_is_host_habitable_cancel()
1148  */
1149 struct GNUNET_TESTBED_HostHabitableCheckHandle *
1150 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
1151                                   const struct GNUNET_CONFIGURATION_Handle
1152                                   *config,
1153                                   GNUNET_TESTBED_HostHabitableCallback cb,
1154                                   void *cb_cls)
1155 {
1156   struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
1157   char **rsh_args;
1158   char **rsh_suffix_args;
1159   char *stat_args[3];
1160   const char *hostname;
1161   char *port;
1162   char *dst;
1163
1164   h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle));
1165   h->cb = cb;
1166   h->cb_cls = cb_cls;
1167   h->host = host;
1168   hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
1169   if (NULL == host->username)
1170     dst = GNUNET_strdup (hostname);
1171   else
1172     GNUNET_asprintf (&dst, "%s@%s", host->username, hostname);
1173   if (GNUNET_OK !=
1174       GNUNET_CONFIGURATION_get_value_string (config, "testbed",
1175                                              "HELPER_BINARY_PATH",
1176                                              &stat_args[1]))
1177     stat_args[1] =
1178         GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);  
1179   GNUNET_asprintf (&port, "%u", host->port);
1180   rsh_args = gen_rsh_args (port, dst);
1181   GNUNET_free (port);
1182   GNUNET_free (dst);
1183   port = NULL;
1184   dst = NULL;
1185   stat_args[0] = "stat";
1186   stat_args[2] = NULL;
1187   rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
1188   h->helper_argv = join_argv ((const char **) rsh_args,
1189                               (const char **) rsh_suffix_args);
1190   GNUNET_free (rsh_suffix_args);
1191   GNUNET_free (rsh_args);
1192   h->auxp =
1193       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
1194                                    NULL, h->helper_argv[0], h->helper_argv);
1195   if (NULL == h->auxp)
1196   {
1197     GNUNET_break (0);           /* Cannot exec SSH? */
1198     GNUNET_free (h);
1199     return NULL;
1200   }
1201   h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1202   h->habitability_check_task =
1203       GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1204   return h;
1205 }
1206
1207
1208 /**
1209  * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
1210  *
1211  * @param handle the habitability check handle
1212  */
1213 void
1214 GNUNET_TESTBED_is_host_habitable_cancel (struct
1215                                          GNUNET_TESTBED_HostHabitableCheckHandle
1216                                          *handle)
1217 {
1218   GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
1219   (void) GNUNET_OS_process_kill (handle->auxp, SIGTERM);
1220   (void) GNUNET_OS_process_wait (handle->auxp);
1221   GNUNET_OS_process_destroy (handle->auxp);
1222   free_argv (handle->helper_argv);
1223   GNUNET_free (handle);
1224 }
1225
1226
1227 /**
1228  * handle for host registration
1229  */
1230 struct GNUNET_TESTBED_HostRegistrationHandle
1231 {
1232   /**
1233    * The host being registered
1234    */
1235   struct GNUNET_TESTBED_Host *host;
1236
1237   /**
1238    * The controller at which this host is being registered
1239    */
1240   struct GNUNET_TESTBED_Controller *c;
1241
1242   /**
1243    * The Registartion completion callback
1244    */
1245   GNUNET_TESTBED_HostRegistrationCompletion cc;
1246
1247   /**
1248    * The closure for above callback
1249    */
1250   void *cc_cls;
1251 };
1252
1253
1254 /**
1255  * Register a host with the controller
1256  *
1257  * @param controller the controller handle
1258  * @param host the host to register
1259  * @param cc the completion callback to call to inform the status of
1260  *          registration. After calling this callback the registration handle
1261  *          will be invalid. Cannot be NULL.
1262  * @param cc_cls the closure for the cc
1263  * @return handle to the host registration which can be used to cancel the
1264  *           registration
1265  */
1266 struct GNUNET_TESTBED_HostRegistrationHandle *
1267 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1268                               struct GNUNET_TESTBED_Host *host,
1269                               GNUNET_TESTBED_HostRegistrationCompletion cc,
1270                               void *cc_cls)
1271 {
1272   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1273   struct GNUNET_TESTBED_AddHostMessage *msg;
1274   const char *username;
1275   const char *hostname;
1276   char *config;
1277   char *cconfig;
1278   void *ptr;
1279   size_t cc_size;
1280   size_t config_size;
1281   uint16_t msg_size;
1282   uint16_t username_length;
1283   uint16_t hostname_length;
1284
1285   if (NULL != controller->rh)
1286     return NULL;
1287   hostname = GNUNET_TESTBED_host_get_hostname (host);
1288   if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1289   {
1290     LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1291          (NULL == hostname) ? "localhost" : hostname);
1292     return NULL;
1293   }
1294   rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1295   rh->host = host;
1296   rh->c = controller;
1297   GNUNET_assert (NULL != cc);
1298   rh->cc = cc;
1299   rh->cc_cls = cc_cls;
1300   controller->rh = rh;
1301   username = GNUNET_TESTBED_host_get_username_ (host);
1302   username_length = 0;
1303   if (NULL != username)
1304     username_length = strlen (username);
1305   GNUNET_assert (NULL != hostname); /* Hostname must be present */
1306   hostname_length = strlen (hostname);
1307   GNUNET_assert (NULL != host->cfg);
1308   config = GNUNET_CONFIGURATION_serialize (host->cfg, &config_size);
1309   cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1310   GNUNET_free (config);
1311   msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1312   msg_size += username_length;
1313   msg_size += hostname_length;
1314   msg_size += cc_size;
1315   msg = GNUNET_malloc (msg_size);
1316   msg->header.size = htons (msg_size);
1317   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST);
1318   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1319   msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1320   ptr = &msg[1];
1321   if (NULL != username)
1322   {
1323     msg->username_length = htons (username_length);
1324     ptr = memcpy (ptr, username, username_length);
1325     ptr += username_length;
1326   }
1327   msg->hostname_length = htons (hostname_length);
1328   ptr = memcpy (ptr, hostname, hostname_length);
1329   ptr += hostname_length;
1330   msg->config_size = htons (config_size);
1331   ptr = memcpy (ptr, cconfig, cc_size);
1332   ptr += cc_size;
1333   GNUNET_assert ((ptr - (void *) msg) == msg_size);
1334   GNUNET_free (cconfig);
1335   GNUNET_TESTBED_queue_message_ (controller,
1336                                  (struct GNUNET_MessageHeader *) msg);
1337   return rh;
1338 }
1339
1340
1341 /**
1342  * Cancel the pending registration. Note that if the registration message is
1343  * already sent to the service the cancellation has only the effect that the
1344  * registration completion callback for the registration is never called.
1345  *
1346  * @param handle the registration handle to cancel
1347  */
1348 void
1349 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1350                                     *handle)
1351 {
1352   if (handle != handle->c->rh)
1353   {
1354     GNUNET_break (0);
1355     return;
1356   }
1357   handle->c->rh = NULL;
1358   GNUNET_free (handle);
1359 }
1360
1361
1362 /**
1363  * Initializes the operation queue for parallel overlay connects
1364  *
1365  * @param h the host handle
1366  * @param npoc the number of parallel overlay connects - the queue size
1367  */
1368 void
1369 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (struct
1370                                                    GNUNET_TESTBED_Host *h,
1371                                                    unsigned int npoc)
1372 {
1373   //fprintf (stderr, "%d", npoc);
1374   GNUNET_free_non_null (h->tslots);
1375   h->tslots_filled = 0;
1376   h->num_parallel_connects = npoc;
1377   h->tslots = GNUNET_malloc (npoc * sizeof (struct TimeSlot));
1378   GNUNET_TESTBED_operation_queue_reset_max_active_
1379       (h->opq_parallel_overlay_connect_operations, npoc);
1380 }
1381
1382
1383 /**
1384  * Returns a timing slot which will be exclusively locked
1385  *
1386  * @param h the host handle
1387  * @param key a pointer which is associated to the returned slot; should not be
1388  *          NULL. It serves as a key to determine the correct owner of the slot
1389  * @return the time slot index in the array of time slots in the controller
1390  *           handle
1391  */
1392 unsigned int
1393 GNUNET_TESTBED_get_tslot_ (struct GNUNET_TESTBED_Host *h, void *key)
1394 {
1395   unsigned int slot;
1396
1397   GNUNET_assert (NULL != h->tslots);
1398   GNUNET_assert (NULL != key);
1399   for (slot = 0; slot < h->num_parallel_connects; slot++)
1400     if (NULL == h->tslots[slot].key)
1401     {
1402       h->tslots[slot].key = key;
1403       return slot;
1404     }
1405   GNUNET_assert (0);            /* We should always find a free tslot */
1406 }
1407
1408
1409 /**
1410  * Decides whether any change in the number of parallel overlay connects is
1411  * necessary to adapt to the load on the system
1412  *
1413  * @param h the host handle
1414  */
1415 static void
1416 decide_npoc (struct GNUNET_TESTBED_Host *h)
1417 {
1418   struct GNUNET_TIME_Relative avg;
1419   int sd;
1420   unsigned int slot;
1421   unsigned int nvals;
1422
1423   if (h->tslots_filled != h->num_parallel_connects)
1424     return;
1425   avg = GNUNET_TIME_UNIT_ZERO;
1426   nvals = 0;
1427   for (slot = 0; slot < h->num_parallel_connects; slot++)
1428   {
1429     avg = GNUNET_TIME_relative_add (avg, h->tslots[slot].time);
1430     nvals += h->tslots[slot].nvals;
1431   }
1432   GNUNET_assert (nvals >= h->num_parallel_connects);
1433   avg = GNUNET_TIME_relative_divide (avg, nvals);
1434   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != avg.rel_value);
1435   sd = GNUNET_TESTBED_SD_deviation_factor_ (h->poc_sd, (unsigned int) avg.rel_value);
1436   if ( (sd <= 5) ||
1437        (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1438                                        h->num_parallel_connects)) )
1439     GNUNET_TESTBED_SD_add_data_ (h->poc_sd, (unsigned int) avg.rel_value);
1440   if (GNUNET_SYSERR == sd)
1441   {
1442     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1443                                                        h->num_parallel_connects);
1444     return;
1445   }
1446   GNUNET_assert (0 <= sd);
1447   if (0 == sd)
1448   {
1449     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1450                                                        h->num_parallel_connects
1451                                                        * 2);
1452     return;
1453   }
1454   if (1 == sd)
1455   {
1456     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1457                                                        h->num_parallel_connects
1458                                                        + 1);
1459     return;
1460   }
1461   if (1 == h->num_parallel_connects)
1462   {
1463     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
1464     return;
1465   }
1466   if (2 == sd)
1467   {
1468     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1469                                                        h->num_parallel_connects
1470                                                        - 1);
1471     return;
1472   }
1473   GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1474                                                      h->num_parallel_connects /
1475                                                      2);
1476 }
1477
1478
1479 /**
1480  * Releases a time slot thus making it available for be used again
1481  *
1482  * @param h the host handle
1483  * @param index the index of the the time slot
1484  * @param key the key to prove ownership of the timeslot
1485  * @return GNUNET_YES if the time slot is successfully removed; GNUNET_NO if the
1486  *           time slot cannot be removed - this could be because of the index
1487  *           greater than existing number of time slots or `key' being different
1488  */
1489 int
1490 GNUNET_TESTBED_release_time_slot_ (struct GNUNET_TESTBED_Host *h,
1491                                    unsigned int index, void *key)
1492 {
1493   struct TimeSlot *slot;
1494
1495   GNUNET_assert (NULL != key);
1496   if (index >= h->num_parallel_connects)
1497     return GNUNET_NO;
1498   slot = &h->tslots[index];
1499   if (key != slot->key)
1500     return GNUNET_NO;
1501   slot->key = NULL;
1502   return GNUNET_YES;
1503 }
1504
1505
1506 /**
1507  * Function to update a time slot
1508  *
1509  * @param h the host handle
1510  * @param index the index of the time slot to update
1511  * @param key the key to identify ownership of the slot
1512  * @param time the new time
1513  * @param failed should this reading be treated as coming from a fail event
1514  */
1515 void
1516 GNUNET_TESTBED_update_time_slot_ (struct GNUNET_TESTBED_Host *h,
1517                                   unsigned int index, void *key,
1518                                   struct GNUNET_TIME_Relative time, int failed)
1519 {
1520   struct TimeSlot *slot;
1521
1522   if (GNUNET_YES == failed)
1523   {
1524     if (1 == h->num_parallel_connects)
1525     {
1526       GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
1527       return;
1528     }
1529     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1530                                                        h->num_parallel_connects
1531                                                        - 1);
1532   }
1533   if (GNUNET_NO == GNUNET_TESTBED_release_time_slot_ (h, index, key))
1534     return;
1535   slot = &h->tslots[index];
1536   slot->nvals++;
1537   if (GNUNET_TIME_UNIT_ZERO.rel_value == slot->time.rel_value)
1538   {
1539     slot->time = time;
1540     h->tslots_filled++;
1541     decide_npoc (h);
1542     return;
1543   }
1544   slot->time = GNUNET_TIME_relative_add (slot->time, time);
1545 }
1546
1547
1548 /**
1549  * Queues the given operation in the queue for parallel overlay connects of the
1550  * given host
1551  *
1552  * @param h the host handle
1553  * @param op the operation to queue in the given host's parally overlay connect
1554  *          queue 
1555  */
1556 void
1557 GNUNET_TESTBED_host_queue_oc_ (struct GNUNET_TESTBED_Host *h, 
1558                                struct GNUNET_TESTBED_Operation *op)
1559 {  
1560   GNUNET_TESTBED_operation_queue_insert_
1561       (h->opq_parallel_overlay_connect_operations, op);
1562 }
1563
1564
1565 /**
1566  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
1567  * controller (testbed service)
1568  *
1569  * @param c the controller handler
1570  * @param msg message received
1571  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
1572  *           not
1573  */
1574 int
1575 GNUNET_TESTBED_host_handle_addhostconfirm_ (struct GNUNET_TESTBED_Controller *c,
1576                                             const struct
1577                                             GNUNET_TESTBED_HostConfirmedMessage
1578                                             *msg)
1579 {
1580   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1581   char *emsg;
1582   uint16_t msg_size;
1583
1584   rh = c->rh;
1585   if (NULL == rh)
1586   {
1587     return GNUNET_OK;
1588   }
1589   if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
1590   {
1591     LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
1592                GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
1593     return GNUNET_OK;
1594   }
1595   c->rh = NULL;
1596   msg_size = ntohs (msg->header.size);
1597   if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
1598   {
1599     LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
1600     GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
1601     rh->cc (rh->cc_cls, NULL);
1602     GNUNET_free (rh);
1603     return GNUNET_OK;
1604   }
1605   /* We have an error message */
1606   emsg = (char *) &msg[1];
1607   if ('\0' !=
1608       emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
1609   {
1610     GNUNET_break (0);
1611     GNUNET_free (rh);
1612     return GNUNET_NO;
1613   }
1614   LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
1615        ntohl (msg->host_id), emsg);
1616   rh->cc (rh->cc_cls, emsg);
1617   GNUNET_free (rh);
1618   return GNUNET_OK;
1619 }
1620
1621 /* end of testbed_api_hosts.c */