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