gnunet-ats.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009--2015 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 ats-tool/gnunet-ats.c
  18. * @brief ATS command line tool
  19. * @author Matthias Wachs
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_ats_service.h"
  25. #include "gnunet_transport_service.h"
  26. /**
  27. * String to respresent unlimited
  28. */
  29. #define UNLIMITED_STRING "unlimited"
  30. /**
  31. * CLI Opt:
  32. */
  33. static int opt_resolve_addresses_numeric;
  34. /**
  35. * CLI Opt: Print verbose ATS information
  36. */
  37. static int opt_verbose;
  38. /**
  39. * CLI Option: List only addresses currently used (active)
  40. */
  41. static int opt_list_used;
  42. /**
  43. * CLI Option: List all addresses
  44. */
  45. static int opt_list_all;
  46. /**
  47. * CLI Option: set preference
  48. */
  49. static int opt_set_pref;
  50. /**
  51. * CLI Option: print quotas configured
  52. */
  53. static int opt_print_quotas;
  54. /**
  55. * CLI Option: Monitor addresses used
  56. */
  57. static int opt_monitor;
  58. /**
  59. * CLI Option: use specific peer
  60. */
  61. static char *opt_pid_str;
  62. /**
  63. * CLI Option: preference type to set
  64. */
  65. static char *opt_type_str;
  66. /**
  67. * CLI Option: preference value to set
  68. */
  69. static unsigned int opt_pref_value;
  70. /**
  71. * Final status code.
  72. */
  73. static int ret;
  74. /**
  75. * Number of results returned from service
  76. */
  77. static int stat_results;
  78. /**
  79. * State: all pending receive operations done?
  80. */
  81. static int stat_receive_done;
  82. /**
  83. * State: number of pending operations
  84. */
  85. static int stat_pending;
  86. /**
  87. * Which peer should we connect to?
  88. */
  89. static char *cpid_str;
  90. /**
  91. * ATS performance handle used
  92. */
  93. static struct GNUNET_ATS_PerformanceHandle *ph;
  94. /**
  95. * Our connectivity handle.
  96. */
  97. static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
  98. /**
  99. * Handle for address suggestion request.
  100. */
  101. static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
  102. /**
  103. * ATS address list handle used
  104. */
  105. static struct GNUNET_ATS_AddressListHandle *alh;
  106. /**
  107. * Configuration handle
  108. */
  109. static struct GNUNET_CONFIGURATION_Handle *cfg;
  110. /**
  111. * Shutdown task
  112. */
  113. static struct GNUNET_SCHEDULER_Task *shutdown_task;
  114. /**
  115. * Hashmap to store addresses
  116. */
  117. static struct GNUNET_CONTAINER_MultiPeerMap *addresses;
  118. /**
  119. * Structure used to remember all pending address resolutions.
  120. * We keep address information in here while we talk to transport
  121. * to map the address to a string.
  122. */
  123. struct PendingResolutions
  124. {
  125. /**
  126. * Kept in a DLL.
  127. */
  128. struct PendingResolutions *next;
  129. /**
  130. * Kept in a DLL.
  131. */
  132. struct PendingResolutions *prev;
  133. /**
  134. * Copy of the address we are resolving.
  135. */
  136. struct GNUNET_HELLO_Address *address;
  137. /**
  138. * Handle to the transport request to convert the address
  139. * to a string.
  140. */
  141. struct GNUNET_TRANSPORT_AddressToStringContext *tats_ctx;
  142. /**
  143. * Performance data.
  144. */
  145. struct GNUNET_ATS_Properties properties;
  146. /**
  147. * Amount of outbound bandwidth assigned by ATS.
  148. */
  149. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
  150. /**
  151. * Amount of inbound bandwidth assigned by ATS.
  152. */
  153. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
  154. /**
  155. * Is this an active address?
  156. */
  157. int active;
  158. };
  159. /**
  160. * Information we keep for an address. Used to avoid
  161. * printing the same data multiple times.
  162. */
  163. struct ATSAddress
  164. {
  165. /**
  166. * Address information.
  167. */
  168. struct GNUNET_HELLO_Address *address;
  169. /**
  170. * Current outbound bandwidth.
  171. */
  172. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
  173. /**
  174. * Current inbound bandwidth.
  175. */
  176. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
  177. /**
  178. * Is this an active address?
  179. */
  180. int active;
  181. };
  182. /**
  183. * Head of list of pending resolution requests.
  184. */
  185. static struct PendingResolutions *head;
  186. /**
  187. * Tail of list of pending resolution requests.
  188. */
  189. static struct PendingResolutions *tail;
  190. /**
  191. * Free address corresponding to a given peer.
  192. *
  193. * @param cls NULL
  194. * @param key peer identity
  195. * @param value the `struct ATSAddress *` to be freed
  196. * @return #GNUNET_YES (always)
  197. */
  198. static int
  199. free_addr_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
  200. {
  201. struct ATSAddress *a = value;
  202. GNUNET_assert (GNUNET_OK ==
  203. GNUNET_CONTAINER_multipeermap_remove (addresses, key, value));
  204. GNUNET_HELLO_address_free (a->address);
  205. GNUNET_free (a);
  206. return GNUNET_OK;
  207. }
  208. /**
  209. * Task run on shutdown.
  210. *
  211. * @param cls NULL
  212. */
  213. static void
  214. end (void *cls)
  215. {
  216. struct PendingResolutions *pr;
  217. struct PendingResolutions *next;
  218. unsigned int pending;
  219. if (NULL != alh)
  220. {
  221. GNUNET_ATS_performance_list_addresses_cancel (alh);
  222. alh = NULL;
  223. }
  224. if (NULL != ph)
  225. {
  226. GNUNET_ATS_performance_done (ph);
  227. ph = NULL;
  228. }
  229. pending = 0;
  230. next = head;
  231. while (NULL != (pr = next))
  232. {
  233. next = pr->next;
  234. GNUNET_CONTAINER_DLL_remove (head, tail, pr);
  235. GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
  236. GNUNET_free (pr->address);
  237. GNUNET_free (pr);
  238. pending++;
  239. }
  240. GNUNET_CONTAINER_multipeermap_iterate (addresses, &free_addr_it, NULL);
  241. GNUNET_CONTAINER_multipeermap_destroy (addresses);
  242. addresses = NULL;
  243. if (0 < pending)
  244. fprintf (stdout, _ ("%u address resolutions had a timeout\n"), pending);
  245. if (opt_list_used || opt_list_all)
  246. fprintf (stdout,
  247. _ ("ATS returned stat_results for %u addresses\n"),
  248. stat_results);
  249. if (NULL != ats_sh)
  250. {
  251. GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
  252. ats_sh = NULL;
  253. }
  254. if (NULL != ats_ch)
  255. {
  256. GNUNET_ATS_connectivity_done (ats_ch);
  257. ats_ch = NULL;
  258. }
  259. ret = 0;
  260. }
  261. /**
  262. * Function to call with a textual representation of an address. This
  263. * function will be called several times with different possible
  264. * textual representations, and a last time with @a address being NULL
  265. * to signal the end of the iteration. Note that @a address NULL
  266. * always is the last call, regardless of the value in @a res.
  267. *
  268. * @param cls closure, a `struct PendingResolutions *`
  269. * @param address NULL on end of iteration,
  270. * otherwise 0-terminated printable UTF-8 string,
  271. * in particular an empty string if @a res is #GNUNET_NO
  272. * @param res result of the address to string conversion:
  273. * if #GNUNET_OK: conversion successful
  274. * if #GNUNET_NO: address was invalid (or not supported)
  275. * if #GNUNET_SYSERR: communication error (IPC error)
  276. */
  277. static void
  278. transport_addr_to_str_cb (void *cls, const char *address, int res)
  279. {
  280. struct PendingResolutions *pr = cls;
  281. if (NULL == address)
  282. {
  283. /* We're done */
  284. GNUNET_CONTAINER_DLL_remove (head, tail, pr);
  285. GNUNET_free (pr->address);
  286. GNUNET_free (pr);
  287. stat_pending--;
  288. if ((GNUNET_YES == stat_receive_done) && (0 == stat_pending))
  289. {
  290. /* All messages received and no resolutions pending*/
  291. if (shutdown_task != NULL)
  292. GNUNET_SCHEDULER_cancel (shutdown_task);
  293. shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
  294. }
  295. return;
  296. }
  297. switch (res)
  298. {
  299. case GNUNET_SYSERR:
  300. fprintf (
  301. stderr,
  302. "Failed to convert address for peer `%s' plugin `%s' length %u to string (communication error)\n",
  303. GNUNET_i2s (&pr->address->peer),
  304. pr->address->transport_name,
  305. (unsigned int) pr->address->address_length);
  306. return;
  307. case GNUNET_NO:
  308. fprintf (
  309. stderr,
  310. "Failed to convert address for peer `%s' plugin `%s' length %u to string (address invalid or not supported)\n",
  311. GNUNET_i2s (&pr->address->peer),
  312. pr->address->transport_name,
  313. (unsigned int) pr->address->address_length);
  314. return;
  315. case GNUNET_OK:
  316. /* continues below */
  317. break;
  318. default:
  319. GNUNET_break (0);
  320. return;
  321. }
  322. fprintf (
  323. stdout,
  324. _ (
  325. "Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
  326. GNUNET_i2s (&pr->address->peer),
  327. pr->address->transport_name,
  328. address,
  329. GNUNET_NT_to_string (pr->properties.scope),
  330. ntohl (pr->bandwidth_out.value__),
  331. ntohl (pr->bandwidth_in.value__),
  332. pr->active ? _ ("active ") : _ ("inactive "));
  333. }
  334. /**
  335. * Closure for #find_address_it().
  336. */
  337. struct AddressFindCtx
  338. {
  339. /**
  340. * Address we are looking for.
  341. */
  342. const struct GNUNET_HELLO_Address *src;
  343. /**
  344. * Where to write the `struct ATSAddress` if we found one that matches.
  345. */
  346. struct ATSAddress *res;
  347. };
  348. /**
  349. * Find address corresponding to a given peer.
  350. *
  351. * @param cls the `struct AddressFindCtx *`
  352. * @param key peer identity
  353. * @param value the `struct ATSAddress *` for an existing address
  354. * @return #GNUNET_NO if we found a match, #GNUNET_YES if not
  355. */
  356. static int
  357. find_address_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
  358. {
  359. struct AddressFindCtx *actx = cls;
  360. struct ATSAddress *exist = value;
  361. if (0 == GNUNET_HELLO_address_cmp (actx->src, exist->address))
  362. {
  363. actx->res = exist;
  364. return GNUNET_NO;
  365. }
  366. return GNUNET_YES;
  367. }
  368. /**
  369. * Signature of a function that is called with QoS information about an address.
  370. *
  371. * @param cls closure (NULL)
  372. * @param address the address, NULL if ATS service was disconnected
  373. * @param active #GNUNET_YES if this address is actively used
  374. * to maintain a connection to a peer;
  375. * #GNUNET_NO if the address is not actively used;
  376. * #GNUNET_SYSERR if this address is no longer available for ATS
  377. * @param bandwidth_out assigned outbound bandwidth for the connection
  378. * @param bandwidth_in assigned inbound bandwidth for the connection
  379. * @param prop performance data for the address (as far as known)
  380. */
  381. static void
  382. ats_perf_mon_cb (void *cls,
  383. const struct GNUNET_HELLO_Address *address,
  384. int active,
  385. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  386. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  387. const struct GNUNET_ATS_Properties *prop)
  388. {
  389. struct PendingResolutions *pr;
  390. struct PendingResolutions *cur;
  391. struct PendingResolutions *next;
  392. if (NULL == address)
  393. {
  394. /* ATS service temporarily disconnected, remove current state */
  395. next = head;
  396. for (cur = next; NULL != cur; cur = next)
  397. {
  398. next = cur->next;
  399. GNUNET_CONTAINER_DLL_remove (head, tail, cur);
  400. GNUNET_TRANSPORT_address_to_string_cancel (cur->tats_ctx);
  401. GNUNET_HELLO_address_free (cur->address);
  402. GNUNET_free (cur);
  403. }
  404. GNUNET_CONTAINER_multipeermap_iterate (addresses, &free_addr_it, NULL);
  405. return;
  406. }
  407. if (GNUNET_SYSERR == active)
  408. {
  409. /* remove address */
  410. struct AddressFindCtx actx;
  411. actx.src = address;
  412. actx.res = NULL;
  413. GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
  414. &address->peer,
  415. &find_address_it,
  416. &actx);
  417. if (NULL == actx.res)
  418. {
  419. GNUNET_break (0);
  420. return;
  421. }
  422. GNUNET_break (GNUNET_OK ==
  423. GNUNET_CONTAINER_multipeermap_remove (addresses,
  424. &address->peer,
  425. actx.res));
  426. fprintf (stdout,
  427. _ ("Removed address of peer `%s' with plugin `%s'\n"),
  428. GNUNET_i2s (&address->peer),
  429. actx.res->address->transport_name);
  430. GNUNET_HELLO_address_free (actx.res);
  431. return;
  432. }
  433. if (GNUNET_NO == opt_verbose)
  434. {
  435. struct AddressFindCtx actx;
  436. struct ATSAddress *a;
  437. actx.src = address;
  438. actx.res = NULL;
  439. GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
  440. &address->peer,
  441. &find_address_it,
  442. &actx);
  443. if ((NULL != actx.res))
  444. {
  445. if ((bandwidth_in.value__ == actx.res->bandwidth_in.value__) &&
  446. (bandwidth_out.value__ == actx.res->bandwidth_out.value__) &&
  447. (active == actx.res->active))
  448. {
  449. return; /* Nothing to do here */
  450. }
  451. else
  452. {
  453. actx.res->bandwidth_in = bandwidth_in;
  454. actx.res->bandwidth_out = bandwidth_out;
  455. }
  456. }
  457. else
  458. {
  459. a = GNUNET_new (struct ATSAddress);
  460. a->address = GNUNET_HELLO_address_copy (address);
  461. a->bandwidth_in = bandwidth_in;
  462. a->bandwidth_out = bandwidth_out;
  463. a->active = active;
  464. GNUNET_CONTAINER_multipeermap_put (
  465. addresses,
  466. &address->peer,
  467. a,
  468. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  469. }
  470. }
  471. pr = GNUNET_new (struct PendingResolutions);
  472. pr->properties = *prop;
  473. pr->address = GNUNET_HELLO_address_copy (address);
  474. pr->bandwidth_in = bandwidth_in;
  475. pr->bandwidth_out = bandwidth_out;
  476. pr->active = active;
  477. pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (
  478. cfg,
  479. address,
  480. opt_resolve_addresses_numeric,
  481. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
  482. &transport_addr_to_str_cb,
  483. pr);
  484. GNUNET_CONTAINER_DLL_insert (head, tail, pr);
  485. stat_results++;
  486. stat_pending++;
  487. }
  488. /**
  489. * Signature of a function that is called with QoS information about an address.
  490. *
  491. * @param cls closure (NULL)
  492. * @param address the address, NULL if ATS service was disconnected
  493. * @param active is this address actively used to maintain a connection
  494. to a peer
  495. * @param bandwidth_out assigned outbound bandwidth for the connection
  496. * @param bandwidth_in assigned inbound bandwidth for the connection
  497. * @param prop performance data for the address (as far as known)
  498. */
  499. static void
  500. ats_perf_cb (void *cls,
  501. const struct GNUNET_HELLO_Address *address,
  502. int active,
  503. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  504. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  505. const struct GNUNET_ATS_Properties *prop)
  506. {
  507. struct PendingResolutions *pr;
  508. if (NULL == address)
  509. {
  510. /* All messages received */
  511. stat_receive_done = GNUNET_YES;
  512. alh = NULL;
  513. if (0 == stat_pending)
  514. {
  515. /* All messages received and no resolutions pending*/
  516. if (shutdown_task != NULL)
  517. GNUNET_SCHEDULER_cancel (shutdown_task);
  518. shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
  519. }
  520. return;
  521. }
  522. pr = GNUNET_new (struct PendingResolutions);
  523. pr->properties = *prop;
  524. pr->address = GNUNET_HELLO_address_copy (address);
  525. pr->bandwidth_in = bandwidth_in;
  526. pr->bandwidth_out = bandwidth_out;
  527. pr->active = active;
  528. pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (
  529. cfg,
  530. address,
  531. opt_resolve_addresses_numeric,
  532. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
  533. &transport_addr_to_str_cb,
  534. pr);
  535. GNUNET_CONTAINER_DLL_insert (head, tail, pr);
  536. stat_results++;
  537. stat_pending++;
  538. }
  539. /**
  540. * Print information about the quotas configured for the various
  541. * network scopes.
  542. *
  543. * @param cfg configuration to obtain quota information from
  544. * @return total number of ATS network types known
  545. */
  546. static unsigned int
  547. print_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg)
  548. {
  549. char *entry_in = NULL;
  550. char *entry_out = NULL;
  551. char *quota_out_str;
  552. char *quota_in_str;
  553. unsigned long long int quota_out;
  554. unsigned long long int quota_in;
  555. int c;
  556. for (c = 0; (c < GNUNET_NT_COUNT); c++)
  557. {
  558. GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", GNUNET_NT_to_string (c));
  559. GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", GNUNET_NT_to_string (c));
  560. /* quota out */
  561. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
  562. "ats",
  563. entry_out,
  564. &quota_out_str))
  565. {
  566. if ((0 == strcmp (quota_out_str, UNLIMITED_STRING)) ||
  567. (GNUNET_SYSERR ==
  568. GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &quota_out)))
  569. quota_out = UINT32_MAX;
  570. GNUNET_free (quota_out_str);
  571. GNUNET_asprintf (&quota_out_str, "%llu", quota_out);
  572. }
  573. else
  574. {
  575. fprintf (stderr,
  576. "Outbound quota for network `%11s' not configured!\n",
  577. GNUNET_NT_to_string (c));
  578. GNUNET_asprintf (&quota_out_str, "-");
  579. }
  580. GNUNET_free (entry_out);
  581. /* quota in */
  582. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
  583. "ats",
  584. entry_in,
  585. &quota_in_str))
  586. {
  587. if ((0 == strcmp (quota_in_str, UNLIMITED_STRING)) ||
  588. (GNUNET_SYSERR ==
  589. GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &quota_in)))
  590. quota_in = UINT32_MAX;
  591. GNUNET_free (quota_in_str);
  592. GNUNET_asprintf (&quota_in_str, "%llu", quota_in);
  593. }
  594. else
  595. {
  596. fprintf (stderr,
  597. "Inbound quota for network `%11s' not configured!\n",
  598. GNUNET_NT_to_string (c));
  599. GNUNET_asprintf (&quota_in_str, "-");
  600. }
  601. GNUNET_free (entry_in);
  602. fprintf (stdout,
  603. _ ("Quota for network `%11s' (in/out): %10s / %10s\n"),
  604. GNUNET_NT_to_string (c),
  605. quota_in_str,
  606. quota_out_str);
  607. GNUNET_free (quota_out_str);
  608. GNUNET_free (quota_in_str);
  609. }
  610. return GNUNET_NT_COUNT;
  611. }
  612. /**
  613. * Main function that will be run by the scheduler.
  614. *
  615. * @param cls closure
  616. * @param args remaining command-line arguments
  617. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  618. * @param my_cfg configuration
  619. */
  620. static void
  621. run (void *cls,
  622. char *const *args,
  623. const char *cfgfile,
  624. const struct GNUNET_CONFIGURATION_Handle *my_cfg)
  625. {
  626. struct GNUNET_PeerIdentity pid;
  627. struct GNUNET_PeerIdentity cpid;
  628. unsigned int c;
  629. unsigned int type;
  630. cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
  631. addresses = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  632. stat_results = 0;
  633. c = 0;
  634. if (NULL != opt_pid_str)
  635. {
  636. if (GNUNET_OK !=
  637. GNUNET_CRYPTO_eddsa_public_key_from_string (opt_pid_str,
  638. strlen (opt_pid_str),
  639. &pid.public_key))
  640. {
  641. fprintf (stderr, _ ("Failed to parse peer identity `%s'\n"), opt_pid_str);
  642. return;
  643. }
  644. }
  645. if (NULL != cpid_str)
  646. {
  647. if (GNUNET_OK !=
  648. GNUNET_CRYPTO_eddsa_public_key_from_string (cpid_str,
  649. strlen (cpid_str),
  650. &cpid.public_key))
  651. {
  652. fprintf (stderr, _ ("Failed to parse peer identity `%s'\n"), cpid_str);
  653. return;
  654. }
  655. c++;
  656. }
  657. c += opt_list_all + opt_list_used + opt_monitor + opt_set_pref;
  658. if (1 < c)
  659. {
  660. fprintf (stderr,
  661. _ ("Please select one operation: %s or %s or %s or %s or %s\n"),
  662. "--used",
  663. "--all",
  664. "--monitor",
  665. "--preference",
  666. "--quotas");
  667. return;
  668. }
  669. if (0 == c)
  670. opt_list_used = GNUNET_YES; /* set default */
  671. if (opt_print_quotas)
  672. {
  673. ret = print_quotas (cfg);
  674. return;
  675. }
  676. if (opt_list_all)
  677. {
  678. ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
  679. if (NULL == ph)
  680. {
  681. fprintf (stderr, "%s", _ ("Cannot connect to ATS service, exiting...\n"));
  682. return;
  683. }
  684. alh = GNUNET_ATS_performance_list_addresses (ph,
  685. (NULL == opt_pid_str) ? NULL
  686. : &pid,
  687. GNUNET_YES,
  688. &ats_perf_cb,
  689. NULL);
  690. if (NULL == alh)
  691. {
  692. fprintf (stderr,
  693. "%s",
  694. _ ("Cannot issue request to ATS service, exiting...\n"));
  695. shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
  696. return;
  697. }
  698. shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end, NULL);
  699. return;
  700. }
  701. if (opt_list_used)
  702. {
  703. ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
  704. if (NULL == ph)
  705. fprintf (stderr, "%s", _ ("Cannot connect to ATS service, exiting...\n"));
  706. alh = GNUNET_ATS_performance_list_addresses (ph,
  707. (NULL == opt_pid_str) ? NULL
  708. : &pid,
  709. GNUNET_NO,
  710. &ats_perf_cb,
  711. NULL);
  712. if (NULL == alh)
  713. {
  714. fprintf (stderr,
  715. "%s",
  716. _ ("Cannot issue request to ATS service, exiting...\n"));
  717. shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
  718. return;
  719. }
  720. shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end, NULL);
  721. return;
  722. }
  723. if (opt_monitor)
  724. {
  725. ph = GNUNET_ATS_performance_init (cfg, &ats_perf_mon_cb, NULL);
  726. shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end, NULL);
  727. if (NULL == ph)
  728. {
  729. fprintf (stderr, "%s", _ ("Cannot connect to ATS service, exiting...\n"));
  730. GNUNET_SCHEDULER_shutdown ();
  731. }
  732. return;
  733. }
  734. if (opt_set_pref)
  735. {
  736. if (NULL == opt_type_str)
  737. {
  738. fprintf (stderr, "%s", _ ("No preference type given!\n"));
  739. return;
  740. }
  741. if (NULL == opt_pid_str)
  742. {
  743. fprintf (stderr, "%s", _ ("No peer given!\n"));
  744. return;
  745. }
  746. for (c = 0; c < strlen (opt_type_str); c++)
  747. {
  748. if (isupper ((unsigned char) opt_type_str[c]))
  749. opt_type_str[c] = tolower ((unsigned char) opt_type_str[c]);
  750. }
  751. if (0 == strcasecmp ("latency", opt_type_str))
  752. type = GNUNET_ATS_PREFERENCE_LATENCY;
  753. else if (0 == strcasecmp ("bandwidth", opt_type_str))
  754. type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
  755. else
  756. {
  757. fprintf (stderr, "%s", _ ("Valid type required\n"));
  758. return;
  759. }
  760. /* set */
  761. ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
  762. if (NULL == ph)
  763. fprintf (stderr, "%s", _ ("Cannot connect to ATS service, exiting...\n"));
  764. GNUNET_ATS_performance_change_preference (ph,
  765. &pid,
  766. type,
  767. (double) opt_pref_value,
  768. GNUNET_ATS_PREFERENCE_END);
  769. shutdown_task =
  770. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
  771. return;
  772. }
  773. if (NULL != cpid_str)
  774. {
  775. ats_ch = GNUNET_ATS_connectivity_init (cfg);
  776. ats_sh = GNUNET_ATS_connectivity_suggest (ats_ch, &cpid, 1000);
  777. shutdown_task =
  778. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
  779. return;
  780. }
  781. ret = 1;
  782. }
  783. /**
  784. * The main function.
  785. *
  786. * @param argc number of arguments from the command line
  787. * @param argv command line arguments
  788. * @return 0 ok, 1 on error
  789. */
  790. int
  791. main (int argc, char *const *argv)
  792. {
  793. int res;
  794. opt_resolve_addresses_numeric = GNUNET_NO;
  795. opt_monitor = GNUNET_NO;
  796. opt_list_all = GNUNET_NO;
  797. opt_list_used = GNUNET_NO;
  798. opt_set_pref = GNUNET_NO;
  799. stat_pending = 0;
  800. stat_receive_done = GNUNET_NO;
  801. opt_type_str = NULL;
  802. struct GNUNET_GETOPT_CommandLineOption options[] =
  803. { GNUNET_GETOPT_option_flag ('u',
  804. "used",
  805. gettext_noop (
  806. "get list of active addresses currently used"),
  807. &opt_list_used),
  808. GNUNET_GETOPT_option_flag ('a',
  809. "all",
  810. gettext_noop (
  811. "get list of all active addresses"),
  812. &opt_list_all),
  813. GNUNET_GETOPT_option_string ('C',
  814. "connect",
  815. NULL,
  816. gettext_noop ("connect to PEER"),
  817. &cpid_str),
  818. GNUNET_GETOPT_option_flag ('n',
  819. "numeric",
  820. gettext_noop (
  821. "do not resolve IP addresses to hostnames"),
  822. &opt_resolve_addresses_numeric),
  823. GNUNET_GETOPT_option_flag ('m',
  824. "monitor",
  825. gettext_noop ("monitor mode"),
  826. &opt_monitor),
  827. GNUNET_GETOPT_option_flag ('p',
  828. "preference",
  829. gettext_noop (
  830. "set preference for the given peer"),
  831. &opt_set_pref),
  832. GNUNET_GETOPT_option_flag ('q',
  833. "quotas",
  834. gettext_noop ("print all configured quotas"),
  835. &opt_print_quotas),
  836. GNUNET_GETOPT_option_string ('i',
  837. "id",
  838. "TYPE",
  839. gettext_noop ("peer id"),
  840. &opt_pid_str),
  841. GNUNET_GETOPT_option_string ('t',
  842. "type",
  843. "TYPE",
  844. gettext_noop (
  845. "preference type to set: latency | bandwidth"),
  846. &opt_type_str),
  847. GNUNET_GETOPT_option_uint ('k',
  848. "value",
  849. "VALUE",
  850. gettext_noop ("preference value"),
  851. &opt_pref_value),
  852. GNUNET_GETOPT_option_flag (
  853. 'V',
  854. "verbose",
  855. gettext_noop ("verbose output (include ATS address properties)"),
  856. &opt_verbose),
  857. GNUNET_GETOPT_OPTION_END };
  858. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  859. return 2;
  860. res = GNUNET_PROGRAM_run (argc,
  861. argv,
  862. "gnunet-ats",
  863. gettext_noop ("Print information about ATS state"),
  864. options,
  865. &run,
  866. NULL);
  867. GNUNET_free_non_null (opt_pid_str);
  868. GNUNET_free_non_null (opt_type_str);
  869. GNUNET_free ((void *) argv);
  870. if (GNUNET_OK == res)
  871. return ret;
  872. else
  873. return 1;
  874. }
  875. /* end of gnunet-ats.c */