gnunet-rps.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C)
  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 rps/gnunet-rps.c
  19. * @brief random peer sampling
  20. * @author Julius Bünger
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_rps_service.h"
  25. #include <inttypes.h>
  26. static int ret;
  27. /**
  28. * RPS handle
  29. */
  30. static struct GNUNET_RPS_Handle *rps_handle;
  31. /**
  32. * Request handle
  33. */
  34. static struct GNUNET_RPS_Request_Handle *req_handle;
  35. /**
  36. * PeerID (Option --seed)
  37. */
  38. static struct GNUNET_PeerIdentity peer_id;
  39. /**
  40. * Task run when user presses CTRL-C to abort.
  41. * Cancels pending request and disconnects.
  42. *
  43. * @param cls NULL
  44. */
  45. static void
  46. do_shutdown (void *cls)
  47. {
  48. if (NULL != req_handle)
  49. GNUNET_RPS_request_cancel (req_handle);
  50. GNUNET_RPS_disconnect (rps_handle);
  51. }
  52. /**
  53. * Callback called on receipt of reply.
  54. * Prints replied PeerIDs.
  55. *
  56. * @param cls closure
  57. * @param n number of peers
  58. * @param recv_peers the received peers
  59. */
  60. static void
  61. reply_handle (void *cls,
  62. uint64_t n,
  63. const struct GNUNET_PeerIdentity *recv_peers)
  64. {
  65. uint64_t i;
  66. req_handle = NULL;
  67. for (i = 0; i < n; i++)
  68. {
  69. FPRINTF (stdout, "%s\n",
  70. GNUNET_i2s_full (&recv_peers[i]));
  71. }
  72. ret = 0;
  73. GNUNET_SCHEDULER_shutdown ();
  74. }
  75. /**
  76. * Main function that will be run by the scheduler.
  77. *
  78. * @param cls closure
  79. * @param args remaining command-line arguments
  80. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  81. * @param cfg configuration
  82. */
  83. static void
  84. run (void *cls,
  85. char *const *args,
  86. const char *cfgfile,
  87. const struct GNUNET_CONFIGURATION_Handle *cfg)
  88. {
  89. static uint64_t num_peers;
  90. static struct GNUNET_PeerIdentity zero_pid;
  91. rps_handle = GNUNET_RPS_connect (cfg);
  92. if (0 == memcmp (&zero_pid,
  93. &peer_id,
  94. sizeof (peer_id)))
  95. { /* Request n PeerIDs */
  96. /* If number was specified use it, else request single peer. */
  97. num_peers = (NULL == args[0]) ? 1 : atoi (args[0]);
  98. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  99. "Requesting %" PRIu64 " PeerIDs\n", num_peers);
  100. req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL);
  101. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  102. }
  103. else
  104. { /* Seed PeerID */
  105. GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
  106. FPRINTF (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
  107. ret = 0;
  108. GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  109. }
  110. }
  111. /**
  112. * The main function to rps.
  113. *
  114. * @param argc number of arguments from the command line
  115. * @param argv command line arguments
  116. * @return 0 ok, 1 on error
  117. */
  118. int
  119. main (int argc, char *const *argv)
  120. {
  121. const char helpstr[] =
  122. "Get random GNUnet peers. If none is specified a single is requested.";
  123. struct GNUNET_GETOPT_CommandLineOption options[] = {
  124. GNUNET_GETOPT_option_base32_auto ('s',
  125. "seed",
  126. "PEER_ID",
  127. gettext_noop ("Seed a PeerID"),
  128. &peer_id),
  129. GNUNET_GETOPT_OPTION_END
  130. };
  131. return (GNUNET_OK ==
  132. GNUNET_PROGRAM_run (argc,
  133. argv,
  134. "gnunet-rps [NUMBER_OF_PEERS]",
  135. gettext_noop
  136. (helpstr),
  137. options, &run, NULL)) ? ret : 1;
  138. }
  139. /* end of gnunet-rps.c */