gnunet-peerinfo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2014, 2016 GNUnet e.V.
  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., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file peerinfo-tool/gnunet-peerinfo.c
  19. * @brief Print information about other known peers.
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_transport_service.h"
  27. #include "gnunet_transport_hello_service.h"
  28. #include "gnunet_peerinfo_service.h"
  29. #include "gnunet-peerinfo_plugins.h"
  30. /**
  31. * How long until we time out during address lookup?
  32. */
  33. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  34. /**
  35. * Structure we use to collect printable address information.
  36. */
  37. struct PrintContext;
  38. /**
  39. * Record we keep for each printable address.
  40. */
  41. struct AddressRecord
  42. {
  43. /**
  44. * Current address-to-string context (if active, otherwise NULL).
  45. */
  46. struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
  47. /**
  48. * Address expiration time
  49. */
  50. struct GNUNET_TIME_Absolute expiration;
  51. /**
  52. * Printable address.
  53. */
  54. char *result;
  55. /**
  56. * Print context this address record belongs to.
  57. */
  58. struct PrintContext *pc;
  59. };
  60. /**
  61. * Structure we use to collect printable address information.
  62. */
  63. struct PrintContext
  64. {
  65. /**
  66. * Kept in DLL.
  67. */
  68. struct PrintContext *next;
  69. /**
  70. * Kept in DLL.
  71. */
  72. struct PrintContext *prev;
  73. /**
  74. * Identity of the peer.
  75. */
  76. struct GNUNET_PeerIdentity peer;
  77. /**
  78. * List of printable addresses.
  79. */
  80. struct AddressRecord *address_list;
  81. /**
  82. * Number of completed addresses in @e address_list.
  83. */
  84. unsigned int num_addresses;
  85. /**
  86. * Number of addresses allocated in @e address_list.
  87. */
  88. unsigned int address_list_size;
  89. /**
  90. * Current offset in @e address_list (counted down).
  91. */
  92. unsigned int off;
  93. /**
  94. * Hello was friend only, #GNUNET_YES or #GNUNET_NO
  95. */
  96. int friend_only;
  97. };
  98. /**
  99. * Option '-n'
  100. */
  101. static int no_resolve;
  102. /**
  103. * Option '-q'
  104. */
  105. static int be_quiet;
  106. /**
  107. * Option '-f'
  108. */
  109. static int include_friend_only;
  110. /**
  111. * Option '-s'
  112. */
  113. static int get_self;
  114. /**
  115. * Option
  116. */
  117. static int get_uri;
  118. /**
  119. * Option
  120. */
  121. static int default_operation;
  122. /**
  123. * Option '-i'
  124. */
  125. static int get_info;
  126. /**
  127. * Option
  128. */
  129. static char *put_uri;
  130. /**
  131. * Option -d
  132. */
  133. static char *dump_hello;
  134. /**
  135. * Handle to peerinfo service.
  136. */
  137. static struct GNUNET_PEERINFO_Handle *peerinfo;
  138. /**
  139. * Configuration handle.
  140. */
  141. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  142. /**
  143. * Main state machine task (if active).
  144. */
  145. static struct GNUNET_SCHEDULER_Task *tt;
  146. /**
  147. * Pending #GNUNET_TRANSPORT_hello_get() operation.
  148. */
  149. static struct GNUNET_TRANSPORT_HelloGetHandle *gh;
  150. /**
  151. * Current iterator context (if active, otherwise NULL).
  152. */
  153. static struct GNUNET_PEERINFO_IteratorContext *pic;
  154. /**
  155. * My peer identity.
  156. */
  157. static struct GNUNET_PeerIdentity my_peer_identity;
  158. /**
  159. * Head of list of print contexts.
  160. */
  161. static struct PrintContext *pc_head;
  162. /**
  163. * Tail of list of print contexts.
  164. */
  165. static struct PrintContext *pc_tail;
  166. /**
  167. * Handle to current #GNUNET_PEERINFO_add_peer() operation.
  168. */
  169. static struct GNUNET_MQ_Envelope *ac;
  170. /**
  171. * Hello of this peer (if initialized).
  172. */
  173. static struct GNUNET_HELLO_Message *my_hello;
  174. /**
  175. * Main state machine that goes over all options and
  176. * runs the next requested function.
  177. *
  178. * @param cls unused
  179. */
  180. static void
  181. state_machine (void *cls);
  182. /* ********************* 'get_info' ******************* */
  183. /**
  184. * Print the collected address information to the console and free @a pc.
  185. *
  186. * @param pc printing context
  187. */
  188. static void
  189. dump_pc (struct PrintContext *pc)
  190. {
  191. unsigned int i;
  192. printf (_("%sPeer `%s'\n"),
  193. (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
  194. GNUNET_i2s_full (&pc->peer));
  195. for (i = 0; i < pc->num_addresses; i++)
  196. {
  197. if (NULL != pc->address_list[i].result)
  198. {
  199. printf (_("\tExpires: %s \t %s\n"),
  200. GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration),
  201. pc->address_list[i].result);
  202. GNUNET_free (pc->address_list[i].result);
  203. }
  204. }
  205. printf ("\n");
  206. GNUNET_free_non_null (pc->address_list);
  207. GNUNET_CONTAINER_DLL_remove (pc_head,
  208. pc_tail,
  209. pc);
  210. GNUNET_free (pc);
  211. if ( (NULL == pc_head) &&
  212. (NULL == pic) )
  213. tt = GNUNET_SCHEDULER_add_now (&state_machine,
  214. NULL);
  215. }
  216. /* ************************* list all known addresses **************** */
  217. /**
  218. * Function to call with a human-readable format of an address
  219. *
  220. * @param cls closure
  221. * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
  222. * @param res result of the address to string conversion:
  223. * if #GNUNET_OK: address was valid (conversion to
  224. * string might still have failed)
  225. * if #GNUNET_SYSERR: address is invalid
  226. */
  227. static void
  228. process_resolved_address (void *cls,
  229. const char *address,
  230. int res)
  231. {
  232. struct AddressRecord *ar = cls;
  233. struct PrintContext *pc = ar->pc;
  234. if (NULL != address)
  235. {
  236. if (0 != strlen (address))
  237. {
  238. if (NULL != ar->result)
  239. GNUNET_free (ar->result);
  240. ar->result = GNUNET_strdup (address);
  241. }
  242. return;
  243. }
  244. ar->atsc = NULL;
  245. if (GNUNET_SYSERR == res)
  246. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  247. _("Failure: Cannot convert address to string for peer `%s'\n"),
  248. GNUNET_i2s (&ar->pc->peer));
  249. pc->num_addresses++;
  250. if (pc->num_addresses == pc->address_list_size)
  251. dump_pc (pc);
  252. }
  253. /**
  254. * Iterator callback to go over all addresses and count them.
  255. *
  256. * @param cls `struct PrintContext *` with `off` to increment
  257. * @param address the address
  258. * @param expiration expiration time
  259. * @return #GNUNET_OK to keep the address and continue
  260. */
  261. static int
  262. count_address (void *cls,
  263. const struct GNUNET_HELLO_Address *address,
  264. struct GNUNET_TIME_Absolute expiration)
  265. {
  266. struct PrintContext *pc = cls;
  267. pc->off++;
  268. return GNUNET_OK;
  269. }
  270. /**
  271. * Iterator callback to go over all addresses.
  272. *
  273. * @param cls closure
  274. * @param address the address
  275. * @param expiration expiration time
  276. * @return #GNUNET_OK to keep the address and continue
  277. */
  278. static int
  279. print_address (void *cls,
  280. const struct GNUNET_HELLO_Address *address,
  281. struct GNUNET_TIME_Absolute expiration)
  282. {
  283. struct PrintContext *pc = cls;
  284. struct AddressRecord *ar;
  285. GNUNET_assert (0 < pc->off);
  286. ar = &pc->address_list[--pc->off];
  287. ar->pc = pc;
  288. ar->expiration = expiration;
  289. GNUNET_asprintf (&ar->result,
  290. "%s:%u:%u",
  291. address->transport_name,
  292. address->address_length,
  293. address->local_info);
  294. ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
  295. address,
  296. no_resolve,
  297. TIMEOUT,
  298. &process_resolved_address, ar);
  299. return GNUNET_OK;
  300. }
  301. /**
  302. * Print information about the peer. Currently prints the `struct
  303. * GNUNET_PeerIdentity` and the transport address.
  304. *
  305. * @param cls the `struct PrintContext *`
  306. * @param peer identity of the peer
  307. * @param hello addresses of the peer
  308. * @param err_msg error message
  309. */
  310. static void
  311. print_peer_info (void *cls,
  312. const struct GNUNET_PeerIdentity *peer,
  313. const struct GNUNET_HELLO_Message *hello,
  314. const char *err_msg)
  315. {
  316. struct PrintContext *pc;
  317. int friend_only;
  318. if (NULL == peer)
  319. {
  320. pic = NULL; /* end of iteration */
  321. if (NULL != err_msg)
  322. {
  323. FPRINTF (stderr,
  324. _("Error in communication with PEERINFO service: %s\n"),
  325. err_msg);
  326. }
  327. if (NULL == pc_head)
  328. tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
  329. return;
  330. }
  331. friend_only = GNUNET_NO;
  332. if (NULL != hello)
  333. friend_only = GNUNET_HELLO_is_friend_only (hello);
  334. if ( (GNUNET_YES == be_quiet) ||
  335. (NULL == hello) )
  336. {
  337. printf ("%s%s\n",
  338. (GNUNET_YES == friend_only) ? "F2F: " : "",
  339. GNUNET_i2s_full (peer));
  340. return;
  341. }
  342. pc = GNUNET_new (struct PrintContext);
  343. GNUNET_CONTAINER_DLL_insert (pc_head,
  344. pc_tail,
  345. pc);
  346. pc->peer = *peer;
  347. pc->friend_only = friend_only;
  348. GNUNET_HELLO_iterate_addresses (hello,
  349. GNUNET_NO,
  350. &count_address,
  351. pc);
  352. if (0 == pc->off)
  353. {
  354. dump_pc (pc);
  355. return;
  356. }
  357. pc->address_list_size = pc->off;
  358. pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
  359. GNUNET_HELLO_iterate_addresses (hello,
  360. GNUNET_NO,
  361. &print_address,
  362. pc);
  363. }
  364. /* ************************* DUMP Hello ************************** */
  365. /**
  366. * Count the number of addresses in the HELLO.
  367. *
  368. * @param cls pointer to an `int *` used for the counter
  369. * @param address an address to count
  370. * @param expiration (unused)
  371. * @return #GNUNET_OK
  372. */
  373. static int
  374. count_addr (void *cls,
  375. const struct GNUNET_HELLO_Address *address,
  376. struct GNUNET_TIME_Absolute expiration)
  377. {
  378. int *c = cls;
  379. (*c)++;
  380. return GNUNET_OK;
  381. }
  382. /**
  383. * Write HELLO of my peer to a file.
  384. *
  385. * @param cls the `struct GetUriContext *`
  386. * @param peer identity of the peer (unused)
  387. * @param hello addresses of the peer
  388. * @param err_msg error message
  389. */
  390. static void
  391. dump_my_hello ()
  392. {
  393. unsigned int size;
  394. unsigned int c_addr;
  395. size = GNUNET_HELLO_size (my_hello);
  396. if (0 == size)
  397. {
  398. FPRINTF (stderr,
  399. _("Failure: Received invalid %s\n"),
  400. "HELLO");
  401. return;
  402. }
  403. if (GNUNET_SYSERR ==
  404. GNUNET_DISK_fn_write (dump_hello,
  405. my_hello,
  406. size,
  407. GNUNET_DISK_PERM_USER_READ |
  408. GNUNET_DISK_PERM_USER_WRITE |
  409. GNUNET_DISK_PERM_GROUP_READ |
  410. GNUNET_DISK_PERM_OTHER_READ))
  411. {
  412. FPRINTF (stderr,
  413. _("Failed to write HELLO with %u bytes to file `%s'\n"),
  414. size,
  415. dump_hello);
  416. if (0 != UNLINK (dump_hello))
  417. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  418. GNUNET_ERROR_TYPE_BULK,
  419. "unlink",
  420. dump_hello);
  421. }
  422. c_addr = 0;
  423. GNUNET_HELLO_iterate_addresses (my_hello,
  424. GNUNET_NO,
  425. count_addr,
  426. &c_addr);
  427. if (! be_quiet)
  428. {
  429. FPRINTF (stderr,
  430. _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
  431. (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only": "public",
  432. c_addr,
  433. size,
  434. dump_hello);
  435. }
  436. GNUNET_free (dump_hello);
  437. dump_hello = NULL;
  438. }
  439. /* ************************* GET URI ************************** */
  440. /**
  441. * Print URI of the peer.
  442. *
  443. * @param cls the `struct GetUriContext *`
  444. * @param peer identity of the peer (unused)
  445. * @param hello addresses of the peer
  446. * @param err_msg error message
  447. */
  448. static void
  449. print_my_uri (void *cls,
  450. const struct GNUNET_PeerIdentity *peer,
  451. const struct GNUNET_HELLO_Message *hello,
  452. const char *err_msg)
  453. {
  454. char *uri;
  455. if (NULL == peer)
  456. {
  457. pic = NULL;
  458. if (NULL != err_msg)
  459. FPRINTF (stderr,
  460. _("Error in communication with PEERINFO service: %s\n"),
  461. err_msg);
  462. tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
  463. return;
  464. }
  465. if (NULL == hello)
  466. return;
  467. uri = GNUNET_HELLO_compose_uri (hello,
  468. &GPI_plugins_find);
  469. if (NULL != uri)
  470. {
  471. printf ("%s\n",
  472. (const char *) uri);
  473. GNUNET_free (uri);
  474. }
  475. }
  476. /* ************************* import HELLO by URI ********************* */
  477. /**
  478. * Continuation called from #GNUNET_PEERINFO_add_peer()
  479. *
  480. * @param cls closure, NULL
  481. */
  482. static void
  483. add_continuation (void *cls)
  484. {
  485. ac = NULL;
  486. tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
  487. }
  488. /**
  489. * Parse the PUT URI given at the command line and add it to our peerinfo
  490. * database.
  491. *
  492. * @param put_uri URI string to parse
  493. * @return #GNUNET_OK on success,
  494. * #GNUNET_SYSERR if the URI was invalid,
  495. * #GNUNET_NO on other errors
  496. */
  497. static int
  498. parse_hello_uri (const char *put_uri)
  499. {
  500. struct GNUNET_HELLO_Message *hello = NULL;
  501. int ret = GNUNET_HELLO_parse_uri (put_uri,
  502. &my_peer_identity.public_key,
  503. &hello,
  504. &GPI_plugins_find);
  505. if (NULL != hello)
  506. {
  507. /* WARNING: this adds the address from URI WITHOUT verification! */
  508. if (GNUNET_OK == ret)
  509. ac = GNUNET_PEERINFO_add_peer (peerinfo,
  510. hello,
  511. &add_continuation,
  512. NULL);
  513. else
  514. tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
  515. GNUNET_free (hello);
  516. }
  517. return ret;
  518. }
  519. /* ************************ Main state machine ********************* */
  520. /**
  521. * Main state machine that goes over all options and
  522. * runs the next requested function.
  523. *
  524. * @param cls unused
  525. */
  526. static void
  527. shutdown_task (void *cls)
  528. {
  529. struct PrintContext *pc;
  530. struct AddressRecord *ar;
  531. unsigned int i;
  532. if (NULL != ac)
  533. {
  534. GNUNET_MQ_send_cancel (ac);
  535. ac = NULL;
  536. }
  537. if (NULL != tt)
  538. {
  539. GNUNET_SCHEDULER_cancel (tt);
  540. tt = NULL;
  541. }
  542. if (NULL != pic)
  543. {
  544. GNUNET_PEERINFO_iterate_cancel (pic);
  545. pic = NULL;
  546. }
  547. if (NULL != gh)
  548. {
  549. GNUNET_TRANSPORT_hello_get_cancel (gh);
  550. gh = NULL;
  551. }
  552. while (NULL != (pc = pc_head))
  553. {
  554. GNUNET_CONTAINER_DLL_remove (pc_head,
  555. pc_tail,
  556. pc);
  557. for (i=0;i<pc->address_list_size;i++)
  558. {
  559. ar = &pc->address_list[i];
  560. GNUNET_free_non_null (ar->result);
  561. if (NULL != ar->atsc)
  562. {
  563. GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
  564. ar->atsc = NULL;
  565. }
  566. }
  567. GNUNET_free_non_null (pc->address_list);
  568. GNUNET_free (pc);
  569. }
  570. GPI_plugins_unload ();
  571. if (NULL != peerinfo)
  572. {
  573. GNUNET_PEERINFO_disconnect (peerinfo);
  574. peerinfo = NULL;
  575. }
  576. if (NULL != my_hello)
  577. {
  578. GNUNET_free (my_hello);
  579. my_hello = NULL;
  580. }
  581. }
  582. /**
  583. * Function called with our peer's HELLO message.
  584. * Used to obtain our peer's public key.
  585. *
  586. * @param cls NULL
  587. * @param hello the HELLO message
  588. */
  589. static void
  590. hello_callback (void *cls,
  591. const struct GNUNET_MessageHeader *hello)
  592. {
  593. if (NULL == hello)
  594. {
  595. fprintf (stderr,
  596. "Failed to get my own HELLO from this peer!\n");
  597. GNUNET_SCHEDULER_shutdown ();
  598. return;
  599. }
  600. my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello);
  601. GNUNET_assert (GNUNET_OK ==
  602. GNUNET_HELLO_get_id (my_hello,
  603. &my_peer_identity));
  604. GNUNET_TRANSPORT_hello_get_cancel (gh);
  605. gh = NULL;
  606. if (NULL != dump_hello)
  607. dump_my_hello ();
  608. tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
  609. }
  610. /**
  611. * Main function that will be run by the scheduler.
  612. *
  613. * @param cls closure
  614. * @param args remaining command-line arguments
  615. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  616. * @param c configuration
  617. */
  618. static void
  619. run (void *cls,
  620. char *const *args,
  621. const char *cfgfile,
  622. const struct GNUNET_CONFIGURATION_Handle *c)
  623. {
  624. cfg = c;
  625. if ( (NULL != args[0]) &&
  626. (NULL == put_uri) &&
  627. (args[0] == strcasestr (args[0],
  628. "gnunet://hello/")) )
  629. {
  630. put_uri = GNUNET_strdup (args[0]);
  631. args++;
  632. }
  633. if (NULL != args[0])
  634. {
  635. FPRINTF (stderr,
  636. _("Invalid command line argument `%s'\n"),
  637. args[0]);
  638. return;
  639. }
  640. if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
  641. {
  642. FPRINTF (stderr,
  643. "%s",
  644. "Could not access PEERINFO service. Exiting.\n");
  645. return;
  646. }
  647. if ( (GNUNET_YES == get_self) ||
  648. (GNUNET_YES == get_uri) ||
  649. (NULL != dump_hello) )
  650. {
  651. gh = GNUNET_TRANSPORT_hello_get (cfg,
  652. GNUNET_TRANSPORT_AC_ANY,
  653. &hello_callback,
  654. NULL);
  655. }
  656. else
  657. {
  658. tt = GNUNET_SCHEDULER_add_now (&state_machine,
  659. NULL);
  660. }
  661. GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
  662. NULL);
  663. }
  664. /**
  665. * Main state machine that goes over all options and
  666. * runs the next requested function.
  667. *
  668. * @param cls unused
  669. */
  670. static void
  671. state_machine (void *cls)
  672. {
  673. tt = NULL;
  674. if (NULL != put_uri)
  675. {
  676. GPI_plugins_load (cfg);
  677. if (GNUNET_SYSERR == parse_hello_uri (put_uri))
  678. {
  679. fprintf (stderr,
  680. _("Invalid URI `%s'\n"),
  681. put_uri);
  682. GNUNET_SCHEDULER_shutdown ();
  683. }
  684. GNUNET_free (put_uri);
  685. put_uri = NULL;
  686. }
  687. else if (GNUNET_YES == get_info)
  688. {
  689. get_info = GNUNET_NO;
  690. GPI_plugins_load (cfg);
  691. pic = GNUNET_PEERINFO_iterate (peerinfo,
  692. include_friend_only,
  693. NULL,
  694. &print_peer_info,
  695. NULL);
  696. }
  697. else if (GNUNET_YES == get_self)
  698. {
  699. get_self = GNUNET_NO;
  700. if (be_quiet)
  701. printf ("%s\n",
  702. GNUNET_i2s_full (&my_peer_identity));
  703. else
  704. printf (_("I am peer `%s'.\n"),
  705. GNUNET_i2s_full (&my_peer_identity));
  706. tt = GNUNET_SCHEDULER_add_now (&state_machine,
  707. NULL);
  708. }
  709. else if (GNUNET_YES == get_uri)
  710. {
  711. GPI_plugins_load (cfg);
  712. pic = GNUNET_PEERINFO_iterate (peerinfo,
  713. include_friend_only,
  714. &my_peer_identity,
  715. &print_my_uri,
  716. NULL);
  717. get_uri = GNUNET_NO;
  718. }
  719. else if (GNUNET_YES == default_operation)
  720. {
  721. /* default operation list all */
  722. default_operation = GNUNET_NO;
  723. get_info = GNUNET_YES;
  724. tt = GNUNET_SCHEDULER_add_now (&state_machine,
  725. NULL);
  726. }
  727. else
  728. {
  729. GNUNET_SCHEDULER_shutdown ();
  730. }
  731. default_operation = GNUNET_NO;
  732. }
  733. /**
  734. * The main function to obtain peer information.
  735. *
  736. * @param argc number of arguments from the command line
  737. * @param argv command line arguments
  738. * @return 0 ok, 1 on error
  739. */
  740. int
  741. main (int argc, char *const *argv)
  742. {
  743. struct GNUNET_GETOPT_CommandLineOption options[] = {
  744. GNUNET_GETOPT_option_flag ('n',
  745. "numeric",
  746. gettext_noop ("don't resolve host names"),
  747. &no_resolve),
  748. GNUNET_GETOPT_option_flag ('q',
  749. "quiet",
  750. gettext_noop ("output only the identity strings"),
  751. &be_quiet),
  752. GNUNET_GETOPT_option_flag ('f',
  753. "friends",
  754. gettext_noop ("include friend-only information"),
  755. &include_friend_only),
  756. GNUNET_GETOPT_option_flag ('s',
  757. "self",
  758. gettext_noop ("output our own identity only"),
  759. &get_self),
  760. GNUNET_GETOPT_option_flag ('i',
  761. "info",
  762. gettext_noop ("list all known peers"),
  763. &get_info),
  764. GNUNET_GETOPT_option_string ('d',
  765. "dump-hello",
  766. NULL,
  767. gettext_noop ("dump hello to file"),
  768. &dump_hello),
  769. GNUNET_GETOPT_option_flag ('g',
  770. "get-hello",
  771. gettext_noop ("also output HELLO uri(s)"),
  772. &get_uri),
  773. GNUNET_GETOPT_option_string ('p',
  774. "put-hello",
  775. "HELLO",
  776. gettext_noop ("add given HELLO uri to the database"),
  777. &put_uri),
  778. GNUNET_GETOPT_OPTION_END
  779. };
  780. int ret;
  781. default_operation = GNUNET_YES;
  782. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
  783. argv,
  784. &argc,
  785. &argv))
  786. return 2;
  787. ret = (GNUNET_OK ==
  788. GNUNET_PROGRAM_run (argc,
  789. argv,
  790. "gnunet-peerinfo",
  791. gettext_noop ("Print information about peers."),
  792. options,
  793. &run,
  794. NULL)) ? 0 : 1;
  795. GNUNET_free ((void*) argv);
  796. return ret;
  797. }
  798. /* end of gnunet-peerinfo.c */