gnunet-core.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011, 2012, 2014 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 core/gnunet-core.c
  19. * @brief Print information about other peers known to CORE.
  20. * @author Nathan Evans
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_core_service.h"
  25. /**
  26. * Option -m.
  27. */
  28. static int monitor_connections;
  29. /**
  30. * Handle to the CORE monitor.
  31. */
  32. static struct GNUNET_CORE_MonitorHandle *mh;
  33. /**
  34. * Task run in monitor mode when the user presses CTRL-C to abort.
  35. * Stops monitoring activity.
  36. *
  37. * @param cls NULL
  38. */
  39. static void
  40. shutdown_task (void *cls)
  41. {
  42. if (NULL != mh)
  43. {
  44. GNUNET_CORE_monitor_stop (mh);
  45. mh = NULL;
  46. }
  47. }
  48. /**
  49. * Function called to notify core users that another
  50. * peer changed its state with us.
  51. *
  52. * @param cls closure
  53. * @param peer the peer that changed state
  54. * @param state new state of the peer
  55. * @param timeout timeout for the new state
  56. */
  57. static void
  58. monitor_cb (void *cls,
  59. const struct GNUNET_PeerIdentity *peer,
  60. enum GNUNET_CORE_KxState state,
  61. struct GNUNET_TIME_Absolute timeout)
  62. {
  63. struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
  64. const char *now_str;
  65. const char *state_str;
  66. if ( ( (NULL == peer) ||
  67. (GNUNET_CORE_KX_ITERATION_FINISHED == state) ) &&
  68. (GNUNET_NO == monitor_connections) )
  69. {
  70. GNUNET_SCHEDULER_shutdown ();
  71. return;
  72. }
  73. switch (state)
  74. {
  75. case GNUNET_CORE_KX_STATE_DOWN:
  76. /* should never happen, as we immediately send the key */
  77. state_str = _("fresh connection");
  78. break;
  79. case GNUNET_CORE_KX_STATE_KEY_SENT:
  80. state_str = _("key sent");
  81. break;
  82. case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
  83. state_str = _("key received");
  84. break;
  85. case GNUNET_CORE_KX_STATE_UP:
  86. state_str = _("connection established");
  87. break;
  88. case GNUNET_CORE_KX_STATE_REKEY_SENT:
  89. state_str = _("rekeying");
  90. break;
  91. case GNUNET_CORE_KX_PEER_DISCONNECT:
  92. state_str = _("disconnected");
  93. break;
  94. case GNUNET_CORE_KX_ITERATION_FINISHED:
  95. return;
  96. case GNUNET_CORE_KX_CORE_DISCONNECT:
  97. FPRINTF (stderr,
  98. "%s\n",
  99. _("Connection to CORE service lost (reconnecting)"));
  100. return;
  101. default:
  102. state_str = _("unknown state");
  103. break;
  104. }
  105. now_str = GNUNET_STRINGS_absolute_time_to_string (now);
  106. FPRINTF (stdout,
  107. _("%24s: %-30s %4s (timeout in %6s)\n"),
  108. now_str,
  109. state_str,
  110. GNUNET_i2s (peer),
  111. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (timeout),
  112. GNUNET_YES));
  113. }
  114. /**
  115. * Main function that will be run by the scheduler.
  116. *
  117. * @param cls closure
  118. * @param args remaining command-line arguments
  119. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  120. * @param cfg configuration
  121. */
  122. static void
  123. run (void *cls, char *const *args, const char *cfgfile,
  124. const struct GNUNET_CONFIGURATION_Handle *cfg)
  125. {
  126. if (NULL != args[0])
  127. {
  128. FPRINTF (stderr,
  129. _("Invalid command line argument `%s'\n"),
  130. args[0]);
  131. return;
  132. }
  133. mh = GNUNET_CORE_monitor_start (cfg,
  134. &monitor_cb,
  135. NULL);
  136. if (NULL == mh)
  137. {
  138. FPRINTF (stderr,
  139. "%s",
  140. _("Failed to connect to CORE service!\n"));
  141. return;
  142. }
  143. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  144. }
  145. /**
  146. * The main function to obtain peer information from CORE.
  147. *
  148. * @param argc number of arguments from the command line
  149. * @param argv command line arguments
  150. * @return 0 ok, 1 on error
  151. */
  152. int
  153. main (int argc,
  154. char *const *argv)
  155. {
  156. int res;
  157. struct GNUNET_GETOPT_CommandLineOption options[] = {
  158. GNUNET_GETOPT_option_flag ('m',
  159. "monitor",
  160. gettext_noop ("provide information about all current connections (continuously)"),
  161. &monitor_connections),
  162. GNUNET_GETOPT_OPTION_END
  163. };
  164. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  165. return 2;
  166. res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
  167. gettext_noop
  168. ("Print information about connected peers."),
  169. options, &run, NULL);
  170. GNUNET_free ((void *) argv);
  171. if (GNUNET_OK == res)
  172. return 0;
  173. return 1;
  174. }
  175. /* end of gnunet-core.c */