testing.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008, 2009, 2012 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 testing/testing.c
  18. * @brief convenience API for writing testcases for GNUnet
  19. * Many testcases need to start and stop a peer/service
  20. * and this library is supposed to make that easier
  21. * for TESTCASES. Normal programs should always
  22. * use functions from gnunet_{util,arm}_lib.h. This API is
  23. * ONLY for writing testcases (or internal use of the testbed).
  24. * @author Christian Grothoff
  25. *
  26. */
  27. #include "platform.h"
  28. #include "gnunet_util_lib.h"
  29. #include "gnunet_arm_service.h"
  30. #include "gnunet_testing_lib.h"
  31. #define LOG(kind,...) \
  32. GNUNET_log_from (kind, "testing-api", __VA_ARGS__)
  33. /**
  34. * We need pipe control only on WINDOWS
  35. */
  36. #if WINDOWS
  37. #define PIPE_CONTROL GNUNET_YES
  38. #else
  39. #define PIPE_CONTROL GNUNET_NO
  40. #endif
  41. /**
  42. * Lowest port used for GNUnet testing. Should be high enough to not
  43. * conflict with other applications running on the hosts but be low
  44. * enough to not conflict with client-ports (typically starting around
  45. * 32k).
  46. */
  47. #define LOW_PORT 12000
  48. /**
  49. * Highest port used for GNUnet testing. Should be low enough to not
  50. * conflict with the port range for "local" ports (client apps; see
  51. * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
  52. */
  53. #define HIGH_PORT 56000
  54. struct SharedServiceInstance
  55. {
  56. struct SharedService *ss;
  57. char *cfg_fn;
  58. struct GNUNET_OS_Process *proc;
  59. char *unix_sock;
  60. char *port_str;
  61. unsigned int n_refs;
  62. };
  63. struct SharedService
  64. {
  65. char *sname;
  66. struct SharedServiceInstance **instances;
  67. struct GNUNET_CONFIGURATION_Handle *cfg;
  68. unsigned int n_peers;
  69. unsigned int share;
  70. unsigned int n_instances;
  71. };
  72. /**
  73. * Handle for a system on which GNUnet peers are executed;
  74. * a system is used for reserving unique paths and ports.
  75. */
  76. struct GNUNET_TESTING_System
  77. {
  78. /**
  79. * Prefix (i.e. "/tmp/gnunet-testing/") we prepend to each
  80. * GNUNET_HOME.
  81. */
  82. char *tmppath;
  83. /**
  84. * The trusted ip. Can either be a single ip address or a network address in
  85. * CIDR notation.
  86. */
  87. char *trusted_ip;
  88. /**
  89. * our hostname
  90. */
  91. char *hostname;
  92. /**
  93. * Hostkeys data, contains "GNUNET_TESTING_HOSTKEYFILESIZE * total_hostkeys" bytes.
  94. */
  95. char *hostkeys_data;
  96. /**
  97. * memory map for @e hostkeys_data.
  98. */
  99. struct GNUNET_DISK_MapHandle *map;
  100. struct SharedService **shared_services;
  101. unsigned int n_shared_services;
  102. /**
  103. * Bitmap where each port that has already been reserved for some GNUnet peer
  104. * is recorded. Note that we make no distinction between TCP and UDP ports
  105. * and test if a port is already in use before assigning it to a peer/service.
  106. * If we detect that a port is already in use, we also mark it in this bitmap.
  107. * So all the bits that are zero merely indicate ports that MIGHT be available
  108. * for peers.
  109. */
  110. uint32_t reserved_ports[65536 / 32];
  111. /**
  112. * Counter we use to make service home paths unique on this system;
  113. * the full path consists of the tmppath and this number. Each
  114. * UNIXPATH for a peer is also modified to include the respective
  115. * path counter to ensure uniqueness. This field is incremented
  116. * by one for each configured peer. Even if peers are destroyed,
  117. * we never re-use path counters.
  118. */
  119. uint32_t path_counter;
  120. /**
  121. * The number of hostkeys
  122. */
  123. uint32_t total_hostkeys;
  124. /**
  125. * Lowest port we are allowed to use.
  126. */
  127. uint16_t lowport;
  128. /**
  129. * Highest port we are allowed to use.
  130. */
  131. uint16_t highport;
  132. };
  133. /**
  134. * Handle for a GNUnet peer controlled by testing.
  135. */
  136. struct GNUNET_TESTING_Peer
  137. {
  138. /**
  139. * The TESTING system associated with this peer
  140. */
  141. struct GNUNET_TESTING_System *system;
  142. /**
  143. * Path to the configuration file for this peer.
  144. */
  145. char *cfgfile;
  146. /**
  147. * Binary to be executed during 'GNUNET_TESTING_peer_start'.
  148. * Typically 'gnunet-service-arm' (but can be set to a
  149. * specific service by 'GNUNET_TESTING_service_run' if
  150. * necessary).
  151. */
  152. char *main_binary;
  153. char *args;
  154. /**
  155. * Handle to the running binary of the service, NULL if the
  156. * peer/service is currently not running.
  157. */
  158. struct GNUNET_OS_Process *main_process;
  159. /**
  160. * The handle to the peer's ARM service
  161. */
  162. struct GNUNET_ARM_Handle *ah;
  163. /**
  164. * The config of the peer
  165. */
  166. struct GNUNET_CONFIGURATION_Handle *cfg;
  167. /**
  168. * The callback to call asynchronously when a peer is stopped
  169. */
  170. GNUNET_TESTING_PeerStopCallback cb;
  171. /**
  172. * The closure for the above callback
  173. */
  174. void *cb_cls;
  175. /**
  176. * The cached identity of this peer. Will be populated on call to
  177. * GNUNET_TESTING_peer_get_identity()
  178. */
  179. struct GNUNET_PeerIdentity *id;
  180. struct SharedServiceInstance **ss_instances;
  181. /**
  182. * Array of ports currently allocated to this peer. These ports will be
  183. * released upon peer destroy and can be used by other peers which are
  184. * configured after.
  185. */
  186. uint16_t *ports;
  187. /**
  188. * The number of ports in the above array
  189. */
  190. unsigned int nports;
  191. /**
  192. * The keynumber of this peer's hostkey
  193. */
  194. uint32_t key_number;
  195. };
  196. /**
  197. * Testing includes a number of pre-created hostkeys for faster peer
  198. * startup. This function loads such keys into memory from a file.
  199. *
  200. * @param system the testing system handle
  201. * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
  202. */
  203. static int
  204. hostkeys_load (struct GNUNET_TESTING_System *system)
  205. {
  206. uint64_t fs;
  207. char *data_dir;
  208. char *filename;
  209. struct GNUNET_DISK_FileHandle *fd;
  210. GNUNET_assert (NULL == system->hostkeys_data);
  211. data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
  212. GNUNET_asprintf (&filename, "%s/testing_hostkeys.ecc", data_dir);
  213. GNUNET_free (data_dir);
  214. if (GNUNET_YES != GNUNET_DISK_file_test (filename))
  215. {
  216. LOG (GNUNET_ERROR_TYPE_ERROR,
  217. _("Hostkeys file not found: %s\n"), filename);
  218. GNUNET_free (filename);
  219. return GNUNET_SYSERR;
  220. }
  221. /* Check hostkey file size, read entire thing into memory */
  222. if (GNUNET_OK !=
  223. GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
  224. fs = 0;
  225. if (0 == fs)
  226. {
  227. GNUNET_free (filename);
  228. return GNUNET_SYSERR; /* File is empty */
  229. }
  230. if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
  231. {
  232. LOG (GNUNET_ERROR_TYPE_ERROR,
  233. _("Incorrect hostkey file format: %s\n"), filename);
  234. GNUNET_free (filename);
  235. return GNUNET_SYSERR;
  236. }
  237. fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
  238. GNUNET_DISK_PERM_NONE);
  239. if (NULL == fd)
  240. {
  241. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
  242. GNUNET_free (filename);
  243. return GNUNET_SYSERR;
  244. }
  245. GNUNET_free (filename);
  246. system->hostkeys_data = GNUNET_DISK_file_map (fd,
  247. &system->map,
  248. GNUNET_DISK_MAP_TYPE_READ,
  249. fs);
  250. GNUNET_DISK_file_close (fd);
  251. if (NULL == system->hostkeys_data)
  252. return GNUNET_SYSERR;
  253. system->total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
  254. return GNUNET_OK;
  255. }
  256. /**
  257. * Function to remove the loaded hostkeys
  258. *
  259. * @param system the testing system handle
  260. */
  261. static void
  262. hostkeys_unload (struct GNUNET_TESTING_System *system)
  263. {
  264. GNUNET_break (NULL != system->hostkeys_data);
  265. system->hostkeys_data = NULL;
  266. GNUNET_DISK_file_unmap (system->map);
  267. system->map = NULL;
  268. system->hostkeys_data = NULL;
  269. system->total_hostkeys = 0;
  270. }
  271. /**
  272. * Function to iterate over options.
  273. *
  274. * @param cls closure
  275. * @param section name of the section
  276. * @param option name of the option
  277. * @param value value of the option
  278. */
  279. static void
  280. cfg_copy_iterator (void *cls, const char *section,
  281. const char *option, const char *value)
  282. {
  283. struct GNUNET_CONFIGURATION_Handle *cfg2 = cls;
  284. GNUNET_CONFIGURATION_set_value_string (cfg2, section, option, value);
  285. }
  286. /**
  287. * Create a system handle. There must only be one system
  288. * handle per operating system.
  289. *
  290. * @param testdir only the directory name without any path. This is used for
  291. * all service homes; the directory will be created in a temporary
  292. * location depending on the underlying OS. This variable will be
  293. * overridden with the value of the environmental variable
  294. * GNUNET_TESTING_PREFIX, if it exists.
  295. * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
  296. * service configurations generated to allow control connections from
  297. * this ip. This can either be a single ip address or a network address
  298. * in CIDR notation.
  299. * @param hostname the hostname of the system we are using for testing; NULL for
  300. * localhost
  301. * @param shared_services NULL terminated array describing services that are to
  302. * be shared among peers
  303. * @param lowport lowest port number this system is allowed to allocate (inclusive)
  304. * @param highport highest port number this system is allowed to allocate (exclusive)
  305. * @return handle to this system, NULL on error
  306. */
  307. struct GNUNET_TESTING_System *
  308. GNUNET_TESTING_system_create_with_portrange (const char *testdir,
  309. const char *trusted_ip,
  310. const char *hostname,
  311. const struct
  312. GNUNET_TESTING_SharedService *
  313. shared_services,
  314. uint16_t lowport,
  315. uint16_t highport)
  316. {
  317. struct GNUNET_TESTING_System *system;
  318. struct GNUNET_TESTING_SharedService tss;
  319. struct SharedService *ss;
  320. unsigned int cnt;
  321. GNUNET_assert (NULL != testdir);
  322. system = GNUNET_new (struct GNUNET_TESTING_System);
  323. if (NULL == (system->tmppath = getenv (GNUNET_TESTING_PREFIX)))
  324. system->tmppath = GNUNET_DISK_mkdtemp (testdir);
  325. else
  326. system->tmppath = GNUNET_strdup (system->tmppath);
  327. system->lowport = lowport;
  328. system->highport = highport;
  329. if (NULL == system->tmppath)
  330. {
  331. GNUNET_free (system);
  332. return NULL;
  333. }
  334. if (NULL != trusted_ip)
  335. system->trusted_ip = GNUNET_strdup (trusted_ip);
  336. if (NULL != hostname)
  337. system->hostname = GNUNET_strdup (hostname);
  338. if (GNUNET_OK != hostkeys_load (system))
  339. {
  340. GNUNET_TESTING_system_destroy (system, GNUNET_YES);
  341. return NULL;
  342. }
  343. if (NULL == shared_services)
  344. return system;
  345. for (cnt = 0; NULL != shared_services[cnt].service; cnt++)
  346. {
  347. tss = shared_services[cnt];
  348. ss = GNUNET_new (struct SharedService);
  349. ss->sname = GNUNET_strdup (tss.service);
  350. ss->cfg = GNUNET_CONFIGURATION_create ();
  351. GNUNET_CONFIGURATION_iterate_section_values (tss.cfg, ss->sname,
  352. &cfg_copy_iterator, ss->cfg);
  353. GNUNET_CONFIGURATION_iterate_section_values (tss.cfg, "TESTING",
  354. &cfg_copy_iterator, ss->cfg);
  355. GNUNET_CONFIGURATION_iterate_section_values (tss.cfg, "PATHS",
  356. &cfg_copy_iterator, ss->cfg);
  357. ss->share = tss.share;
  358. GNUNET_array_append (system->shared_services, system->n_shared_services,
  359. ss);
  360. }
  361. return system;
  362. }
  363. /**
  364. * Create a system handle. There must only be one system handle per operating
  365. * system. Uses a default range for allowed ports. Ports are still tested for
  366. * availability.
  367. *
  368. * @param testdir only the directory name without any path. This is used for all
  369. * service homes; the directory will be created in a temporary location
  370. * depending on the underlying OS. This variable will be
  371. * overridden with the value of the environmental variable
  372. * GNUNET_TESTING_PREFIX, if it exists.
  373. * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
  374. * service configurations generated to allow control connections from
  375. * this ip. This can either be a single ip address or a network address
  376. * in CIDR notation.
  377. * @param hostname the hostname of the system we are using for testing; NULL for
  378. * localhost
  379. * @param shared_services NULL terminated array describing services that are to
  380. * be shared among peers
  381. * @return handle to this system, NULL on error
  382. */
  383. struct GNUNET_TESTING_System *
  384. GNUNET_TESTING_system_create (const char *testdir,
  385. const char *trusted_ip,
  386. const char *hostname,
  387. const struct GNUNET_TESTING_SharedService *
  388. shared_services)
  389. {
  390. return GNUNET_TESTING_system_create_with_portrange (testdir,
  391. trusted_ip,
  392. hostname,
  393. shared_services,
  394. LOW_PORT,
  395. HIGH_PORT);
  396. }
  397. static void
  398. cleanup_shared_service_instance (struct SharedServiceInstance *i)
  399. {
  400. if (NULL != i->cfg_fn)
  401. {
  402. (void) unlink (i->cfg_fn);
  403. GNUNET_free (i->cfg_fn);
  404. }
  405. GNUNET_free_non_null (i->unix_sock);
  406. GNUNET_free_non_null (i->port_str);
  407. GNUNET_break (NULL == i->proc);
  408. GNUNET_break (0 == i->n_refs);
  409. GNUNET_free (i);
  410. }
  411. static int
  412. start_shared_service_instance (struct SharedServiceInstance *i)
  413. {
  414. char *binary;
  415. char *libexec_binary;
  416. GNUNET_assert (NULL == i->proc);
  417. GNUNET_assert (NULL != i->cfg_fn);
  418. (void) GNUNET_asprintf (&binary, "gnunet-service-%s", i->ss->sname);
  419. libexec_binary = GNUNET_OS_get_libexec_binary_path (binary);
  420. GNUNET_free (binary);
  421. i->proc = GNUNET_OS_start_process (PIPE_CONTROL,
  422. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  423. NULL, NULL, NULL,
  424. libexec_binary,
  425. libexec_binary,
  426. "-c",
  427. i->cfg_fn,
  428. NULL);
  429. GNUNET_free (libexec_binary);
  430. if (NULL == i->proc)
  431. return GNUNET_SYSERR;
  432. return GNUNET_OK;
  433. }
  434. static void
  435. stop_shared_service_instance (struct SharedServiceInstance *i)
  436. {
  437. GNUNET_break (0 == i->n_refs);
  438. if (0 != GNUNET_OS_process_kill (i->proc, GNUNET_TERM_SIG))
  439. LOG (GNUNET_ERROR_TYPE_WARNING,
  440. "Killing shared service instance (%s) failed\n", i->ss->sname);
  441. (void) GNUNET_OS_process_wait (i->proc);
  442. GNUNET_OS_process_destroy (i->proc);
  443. i->proc = NULL;
  444. }
  445. /**
  446. * Free system resources.
  447. *
  448. * @param system system to be freed
  449. * @param remove_paths should the 'testdir' and all subdirectories
  450. * be removed (clean up on shutdown)?
  451. */
  452. void
  453. GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
  454. int remove_paths)
  455. {
  456. struct SharedService *ss;
  457. struct SharedServiceInstance *i;
  458. unsigned int ss_cnt;
  459. unsigned int i_cnt;
  460. if (NULL != system->hostkeys_data)
  461. hostkeys_unload (system);
  462. for (ss_cnt = 0; ss_cnt < system->n_shared_services; ss_cnt++)
  463. {
  464. ss = system->shared_services[ss_cnt];
  465. for (i_cnt = 0; i_cnt < ss->n_instances; i_cnt++)
  466. {
  467. i = ss->instances[i_cnt];
  468. if (NULL != i->proc)
  469. stop_shared_service_instance (i);
  470. cleanup_shared_service_instance (i);
  471. }
  472. GNUNET_free_non_null (ss->instances);
  473. GNUNET_CONFIGURATION_destroy (ss->cfg);
  474. GNUNET_free (ss->sname);
  475. GNUNET_free (ss);
  476. }
  477. GNUNET_free_non_null (system->shared_services);
  478. if (GNUNET_YES == remove_paths)
  479. GNUNET_DISK_directory_remove (system->tmppath);
  480. GNUNET_free (system->tmppath);
  481. GNUNET_free_non_null (system->trusted_ip);
  482. GNUNET_free_non_null (system->hostname);
  483. GNUNET_free (system);
  484. }
  485. /**
  486. * Reserve a TCP or UDP port for a peer.
  487. *
  488. * @param system system to use for reservation tracking
  489. * @return 0 if no free port was available
  490. */
  491. uint16_t
  492. GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system)
  493. {
  494. struct GNUNET_NETWORK_Handle *socket;
  495. struct addrinfo hint;
  496. struct addrinfo *ret;
  497. struct addrinfo *ai;
  498. uint32_t *port_buckets;
  499. char *open_port_str;
  500. int bind_status;
  501. uint32_t xor_image;
  502. uint16_t index;
  503. uint16_t open_port;
  504. uint16_t pos;
  505. /*
  506. FIXME: Instead of using getaddrinfo we should try to determine the port
  507. status by the following heurestics.
  508. On systems which support both IPv4 and IPv6, only ports open on both
  509. address families are considered open.
  510. On system with either IPv4 or IPv6. A port is considered open if it's
  511. open in the respective address family
  512. */
  513. hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
  514. hint.ai_socktype = 0;
  515. hint.ai_protocol = 0;
  516. hint.ai_addrlen = 0;
  517. hint.ai_addr = NULL;
  518. hint.ai_canonname = NULL;
  519. hint.ai_next = NULL;
  520. hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
  521. port_buckets = system->reserved_ports;
  522. for (index = (system->lowport / 32) + 1; index < (system->highport / 32); index++)
  523. {
  524. xor_image = (UINT32_MAX ^ port_buckets[index]);
  525. if (0 == xor_image) /* Ports in the bucket are full */
  526. continue;
  527. pos = system->lowport % 32;
  528. while (pos < 32)
  529. {
  530. if (0 == ((xor_image >> pos) & 1U))
  531. {
  532. pos++;
  533. continue;
  534. }
  535. open_port = (index * 32) + pos;
  536. if (open_port >= system->highport)
  537. return 0;
  538. GNUNET_asprintf (&open_port_str, "%u", (unsigned int) open_port);
  539. ret = NULL;
  540. GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret));
  541. GNUNET_free (open_port_str);
  542. bind_status = GNUNET_NO;
  543. for (ai = ret; NULL != ai; ai = ai->ai_next)
  544. {
  545. socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_STREAM, 0);
  546. if (NULL == socket)
  547. continue;
  548. bind_status = GNUNET_NETWORK_socket_bind (socket,
  549. ai->ai_addr,
  550. ai->ai_addrlen);
  551. GNUNET_NETWORK_socket_close (socket);
  552. if (GNUNET_OK != bind_status)
  553. break;
  554. socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_DGRAM, 0);
  555. if (NULL == socket)
  556. continue;
  557. bind_status = GNUNET_NETWORK_socket_bind (socket,
  558. ai->ai_addr,
  559. ai->ai_addrlen);
  560. GNUNET_NETWORK_socket_close (socket);
  561. if (GNUNET_OK != bind_status)
  562. break;
  563. }
  564. port_buckets[index] |= (1U << pos); /* Set the port bit */
  565. freeaddrinfo (ret);
  566. if (GNUNET_OK == bind_status)
  567. {
  568. LOG (GNUNET_ERROR_TYPE_DEBUG,
  569. "Found a free port %u\n", (unsigned int) open_port);
  570. return open_port;
  571. }
  572. pos++;
  573. }
  574. }
  575. return 0;
  576. }
  577. /**
  578. * Release reservation of a TCP or UDP port for a peer
  579. * (used during #GNUNET_TESTING_peer_destroy()).
  580. *
  581. * @param system system to use for reservation tracking
  582. * @param port reserved port to release
  583. */
  584. void
  585. GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
  586. uint16_t port)
  587. {
  588. uint32_t *port_buckets;
  589. uint16_t bucket;
  590. uint16_t pos;
  591. port_buckets = system->reserved_ports;
  592. bucket = port / 32;
  593. pos = port % 32;
  594. LOG (GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port);
  595. if (0 == (port_buckets[bucket] & (1U << pos)))
  596. {
  597. GNUNET_break(0); /* Port was not reserved by us using reserve_port() */
  598. return;
  599. }
  600. port_buckets[bucket] &= ~(1U << pos);
  601. }
  602. /**
  603. * Testing includes a number of pre-created hostkeys for
  604. * faster peer startup. This function can be used to
  605. * access the n-th key of those pre-created hostkeys; note
  606. * that these keys are ONLY useful for testing and not
  607. * secure as the private keys are part of the public
  608. * GNUnet source code.
  609. *
  610. * This is primarily a helper function used internally
  611. * by #GNUNET_TESTING_peer_configure.
  612. *
  613. * @param system the testing system handle
  614. * @param key_number desired pre-created hostkey to obtain
  615. * @param id set to the peer's identity (hash of the public
  616. * key; if NULL, NULL is returned immediately
  617. * @return NULL on error (not enough keys)
  618. */
  619. struct GNUNET_CRYPTO_EddsaPrivateKey *
  620. GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
  621. uint32_t key_number,
  622. struct GNUNET_PeerIdentity *id)
  623. {
  624. struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
  625. if ((NULL == id) || (NULL == system->hostkeys_data))
  626. return NULL;
  627. if (key_number >= system->total_hostkeys)
  628. {
  629. LOG (GNUNET_ERROR_TYPE_ERROR,
  630. _("Key number %u does not exist\n"),
  631. key_number);
  632. return NULL;
  633. }
  634. private_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey);
  635. GNUNET_memcpy (private_key,
  636. system->hostkeys_data +
  637. (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
  638. GNUNET_TESTING_HOSTKEYFILESIZE);
  639. GNUNET_CRYPTO_eddsa_key_get_public (private_key,
  640. &id->public_key);
  641. return private_key;
  642. }
  643. /**
  644. * Structure for holding data to build new configurations from a configuration
  645. * template
  646. */
  647. struct UpdateContext
  648. {
  649. /**
  650. * The system for which we are building configurations
  651. */
  652. struct GNUNET_TESTING_System *system;
  653. /**
  654. * The configuration we are building
  655. */
  656. struct GNUNET_CONFIGURATION_Handle *cfg;
  657. /**
  658. * The customized service home path for this peer
  659. */
  660. char *gnunet_home;
  661. /**
  662. * Array of ports currently allocated to this peer. These ports will be
  663. * released upon peer destroy and can be used by other peers which are
  664. * configured after.
  665. */
  666. uint16_t *ports;
  667. /**
  668. * The number of ports in the above array
  669. */
  670. unsigned int nports;
  671. /**
  672. * build status - to signal error while building a configuration
  673. */
  674. int status;
  675. };
  676. /**
  677. * Function to iterate over options. Copies
  678. * the options to the target configuration,
  679. * updating PORT values as needed.
  680. *
  681. * @param cls the UpdateContext
  682. * @param section name of the section
  683. * @param option name of the option
  684. * @param value value of the option
  685. */
  686. static void
  687. update_config (void *cls,
  688. const char *section,
  689. const char *option,
  690. const char *value)
  691. {
  692. struct UpdateContext *uc = cls;
  693. unsigned int ival;
  694. char cval[12];
  695. char uval[PATH_MAX];
  696. char *single_variable;
  697. char *per_host_variable;
  698. unsigned long long num_per_host;
  699. uint16_t new_port;
  700. if (GNUNET_OK != uc->status)
  701. return;
  702. if (! ((0 == strcmp (option, "PORT"))
  703. || (0 == strcmp (option, "UNIXPATH"))
  704. || (0 == strcmp (option, "HOSTNAME"))))
  705. return;
  706. GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
  707. GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
  708. if ((0 == strcmp (option, "PORT")) && (1 == SSCANF (value, "%u", &ival)))
  709. {
  710. if ((ival != 0) &&
  711. (GNUNET_YES !=
  712. GNUNET_CONFIGURATION_get_value_yesno (uc->cfg, "testing",
  713. single_variable)))
  714. {
  715. new_port = GNUNET_TESTING_reserve_port (uc->system);
  716. if (0 == new_port)
  717. {
  718. uc->status = GNUNET_SYSERR;
  719. GNUNET_free (single_variable);
  720. GNUNET_free (per_host_variable);
  721. return;
  722. }
  723. GNUNET_snprintf (cval, sizeof (cval), "%u", new_port);
  724. value = cval;
  725. GNUNET_array_append (uc->ports, uc->nports, new_port);
  726. }
  727. else if ((ival != 0) &&
  728. (GNUNET_YES ==
  729. GNUNET_CONFIGURATION_get_value_yesno (uc->cfg, "testing",
  730. single_variable)) &&
  731. GNUNET_CONFIGURATION_get_value_number (uc->cfg, "testing",
  732. per_host_variable,
  733. &num_per_host))
  734. {
  735. /* GNUNET_snprintf (cval, sizeof (cval), "%u", */
  736. /* ival + ctx->fdnum % num_per_host); */
  737. /* value = cval; */
  738. GNUNET_break (0); /* FIXME */
  739. }
  740. }
  741. if (0 == strcmp (option, "UNIXPATH"))
  742. {
  743. if (GNUNET_YES !=
  744. GNUNET_CONFIGURATION_get_value_yesno (uc->cfg, "testing",
  745. single_variable))
  746. {
  747. GNUNET_snprintf (uval,
  748. sizeof (uval),
  749. "%s/%s.sock",
  750. uc->gnunet_home, section);
  751. value = uval;
  752. }
  753. else if ((GNUNET_YES ==
  754. GNUNET_CONFIGURATION_get_value_number (uc->cfg, "testing",
  755. per_host_variable,
  756. &num_per_host)) &&
  757. (num_per_host > 0))
  758. {
  759. GNUNET_break(0); /* FIXME */
  760. }
  761. }
  762. if (0 == strcmp (option, "HOSTNAME"))
  763. {
  764. value = (NULL == uc->system->hostname) ? "localhost" : uc->system->hostname;
  765. }
  766. GNUNET_free (single_variable);
  767. GNUNET_free (per_host_variable);
  768. GNUNET_CONFIGURATION_set_value_string (uc->cfg, section, option, value);
  769. }
  770. /**
  771. * Section iterator to set ACCEPT_FROM/ACCEPT_FROM6 to include the address of
  772. * 'trusted_hosts' in all sections
  773. *
  774. * @param cls the UpdateContext
  775. * @param section name of the section
  776. */
  777. static void
  778. update_config_sections (void *cls,
  779. const char *section)
  780. {
  781. struct UpdateContext *uc = cls;
  782. char **ikeys;
  783. char *val;
  784. char *ptr;
  785. char *orig_allowed_hosts;
  786. char *allowed_hosts;
  787. char *ACCEPT_FROM_key;
  788. uint16_t ikeys_cnt;
  789. uint16_t key;
  790. ikeys_cnt = 0;
  791. val = NULL;
  792. /* Ignore certain options from sections. See
  793. https://gnunet.org/bugs/view.php?id=2476 */
  794. if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (uc->cfg, section,
  795. "TESTING_IGNORE_KEYS"))
  796. {
  797. GNUNET_assert
  798. (GNUNET_YES ==
  799. GNUNET_CONFIGURATION_get_value_string (uc->cfg, section,
  800. "TESTING_IGNORE_KEYS", &val));
  801. ptr = val;
  802. for (ikeys_cnt = 0; NULL != (ptr = strstr (ptr, ";")); ikeys_cnt++)
  803. ptr++;
  804. if (0 == ikeys_cnt)
  805. GNUNET_break (0);
  806. else
  807. {
  808. ikeys = GNUNET_malloc ((sizeof (char *)) * ikeys_cnt);
  809. ptr = val;
  810. for (key = 0; key < ikeys_cnt; key++)
  811. {
  812. ikeys[key] = ptr;
  813. ptr = strstr (ptr, ";");
  814. GNUNET_assert (NULL != ptr); /* worked just before... */
  815. *ptr = '\0';
  816. ptr++;
  817. }
  818. }
  819. }
  820. if (0 != ikeys_cnt)
  821. {
  822. for (key = 0; key < ikeys_cnt; key++)
  823. {
  824. if (NULL != strstr (ikeys[key], "ADVERTISED_PORT"))
  825. break;
  826. }
  827. if ((key == ikeys_cnt) &&
  828. (GNUNET_YES == GNUNET_CONFIGURATION_have_value (uc->cfg, section,
  829. "ADVERTISED_PORT")))
  830. {
  831. if (GNUNET_OK ==
  832. GNUNET_CONFIGURATION_get_value_string (uc->cfg, section, "PORT", &ptr))
  833. {
  834. GNUNET_CONFIGURATION_set_value_string (uc->cfg, section,
  835. "ADVERTISED_PORT", ptr);
  836. GNUNET_free (ptr);
  837. }
  838. }
  839. for (key = 0; key < ikeys_cnt; key++)
  840. {
  841. if (NULL != strstr (ikeys[key], "ACCEPT_FROM"))
  842. {
  843. GNUNET_free (ikeys);
  844. GNUNET_free (val);
  845. return;
  846. }
  847. }
  848. GNUNET_free (ikeys);
  849. }
  850. GNUNET_free_non_null (val);
  851. ACCEPT_FROM_key = "ACCEPT_FROM";
  852. if ((NULL != uc->system->trusted_ip) &&
  853. (NULL != strstr (uc->system->trusted_ip, ":"))) /* IPv6 in use */
  854. ACCEPT_FROM_key = "ACCEPT_FROM6";
  855. if (GNUNET_OK !=
  856. GNUNET_CONFIGURATION_get_value_string (uc->cfg, section, ACCEPT_FROM_key,
  857. &orig_allowed_hosts))
  858. {
  859. orig_allowed_hosts = GNUNET_strdup ("127.0.0.1;");
  860. }
  861. if (NULL == uc->system->trusted_ip)
  862. allowed_hosts = GNUNET_strdup (orig_allowed_hosts);
  863. else
  864. GNUNET_asprintf (&allowed_hosts, "%s%s;", orig_allowed_hosts,
  865. uc->system->trusted_ip);
  866. GNUNET_free (orig_allowed_hosts);
  867. GNUNET_CONFIGURATION_set_value_string (uc->cfg, section, ACCEPT_FROM_key,
  868. allowed_hosts);
  869. GNUNET_free (allowed_hosts);
  870. }
  871. static struct SharedServiceInstance *
  872. associate_shared_service (struct GNUNET_TESTING_System *system,
  873. struct SharedService *ss,
  874. struct GNUNET_CONFIGURATION_Handle *cfg)
  875. {
  876. struct SharedServiceInstance *i;
  877. struct GNUNET_CONFIGURATION_Handle *temp;
  878. char *gnunet_home;
  879. uint32_t port;
  880. ss->n_peers++;
  881. if ( ((0 == ss->share) && (NULL == ss->instances))
  882. ||
  883. ( (0 != ss->share)
  884. && (ss->n_instances < ((ss->n_peers + ss->share - 1) / ss->share)) ) )
  885. {
  886. i = GNUNET_new (struct SharedServiceInstance);
  887. i->ss = ss;
  888. (void) GNUNET_asprintf (&gnunet_home, "%s/shared/%s/%u",
  889. system->tmppath, ss->sname, ss->n_instances);
  890. (void) GNUNET_asprintf (&i->unix_sock, "%s/sock", gnunet_home);
  891. port = GNUNET_TESTING_reserve_port (system);
  892. if (0 == port)
  893. {
  894. GNUNET_free (gnunet_home);
  895. cleanup_shared_service_instance (i);
  896. return NULL;
  897. }
  898. GNUNET_array_append (ss->instances, ss->n_instances, i);
  899. temp = GNUNET_CONFIGURATION_dup (ss->cfg);
  900. (void) GNUNET_asprintf (&i->port_str, "%u", port);
  901. (void) GNUNET_asprintf (&i->cfg_fn, "%s/config", gnunet_home);
  902. GNUNET_CONFIGURATION_set_value_string (temp, "PATHS", "GNUNET_HOME",
  903. gnunet_home);
  904. GNUNET_free (gnunet_home);
  905. GNUNET_CONFIGURATION_set_value_string (temp, ss->sname, "UNIXPATH",
  906. i->unix_sock);
  907. GNUNET_CONFIGURATION_set_value_string (temp, ss->sname, "PORT",
  908. i->port_str);
  909. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_write (temp, i->cfg_fn))
  910. {
  911. GNUNET_CONFIGURATION_destroy (temp);
  912. cleanup_shared_service_instance (i);
  913. return NULL;
  914. }
  915. GNUNET_CONFIGURATION_destroy (temp);
  916. }
  917. else
  918. {
  919. GNUNET_assert (NULL != ss->instances);
  920. GNUNET_assert (0 < ss->n_instances);
  921. i = ss->instances[ss->n_instances - 1];
  922. }
  923. GNUNET_CONFIGURATION_iterate_section_values(ss->cfg, ss->sname,
  924. &cfg_copy_iterator, cfg);
  925. GNUNET_CONFIGURATION_set_value_string (cfg, ss->sname, "UNIXPATH",
  926. i->unix_sock);
  927. GNUNET_CONFIGURATION_set_value_string (cfg, ss->sname, "PORT", i->port_str);
  928. return i;
  929. }
  930. /**
  931. * Create a new configuration using the given configuration as a template;
  932. * ports and paths will be modified to select available ports on the local
  933. * system. The default configuration will be available in PATHS section under
  934. * the option DEFAULTCONFIG after the call. GNUNET_HOME is also set in PATHS
  935. * section to the temporary directory specific to this configuration. If we run
  936. * out of "*port" numbers, return #GNUNET_SYSERR.
  937. *
  938. * This is primarily a helper function used internally
  939. * by 'GNUNET_TESTING_peer_configure'.
  940. *
  941. * @param system system to use to coordinate resource usage
  942. * @param cfg template configuration to update
  943. * @param ports array with port numbers used in the created configuration.
  944. * Will be updated upon successful return. Can be NULL
  945. * @param nports the size of the `ports' array. Will be updated.
  946. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - the configuration will
  947. * be incomplete and should not be used there upon
  948. */
  949. static int
  950. GNUNET_TESTING_configuration_create_ (struct GNUNET_TESTING_System *system,
  951. struct GNUNET_CONFIGURATION_Handle *cfg,
  952. uint16_t **ports,
  953. unsigned int *nports)
  954. {
  955. struct UpdateContext uc;
  956. char *default_config;
  957. uc.system = system;
  958. uc.cfg = cfg;
  959. uc.status = GNUNET_OK;
  960. uc.ports = NULL;
  961. uc.nports = 0;
  962. GNUNET_asprintf (&uc.gnunet_home, "%s/%u", system->tmppath,
  963. system->path_counter++);
  964. GNUNET_asprintf (&default_config, "%s/config", uc.gnunet_home);
  965. GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
  966. default_config);
  967. GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG",
  968. default_config);
  969. GNUNET_free (default_config);
  970. GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "GNUNET_HOME",
  971. uc.gnunet_home);
  972. /* make PORTs and UNIXPATHs unique */
  973. GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
  974. /* allow connections to services from system trusted_ip host */
  975. GNUNET_CONFIGURATION_iterate_sections (cfg, &update_config_sections, &uc);
  976. /* enable loopback-based connections between peers */
  977. GNUNET_CONFIGURATION_set_value_string (cfg,
  978. "nat",
  979. "USE_LOCALADDR", "YES");
  980. GNUNET_free (uc.gnunet_home);
  981. if ((NULL != ports) && (NULL != nports))
  982. {
  983. *ports = uc.ports;
  984. *nports = uc.nports;
  985. }
  986. else
  987. GNUNET_free_non_null (uc.ports);
  988. return uc.status;
  989. }
  990. /**
  991. * Create a new configuration using the given configuration as a template;
  992. * ports and paths will be modified to select available ports on the local
  993. * system. The default configuration will be available in PATHS section under
  994. * the option DEFAULTCONFIG after the call. GNUNET_HOME is also set in PATHS
  995. * section to the temporary directory specific to this configuration. If we run
  996. * out of "*port" numbers, return #GNUNET_SYSERR.
  997. *
  998. * This is primarily a helper function used internally
  999. * by #GNUNET_TESTING_peer_configure().
  1000. *
  1001. * @param system system to use to coordinate resource usage
  1002. * @param cfg template configuration to update
  1003. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - the configuration will
  1004. * be incomplete and should not be used there upon
  1005. */
  1006. int
  1007. GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
  1008. struct GNUNET_CONFIGURATION_Handle *cfg)
  1009. {
  1010. return GNUNET_TESTING_configuration_create_ (system, cfg, NULL, NULL);
  1011. }
  1012. /**
  1013. * Configure a GNUnet peer. GNUnet must be installed on the local
  1014. * system and available in the PATH.
  1015. *
  1016. * @param system system to use to coordinate resource usage
  1017. * @param cfg configuration to use; will be UPDATED (to reflect needed
  1018. * changes in port numbers and paths)
  1019. * @param key_number number of the hostkey to use for the peer
  1020. * @param id identifier for the daemon, will be set, can be NULL
  1021. * @param emsg set to freshly allocated error message (set to NULL on success),
  1022. * can be NULL
  1023. * @return handle to the peer, NULL on error
  1024. */
  1025. struct GNUNET_TESTING_Peer *
  1026. GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
  1027. struct GNUNET_CONFIGURATION_Handle *cfg,
  1028. uint32_t key_number,
  1029. struct GNUNET_PeerIdentity *id,
  1030. char **emsg)
  1031. {
  1032. struct GNUNET_TESTING_Peer *peer;
  1033. struct GNUNET_DISK_FileHandle *fd;
  1034. char *hostkey_filename;
  1035. char *config_filename;
  1036. char *libexec_binary;
  1037. char *emsg_;
  1038. struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
  1039. uint16_t *ports;
  1040. struct SharedService *ss;
  1041. struct SharedServiceInstance **ss_instances;
  1042. unsigned int cnt;
  1043. unsigned int nports;
  1044. ports = NULL;
  1045. nports = 0;
  1046. ss_instances = NULL;
  1047. if (NULL != emsg)
  1048. *emsg = NULL;
  1049. if (key_number >= system->total_hostkeys)
  1050. {
  1051. GNUNET_asprintf (&emsg_,
  1052. _("You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"),
  1053. (unsigned int) system->total_hostkeys);
  1054. goto err_ret;
  1055. }
  1056. pk = NULL;
  1057. if ((NULL != id) &&
  1058. (NULL == (pk = GNUNET_TESTING_hostkey_get (system, key_number, id))))
  1059. {
  1060. GNUNET_asprintf (&emsg_,
  1061. _("Failed to initialize hostkey for peer %u\n"),
  1062. (unsigned int) key_number);
  1063. goto err_ret;
  1064. }
  1065. if (NULL != pk)
  1066. GNUNET_free (pk);
  1067. if (GNUNET_NO ==
  1068. GNUNET_CONFIGURATION_have_value (cfg, "PEER", "PRIVATE_KEY"))
  1069. {
  1070. GNUNET_asprintf (&emsg_,
  1071. _("PRIVATE_KEY option in PEER section missing in configuration\n"));
  1072. goto err_ret;
  1073. }
  1074. /* Remove sections for shared services */
  1075. for (cnt = 0; cnt < system->n_shared_services; cnt++)
  1076. {
  1077. ss = system->shared_services[cnt];
  1078. GNUNET_CONFIGURATION_remove_section (cfg, ss->sname);
  1079. }
  1080. if (GNUNET_OK != GNUNET_TESTING_configuration_create_ (system, cfg,
  1081. &ports, &nports))
  1082. {
  1083. GNUNET_asprintf (&emsg_,
  1084. _("Failed to create configuration for peer "
  1085. "(not enough free ports?)\n"));
  1086. goto err_ret;
  1087. }
  1088. GNUNET_assert (GNUNET_OK ==
  1089. GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER",
  1090. "PRIVATE_KEY",
  1091. &hostkey_filename));
  1092. fd = GNUNET_DISK_file_open (hostkey_filename,
  1093. GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_WRITE,
  1094. GNUNET_DISK_PERM_USER_READ
  1095. | GNUNET_DISK_PERM_USER_WRITE);
  1096. if (NULL == fd)
  1097. {
  1098. GNUNET_asprintf (&emsg_, _("Cannot open hostkey file `%s': %s\n"),
  1099. hostkey_filename, STRERROR (errno));
  1100. GNUNET_free (hostkey_filename);
  1101. goto err_ret;
  1102. }
  1103. GNUNET_free (hostkey_filename);
  1104. if (GNUNET_TESTING_HOSTKEYFILESIZE !=
  1105. GNUNET_DISK_file_write (fd, system->hostkeys_data
  1106. + (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
  1107. GNUNET_TESTING_HOSTKEYFILESIZE))
  1108. {
  1109. GNUNET_asprintf (&emsg_,
  1110. _("Failed to write hostkey file for peer %u: %s\n"),
  1111. (unsigned int) key_number,
  1112. STRERROR (errno));
  1113. GNUNET_DISK_file_close (fd);
  1114. goto err_ret;
  1115. }
  1116. GNUNET_DISK_file_close (fd);
  1117. ss_instances = GNUNET_malloc (sizeof (struct SharedServiceInstance *)
  1118. * system->n_shared_services);
  1119. for (cnt=0; cnt < system->n_shared_services; cnt++)
  1120. {
  1121. ss = system->shared_services[cnt];
  1122. ss_instances[cnt] = associate_shared_service (system, ss, cfg);
  1123. if (NULL == ss_instances[cnt])
  1124. {
  1125. emsg_ = GNUNET_strdup ("FIXME");
  1126. goto err_ret;
  1127. }
  1128. }
  1129. GNUNET_assert (GNUNET_OK ==
  1130. GNUNET_CONFIGURATION_get_value_filename
  1131. (cfg, "PATHS", "DEFAULTCONFIG", &config_filename));
  1132. if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config_filename))
  1133. {
  1134. GNUNET_asprintf (&emsg_,
  1135. _("Failed to write configuration file `%s' for peer %u: %s\n"),
  1136. config_filename,
  1137. (unsigned int) key_number,
  1138. STRERROR (errno));
  1139. GNUNET_free (config_filename);
  1140. goto err_ret;
  1141. }
  1142. peer = GNUNET_new (struct GNUNET_TESTING_Peer);
  1143. peer->ss_instances = ss_instances;
  1144. peer->cfgfile = config_filename; /* Free in peer_destroy */
  1145. peer->cfg = GNUNET_CONFIGURATION_dup (cfg);
  1146. libexec_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  1147. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "arm", "PREFIX", &peer->main_binary))
  1148. {
  1149. /* No prefix */
  1150. GNUNET_asprintf(&peer->main_binary, "%s", libexec_binary);
  1151. peer->args = GNUNET_strdup ("");
  1152. }
  1153. else
  1154. {
  1155. peer->args = GNUNET_strdup (libexec_binary);
  1156. }
  1157. peer->system = system;
  1158. peer->key_number = key_number;
  1159. GNUNET_free (libexec_binary);
  1160. peer->ports = ports; /* Free in peer_destroy */
  1161. peer->nports = nports;
  1162. return peer;
  1163. err_ret:
  1164. GNUNET_free_non_null (ss_instances);
  1165. GNUNET_free_non_null (ports);
  1166. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
  1167. if (NULL != emsg)
  1168. *emsg = emsg_;
  1169. else
  1170. GNUNET_free (emsg_);
  1171. return NULL;
  1172. }
  1173. /**
  1174. * Obtain the peer identity from a peer handle.
  1175. *
  1176. * @param peer peer handle for which we want the peer's identity
  1177. * @param id identifier for the daemon, will be set
  1178. */
  1179. void
  1180. GNUNET_TESTING_peer_get_identity (struct GNUNET_TESTING_Peer *peer,
  1181. struct GNUNET_PeerIdentity *id)
  1182. {
  1183. if (NULL != peer->id)
  1184. {
  1185. GNUNET_memcpy (id, peer->id, sizeof (struct GNUNET_PeerIdentity));
  1186. return;
  1187. }
  1188. peer->id = GNUNET_new (struct GNUNET_PeerIdentity);
  1189. GNUNET_free (GNUNET_TESTING_hostkey_get (peer->system,
  1190. peer->key_number,
  1191. peer->id));
  1192. GNUNET_memcpy (id, peer->id, sizeof (struct GNUNET_PeerIdentity));
  1193. }
  1194. /**
  1195. * Start the peer.
  1196. *
  1197. * @param peer peer to start
  1198. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (i.e. peer already running)
  1199. */
  1200. int
  1201. GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer)
  1202. {
  1203. struct SharedServiceInstance *i;
  1204. unsigned int cnt;
  1205. if (NULL != peer->main_process)
  1206. {
  1207. GNUNET_break (0);
  1208. return GNUNET_SYSERR;
  1209. }
  1210. GNUNET_assert (NULL != peer->cfgfile);
  1211. for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
  1212. {
  1213. i = peer->ss_instances[cnt];
  1214. if ((0 == i->n_refs)
  1215. && (GNUNET_SYSERR == start_shared_service_instance (i)) )
  1216. return GNUNET_SYSERR;
  1217. i->n_refs++;
  1218. }
  1219. peer->main_binary = GNUNET_CONFIGURATION_expand_dollar (peer->cfg, peer->main_binary);
  1220. peer->main_process = GNUNET_OS_start_process_s (PIPE_CONTROL,
  1221. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  1222. NULL,
  1223. peer->main_binary,
  1224. peer->args,
  1225. "-c",
  1226. peer->cfgfile,
  1227. NULL);
  1228. if (NULL == peer->main_process)
  1229. {
  1230. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1231. _("Failed to start `%s': %s\n"),
  1232. peer->main_binary,
  1233. STRERROR (errno));
  1234. return GNUNET_SYSERR;
  1235. }
  1236. return GNUNET_OK;
  1237. }
  1238. /**
  1239. * Sends SIGTERM to the peer's main process
  1240. *
  1241. * @param peer the handle to the peer
  1242. * @return #GNUNET_OK if successful; #GNUNET_SYSERR if the main process is NULL
  1243. * or upon any error while sending SIGTERM
  1244. */
  1245. int
  1246. GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer)
  1247. {
  1248. struct SharedServiceInstance *i;
  1249. unsigned int cnt;
  1250. if (NULL == peer->main_process)
  1251. {
  1252. GNUNET_break (0);
  1253. return GNUNET_SYSERR;
  1254. }
  1255. if (0 != GNUNET_OS_process_kill (peer->main_process, GNUNET_TERM_SIG))
  1256. return GNUNET_SYSERR;
  1257. for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
  1258. {
  1259. i = peer->ss_instances[cnt];
  1260. GNUNET_assert (0 != i->n_refs);
  1261. i->n_refs--;
  1262. if (0 == i->n_refs)
  1263. stop_shared_service_instance (i);
  1264. }
  1265. return GNUNET_OK;
  1266. }
  1267. /**
  1268. * Waits for a peer to terminate. The peer's main process will also be destroyed.
  1269. *
  1270. * @param peer the handle to the peer
  1271. * @return #GNUNET_OK if successful; #GNUNET_SYSERR if the main process is NULL
  1272. * or upon any error while waiting
  1273. */
  1274. int
  1275. GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer)
  1276. {
  1277. int ret;
  1278. if (NULL == peer->main_process)
  1279. {
  1280. GNUNET_break (0);
  1281. return GNUNET_SYSERR;
  1282. }
  1283. ret = GNUNET_OS_process_wait (peer->main_process);
  1284. GNUNET_OS_process_destroy (peer->main_process);
  1285. peer->main_process = NULL;
  1286. return ret;
  1287. }
  1288. /**
  1289. * Stop the peer.
  1290. *
  1291. * @param peer peer to stop
  1292. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  1293. */
  1294. int
  1295. GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer)
  1296. {
  1297. if (GNUNET_SYSERR == GNUNET_TESTING_peer_kill (peer))
  1298. return GNUNET_SYSERR;
  1299. if (GNUNET_SYSERR == GNUNET_TESTING_peer_wait (peer))
  1300. return GNUNET_SYSERR;
  1301. return GNUNET_OK;
  1302. }
  1303. /**
  1304. * Function called whenever we connect to or disconnect from ARM.
  1305. *
  1306. * @param cls closure
  1307. * @param connected #GNUNET_YES if connected, #GNUNET_NO if disconnected,
  1308. * #GNUNET_SYSERR on error.
  1309. */
  1310. static void
  1311. disconn_status (void *cls,
  1312. int connected)
  1313. {
  1314. struct GNUNET_TESTING_Peer *peer = cls;
  1315. if (GNUNET_SYSERR == connected)
  1316. {
  1317. peer->cb (peer->cb_cls, peer, connected);
  1318. return;
  1319. }
  1320. if (GNUNET_YES == connected)
  1321. {
  1322. GNUNET_break (GNUNET_OK == GNUNET_TESTING_peer_kill (peer));
  1323. return;
  1324. }
  1325. GNUNET_break (GNUNET_OK == GNUNET_TESTING_peer_wait (peer));
  1326. GNUNET_ARM_disconnect (peer->ah);
  1327. peer->ah = NULL;
  1328. peer->cb (peer->cb_cls, peer, GNUNET_YES);
  1329. }
  1330. /**
  1331. * Stop a peer asynchronously using ARM API. Peer's shutdown is signaled
  1332. * through the GNUNET_TESTING_PeerStopCallback().
  1333. *
  1334. * @param peer the peer to stop
  1335. * @param cb the callback to signal peer shutdown
  1336. * @param cb_cls closure for the above callback
  1337. * @return #GNUNET_OK upon successfully giving the request to the ARM API (this
  1338. * does not mean that the peer is successfully stopped); #GNUNET_SYSERR
  1339. * upon any error.
  1340. */
  1341. int
  1342. GNUNET_TESTING_peer_stop_async (struct GNUNET_TESTING_Peer *peer,
  1343. GNUNET_TESTING_PeerStopCallback cb,
  1344. void *cb_cls)
  1345. {
  1346. if (NULL == peer->main_process)
  1347. return GNUNET_SYSERR;
  1348. peer->ah = GNUNET_ARM_connect (peer->cfg, &disconn_status, peer);
  1349. if (NULL == peer->ah)
  1350. return GNUNET_SYSERR;
  1351. peer->cb = cb;
  1352. peer->cb_cls = cb_cls;
  1353. return GNUNET_OK;
  1354. }
  1355. /**
  1356. * Cancel a previous asynchronous peer stop request.
  1357. * GNUNET_TESTING_peer_stop_async() should have been called before on the given
  1358. * peer. It is an error to call this function if the peer stop callback was
  1359. * already called
  1360. *
  1361. * @param peer the peer on which GNUNET_TESTING_peer_stop_async() was called
  1362. * before.
  1363. */
  1364. void
  1365. GNUNET_TESTING_peer_stop_async_cancel (struct GNUNET_TESTING_Peer *peer)
  1366. {
  1367. GNUNET_assert (NULL != peer->ah);
  1368. GNUNET_ARM_disconnect (peer->ah);
  1369. peer->ah = NULL;
  1370. }
  1371. /**
  1372. * Destroy the peer. Releases resources locked during peer configuration.
  1373. * If the peer is still running, it will be stopped AND a warning will be
  1374. * printed (users of the API should stop the peer explicitly first).
  1375. *
  1376. * @param peer peer to destroy
  1377. */
  1378. void
  1379. GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer)
  1380. {
  1381. unsigned int cnt;
  1382. if (NULL != peer->main_process)
  1383. GNUNET_TESTING_peer_stop (peer);
  1384. if (NULL != peer->ah)
  1385. GNUNET_ARM_disconnect (peer->ah);
  1386. GNUNET_free (peer->cfgfile);
  1387. if (NULL != peer->cfg)
  1388. GNUNET_CONFIGURATION_destroy (peer->cfg);
  1389. GNUNET_free (peer->main_binary);
  1390. GNUNET_free (peer->args);
  1391. GNUNET_free_non_null (peer->id);
  1392. GNUNET_free_non_null (peer->ss_instances);
  1393. if (NULL != peer->ports)
  1394. {
  1395. for (cnt = 0; cnt < peer->nports; cnt++)
  1396. GNUNET_TESTING_release_port (peer->system, peer->ports[cnt]);
  1397. GNUNET_free (peer->ports);
  1398. }
  1399. GNUNET_free (peer);
  1400. }
  1401. /**
  1402. * Start a single peer and run a test using the testing library.
  1403. * Starts a peer using the given configuration and then invokes the
  1404. * given callback. This function ALSO initializes the scheduler loop
  1405. * and should thus be called directly from "main". The testcase
  1406. * should self-terminate by invoking #GNUNET_SCHEDULER_shutdown().
  1407. *
  1408. * @param testdir only the directory name without any path. This is used for
  1409. * all service homes; the directory will be created in a temporary
  1410. * location depending on the underlying OS
  1411. * @param cfgfilename name of the configuration file to use;
  1412. * use NULL to only run with defaults
  1413. * @param tm main function of the testcase
  1414. * @param tm_cls closure for @a tm
  1415. * @return 0 on success, 1 on error
  1416. */
  1417. int
  1418. GNUNET_TESTING_peer_run (const char *testdir,
  1419. const char *cfgfilename,
  1420. GNUNET_TESTING_TestMain tm,
  1421. void *tm_cls)
  1422. {
  1423. return GNUNET_TESTING_service_run (testdir, "arm",
  1424. cfgfilename, tm, tm_cls);
  1425. }
  1426. /**
  1427. * Structure for holding service data
  1428. */
  1429. struct ServiceContext
  1430. {
  1431. /**
  1432. * The configuration of the peer in which the service is run
  1433. */
  1434. const struct GNUNET_CONFIGURATION_Handle *cfg;
  1435. /**
  1436. * Callback to signal service startup
  1437. */
  1438. GNUNET_TESTING_TestMain tm;
  1439. /**
  1440. * The peer in which the service is run.
  1441. */
  1442. struct GNUNET_TESTING_Peer *peer;
  1443. /**
  1444. * Closure for the above callback
  1445. */
  1446. void *tm_cls;
  1447. };
  1448. /**
  1449. * Callback to be called when SCHEDULER has been started
  1450. *
  1451. * @param cls the ServiceContext
  1452. */
  1453. static void
  1454. service_run_main (void *cls)
  1455. {
  1456. struct ServiceContext *sc = cls;
  1457. sc->tm (sc->tm_cls, sc->cfg, sc->peer);
  1458. }
  1459. /**
  1460. * Start a single service (no ARM, except of course if the given
  1461. * service name is 'arm') and run a test using the testing library.
  1462. * Starts a service using the given configuration and then invokes the
  1463. * given callback. This function ALSO initializes the scheduler loop
  1464. * and should thus be called directly from "main". The testcase
  1465. * should self-terminate by invoking #GNUNET_SCHEDULER_shutdown().
  1466. *
  1467. * This function is useful if the testcase is for a single service
  1468. * and if that service doesn't itself depend on other services.
  1469. *
  1470. * @param testdir only the directory name without any path. This is used for
  1471. * all service homes; the directory will be created in a temporary
  1472. * location depending on the underlying OS
  1473. * @param service_name name of the service to run
  1474. * @param cfgfilename name of the configuration file to use;
  1475. * use NULL to only run with defaults
  1476. * @param tm main function of the testcase
  1477. * @param tm_cls closure for @a tm
  1478. * @return 0 on success, 1 on error
  1479. */
  1480. int
  1481. GNUNET_TESTING_service_run (const char *testdir,
  1482. const char *service_name,
  1483. const char *cfgfilename,
  1484. GNUNET_TESTING_TestMain tm,
  1485. void *tm_cls)
  1486. {
  1487. struct ServiceContext sc;
  1488. struct GNUNET_TESTING_System *system;
  1489. struct GNUNET_TESTING_Peer *peer;
  1490. struct GNUNET_CONFIGURATION_Handle *cfg;
  1491. char *binary;
  1492. char *libexec_binary;
  1493. GNUNET_log_setup (testdir,
  1494. "WARNING",
  1495. NULL);
  1496. system = GNUNET_TESTING_system_create (testdir, "127.0.0.1", NULL, NULL);
  1497. if (NULL == system)
  1498. return 1;
  1499. cfg = GNUNET_CONFIGURATION_create ();
  1500. if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgfilename))
  1501. {
  1502. LOG (GNUNET_ERROR_TYPE_ERROR,
  1503. _("Failed to load configuration from %s\n"), cfgfilename);
  1504. GNUNET_CONFIGURATION_destroy (cfg);
  1505. GNUNET_TESTING_system_destroy (system, GNUNET_YES);
  1506. return 1;
  1507. }
  1508. peer = GNUNET_TESTING_peer_configure (system, cfg, 0, NULL, NULL);
  1509. if (NULL == peer)
  1510. {
  1511. GNUNET_CONFIGURATION_destroy (cfg);
  1512. hostkeys_unload (system);
  1513. GNUNET_TESTING_system_destroy (system, GNUNET_YES);
  1514. return 1;
  1515. }
  1516. GNUNET_free (peer->main_binary);
  1517. GNUNET_free (peer->args);
  1518. GNUNET_asprintf (&binary, "gnunet-service-%s", service_name);
  1519. libexec_binary = GNUNET_OS_get_libexec_binary_path (binary);
  1520. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, service_name, "PREFIX", &peer->main_binary))
  1521. {
  1522. /* No prefix */
  1523. GNUNET_asprintf(&peer->main_binary, "%s", libexec_binary);
  1524. peer->args = GNUNET_strdup ("");
  1525. }
  1526. else
  1527. peer->args = GNUNET_strdup (libexec_binary);
  1528. GNUNET_free (libexec_binary);
  1529. GNUNET_free (binary);
  1530. if (GNUNET_OK != GNUNET_TESTING_peer_start (peer))
  1531. {
  1532. GNUNET_TESTING_peer_destroy (peer);
  1533. GNUNET_CONFIGURATION_destroy (cfg);
  1534. GNUNET_TESTING_system_destroy (system, GNUNET_YES);
  1535. return 1;
  1536. }
  1537. sc.cfg = cfg;
  1538. sc.tm = tm;
  1539. sc.tm_cls = tm_cls;
  1540. sc.peer = peer;
  1541. GNUNET_SCHEDULER_run (&service_run_main, &sc); /* Scheduler loop */
  1542. if ((NULL != peer->main_process) &&
  1543. (GNUNET_OK != GNUNET_TESTING_peer_stop (peer)))
  1544. {
  1545. GNUNET_TESTING_peer_destroy (peer);
  1546. GNUNET_CONFIGURATION_destroy (cfg);
  1547. GNUNET_TESTING_system_destroy (system, GNUNET_YES);
  1548. return 1;
  1549. }
  1550. GNUNET_TESTING_peer_destroy (peer);
  1551. GNUNET_CONFIGURATION_destroy (cfg);
  1552. GNUNET_TESTING_system_destroy (system, GNUNET_YES);
  1553. return 0;
  1554. }
  1555. /**
  1556. * Sometimes we use the binary name to determine which specific
  1557. * test to run. In those cases, the string after the last "_"
  1558. * in 'argv[0]' specifies a string that determines the configuration
  1559. * file or plugin to use.
  1560. *
  1561. * This function returns the respective substring, taking care
  1562. * of issues such as binaries ending in '.exe' on W32.
  1563. *
  1564. * @param argv0 the name of the binary
  1565. * @return string between the last '_' and the '.exe' (or the end of the string),
  1566. * NULL if argv0 has no '_'
  1567. */
  1568. char *
  1569. GNUNET_TESTING_get_testname_from_underscore (const char *argv0)
  1570. {
  1571. size_t slen = strlen (argv0) + 1;
  1572. char sbuf[slen];
  1573. char *ret;
  1574. char *dot;
  1575. GNUNET_memcpy (sbuf, argv0, slen);
  1576. ret = strrchr (sbuf, '_');
  1577. if (NULL == ret)
  1578. return NULL;
  1579. ret++; /* skip underscore */
  1580. dot = strchr (ret, '.');
  1581. if (NULL != dot)
  1582. *dot = '\0';
  1583. return GNUNET_strdup (ret);
  1584. }
  1585. /* end of testing.c */