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