gnunet-ats.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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,
  200. const struct GNUNET_PeerIdentity *key,
  201. void *value)
  202. {
  203. struct ATSAddress *a = value;
  204. GNUNET_assert (GNUNET_OK ==
  205. GNUNET_CONTAINER_multipeermap_remove (addresses,
  206. key,
  207. value));
  208. GNUNET_HELLO_address_free (a->address);
  209. GNUNET_free (a);
  210. return GNUNET_OK;
  211. }
  212. /**
  213. * Task run on shutdown.
  214. *
  215. * @param cls NULL
  216. */
  217. static void
  218. end (void *cls)
  219. {
  220. struct PendingResolutions * pr;
  221. struct PendingResolutions * next;
  222. unsigned int pending;
  223. if (NULL != alh)
  224. {
  225. GNUNET_ATS_performance_list_addresses_cancel (alh);
  226. alh = NULL;
  227. }
  228. if (NULL != ph)
  229. {
  230. GNUNET_ATS_performance_done (ph);
  231. ph = NULL;
  232. }
  233. pending = 0;
  234. next = head;
  235. while (NULL != (pr = next))
  236. {
  237. next = pr->next;
  238. GNUNET_CONTAINER_DLL_remove(head, tail, pr);
  239. GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
  240. GNUNET_free(pr->address);
  241. GNUNET_free(pr);
  242. pending++;
  243. }
  244. GNUNET_CONTAINER_multipeermap_iterate (addresses,
  245. &free_addr_it,
  246. NULL);
  247. GNUNET_CONTAINER_multipeermap_destroy (addresses);
  248. addresses = NULL;
  249. if (0 < pending)
  250. FPRINTF (stdout,
  251. _("%u address resolutions had a timeout\n"),
  252. pending);
  253. if (opt_list_used || opt_list_all)
  254. FPRINTF (stdout,
  255. _("ATS returned stat_results for %u addresses\n"),
  256. stat_results);
  257. if (NULL != ats_sh)
  258. {
  259. GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
  260. ats_sh = NULL;
  261. }
  262. if (NULL != ats_ch)
  263. {
  264. GNUNET_ATS_connectivity_done (ats_ch);
  265. ats_ch = NULL;
  266. }
  267. ret = 0;
  268. }
  269. /**
  270. * Function to call with a textual representation of an address. This
  271. * function will be called several times with different possible
  272. * textual representations, and a last time with @a address being NULL
  273. * to signal the end of the iteration. Note that @a address NULL
  274. * always is the last call, regardless of the value in @a res.
  275. *
  276. * @param cls closure, a `struct PendingResolutions *`
  277. * @param address NULL on end of iteration,
  278. * otherwise 0-terminated printable UTF-8 string,
  279. * in particular an empty string if @a res is #GNUNET_NO
  280. * @param res result of the address to string conversion:
  281. * if #GNUNET_OK: conversion successful
  282. * if #GNUNET_NO: address was invalid (or not supported)
  283. * if #GNUNET_SYSERR: communication error (IPC error)
  284. */
  285. static void
  286. transport_addr_to_str_cb (void *cls,
  287. const char *address,
  288. int res)
  289. {
  290. struct PendingResolutions *pr = cls;
  291. if (NULL == address)
  292. {
  293. /* We're done */
  294. GNUNET_CONTAINER_DLL_remove (head,
  295. tail,
  296. pr);
  297. GNUNET_free (pr->address);
  298. GNUNET_free (pr);
  299. stat_pending--;
  300. if ((GNUNET_YES == stat_receive_done) && (0 == stat_pending))
  301. {
  302. /* All messages received and no resolutions pending*/
  303. if (shutdown_task != NULL)
  304. GNUNET_SCHEDULER_cancel (shutdown_task);
  305. shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
  306. }
  307. return;
  308. }
  309. switch (res)
  310. {
  311. case GNUNET_SYSERR:
  312. FPRINTF (stderr,
  313. "Failed to convert address for peer `%s' plugin `%s' length %u to string (communication error)\n",
  314. GNUNET_i2s (&pr->address->peer),
  315. pr->address->transport_name,
  316. (unsigned int) pr->address->address_length);
  317. return;
  318. case GNUNET_NO:
  319. FPRINTF (stderr,
  320. "Failed to convert address for peer `%s' plugin `%s' length %u to string (address invalid or not supported)\n",
  321. GNUNET_i2s (&pr->address->peer),
  322. pr->address->transport_name,
  323. (unsigned int) pr->address->address_length);
  324. return;
  325. case GNUNET_OK:
  326. /* continues below */
  327. break;
  328. default:
  329. GNUNET_break (0);
  330. return;
  331. }
  332. FPRINTF (stdout,
  333. _("Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
  334. GNUNET_i2s (&pr->address->peer),
  335. pr->address->transport_name,
  336. address,
  337. GNUNET_NT_to_string (pr->properties.scope),
  338. ntohl (pr->bandwidth_out.value__),
  339. ntohl (pr->bandwidth_in.value__),
  340. pr->active ? _("active ") : _("inactive "));
  341. }
  342. /**
  343. * Closure for #find_address_it().
  344. */
  345. struct AddressFindCtx
  346. {
  347. /**
  348. * Address we are looking for.
  349. */
  350. const struct GNUNET_HELLO_Address *src;
  351. /**
  352. * Where to write the `struct ATSAddress` if we found one that matches.
  353. */
  354. struct ATSAddress *res;
  355. };
  356. /**
  357. * Find address corresponding to a given peer.
  358. *
  359. * @param cls the `struct AddressFindCtx *`
  360. * @param key peer identity
  361. * @param value the `struct ATSAddress *` for an existing address
  362. * @return #GNUNET_NO if we found a match, #GNUNET_YES if not
  363. */
  364. static int
  365. find_address_it (void *cls,
  366. const struct GNUNET_PeerIdentity *key,
  367. void *value)
  368. {
  369. struct AddressFindCtx *actx = cls;
  370. struct ATSAddress *exist = value;
  371. if (0 == GNUNET_HELLO_address_cmp (actx->src, exist->address))
  372. {
  373. actx->res = exist;
  374. return GNUNET_NO;
  375. }
  376. return GNUNET_YES;
  377. }
  378. /**
  379. * Signature of a function that is called with QoS information about an address.
  380. *
  381. * @param cls closure (NULL)
  382. * @param address the address, NULL if ATS service was disconnected
  383. * @param active #GNUNET_YES if this address is actively used
  384. * to maintain a connection to a peer;
  385. * #GNUNET_NO if the address is not actively used;
  386. * #GNUNET_SYSERR if this address is no longer available for ATS
  387. * @param bandwidth_out assigned outbound bandwidth for the connection
  388. * @param bandwidth_in assigned inbound bandwidth for the connection
  389. * @param prop performance data for the address (as far as known)
  390. */
  391. static void
  392. ats_perf_mon_cb (void *cls,
  393. const struct GNUNET_HELLO_Address *address,
  394. int active,
  395. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  396. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  397. const struct GNUNET_ATS_Properties *prop)
  398. {
  399. struct PendingResolutions *pr;
  400. struct PendingResolutions *cur;
  401. struct PendingResolutions *next;
  402. if (NULL == address)
  403. {
  404. /* ATS service temporarily disconnected, remove current state */
  405. next = head;
  406. for (cur = next; NULL != cur; cur = next)
  407. {
  408. next = cur->next;
  409. GNUNET_CONTAINER_DLL_remove (head, tail, cur);
  410. GNUNET_TRANSPORT_address_to_string_cancel (cur->tats_ctx);
  411. GNUNET_HELLO_address_free (cur->address);
  412. GNUNET_free (cur);
  413. }
  414. GNUNET_CONTAINER_multipeermap_iterate (addresses,
  415. &free_addr_it,
  416. NULL);
  417. return;
  418. }
  419. if (GNUNET_SYSERR == active)
  420. {
  421. /* remove address */
  422. struct AddressFindCtx actx;
  423. actx.src = address;
  424. actx.res = NULL;
  425. GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
  426. &address->peer,
  427. &find_address_it,
  428. &actx);
  429. if (NULL == actx.res)
  430. {
  431. GNUNET_break (0);
  432. return;
  433. }
  434. GNUNET_break(GNUNET_OK ==
  435. GNUNET_CONTAINER_multipeermap_remove (addresses,
  436. &address->peer,
  437. actx.res));
  438. FPRINTF (stdout,
  439. _("Removed address of peer `%s' with plugin `%s'\n"),
  440. GNUNET_i2s (&address->peer),
  441. actx.res->address->transport_name);
  442. GNUNET_HELLO_address_free (actx.res);
  443. return;
  444. }
  445. if (GNUNET_NO == opt_verbose)
  446. {
  447. struct AddressFindCtx actx;
  448. struct ATSAddress *a;
  449. actx.src = address;
  450. actx.res = NULL;
  451. GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
  452. &address->peer,
  453. &find_address_it,
  454. &actx);
  455. if ((NULL != actx.res))
  456. {
  457. if ((bandwidth_in.value__ == actx.res->bandwidth_in.value__) &&
  458. (bandwidth_out.value__ == actx.res->bandwidth_out.value__) &&
  459. (active == actx.res->active))
  460. {
  461. return; /* Nothing to do here */
  462. }
  463. else
  464. {
  465. actx.res->bandwidth_in = bandwidth_in;
  466. actx.res->bandwidth_out = bandwidth_out;
  467. }
  468. }
  469. else
  470. {
  471. a = GNUNET_new (struct ATSAddress);
  472. a->address = GNUNET_HELLO_address_copy(address);
  473. a->bandwidth_in = bandwidth_in;
  474. a->bandwidth_out = bandwidth_out;
  475. a->active = active;
  476. GNUNET_CONTAINER_multipeermap_put (addresses,
  477. &address->peer,
  478. a,
  479. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  480. }
  481. }
  482. pr = GNUNET_new (struct PendingResolutions);
  483. pr->properties = *prop;
  484. pr->address = GNUNET_HELLO_address_copy (address);
  485. pr->bandwidth_in = bandwidth_in;
  486. pr->bandwidth_out = bandwidth_out;
  487. pr->active = active;
  488. pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (cfg, address,
  489. opt_resolve_addresses_numeric,
  490. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
  491. &transport_addr_to_str_cb,
  492. pr);
  493. GNUNET_CONTAINER_DLL_insert (head, tail, pr);
  494. stat_results++;
  495. stat_pending++;
  496. }
  497. /**
  498. * Signature of a function that is called with QoS information about an address.
  499. *
  500. * @param cls closure (NULL)
  501. * @param address the address, NULL if ATS service was disconnected
  502. * @param active is this address actively used to maintain a connection
  503. to a peer
  504. * @param bandwidth_out assigned outbound bandwidth for the connection
  505. * @param bandwidth_in assigned inbound bandwidth for the connection
  506. * @param prop performance data for the address (as far as known)
  507. */
  508. static void
  509. ats_perf_cb (void *cls,
  510. const struct GNUNET_HELLO_Address *address,
  511. int active,
  512. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  513. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  514. const struct GNUNET_ATS_Properties *prop)
  515. {
  516. struct PendingResolutions *pr;
  517. if (NULL == address)
  518. {
  519. /* All messages received */
  520. stat_receive_done = GNUNET_YES;
  521. alh = NULL;
  522. if (0 == stat_pending)
  523. {
  524. /* All messages received and no resolutions pending*/
  525. if (shutdown_task != NULL)
  526. GNUNET_SCHEDULER_cancel (shutdown_task);
  527. shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
  528. }
  529. return;
  530. }
  531. pr = GNUNET_new (struct PendingResolutions);
  532. pr->properties = *prop;
  533. pr->address = GNUNET_HELLO_address_copy (address);
  534. pr->bandwidth_in = bandwidth_in;
  535. pr->bandwidth_out = bandwidth_out;
  536. pr->active = active;
  537. pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (cfg, address,
  538. opt_resolve_addresses_numeric,
  539. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
  540. &transport_addr_to_str_cb, pr);
  541. GNUNET_CONTAINER_DLL_insert (head, tail, pr);
  542. stat_results++;
  543. stat_pending++;
  544. }
  545. /**
  546. * Print information about the quotas configured for the various
  547. * network scopes.
  548. *
  549. * @param cfg configuration to obtain quota information from
  550. * @return total number of ATS network types known
  551. */
  552. static unsigned int
  553. print_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg)
  554. {
  555. char * entry_in = NULL;
  556. char * entry_out = NULL;
  557. char * quota_out_str;
  558. char * quota_in_str;
  559. unsigned long long int quota_out;
  560. unsigned long long int quota_in;
  561. int c;
  562. for (c = 0; (c < GNUNET_NT_COUNT); c++)
  563. {
  564. GNUNET_asprintf (&entry_out,
  565. "%s_QUOTA_OUT",
  566. GNUNET_NT_to_string (c));
  567. GNUNET_asprintf (&entry_in,
  568. "%s_QUOTA_IN",
  569. GNUNET_NT_to_string (c));
  570. /* quota out */
  571. if (GNUNET_OK ==
  572. GNUNET_CONFIGURATION_get_value_string (cfg,
  573. "ats",
  574. entry_out,
  575. &quota_out_str))
  576. {
  577. if (0 == strcmp (quota_out_str, UNLIMITED_STRING)
  578. || (GNUNET_SYSERR ==
  579. GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
  580. &quota_out)))
  581. quota_out = UINT32_MAX;
  582. GNUNET_free(quota_out_str);
  583. GNUNET_asprintf (&quota_out_str, "%llu", quota_out);
  584. }
  585. else
  586. {
  587. FPRINTF (stderr,
  588. "Outbound quota for network `%11s' not configured!\n",
  589. GNUNET_NT_to_string (c));
  590. GNUNET_asprintf (&quota_out_str, "-");
  591. }
  592. GNUNET_free (entry_out);
  593. /* quota in */
  594. if (GNUNET_OK ==
  595. GNUNET_CONFIGURATION_get_value_string (cfg,
  596. "ats",
  597. entry_in,
  598. &quota_in_str))
  599. {
  600. if (0 == strcmp (quota_in_str, UNLIMITED_STRING)
  601. || (GNUNET_SYSERR ==
  602. GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &quota_in)))
  603. quota_in = UINT32_MAX;
  604. GNUNET_free (quota_in_str);
  605. GNUNET_asprintf (&quota_in_str, "%llu", quota_in);
  606. }
  607. else
  608. {
  609. FPRINTF (stderr,
  610. "Inbound quota for network `%11s' not configured!\n",
  611. GNUNET_NT_to_string (c));
  612. GNUNET_asprintf (&quota_in_str, "-");
  613. }
  614. GNUNET_free(entry_in);
  615. FPRINTF (stdout,
  616. _("Quota for network `%11s' (in/out): %10s / %10s\n"),
  617. GNUNET_NT_to_string (c),
  618. quota_in_str,
  619. quota_out_str);
  620. GNUNET_free(quota_out_str);
  621. GNUNET_free(quota_in_str);
  622. }
  623. return GNUNET_NT_COUNT;
  624. }
  625. /**
  626. * Main function that will be run by the scheduler.
  627. *
  628. * @param cls closure
  629. * @param args remaining command-line arguments
  630. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  631. * @param my_cfg configuration
  632. */
  633. static void
  634. run (void *cls,
  635. char * const *args,
  636. const char *cfgfile,
  637. const struct GNUNET_CONFIGURATION_Handle *my_cfg)
  638. {
  639. struct GNUNET_PeerIdentity pid;
  640. struct GNUNET_PeerIdentity cpid;
  641. unsigned int c;
  642. unsigned int type;
  643. cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
  644. addresses = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  645. stat_results = 0;
  646. c = 0;
  647. if (NULL != opt_pid_str)
  648. {
  649. if (GNUNET_OK !=
  650. GNUNET_CRYPTO_eddsa_public_key_from_string (opt_pid_str,
  651. strlen (opt_pid_str),
  652. &pid.public_key))
  653. {
  654. FPRINTF (stderr,
  655. _("Failed to parse peer identity `%s'\n"),
  656. opt_pid_str);
  657. return;
  658. }
  659. }
  660. if (NULL != cpid_str)
  661. {
  662. if (GNUNET_OK !=
  663. GNUNET_CRYPTO_eddsa_public_key_from_string (cpid_str,
  664. strlen (cpid_str),
  665. &cpid.public_key))
  666. {
  667. FPRINTF (stderr,
  668. _("Failed to parse peer identity `%s'\n"),
  669. cpid_str);
  670. return;
  671. }
  672. c++;
  673. }
  674. c += opt_list_all + opt_list_used + opt_monitor + opt_set_pref;
  675. if (1 < c)
  676. {
  677. FPRINTF (stderr,
  678. _("Please select one operation: %s or %s or %s or %s or %s\n"),
  679. "--used",
  680. "--all",
  681. "--monitor",
  682. "--preference",
  683. "--quotas");
  684. return;
  685. }
  686. if (0 == c)
  687. opt_list_used = GNUNET_YES; /* set default */
  688. if (opt_print_quotas)
  689. {
  690. ret = print_quotas (cfg);
  691. return;
  692. }
  693. if (opt_list_all)
  694. {
  695. ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
  696. if (NULL == ph)
  697. {
  698. FPRINTF (stderr,
  699. "%s",
  700. _("Cannot connect to ATS service, exiting...\n"));
  701. return;
  702. }
  703. alh = GNUNET_ATS_performance_list_addresses (ph,
  704. (NULL == opt_pid_str) ? NULL : &pid,
  705. GNUNET_YES,
  706. &ats_perf_cb, NULL);
  707. if (NULL == alh)
  708. {
  709. FPRINTF (stderr,
  710. "%s",
  711. _("Cannot issue request to ATS service, exiting...\n"));
  712. shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
  713. return;
  714. }
  715. shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end,
  716. NULL);
  717. return;
  718. }
  719. if (opt_list_used)
  720. {
  721. ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
  722. if (NULL == ph)
  723. FPRINTF (stderr,
  724. "%s",
  725. _("Cannot connect to ATS service, exiting...\n"));
  726. alh = GNUNET_ATS_performance_list_addresses (ph,
  727. (NULL == opt_pid_str)
  728. ? NULL
  729. : &pid,
  730. GNUNET_NO,
  731. &ats_perf_cb, NULL);
  732. if (NULL == alh)
  733. {
  734. FPRINTF (stderr,
  735. "%s",
  736. _("Cannot issue request to ATS service, exiting...\n"));
  737. shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
  738. return;
  739. }
  740. shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end,
  741. NULL);
  742. return;
  743. }
  744. if (opt_monitor)
  745. {
  746. ph = GNUNET_ATS_performance_init (cfg,
  747. &ats_perf_mon_cb,
  748. NULL);
  749. shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end,
  750. NULL);
  751. if (NULL == ph)
  752. {
  753. FPRINTF (stderr,
  754. "%s",
  755. _("Cannot connect to ATS service, exiting...\n"));
  756. GNUNET_SCHEDULER_shutdown ();
  757. }
  758. return;
  759. }
  760. if (opt_set_pref)
  761. {
  762. if (NULL == opt_type_str)
  763. {
  764. FPRINTF (stderr,
  765. "%s",
  766. _("No preference type given!\n"));
  767. return;
  768. }
  769. if (NULL == opt_pid_str)
  770. {
  771. FPRINTF (stderr,
  772. "%s",
  773. _("No peer given!\n"));
  774. return;
  775. }
  776. for (c = 0; c < strlen (opt_type_str); c++)
  777. {
  778. if (isupper ((unsigned char) opt_type_str[c]))
  779. opt_type_str[c] = tolower ((unsigned char) opt_type_str[c]);
  780. }
  781. if (0 == strcasecmp ("latency", opt_type_str))
  782. type = GNUNET_ATS_PREFERENCE_LATENCY;
  783. else if (0 == strcasecmp ("bandwidth", opt_type_str))
  784. type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
  785. else
  786. {
  787. FPRINTF (stderr,
  788. "%s",
  789. _("Valid type required\n"));
  790. return;
  791. }
  792. /* set */
  793. ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
  794. if (NULL == ph)
  795. FPRINTF (stderr,
  796. "%s",
  797. _("Cannot connect to ATS service, exiting...\n"));
  798. GNUNET_ATS_performance_change_preference (ph,
  799. &pid,
  800. type,
  801. (double) opt_pref_value,
  802. GNUNET_ATS_PREFERENCE_END);
  803. shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  804. &end,
  805. NULL);
  806. return;
  807. }
  808. if (NULL != cpid_str)
  809. {
  810. ats_ch = GNUNET_ATS_connectivity_init (cfg);
  811. ats_sh = GNUNET_ATS_connectivity_suggest (ats_ch,
  812. &cpid,
  813. 1000);
  814. shutdown_task
  815. = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  816. &end,
  817. NULL);
  818. return;
  819. }
  820. ret = 1;
  821. }
  822. /**
  823. * The main function.
  824. *
  825. * @param argc number of arguments from the command line
  826. * @param argv command line arguments
  827. * @return 0 ok, 1 on error
  828. */
  829. int
  830. main (int argc,
  831. char * const *argv)
  832. {
  833. int res;
  834. opt_resolve_addresses_numeric = GNUNET_NO;
  835. opt_monitor = GNUNET_NO;
  836. opt_list_all = GNUNET_NO;
  837. opt_list_used = GNUNET_NO;
  838. opt_set_pref = GNUNET_NO;
  839. stat_pending = 0;
  840. stat_receive_done = GNUNET_NO;
  841. opt_type_str = NULL;
  842. struct GNUNET_GETOPT_CommandLineOption options[] = {
  843. GNUNET_GETOPT_option_flag ('u',
  844. "used",
  845. gettext_noop ("get list of active addresses currently used"),
  846. &opt_list_used),
  847. GNUNET_GETOPT_option_flag ('a',
  848. "all",
  849. gettext_noop ("get list of all active addresses"),
  850. &opt_list_all),
  851. GNUNET_GETOPT_option_string ('C',
  852. "connect",
  853. NULL,
  854. gettext_noop ("connect to PEER"),
  855. &cpid_str),
  856. GNUNET_GETOPT_option_flag ('n',
  857. "numeric",
  858. gettext_noop ("do not resolve IP addresses to hostnames"),
  859. &opt_resolve_addresses_numeric),
  860. GNUNET_GETOPT_option_flag ('m',
  861. "monitor",
  862. gettext_noop ("monitor mode"),
  863. &opt_monitor),
  864. GNUNET_GETOPT_option_flag ('p',
  865. "preference",
  866. gettext_noop ("set preference for the given peer"),
  867. &opt_set_pref),
  868. GNUNET_GETOPT_option_flag ('q',
  869. "quotas",
  870. gettext_noop ("print all configured quotas"),
  871. &opt_print_quotas),
  872. GNUNET_GETOPT_option_string ('i',
  873. "id",
  874. "TYPE",
  875. gettext_noop ("peer id"),
  876. &opt_pid_str),
  877. GNUNET_GETOPT_option_string ('t',
  878. "type",
  879. "TYPE",
  880. gettext_noop ("preference type to set: latency | bandwidth"),
  881. &opt_type_str),
  882. GNUNET_GETOPT_option_uint ('k',
  883. "value",
  884. "VALUE",
  885. gettext_noop ("preference value"),
  886. &opt_pref_value),
  887. GNUNET_GETOPT_option_flag ('V',
  888. "verbose",
  889. gettext_noop ("verbose output (include ATS address properties)"),
  890. &opt_verbose),
  891. GNUNET_GETOPT_OPTION_END
  892. };
  893. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  894. return 2;
  895. res = GNUNET_PROGRAM_run (argc, argv,
  896. "gnunet-ats",
  897. gettext_noop ("Print information about ATS state"),
  898. options,
  899. &run, NULL);
  900. GNUNET_free_non_null(opt_pid_str);
  901. GNUNET_free_non_null(opt_type_str);
  902. GNUNET_free((void *) argv);
  903. if (GNUNET_OK == res)
  904. return ret;
  905. else
  906. return 1;
  907. }
  908. /* end of gnunet-ats.c */