testing.c 54 KB

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