gnunet-transport.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2011-2014 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file src/transport/gnunet-transport.c
  19. * @brief Tool to help configure, measure and control the transport subsystem.
  20. * @author Christian Grothoff
  21. * @author Nathan Evans
  22. *
  23. * This utility can be used to test if a transport mechanism for
  24. * GNUnet is properly configured.
  25. */
  26. #include "platform.h"
  27. #include "gnunet_util_lib.h"
  28. #include "gnunet_resolver_service.h"
  29. #include "gnunet_protocols.h"
  30. #include "gnunet_transport_service.h"
  31. #include "gnunet_nat_lib.h"
  32. /**
  33. * How long do we wait for the NAT test to report success?
  34. * Should match NAT_SERVER_TIMEOUT in 'nat_test.c'.
  35. */
  36. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
  37. /**
  38. * Timeout for a name resolution
  39. */
  40. #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  41. /**
  42. * Timeout for an operations
  43. */
  44. #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  45. /**
  46. * Context to store name resolutions for valiation
  47. */
  48. struct ValidationResolutionContext
  49. {
  50. /**
  51. * Next in DLL
  52. */
  53. struct ValidationResolutionContext *next;
  54. /**
  55. * Previous in DLL
  56. */
  57. struct ValidationResolutionContext *prev;
  58. /**
  59. * Peer identity
  60. */
  61. struct GNUNET_PeerIdentity id;
  62. /**
  63. * Address to resolve
  64. */
  65. struct GNUNET_HELLO_Address *addrcp;
  66. /**
  67. * Time of last validation
  68. */
  69. struct GNUNET_TIME_Absolute last_validation;
  70. /**
  71. * Address is valid until
  72. */
  73. struct GNUNET_TIME_Absolute valid_until;
  74. /**
  75. * Time of next validation
  76. */
  77. struct GNUNET_TIME_Absolute next_validation;
  78. /**
  79. * state of validation process
  80. */
  81. enum GNUNET_TRANSPORT_ValidationState state;
  82. /**
  83. * Tranport conversion handle
  84. */
  85. struct GNUNET_TRANSPORT_AddressToStringContext *asc;
  86. /**
  87. * plugin name
  88. */
  89. char *transport;
  90. /**
  91. * was the entry printed
  92. */
  93. int printed;
  94. };
  95. /**
  96. * Struct to store information about peers in monitor mode
  97. */
  98. struct MonitoredPeer
  99. {
  100. /**
  101. * State of the peer
  102. */
  103. enum GNUNET_TRANSPORT_PeerState state;
  104. /**
  105. * Timeout
  106. */
  107. struct GNUNET_TIME_Absolute state_timeout;
  108. /**
  109. * The address to convert
  110. */
  111. struct GNUNET_HELLO_Address *address;
  112. };
  113. /**
  114. * Context to store name resolutions for valiation
  115. */
  116. struct PeerResolutionContext
  117. {
  118. /**
  119. * Next in DLL
  120. */
  121. struct PeerResolutionContext *next;
  122. /**
  123. * Prev in DLL
  124. */
  125. struct PeerResolutionContext *prev;
  126. /**
  127. * The peer id
  128. */
  129. struct GNUNET_PeerIdentity id;
  130. /**
  131. * address to resolve
  132. */
  133. struct GNUNET_HELLO_Address *addrcp;
  134. /**
  135. * transport conversiion context
  136. */
  137. struct GNUNET_TRANSPORT_AddressToStringContext *asc;
  138. /**
  139. * peer state
  140. */
  141. enum GNUNET_TRANSPORT_PeerState state;
  142. /**
  143. * state timeout
  144. */
  145. struct GNUNET_TIME_Absolute state_timeout;
  146. /**
  147. * transport plugin
  148. */
  149. char *transport;
  150. /**
  151. * was the entry printed
  152. */
  153. int printed;
  154. };
  155. /**
  156. * Context for a plugin test.
  157. */
  158. struct TestContext
  159. {
  160. /**
  161. * Previous in DLL
  162. */
  163. struct TestContext *prev;
  164. /**
  165. * Next in DLL
  166. */
  167. struct TestContext *next;
  168. /**
  169. * Handle to the active NAT test.
  170. */
  171. struct GNUNET_NAT_Test *tst;
  172. /**
  173. * Task identifier for the timeout.
  174. */
  175. GNUNET_SCHEDULER_TaskIdentifier tsk;
  176. /**
  177. * Name of plugin under test.
  178. */
  179. char *name;
  180. /**
  181. * Bound port
  182. */
  183. unsigned long long bnd_port;
  184. /**
  185. * Advertised ports
  186. */
  187. unsigned long long adv_port;
  188. };
  189. /**
  190. *
  191. */
  192. enum TestResult
  193. {
  194. /**
  195. * NAT returned success
  196. */
  197. NAT_TEST_SUCCESS = GNUNET_OK,
  198. /**
  199. * NAT returned failure
  200. */
  201. NAT_TEST_FAIL = GNUNET_NO,
  202. /**
  203. * NAT returned failure while running test
  204. */
  205. NAT_TEST_INTERNAL_FAIL = GNUNET_SYSERR,
  206. /**
  207. * We could not start the test
  208. */
  209. NAT_TEST_FAILED_TO_START = 2,
  210. /**
  211. * We had a timeout while running the test
  212. */
  213. NAT_TEST_TIMEOUT = 3
  214. };
  215. /**
  216. * Benchmarking block size in KB
  217. */
  218. #define BLOCKSIZE 4
  219. /**
  220. * Which peer should we connect to?
  221. */
  222. static char *cpid;
  223. /**
  224. * Handle to transport service.
  225. */
  226. static struct GNUNET_TRANSPORT_Handle *handle;
  227. /**
  228. * Configuration handle
  229. */
  230. static struct GNUNET_CONFIGURATION_Handle *cfg;
  231. /**
  232. * Try_connect handle
  233. */
  234. struct GNUNET_TRANSPORT_TryConnectHandle *tc_handle;
  235. /**
  236. * Option -s.
  237. */
  238. static int benchmark_send;
  239. /**
  240. * Option -b.
  241. */
  242. static int benchmark_receive;
  243. /**
  244. * Option -l.
  245. */
  246. static int benchmark_receive;
  247. /**
  248. * Option -i.
  249. */
  250. static int iterate_connections;
  251. /**
  252. * Option -d.
  253. */
  254. static int iterate_validation;
  255. /**
  256. * Option -a.
  257. */
  258. static int iterate_all;
  259. /**
  260. * Option -t.
  261. */
  262. static int test_configuration;
  263. /**
  264. * Option -c.
  265. */
  266. static int monitor_connects;
  267. /**
  268. * Option -m.
  269. */
  270. static int monitor_connections;
  271. /**
  272. * Option -f.
  273. */
  274. static int monitor_validation;
  275. /**
  276. * Option -P.
  277. */
  278. static int monitor_plugins;
  279. /**
  280. * Option -C.
  281. */
  282. static int try_connect;
  283. /**
  284. * Option -D.
  285. */
  286. static int try_disconnect;
  287. /**
  288. * Option -n.
  289. */
  290. static int numeric;
  291. /**
  292. * Global return value (0 success).
  293. */
  294. static int ret;
  295. /**
  296. * Current number of connections in monitor mode
  297. */
  298. static int monitor_connect_counter;
  299. /**
  300. * Number of bytes of traffic we received so far.
  301. */
  302. static unsigned long long traffic_received;
  303. /**
  304. * Number of bytes of traffic we sent so far.
  305. */
  306. static unsigned long long traffic_sent;
  307. /**
  308. * Starting time of transmitting/receiving data.
  309. */
  310. static struct GNUNET_TIME_Absolute start_time;
  311. /**
  312. * Handle for current transmission request.
  313. */
  314. static struct GNUNET_TRANSPORT_TransmitHandle *th;
  315. /**
  316. * Map storing information about monitored peers
  317. */
  318. static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
  319. /**
  320. * Map storing information about monitored plugins's sessions.
  321. */
  322. static struct GNUNET_CONTAINER_MultiPeerMap *monitored_plugins;
  323. /**
  324. * Handle if we are monitoring peers at the transport level.
  325. */
  326. static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
  327. /**
  328. * Handle if we are monitoring transport validation activity.
  329. */
  330. static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic;
  331. /**
  332. * Handle if we are monitoring plugin session activity.
  333. */
  334. static struct GNUNET_TRANSPORT_PluginMonitor *pm;
  335. /**
  336. * Identity of the peer we transmit to / connect to.
  337. * (equivalent to 'cpid' string).
  338. */
  339. static struct GNUNET_PeerIdentity pid;
  340. /**
  341. * Task scheduled for cleanup / termination of the process.
  342. */
  343. static GNUNET_SCHEDULER_TaskIdentifier end;
  344. /**
  345. * Task for operation timeout
  346. */
  347. static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
  348. /**
  349. * Selected level of verbosity.
  350. */
  351. static int verbosity;
  352. /**
  353. * Resolver process handle.
  354. */
  355. struct GNUNET_OS_Process *resolver;
  356. /**
  357. * Number of address resolutions pending
  358. */
  359. static unsigned int address_resolutions;
  360. /**
  361. * Address resolutions pending in progress
  362. */
  363. static unsigned int address_resolution_in_progress;
  364. /**
  365. * DLL for NAT Test Contexts: head
  366. */
  367. struct TestContext *head;
  368. /**
  369. * DLL for NAT Test Contexts: tail
  370. */
  371. struct TestContext *tail;
  372. /**
  373. * DLL: head of validation resolution entries
  374. */
  375. static struct ValidationResolutionContext *vc_head;
  376. /**
  377. * DLL: tail of validation resolution entries
  378. */
  379. static struct ValidationResolutionContext *vc_tail;
  380. /**
  381. * DLL: head of resolution entries
  382. */
  383. static struct PeerResolutionContext *rc_head;
  384. /**
  385. * DLL: head of resolution entries
  386. */
  387. static struct PeerResolutionContext *rc_tail;
  388. /**
  389. * Function called to release data stored in the #monitored_peers map.
  390. *
  391. * @param cls unused
  392. * @param key the peer identity
  393. * @param value a `struct MonitoredPeer` to release
  394. * @return #GNUNET_OK (continue to iterate)
  395. */
  396. static int
  397. destroy_it (void *cls,
  398. const struct GNUNET_PeerIdentity *key,
  399. void *value)
  400. {
  401. struct MonitoredPeer *m = value;
  402. GNUNET_assert (GNUNET_OK ==
  403. GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
  404. key,
  405. value));
  406. GNUNET_free_non_null (m->address);
  407. GNUNET_free (value);
  408. return GNUNET_OK;
  409. }
  410. /**
  411. * Task run in monitor mode when the user presses CTRL-C to abort.
  412. * Stops monitoring activity.
  413. *
  414. * @param cls NULL
  415. * @param tc scheduler context
  416. */
  417. static void
  418. shutdown_task (void *cls,
  419. const struct GNUNET_SCHEDULER_TaskContext *tc)
  420. {
  421. struct GNUNET_TIME_Relative duration;
  422. struct ValidationResolutionContext *cur;
  423. struct ValidationResolutionContext *next;
  424. end = GNUNET_SCHEDULER_NO_TASK;
  425. if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
  426. {
  427. GNUNET_SCHEDULER_cancel (op_timeout);
  428. op_timeout = GNUNET_SCHEDULER_NO_TASK;
  429. }
  430. if (NULL != tc_handle)
  431. {
  432. GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
  433. tc_handle = NULL;
  434. }
  435. if (NULL != pic)
  436. {
  437. GNUNET_TRANSPORT_monitor_peers_cancel (pic);
  438. pic = NULL;
  439. }
  440. if (NULL != vic)
  441. {
  442. GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
  443. vic = NULL;
  444. }
  445. if (NULL != pm)
  446. {
  447. GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
  448. pm = NULL;
  449. }
  450. next = vc_head;
  451. for (cur = next; NULL != cur; cur = next)
  452. {
  453. next = cur->next;
  454. GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
  455. GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, cur);
  456. GNUNET_free (cur->transport);
  457. GNUNET_HELLO_address_free (cur->addrcp);
  458. GNUNET_free (cur);
  459. }
  460. if (NULL != th)
  461. {
  462. GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
  463. th = NULL;
  464. }
  465. if (NULL != handle)
  466. {
  467. GNUNET_TRANSPORT_disconnect (handle);
  468. handle = NULL;
  469. }
  470. if (benchmark_send)
  471. {
  472. duration = GNUNET_TIME_absolute_get_duration (start_time);
  473. FPRINTF (stdout,
  474. _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
  475. 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
  476. traffic_sent,
  477. GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
  478. }
  479. if (benchmark_receive)
  480. {
  481. duration = GNUNET_TIME_absolute_get_duration (start_time);
  482. FPRINTF (stdout,
  483. _("Received %llu bytes/s (%llu bytes in %s)\n"),
  484. 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
  485. traffic_received,
  486. GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
  487. }
  488. if (NULL != monitored_peers)
  489. {
  490. GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
  491. GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
  492. monitored_peers = NULL;
  493. }
  494. if (NULL != monitored_plugins)
  495. {
  496. GNUNET_break (0 ==
  497. GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
  498. GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
  499. monitored_plugins = NULL;
  500. }
  501. }
  502. static void
  503. operation_timeout (void *cls,
  504. const struct GNUNET_SCHEDULER_TaskContext *tc)
  505. {
  506. struct PeerResolutionContext *cur;
  507. struct PeerResolutionContext *next;
  508. op_timeout = GNUNET_SCHEDULER_NO_TASK;
  509. if ((try_connect) || (benchmark_send) || (benchmark_receive))
  510. {
  511. FPRINTF (stdout,
  512. _("Failed to connect to `%s'\n"),
  513. GNUNET_i2s_full (&pid));
  514. if (GNUNET_SCHEDULER_NO_TASK != end)
  515. GNUNET_SCHEDULER_cancel (end);
  516. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  517. ret = 1;
  518. return;
  519. }
  520. if (iterate_connections)
  521. {
  522. next = rc_head;
  523. while (NULL != (cur = next))
  524. {
  525. next = cur->next;
  526. FPRINTF (stdout,
  527. _("Failed to resolve address for peer `%s'\n"),
  528. GNUNET_i2s (&cur->addrcp->peer));
  529. GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
  530. GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
  531. GNUNET_free(cur->transport);
  532. GNUNET_free(cur->addrcp);
  533. GNUNET_free(cur);
  534. }
  535. FPRINTF (stdout,
  536. "%s",
  537. _("Failed to list connections, timeout occured\n"));
  538. if (GNUNET_SCHEDULER_NO_TASK != end)
  539. GNUNET_SCHEDULER_cancel (end);
  540. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  541. ret = 1;
  542. return;
  543. }
  544. }
  545. static void
  546. run_nat_test ();
  547. /**
  548. * Display the result of the test.
  549. *
  550. * @param tc test context
  551. * @param result #GNUNET_YES on success
  552. */
  553. static void
  554. display_test_result (struct TestContext *tc,
  555. enum TestResult result)
  556. {
  557. switch (result) {
  558. case NAT_TEST_FAIL:
  559. FPRINTF (stderr,
  560. _("Configuration for plugin `%s' did not work!\n"),
  561. tc->name);
  562. break;
  563. case NAT_TEST_SUCCESS:
  564. FPRINTF (stderr,
  565. _("Configuration for plugin `%s' did work!\n"),
  566. tc->name);
  567. break;
  568. case NAT_TEST_INTERNAL_FAIL:
  569. FPRINTF (stderr,
  570. _("Internal NAT error while running test for plugin `%s'\n"),
  571. tc->name);
  572. break;
  573. case NAT_TEST_FAILED_TO_START:
  574. FPRINTF (stderr,
  575. _("Failed to start NAT test for plugin `%s'\n"),
  576. tc->name);
  577. break;
  578. case NAT_TEST_TIMEOUT:
  579. FPRINTF (stderr,
  580. _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
  581. tc->name);
  582. break;
  583. default:
  584. break;
  585. }
  586. if (GNUNET_YES != result)
  587. {
  588. FPRINTF (stderr,
  589. _("Configuration for plugin `%s' did not work!\n"),
  590. tc->name);
  591. }
  592. else
  593. {
  594. FPRINTF (stderr,
  595. _("Configuration for plugin `%s' is working!\n"),
  596. tc->name);
  597. }
  598. if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
  599. {
  600. GNUNET_SCHEDULER_cancel (tc->tsk);
  601. tc->tsk = GNUNET_SCHEDULER_NO_TASK;
  602. }
  603. if (NULL != tc->tst)
  604. {
  605. GNUNET_NAT_test_stop (tc->tst);
  606. tc->tst = NULL;
  607. }
  608. GNUNET_CONTAINER_DLL_remove (head, tail, tc);
  609. GNUNET_free (tc->name);
  610. GNUNET_free (tc);
  611. if ((NULL == head) && (NULL != resolver))
  612. {
  613. GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
  614. GNUNET_TERM_SIG));
  615. GNUNET_OS_process_destroy (resolver);
  616. resolver = NULL;
  617. }
  618. if (NULL != head)
  619. run_nat_test ();
  620. }
  621. /**
  622. * Function called by NAT to report the outcome of the nat-test.
  623. * Clean up and update GUI.
  624. *
  625. * @param cls test context
  626. * @param result status code
  627. */
  628. static void
  629. result_callback (void *cls,
  630. enum GNUNET_NAT_StatusCode result)
  631. {
  632. struct TestContext *tc = cls;
  633. display_test_result (tc, result);
  634. }
  635. /**
  636. * Resolve address we got a validation state for to a string.
  637. *
  638. * @param id peer identity the address is for
  639. * @param address the address itself
  640. * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
  641. * @param last_validation when was the address validated last
  642. * @param valid_until until when is the address valid
  643. * @param next_validation when will we try to revalidate the address next
  644. * @param state where are we in the validation state machine
  645. */
  646. static void
  647. resolve_validation_address (const struct GNUNET_PeerIdentity *id,
  648. const struct GNUNET_HELLO_Address *address,
  649. int numeric,
  650. struct GNUNET_TIME_Absolute last_validation,
  651. struct GNUNET_TIME_Absolute valid_until,
  652. struct GNUNET_TIME_Absolute next_validation,
  653. enum GNUNET_TRANSPORT_ValidationState state);
  654. /**
  655. * Function to call with a textual representation of an address. This
  656. * function will be called several times with different possible
  657. * textual representations, and a last time with @a address being NULL
  658. * to signal the end of the iteration. Note that @a address NULL
  659. * always is the last call, regardless of the value in @a res.
  660. *
  661. * @param cls closure
  662. * @param address NULL on end of iteration,
  663. * otherwise 0-terminated printable UTF-8 string,
  664. * in particular an empty string if @a res is #GNUNET_NO
  665. * @param res result of the address to string conversion:
  666. * if #GNUNET_OK: conversion successful
  667. * if #GNUNET_NO: address was invalid (or not supported)
  668. * if #GNUNET_SYSERR: communication error (IPC error)
  669. */
  670. static void
  671. process_validation_string (void *cls,
  672. const char *address,
  673. int res)
  674. {
  675. struct ValidationResolutionContext *vc = cls;
  676. char *s_valid;
  677. char *s_last;
  678. char *s_next;
  679. if (NULL != address)
  680. {
  681. if (GNUNET_SYSERR == res)
  682. {
  683. FPRINTF (stderr,
  684. "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
  685. GNUNET_i2s (&vc->id),
  686. vc->addrcp->transport_name,
  687. (unsigned int) vc->addrcp->address_length);
  688. }
  689. if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
  690. s_valid = GNUNET_strdup ("never");
  691. else
  692. s_valid = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
  693. if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
  694. s_last = GNUNET_strdup ("never");
  695. else
  696. s_last = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
  697. if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
  698. s_next = GNUNET_strdup ("never");
  699. else
  700. s_next = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
  701. FPRINTF (stdout,
  702. _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
  703. GNUNET_i2s (&vc->id),
  704. (GNUNET_OK == res) ? address : "<invalid address>",
  705. (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
  706. "Valid until : ", s_valid,
  707. "Last validation: ",s_last,
  708. "Next validation: ", s_next);
  709. GNUNET_free (s_valid);
  710. GNUNET_free (s_last);
  711. GNUNET_free (s_next);
  712. vc->printed = GNUNET_YES;
  713. return;
  714. }
  715. /* last call, we are done */
  716. GNUNET_assert (address_resolutions > 0);
  717. address_resolutions--;
  718. if ( (GNUNET_SYSERR == res) &&
  719. (GNUNET_NO == vc->printed))
  720. {
  721. if (numeric == GNUNET_NO)
  722. {
  723. /* Failed to resolve address, try numeric lookup
  724. (note: this should be unnecessary, as
  725. transport should fallback to numeric lookup
  726. internally if DNS takes too long anyway) */
  727. resolve_validation_address (&vc->id,
  728. vc->addrcp,
  729. GNUNET_NO,
  730. vc->last_validation,
  731. vc->valid_until,
  732. vc->next_validation,
  733. vc->state);
  734. }
  735. else
  736. {
  737. FPRINTF (stdout,
  738. _("Peer `%s' %s `%s' \n"),
  739. GNUNET_i2s (&vc->id),
  740. "<unable to resolve address>",
  741. GNUNET_TRANSPORT_vs2s (vc->state));
  742. }
  743. }
  744. GNUNET_free (vc->transport);
  745. GNUNET_free (vc->addrcp);
  746. GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, vc);
  747. GNUNET_free (vc);
  748. if ((0 == address_resolutions) && (iterate_validation))
  749. {
  750. if (GNUNET_SCHEDULER_NO_TASK != end)
  751. {
  752. GNUNET_SCHEDULER_cancel (end);
  753. end = GNUNET_SCHEDULER_NO_TASK;
  754. }
  755. if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
  756. {
  757. GNUNET_SCHEDULER_cancel (op_timeout);
  758. op_timeout = GNUNET_SCHEDULER_NO_TASK;
  759. }
  760. ret = 0;
  761. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  762. }
  763. }
  764. /**
  765. * Resolve address we got a validation state for to a string.
  766. *
  767. * @param id peer identity the address is for
  768. * @param address the address itself
  769. * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
  770. * @param last_validation when was the address validated last
  771. * @param valid_until until when is the address valid
  772. * @param next_validation when will we try to revalidate the address next
  773. * @param state where are we in the validation state machine
  774. */
  775. static void
  776. resolve_validation_address (const struct GNUNET_PeerIdentity *id,
  777. const struct GNUNET_HELLO_Address *address,
  778. int numeric,
  779. struct GNUNET_TIME_Absolute last_validation,
  780. struct GNUNET_TIME_Absolute valid_until,
  781. struct GNUNET_TIME_Absolute next_validation,
  782. enum GNUNET_TRANSPORT_ValidationState state)
  783. {
  784. struct ValidationResolutionContext *vc;
  785. vc = GNUNET_new (struct ValidationResolutionContext);
  786. GNUNET_assert(NULL != vc);
  787. GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
  788. address_resolutions++;
  789. vc->id = (*id);
  790. vc->transport = GNUNET_strdup(address->transport_name);
  791. vc->addrcp = GNUNET_HELLO_address_copy (address);
  792. vc->printed = GNUNET_NO;
  793. vc->state = state;
  794. vc->last_validation = last_validation;
  795. vc->valid_until = valid_until;
  796. vc->next_validation = next_validation;
  797. /* Resolve address to string */
  798. vc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
  799. address,
  800. numeric,
  801. RESOLUTION_TIMEOUT,
  802. &process_validation_string, vc);
  803. }
  804. /**
  805. * Resolve address we got a validation state for to a string.
  806. *
  807. * @param cls NULL
  808. * @param peer peer identity the address is for
  809. * @param address the address itself
  810. * @param last_validation when was the address validated last
  811. * @param valid_until until when is the address valid
  812. * @param next_validation when will we try to revalidate the address next
  813. * @param state where are we in the validation state machine
  814. */
  815. static void
  816. process_validation_cb (void *cls,
  817. const struct GNUNET_PeerIdentity *peer,
  818. const struct GNUNET_HELLO_Address *address,
  819. struct GNUNET_TIME_Absolute last_validation,
  820. struct GNUNET_TIME_Absolute valid_until,
  821. struct GNUNET_TIME_Absolute next_validation,
  822. enum GNUNET_TRANSPORT_ValidationState state)
  823. {
  824. if ((NULL == peer) && (NULL == address))
  825. {
  826. if (monitor_validation)
  827. {
  828. FPRINTF (stdout,
  829. "%s",
  830. _("Monitor disconnected from transport service. Reconnecting.\n"));
  831. return;
  832. }
  833. /* done */
  834. vic = NULL;
  835. if (GNUNET_SCHEDULER_NO_TASK != end)
  836. GNUNET_SCHEDULER_cancel (end);
  837. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  838. return;
  839. }
  840. if ((NULL == peer) || (NULL == address))
  841. {
  842. /* invalid response */
  843. vic = NULL;
  844. if (GNUNET_SCHEDULER_NO_TASK != end)
  845. GNUNET_SCHEDULER_cancel (end);
  846. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  847. return;
  848. }
  849. resolve_validation_address (peer,
  850. address,
  851. numeric,
  852. last_validation,
  853. valid_until,
  854. next_validation,
  855. state);
  856. }
  857. static void
  858. run_nat_test ()
  859. {
  860. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  861. "Running test for plugin `%s' using bind port %u and advertised port %u \n",
  862. head->name,
  863. (uint16_t) head->bnd_port,
  864. (uint16_t) head->adv_port);
  865. head->tst = GNUNET_NAT_test_start (cfg,
  866. (0 == strcasecmp (head->name, "udp")) ? GNUNET_NO : GNUNET_YES,
  867. (uint16_t) head->bnd_port,
  868. (uint16_t) head->adv_port,
  869. TIMEOUT,
  870. &result_callback, head);
  871. }
  872. /**
  873. * Test our plugin's configuration (NAT traversal, etc.).
  874. *
  875. * @param cfg configuration to test
  876. */
  877. static void
  878. do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
  879. {
  880. char *plugins;
  881. char *tok;
  882. unsigned long long bnd_port;
  883. unsigned long long adv_port;
  884. struct TestContext *tc;
  885. char *binary;
  886. if (GNUNET_OK
  887. != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
  888. &plugins))
  889. {
  890. FPRINTF (stderr, "%s", _
  891. ("No transport plugins configured, peer will never communicate\n"));
  892. ret = 4;
  893. return;
  894. }
  895. for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
  896. {
  897. char section[12 + strlen (tok)];
  898. GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
  899. if (GNUNET_OK
  900. != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
  901. &bnd_port))
  902. {
  903. FPRINTF (stderr,
  904. _("No port configured for plugin `%s', cannot test it\n"), tok);
  905. continue;
  906. }
  907. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
  908. "ADVERTISED_PORT", &adv_port))
  909. adv_port = bnd_port;
  910. tc = GNUNET_new (struct TestContext);
  911. tc->name = GNUNET_strdup (tok);
  912. tc->adv_port = adv_port;
  913. tc->bnd_port = bnd_port;
  914. GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
  915. }
  916. GNUNET_free(plugins);
  917. if ((NULL != head) && (NULL == resolver))
  918. {
  919. binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
  920. resolver = GNUNET_OS_start_process (GNUNET_YES,
  921. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  922. NULL, NULL, NULL,
  923. binary,
  924. "gnunet-service-resolver", NULL);
  925. if (NULL == resolver)
  926. {
  927. FPRINTF (stderr, _("Failed to start resolver!\n"));
  928. return;
  929. }
  930. GNUNET_free(binary);
  931. GNUNET_RESOLVER_connect (cfg);
  932. run_nat_test ();
  933. }
  934. }
  935. /**
  936. * Function called to notify a client about the socket
  937. * begin ready to queue more data. @a buf will be
  938. * NULL and @a size zero if the socket was closed for
  939. * writing in the meantime.
  940. *
  941. * @param cls closure
  942. * @param size number of bytes available in @a buf
  943. * @param buf where the callee should write the message
  944. * @return number of bytes written to @a buf
  945. */
  946. static size_t
  947. transmit_data (void *cls,
  948. size_t size,
  949. void *buf)
  950. {
  951. struct GNUNET_MessageHeader *m = buf;
  952. if ((NULL == buf) || (0 == size))
  953. {
  954. th = NULL;
  955. return 0;
  956. }
  957. GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
  958. GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
  959. m->size = ntohs (size);
  960. m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
  961. memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
  962. traffic_sent += size;
  963. th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
  964. BLOCKSIZE * 1024,
  965. GNUNET_TIME_UNIT_FOREVER_REL,
  966. &transmit_data, NULL);
  967. if (verbosity > 0)
  968. FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
  969. GNUNET_i2s (&pid));
  970. return size;
  971. }
  972. /**
  973. * Function called to notify transport users that another
  974. * peer connected to us.
  975. *
  976. * @param cls closure
  977. * @param peer the peer that connected
  978. */
  979. static void
  980. notify_connect (void *cls,
  981. const struct GNUNET_PeerIdentity *peer)
  982. {
  983. if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
  984. return;
  985. ret = 0;
  986. if (try_connect)
  987. {
  988. /* all done, terminate instantly */
  989. FPRINTF (stdout, _("Successfully connected to `%s'\n"),
  990. GNUNET_i2s_full (peer));
  991. ret = 0;
  992. if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
  993. {
  994. GNUNET_SCHEDULER_cancel (op_timeout);
  995. op_timeout = GNUNET_SCHEDULER_NO_TASK;
  996. }
  997. if (GNUNET_SCHEDULER_NO_TASK != end)
  998. GNUNET_SCHEDULER_cancel (end);
  999. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  1000. return;
  1001. }
  1002. if (benchmark_send)
  1003. {
  1004. if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
  1005. {
  1006. GNUNET_SCHEDULER_cancel (op_timeout);
  1007. op_timeout = GNUNET_SCHEDULER_NO_TASK;
  1008. }
  1009. if (verbosity > 0)
  1010. FPRINTF (stdout,
  1011. _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
  1012. GNUNET_i2s (&pid), BLOCKSIZE);
  1013. start_time = GNUNET_TIME_absolute_get ();
  1014. if (NULL == th)
  1015. th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
  1016. BLOCKSIZE * 1024,
  1017. GNUNET_TIME_UNIT_FOREVER_REL,
  1018. &transmit_data,
  1019. NULL);
  1020. else
  1021. GNUNET_break(0);
  1022. return;
  1023. }
  1024. }
  1025. /**
  1026. * Function called to notify transport users that another
  1027. * peer disconnected from us.
  1028. *
  1029. * @param cls closure
  1030. * @param peer the peer that disconnected
  1031. */
  1032. static void
  1033. notify_disconnect (void *cls,
  1034. const struct GNUNET_PeerIdentity *peer)
  1035. {
  1036. if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
  1037. return;
  1038. if (try_disconnect)
  1039. {
  1040. /* all done, terminate instantly */
  1041. FPRINTF (stdout, _("Successfully disconnected from `%s'\n"),
  1042. GNUNET_i2s_full (peer));
  1043. ret = 0;
  1044. if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
  1045. {
  1046. GNUNET_SCHEDULER_cancel (op_timeout);
  1047. op_timeout = GNUNET_SCHEDULER_NO_TASK;
  1048. }
  1049. if (GNUNET_SCHEDULER_NO_TASK != end)
  1050. GNUNET_SCHEDULER_cancel (end);
  1051. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  1052. return;
  1053. }
  1054. if (NULL != th)
  1055. {
  1056. GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
  1057. th = NULL;
  1058. }
  1059. if (benchmark_send)
  1060. {
  1061. FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
  1062. GNUNET_i2s (&pid));
  1063. if (GNUNET_SCHEDULER_NO_TASK != end)
  1064. GNUNET_SCHEDULER_cancel (end);
  1065. return;
  1066. }
  1067. }
  1068. /**
  1069. * Function called to notify transport users that another
  1070. * peer connected to us.
  1071. *
  1072. * @param cls closure
  1073. * @param peer the peer that connected
  1074. */
  1075. static void
  1076. monitor_notify_connect (void *cls,
  1077. const struct GNUNET_PeerIdentity *peer)
  1078. {
  1079. monitor_connect_counter++;
  1080. struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
  1081. const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
  1082. FPRINTF (stdout,
  1083. _("%24s: %-17s %4s (%u connections in total)\n"),
  1084. now_str,
  1085. _("Connected to"),
  1086. GNUNET_i2s (peer),
  1087. monitor_connect_counter);
  1088. }
  1089. /**
  1090. * Function called to notify transport users that another
  1091. * peer disconnected from us.
  1092. *
  1093. * @param cls closure
  1094. * @param peer the peer that disconnected
  1095. */
  1096. static void
  1097. monitor_notify_disconnect (void *cls,
  1098. const struct GNUNET_PeerIdentity *peer)
  1099. {
  1100. struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
  1101. const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
  1102. GNUNET_assert(monitor_connect_counter > 0);
  1103. monitor_connect_counter--;
  1104. FPRINTF (stdout,
  1105. _("%24s: %-17s %4s (%u connections in total)\n"),
  1106. now_str,
  1107. _("Disconnected from"),
  1108. GNUNET_i2s (peer),
  1109. monitor_connect_counter);
  1110. }
  1111. /**
  1112. * Function called by the transport for each received message.
  1113. *
  1114. * @param cls closure
  1115. * @param peer (claimed) identity of the other peer
  1116. * @param message the message
  1117. */
  1118. static void
  1119. notify_receive (void *cls,
  1120. const struct GNUNET_PeerIdentity *peer,
  1121. const struct GNUNET_MessageHeader *message)
  1122. {
  1123. if (benchmark_receive)
  1124. {
  1125. if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
  1126. return;
  1127. if (verbosity > 0)
  1128. FPRINTF (stdout,
  1129. _("Received %u bytes from %s\n"),
  1130. (unsigned int) ntohs (message->size),
  1131. GNUNET_i2s (peer));
  1132. if (traffic_received == 0)
  1133. start_time = GNUNET_TIME_absolute_get ();
  1134. traffic_received += ntohs (message->size);
  1135. return;
  1136. }
  1137. }
  1138. static void
  1139. resolve_peer_address (const struct GNUNET_PeerIdentity *id,
  1140. const struct GNUNET_HELLO_Address *address,
  1141. int numeric,
  1142. enum GNUNET_TRANSPORT_PeerState state,
  1143. struct GNUNET_TIME_Absolute state_timeout);
  1144. static void
  1145. print_info (const struct GNUNET_PeerIdentity *id,
  1146. const char *transport,
  1147. const char *addr,
  1148. enum GNUNET_TRANSPORT_PeerState state,
  1149. struct GNUNET_TIME_Absolute state_timeout)
  1150. {
  1151. if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
  1152. (GNUNET_YES == monitor_connections))
  1153. {
  1154. FPRINTF (stdout,
  1155. _("Peer `%s': %s %s in state `%s' until %s\n"),
  1156. GNUNET_i2s (id),
  1157. (NULL == transport) ? "<none>" : transport,
  1158. (NULL == transport) ? "<none>" : addr,
  1159. GNUNET_TRANSPORT_ps2s (state),
  1160. GNUNET_STRINGS_absolute_time_to_string (state_timeout));
  1161. }
  1162. else if ( (GNUNET_YES == iterate_connections) &&
  1163. (GNUNET_TRANSPORT_is_connected(state)))
  1164. {
  1165. /* Only connected peers, skip state */
  1166. FPRINTF (stdout,
  1167. _("Peer `%s': %s %s\n"),
  1168. GNUNET_i2s (id),
  1169. transport,
  1170. addr);
  1171. }
  1172. }
  1173. /**
  1174. * Function called with a textual representation of an address. This
  1175. * function will be called several times with different possible
  1176. * textual representations, and a last time with @a address being NULL
  1177. * to signal the end of the iteration. Note that @a address NULL
  1178. * always is the last call, regardless of the value in @a res.
  1179. *
  1180. * @param cls closure
  1181. * @param address NULL on end of iteration,
  1182. * otherwise 0-terminated printable UTF-8 string,
  1183. * in particular an empty string if @a res is #GNUNET_NO
  1184. * @param res result of the address to string conversion:
  1185. * if #GNUNET_OK: conversion successful
  1186. * if #GNUNET_NO: address was invalid (or not supported)
  1187. * if #GNUNET_SYSERR: communication error (IPC error)
  1188. */
  1189. static void
  1190. process_peer_string (void *cls,
  1191. const char *address,
  1192. int res)
  1193. {
  1194. struct PeerResolutionContext *rc = cls;
  1195. if (NULL != address)
  1196. {
  1197. if (GNUNET_SYSERR == res)
  1198. {
  1199. FPRINTF (stderr,
  1200. "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
  1201. GNUNET_i2s (&rc->id),
  1202. rc->addrcp->transport_name,
  1203. (unsigned int) rc->addrcp->address_length);
  1204. print_info (&rc->id,
  1205. rc->transport,
  1206. NULL,
  1207. rc->state,
  1208. rc->state_timeout);
  1209. rc->printed = GNUNET_YES;
  1210. return;
  1211. }
  1212. if (GNUNET_OK == res)
  1213. {
  1214. print_info (&rc->id,
  1215. rc->transport,
  1216. address,
  1217. rc->state,
  1218. rc->state_timeout);
  1219. rc->printed = GNUNET_YES;
  1220. return; /* Wait for done call */
  1221. }
  1222. /* GNUNET_NO == res: ignore, was simply not supported */
  1223. return;
  1224. }
  1225. /* NULL == address, last call, we are done */
  1226. GNUNET_assert (address_resolutions > 0);
  1227. address_resolutions--;
  1228. if (GNUNET_NO == rc->printed)
  1229. {
  1230. if (numeric == GNUNET_NO)
  1231. {
  1232. /* Failed to resolve address, try numeric lookup
  1233. (note: this should not be needed, as transport
  1234. should fallback to numeric conversion if DNS takes
  1235. too long) */
  1236. resolve_peer_address (&rc->id,
  1237. rc->addrcp,
  1238. GNUNET_YES,
  1239. rc->state,
  1240. rc->state_timeout);
  1241. }
  1242. else
  1243. {
  1244. print_info (&rc->id,
  1245. rc->transport,
  1246. NULL,
  1247. rc->state,
  1248. rc->state_timeout);
  1249. }
  1250. }
  1251. GNUNET_free (rc->transport);
  1252. GNUNET_free (rc->addrcp);
  1253. GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
  1254. GNUNET_free (rc);
  1255. if ((0 == address_resolutions) && (iterate_connections))
  1256. {
  1257. if (GNUNET_SCHEDULER_NO_TASK != end)
  1258. {
  1259. GNUNET_SCHEDULER_cancel (end);
  1260. end = GNUNET_SCHEDULER_NO_TASK;
  1261. }
  1262. if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
  1263. {
  1264. GNUNET_SCHEDULER_cancel (op_timeout);
  1265. op_timeout = GNUNET_SCHEDULER_NO_TASK;
  1266. }
  1267. ret = 0;
  1268. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  1269. }
  1270. }
  1271. static void
  1272. resolve_peer_address (const struct GNUNET_PeerIdentity *id,
  1273. const struct GNUNET_HELLO_Address *address,
  1274. int numeric,
  1275. enum GNUNET_TRANSPORT_PeerState state,
  1276. struct GNUNET_TIME_Absolute state_timeout)
  1277. {
  1278. struct PeerResolutionContext *rc;
  1279. rc = GNUNET_new (struct PeerResolutionContext);
  1280. GNUNET_assert(NULL != rc);
  1281. GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
  1282. address_resolutions++;
  1283. rc->id = (*id);
  1284. rc->transport = GNUNET_strdup(address->transport_name);
  1285. rc->addrcp = GNUNET_HELLO_address_copy (address);
  1286. rc->printed = GNUNET_NO;
  1287. rc->state = state;
  1288. rc->state_timeout = state_timeout;
  1289. /* Resolve address to string */
  1290. rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
  1291. address,
  1292. numeric,
  1293. RESOLUTION_TIMEOUT,
  1294. &process_peer_string, rc);
  1295. }
  1296. /**
  1297. * Function called with information about a peers during a one shot iteration
  1298. *
  1299. * @param cls closure
  1300. * @param peer identity of the peer, NULL for final callback when operation done
  1301. * @param address binary address used to communicate with this peer,
  1302. * NULL on disconnect or when done
  1303. * @param state current state this peer is in
  1304. * @param state_timeout time out for the current state
  1305. */
  1306. static void
  1307. process_peer_iteration_cb (void *cls,
  1308. const struct GNUNET_PeerIdentity *peer,
  1309. const struct GNUNET_HELLO_Address *address,
  1310. enum GNUNET_TRANSPORT_PeerState state,
  1311. struct GNUNET_TIME_Absolute state_timeout)
  1312. {
  1313. if (NULL == peer)
  1314. {
  1315. /* done */
  1316. address_resolution_in_progress = GNUNET_NO;
  1317. pic = NULL;
  1318. if (GNUNET_SCHEDULER_NO_TASK != end)
  1319. GNUNET_SCHEDULER_cancel (end);
  1320. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  1321. return;
  1322. }
  1323. if ( (GNUNET_NO == iterate_all) &&
  1324. (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
  1325. return; /* Display only connected peers */
  1326. if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
  1327. GNUNET_SCHEDULER_cancel (op_timeout);
  1328. op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
  1329. &operation_timeout,
  1330. NULL);
  1331. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1332. "Received address for peer `%s': %s\n",
  1333. GNUNET_i2s (peer),
  1334. address->transport_name);
  1335. if (NULL != address)
  1336. resolve_peer_address (peer, address, numeric, state, state_timeout);
  1337. else
  1338. print_info (peer, NULL, NULL, state, state_timeout);
  1339. }
  1340. /**
  1341. * Context for address resolution by #plugin_monitoring_cb().
  1342. */
  1343. struct PluginMonitorAddress
  1344. {
  1345. /**
  1346. * Ongoing resolution request.
  1347. */
  1348. struct GNUNET_TRANSPORT_AddressToStringContext *asc;
  1349. /**
  1350. * Resolved address as string.
  1351. */
  1352. char *str;
  1353. };
  1354. /**
  1355. * Function called with a textual representation of an address. This
  1356. * function will be called several times with different possible
  1357. * textual representations, and a last time with @a address being NULL
  1358. * to signal the end of the iteration. Note that @a address NULL
  1359. * always is the last call, regardless of the value in @a res.
  1360. *
  1361. * @param cls closure
  1362. * @param address NULL on end of iteration,
  1363. * otherwise 0-terminated printable UTF-8 string,
  1364. * in particular an empty string if @a res is #GNUNET_NO
  1365. * @param res result of the address to string conversion:
  1366. * if #GNUNET_OK: conversion successful
  1367. * if #GNUNET_NO: address was invalid (or not supported)
  1368. * if #GNUNET_SYSERR: communication error (IPC error)
  1369. */
  1370. static void
  1371. address_cb (void *cls,
  1372. const char *address,
  1373. int res)
  1374. {
  1375. struct PluginMonitorAddress *addr = cls;
  1376. if (NULL == address)
  1377. {
  1378. addr->asc = NULL;
  1379. return;
  1380. }
  1381. if (NULL != addr->str)
  1382. return;
  1383. addr->str = GNUNET_strdup (address);
  1384. }
  1385. /**
  1386. * Function called by the plugin with information about the
  1387. * current sessions managed by the plugin (for monitoring).
  1388. *
  1389. * @param cls closure (NULL)
  1390. * @param session session handle this information is about,
  1391. * NULL to indicate that we are "in sync" (initial
  1392. * iteration complete)
  1393. * @param session_ctx storage location where the application
  1394. * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
  1395. * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
  1396. * @param info information about the state of the session,
  1397. * NULL if @a session is also NULL and we are
  1398. * merely signalling that the initial iteration is over;
  1399. * NULL with @a session being non-NULL if the monitor
  1400. * was being cancelled while sessions were active
  1401. */
  1402. static void
  1403. plugin_monitoring_cb (void *cls,
  1404. struct GNUNET_TRANSPORT_PluginSession *session,
  1405. void **session_ctx,
  1406. const struct GNUNET_TRANSPORT_SessionInfo *info)
  1407. {
  1408. const char *state;
  1409. struct PluginMonitorAddress *addr;
  1410. if ( (NULL == info) &&
  1411. (NULL == session) )
  1412. return; /* in sync with transport service */
  1413. addr = *session_ctx;
  1414. if (NULL == info)
  1415. {
  1416. if (NULL != addr)
  1417. {
  1418. if (NULL != addr->asc)
  1419. {
  1420. GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
  1421. addr->asc = NULL;
  1422. }
  1423. GNUNET_free_non_null (addr->str);
  1424. GNUNET_free (addr);
  1425. *session_ctx = NULL;
  1426. }
  1427. return; /* shutdown */
  1428. }
  1429. if ( (NULL != cpid) &&
  1430. (0 != memcmp (&info->address->peer,
  1431. cpid,
  1432. sizeof (struct GNUNET_PeerIdentity))) )
  1433. return; /* filtered */
  1434. if (NULL == addr)
  1435. {
  1436. addr = GNUNET_new (struct PluginMonitorAddress);
  1437. addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
  1438. info->address,
  1439. GNUNET_NO,
  1440. GNUNET_TIME_UNIT_FOREVER_REL,
  1441. &address_cb,
  1442. addr);
  1443. *session_ctx = addr;
  1444. }
  1445. switch (info->state)
  1446. {
  1447. case GNUNET_TRANSPORT_SS_INIT:
  1448. state = "INIT";
  1449. break;
  1450. case GNUNET_TRANSPORT_SS_HANDSHAKE:
  1451. state = "HANDSHAKE";
  1452. break;
  1453. case GNUNET_TRANSPORT_SS_UP:
  1454. state = "UP";
  1455. break;
  1456. case GNUNET_TRANSPORT_SS_UPDATE:
  1457. state = "UPDATE";
  1458. break;
  1459. case GNUNET_TRANSPORT_SS_DONE:
  1460. state = "DONE";
  1461. break;
  1462. default:
  1463. state = "UNKNOWN";
  1464. break;
  1465. }
  1466. fprintf (stdout,
  1467. "%s: %s %s (# %u/%u b) blocked until %s timeout in %s [%s]\n",
  1468. GNUNET_i2s (&info->address->peer),
  1469. addr->str,
  1470. (info->is_inbound == GNUNET_YES) ? "<-" : ((info->is_inbound == GNUNET_NO) ? "->" : "<>"),
  1471. info->num_msg_pending,
  1472. info->num_bytes_pending,
  1473. GNUNET_STRINGS_absolute_time_to_string (info->receive_delay),
  1474. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
  1475. GNUNET_YES),
  1476. state);
  1477. if (GNUNET_TRANSPORT_SS_DONE == info->state)
  1478. {
  1479. if (NULL != addr->asc)
  1480. {
  1481. GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
  1482. addr->asc = NULL;
  1483. }
  1484. GNUNET_free_non_null (addr->str);
  1485. GNUNET_free (addr);
  1486. *session_ctx = NULL;
  1487. }
  1488. }
  1489. /**
  1490. * Function called with information about a peers
  1491. *
  1492. * @param cls closure
  1493. * @param peer identity of the peer, NULL for final callback when operation done
  1494. * @param address binary address used to communicate with this peer,
  1495. * NULL on disconnect or when done
  1496. * @param state current state this peer is in
  1497. * @param state_timeout time out for the current state
  1498. *
  1499. */
  1500. static void
  1501. process_peer_monitoring_cb (void *cls,
  1502. const struct GNUNET_PeerIdentity *peer,
  1503. const struct GNUNET_HELLO_Address *address,
  1504. enum GNUNET_TRANSPORT_PeerState state,
  1505. struct GNUNET_TIME_Absolute state_timeout)
  1506. {
  1507. struct MonitoredPeer *m;
  1508. if (NULL == peer)
  1509. {
  1510. FPRINTF (stdout,
  1511. "%s",
  1512. _("Monitor disconnected from transport service. Reconnecting.\n"));
  1513. return;
  1514. }
  1515. if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
  1516. GNUNET_SCHEDULER_cancel (op_timeout);
  1517. op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
  1518. &operation_timeout,
  1519. NULL);
  1520. if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
  1521. {
  1522. m = GNUNET_new (struct MonitoredPeer);
  1523. GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
  1524. m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  1525. }
  1526. else
  1527. {
  1528. if ( (m->state == state) &&
  1529. (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
  1530. ((NULL == address) && (NULL == m->address)))
  1531. {
  1532. return; /* No real change */
  1533. }
  1534. if ( (m->state == state) && ((NULL != address) && (NULL != m->address)) &&
  1535. (0 == GNUNET_HELLO_address_cmp(m->address, address)))
  1536. return; /* No real change */
  1537. }
  1538. if (NULL != m->address)
  1539. {
  1540. GNUNET_free (m->address);
  1541. m->address = NULL;
  1542. }
  1543. if (NULL != address)
  1544. m->address = GNUNET_HELLO_address_copy (address);
  1545. m->state = state;
  1546. m->state_timeout = state_timeout;
  1547. if (NULL != address)
  1548. resolve_peer_address (peer,
  1549. m->address,
  1550. numeric,
  1551. m->state,
  1552. m->state_timeout);
  1553. else
  1554. print_info (peer,
  1555. NULL,
  1556. NULL,
  1557. m->state,
  1558. m->state_timeout);
  1559. }
  1560. /**
  1561. * Function called with our result of trying to connect to a peer
  1562. * using the transport service. Will retry 10 times, and if we still
  1563. * fail to connect terminate with an error message.
  1564. *
  1565. * @param cls NULL
  1566. * @param result #GNUNET_OK if we connected to the service
  1567. */
  1568. static void
  1569. try_connect_cb (void *cls,
  1570. const int result)
  1571. {
  1572. static int retries = 0;
  1573. if (GNUNET_OK == result)
  1574. {
  1575. tc_handle = NULL;
  1576. return;
  1577. }
  1578. retries++;
  1579. if (retries < 10)
  1580. {
  1581. tc_handle = GNUNET_TRANSPORT_try_connect (handle,
  1582. &pid,
  1583. &try_connect_cb,
  1584. NULL);
  1585. }
  1586. else
  1587. {
  1588. FPRINTF (stderr,
  1589. "%s",
  1590. _("Failed to send connect request to transport service\n"));
  1591. if (GNUNET_SCHEDULER_NO_TASK != end)
  1592. GNUNET_SCHEDULER_cancel (end);
  1593. ret = 1;
  1594. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  1595. return;
  1596. }
  1597. }
  1598. /**
  1599. * Function called with our result of trying to disconnect a peer
  1600. * using the transport service. Will retry 10 times, and if we still
  1601. * fail to disconnect, terminate with an error message.
  1602. *
  1603. * @param cls NULL
  1604. * @param result #GNUNET_OK if we connected to the service
  1605. */
  1606. static void
  1607. try_disconnect_cb (void *cls,
  1608. const int result)
  1609. {
  1610. static int retries = 0;
  1611. if (GNUNET_OK == result)
  1612. {
  1613. tc_handle = NULL;
  1614. return;
  1615. }
  1616. retries++;
  1617. if (retries < 10)
  1618. tc_handle = GNUNET_TRANSPORT_try_disconnect (handle,
  1619. &pid,
  1620. &try_disconnect_cb,
  1621. NULL);
  1622. else
  1623. {
  1624. FPRINTF (stderr, "%s",
  1625. _("Failed to send disconnect request to transport service\n"));
  1626. if (GNUNET_SCHEDULER_NO_TASK != end)
  1627. GNUNET_SCHEDULER_cancel (end);
  1628. ret = 1;
  1629. end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  1630. return;
  1631. }
  1632. }
  1633. /**
  1634. * Function called with the result of the check if the 'transport'
  1635. * service is running.
  1636. *
  1637. * @param cls closure with our configuration
  1638. * @param result #GNUNET_YES if transport is running
  1639. */
  1640. static void
  1641. testservice_task (void *cls,
  1642. int result)
  1643. {
  1644. int counter = 0;
  1645. ret = 1;
  1646. if (GNUNET_YES != result)
  1647. {
  1648. FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
  1649. return;
  1650. }
  1651. if ((NULL != cpid)
  1652. && (GNUNET_OK
  1653. != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
  1654. &pid.public_key)))
  1655. {
  1656. FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
  1657. return;
  1658. }
  1659. counter = benchmark_send + benchmark_receive + iterate_connections
  1660. + monitor_connections + monitor_connects + try_connect + try_disconnect +
  1661. + iterate_validation + monitor_validation + monitor_plugins;
  1662. if (1 < counter)
  1663. {
  1664. FPRINTF (stderr,
  1665. _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
  1666. "connect", "benchmark send", "benchmark receive", "information",
  1667. "monitor", "events", "plugins");
  1668. return;
  1669. }
  1670. if (0 == counter)
  1671. {
  1672. FPRINTF (stderr,
  1673. _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
  1674. "connect", "benchmark send", "benchmark receive", "information",
  1675. "monitor", "events", "plugins");
  1676. return;
  1677. }
  1678. if (try_connect) /* -C: Connect to peer */
  1679. {
  1680. if (NULL == cpid)
  1681. {
  1682. FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
  1683. "-C", "-p");
  1684. ret = 1;
  1685. return;
  1686. }
  1687. handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
  1688. &notify_connect, &notify_disconnect);
  1689. if (NULL == handle)
  1690. {
  1691. FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
  1692. ret = 1;
  1693. return;
  1694. }
  1695. tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
  1696. NULL);
  1697. if (NULL == tc_handle)
  1698. {
  1699. FPRINTF (stderr, "%s",
  1700. _("Failed to send request to transport service\n"));
  1701. ret = 1;
  1702. return;
  1703. }
  1704. op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
  1705. NULL);
  1706. }
  1707. else if (try_disconnect) /* -D: Disconnect from peer */
  1708. {
  1709. if (NULL == cpid)
  1710. {
  1711. FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
  1712. "-D", "-p");
  1713. ret = 1;
  1714. return;
  1715. }
  1716. handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
  1717. &notify_connect, &notify_disconnect);
  1718. if (NULL == handle)
  1719. {
  1720. FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
  1721. ret = 1;
  1722. return;
  1723. }
  1724. tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
  1725. NULL);
  1726. if (NULL == tc_handle)
  1727. {
  1728. FPRINTF (stderr, "%s",
  1729. _("Failed to send request to transport service\n"));
  1730. ret = 1;
  1731. return;
  1732. }
  1733. op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
  1734. NULL);
  1735. }
  1736. else if (benchmark_send) /* -s: Benchmark sending */
  1737. {
  1738. if (NULL == cpid)
  1739. {
  1740. FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
  1741. "-s", "-p");
  1742. ret = 1;
  1743. return;
  1744. }
  1745. handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
  1746. &notify_receive,
  1747. &notify_connect,
  1748. &notify_disconnect);
  1749. if (NULL == handle)
  1750. {
  1751. FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
  1752. ret = 1;
  1753. return;
  1754. }
  1755. tc_handle = GNUNET_TRANSPORT_try_connect (handle,
  1756. &pid,
  1757. &try_connect_cb,
  1758. NULL);
  1759. if (NULL == tc_handle)
  1760. {
  1761. FPRINTF (stderr, "%s",
  1762. _("Failed to send request to transport service\n"));
  1763. ret = 1;
  1764. return;
  1765. }
  1766. start_time = GNUNET_TIME_absolute_get ();
  1767. op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
  1768. &operation_timeout,
  1769. NULL);
  1770. }
  1771. else if (benchmark_receive) /* -b: Benchmark receiving */
  1772. {
  1773. handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive, NULL,
  1774. NULL);
  1775. if (NULL == handle)
  1776. {
  1777. FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
  1778. ret = 1;
  1779. return;
  1780. }
  1781. if (verbosity > 0)
  1782. FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
  1783. start_time = GNUNET_TIME_absolute_get ();
  1784. }
  1785. else if (iterate_connections) /* -i: List information about peers once */
  1786. {
  1787. address_resolution_in_progress = GNUNET_YES;
  1788. pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
  1789. GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
  1790. op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
  1791. NULL);
  1792. }
  1793. else if (monitor_connections) /* -m: List information about peers continuously */
  1794. {
  1795. monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1796. address_resolution_in_progress = GNUNET_YES;
  1797. pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
  1798. GNUNET_NO, TIMEOUT,
  1799. &process_peer_monitoring_cb, (void *) cfg);
  1800. }
  1801. else if (monitor_plugins) /* -P: List information about plugins continuously */
  1802. {
  1803. monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1804. pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
  1805. &plugin_monitoring_cb,
  1806. NULL);
  1807. }
  1808. else if (iterate_validation) /* -d: Print information about validations */
  1809. {
  1810. vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
  1811. (NULL == cpid) ? NULL : &pid,
  1812. GNUNET_YES, TIMEOUT,
  1813. &process_validation_cb, (void *) cfg);
  1814. }
  1815. else if (monitor_validation) /* -f: Print information about validations continuously */
  1816. {
  1817. vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
  1818. (NULL == cpid) ? NULL : &pid,
  1819. GNUNET_NO, TIMEOUT,
  1820. &process_validation_cb, (void *) cfg);
  1821. }
  1822. else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
  1823. {
  1824. monitor_connect_counter = 0;
  1825. handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
  1826. &monitor_notify_connect,
  1827. &monitor_notify_disconnect);
  1828. if (NULL == handle)
  1829. {
  1830. FPRINTF (stderr,
  1831. "%s",
  1832. _("Failed to connect to transport service\n"));
  1833. ret = 1;
  1834. return;
  1835. }
  1836. ret = 0;
  1837. }
  1838. else
  1839. {
  1840. GNUNET_break(0);
  1841. return;
  1842. }
  1843. end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  1844. &shutdown_task,
  1845. NULL);
  1846. }
  1847. /**
  1848. * Main function that will be run by the scheduler.
  1849. *
  1850. * @param cls closure
  1851. * @param args remaining command-line arguments
  1852. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  1853. * @param mycfg configuration
  1854. */
  1855. static void
  1856. run (void *cls,
  1857. char * const *args,
  1858. const char *cfgfile,
  1859. const struct GNUNET_CONFIGURATION_Handle *mycfg)
  1860. {
  1861. cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
  1862. if (test_configuration)
  1863. {
  1864. do_test_configuration (cfg);
  1865. return;
  1866. }
  1867. GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
  1868. &testservice_task, (void *) cfg);
  1869. }
  1870. int
  1871. main (int argc, char * const *argv)
  1872. {
  1873. int res;
  1874. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  1875. { 'a', "all", NULL,
  1876. gettext_noop ("print information for all peers (instead of only connected peers)"),
  1877. 0, &GNUNET_GETOPT_set_one, &iterate_all },
  1878. { 'b', "benchmark", NULL,
  1879. gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
  1880. 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
  1881. { 'C', "connect",
  1882. NULL, gettext_noop ("connect to a peer"), 0,
  1883. &GNUNET_GETOPT_set_one, &try_connect },
  1884. { 'D', "disconnect",
  1885. NULL, gettext_noop ("disconnect to a peer"), 0,
  1886. &GNUNET_GETOPT_set_one, &try_disconnect },
  1887. { 'd', "validation", NULL,
  1888. gettext_noop ("print information for all pending validations "),
  1889. 0, &GNUNET_GETOPT_set_one, &iterate_validation },
  1890. { 'f', "monitorvalidation", NULL,
  1891. gettext_noop ("print information for all pending validations continuously"),
  1892. 0, &GNUNET_GETOPT_set_one, &monitor_validation },
  1893. { 'i', "information", NULL,
  1894. gettext_noop ("provide information about all current connections (once)"),
  1895. 0, &GNUNET_GETOPT_set_one, &iterate_connections },
  1896. { 'm', "monitor", NULL,
  1897. gettext_noop ("provide information about all current connections (continuously)"),
  1898. 0, &GNUNET_GETOPT_set_one, &monitor_connections },
  1899. { 'e', "events", NULL,
  1900. gettext_noop ("provide information about all connects and disconnect events (continuously)"),
  1901. 0, &GNUNET_GETOPT_set_one, &monitor_connects },
  1902. { 'n', "numeric",
  1903. NULL, gettext_noop ("do not resolve hostnames"), 0,
  1904. &GNUNET_GETOPT_set_one, &numeric },
  1905. { 'p', "peer", "PEER",
  1906. gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
  1907. &cpid },
  1908. { 'P', "plugins", NULL,
  1909. gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
  1910. &monitor_plugins },
  1911. { 's', "send", NULL, gettext_noop
  1912. ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
  1913. &GNUNET_GETOPT_set_one, &benchmark_send },
  1914. { 't', "test", NULL,
  1915. gettext_noop ("test transport configuration (involves external server)"),
  1916. 0, &GNUNET_GETOPT_set_one, &test_configuration },
  1917. GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
  1918. GNUNET_GETOPT_OPTION_END
  1919. };
  1920. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  1921. return 2;
  1922. res = GNUNET_PROGRAM_run (argc, argv,
  1923. "gnunet-transport",
  1924. gettext_noop ("Direct access to transport service."),
  1925. options,
  1926. &run, NULL);
  1927. GNUNET_free((void *) argv);
  1928. if (GNUNET_OK == res)
  1929. return ret;
  1930. return 1;
  1931. }
  1932. /* end of gnunet-transport.c */