2
0

testbed_api_hosts.c 45 KB

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