gnunet-dv.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2013 Christian Grothoff (and other contributing authors)
  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., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, 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 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. * @param tc unused
  108. */
  109. static void
  110. shutdown_task (void *cls,
  111. const struct GNUNET_SCHEDULER_TaskContext *tc)
  112. {
  113. GNUNET_DV_service_disconnect (sh);
  114. sh = NULL;
  115. }
  116. /**
  117. * Main function that will be run by the scheduler.
  118. *
  119. * @param cls closure
  120. * @param args remaining command-line arguments
  121. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  122. * @param cfg configuration
  123. */
  124. static void
  125. run (void *cls, char *const *args, const char *cfgfile,
  126. const struct GNUNET_CONFIGURATION_Handle *cfg)
  127. {
  128. sh = GNUNET_DV_service_connect (cfg, NULL,
  129. &connect_cb,
  130. &change_cb,
  131. &disconnect_cb,
  132. &message_cb);
  133. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  134. &shutdown_task, NULL);
  135. }
  136. /**
  137. * The main function.
  138. *
  139. * @param argc number of arguments from the command line
  140. * @param argv command line arguments
  141. * @return 0 ok, 1 on error
  142. */
  143. int
  144. main (int argc, char *const *argv)
  145. {
  146. int res;
  147. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  148. {'V', "verbose", NULL,
  149. gettext_noop ("verbose output"),
  150. 0, &GNUNET_GETOPT_set_one, &verbose},
  151. GNUNET_GETOPT_OPTION_END
  152. };
  153. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  154. return 2;
  155. res = GNUNET_PROGRAM_run (argc, argv, "gnunet-dv",
  156. gettext_noop ("Print information about DV state"),
  157. options, &run,
  158. NULL);
  159. GNUNET_free ((void *) argv);
  160. if (GNUNET_OK != res)
  161. return 1;
  162. return 0;
  163. }
  164. /* end of gnunet-dv.c */