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