gnunet-dv.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 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 dv/gnunet-dv.c
  19. * @brief DV monitoring command line tool
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_dv_service.h"
  25. /**
  26. * Handle to DV service.
  27. */
  28. static struct GNUNET_DV_ServiceHandle *sh;
  29. /**
  30. * Was verbose specified?
  31. */
  32. static unsigned int verbose;
  33. /**
  34. * Function called if DV starts to be able to talk to a peer.
  35. *
  36. * @param cls closure
  37. * @param peer newly connected peer
  38. * @param distance distance to the peer
  39. * @param network the network the next hop is located in
  40. */
  41. static void
  42. connect_cb (void *cls,
  43. const struct GNUNET_PeerIdentity *peer,
  44. uint32_t distance,
  45. enum GNUNET_ATS_Network_Type network)
  46. {
  47. fprintf (stderr, "Connect: %s at %u\n",
  48. GNUNET_i2s (peer),
  49. (unsigned int) distance);
  50. }
  51. /**
  52. * Function called if DV distance to a peer is changed.
  53. *
  54. * @param cls closure
  55. * @param peer connected peer
  56. * @param distance new distance to the peer
  57. * @param network network used on first hop to peer
  58. */
  59. static void
  60. change_cb (void *cls,
  61. const struct GNUNET_PeerIdentity *peer,
  62. uint32_t distance,
  63. enum GNUNET_ATS_Network_Type network)
  64. {
  65. fprintf (stderr, "Change: %s at %u\n",
  66. GNUNET_i2s (peer),
  67. (unsigned int) distance);
  68. }
  69. /**
  70. * Function called if DV is no longer able to talk to a peer.
  71. *
  72. * @param cls closure
  73. * @param peer peer that disconnected
  74. */
  75. static void
  76. disconnect_cb (void *cls,
  77. const struct GNUNET_PeerIdentity *peer)
  78. {
  79. fprintf (stderr, "Disconnect: %s\n",
  80. GNUNET_i2s (peer));
  81. }
  82. /**
  83. * Function called if DV receives a message for this peer.
  84. *
  85. * @param cls closure
  86. * @param sender sender of the message
  87. * @param distance how far did the message travel
  88. * @param msg actual message payload
  89. */
  90. static void
  91. message_cb (void *cls,
  92. const struct GNUNET_PeerIdentity *sender,
  93. uint32_t distance,
  94. const struct GNUNET_MessageHeader *msg)
  95. {
  96. if (verbose)
  97. fprintf (stderr, "Message: %s at %u sends %u bytes of type %u\n",
  98. GNUNET_i2s (sender),
  99. (unsigned int) distance,
  100. (unsigned int) ntohs (msg->size),
  101. (unsigned int) ntohs (msg->type));
  102. }
  103. /**
  104. * Task run on shutdown.
  105. *
  106. * @param cls NULL
  107. */
  108. static void
  109. shutdown_task (void *cls)
  110. {
  111. GNUNET_DV_service_disconnect (sh);
  112. sh = NULL;
  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. sh = GNUNET_DV_service_connect (cfg, NULL,
  127. &connect_cb,
  128. &change_cb,
  129. &disconnect_cb,
  130. &message_cb);
  131. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  132. }
  133. /**
  134. * The main function.
  135. *
  136. * @param argc number of arguments from the command line
  137. * @param argv command line arguments
  138. * @return 0 ok, 1 on error
  139. */
  140. int
  141. main (int argc, char *const *argv)
  142. {
  143. int res;
  144. struct GNUNET_GETOPT_CommandLineOption options[] = {
  145. GNUNET_GETOPT_option_verbose (&verbose),
  146. GNUNET_GETOPT_OPTION_END
  147. };
  148. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  149. return 2;
  150. res = GNUNET_PROGRAM_run (argc, argv, "gnunet-dv",
  151. gettext_noop ("Print information about DV state"),
  152. options, &run,
  153. NULL);
  154. GNUNET_free ((void *) argv);
  155. if (GNUNET_OK != res)
  156. return 1;
  157. return 0;
  158. }
  159. /* end of gnunet-dv.c */