gnunet-dht-get.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 dht/gnunet-dht-get.c
  19. * @brief search for data in DHT
  20. * @author Christian Grothoff
  21. * @author Nathan Evans
  22. */
  23. #include "platform.h"
  24. #include "gnunet_dht_service.h"
  25. #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
  26. /**
  27. * The type of the query
  28. */
  29. static unsigned int query_type;
  30. /**
  31. * Desired replication level
  32. */
  33. static unsigned int replication = 5;
  34. /**
  35. * The key for the query
  36. */
  37. static char *query_key;
  38. /**
  39. * User supplied timeout value
  40. */
  41. static struct GNUNET_TIME_Relative timeout_request = { 60000 };
  42. /**
  43. * Be verbose
  44. */
  45. static int verbose;
  46. /**
  47. * Use DHT demultixplex_everywhere
  48. */
  49. static int demultixplex_everywhere;
  50. /**
  51. * Handle to the DHT
  52. */
  53. static struct GNUNET_DHT_Handle *dht_handle;
  54. /**
  55. * Global handle of the configuration
  56. */
  57. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  58. /**
  59. * Handle for the get request
  60. */
  61. static struct GNUNET_DHT_GetHandle *get_handle;
  62. /**
  63. * Count of results found
  64. */
  65. static unsigned int result_count;
  66. /**
  67. * Global status value
  68. */
  69. static int ret;
  70. /**
  71. * Task run to clean up on timeout.
  72. *
  73. * @param cls unused
  74. * @param tc unused
  75. */
  76. static void
  77. cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  78. {
  79. if (NULL != get_handle)
  80. {
  81. GNUNET_DHT_get_stop (get_handle);
  82. get_handle = NULL;
  83. }
  84. if (NULL != dht_handle)
  85. {
  86. GNUNET_DHT_disconnect (dht_handle);
  87. dht_handle = NULL;
  88. }
  89. }
  90. /**
  91. * Iterator called on each result obtained for a DHT
  92. * operation that expects a reply
  93. *
  94. * @param cls closure
  95. * @param exp when will this value expire
  96. * @param key key of the result
  97. * @param get_path peers on reply path (or NULL if not recorded)
  98. * @param get_path_length number of entries in get_path
  99. * @param put_path peers on the PUT path (or NULL if not recorded)
  100. * @param put_path_length number of entries in get_path
  101. * @param type type of the result
  102. * @param size number of bytes in data
  103. * @param data pointer to the result data
  104. */
  105. static void
  106. get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
  107. const struct GNUNET_HashCode * key,
  108. const struct GNUNET_PeerIdentity *get_path,
  109. unsigned int get_path_length,
  110. const struct GNUNET_PeerIdentity *put_path,
  111. unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
  112. size_t size, const void *data)
  113. {
  114. FPRINTF (stdout,
  115. _("Result %d, type %d:\n%.*s\n"),
  116. result_count, type,
  117. (unsigned int) size, (char *) data);
  118. result_count++;
  119. }
  120. /**
  121. * Main function that will be run by the scheduler.
  122. *
  123. * @param cls closure
  124. * @param args remaining command-line arguments
  125. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  126. * @param c configuration
  127. */
  128. static void
  129. run (void *cls, char *const *args, const char *cfgfile,
  130. const struct GNUNET_CONFIGURATION_Handle *c)
  131. {
  132. struct GNUNET_HashCode key;
  133. cfg = c;
  134. if (NULL == query_key)
  135. {
  136. FPRINTF (stderr, "%s", _("Must provide key for DHT GET!\n"));
  137. ret = 1;
  138. return;
  139. }
  140. if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
  141. {
  142. FPRINTF (stderr, "%s", _("Failed to connect to DHT service!\n"));
  143. ret = 1;
  144. return;
  145. }
  146. if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
  147. query_type = GNUNET_BLOCK_TYPE_TEST;
  148. GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
  149. if (verbose)
  150. FPRINTF (stderr, "%s `%s' \n", _("Issueing DHT GET with key"), GNUNET_h2s_full (&key));
  151. GNUNET_SCHEDULER_add_delayed (timeout_request,
  152. &cleanup_task, NULL);
  153. get_handle =
  154. GNUNET_DHT_get_start (dht_handle, query_type, &key, replication,
  155. (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
  156. NULL, 0, &get_result_iterator, NULL);
  157. }
  158. /**
  159. * gnunet-dht-get command line options
  160. */
  161. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  162. {'k', "key", "KEY",
  163. gettext_noop ("the query key"),
  164. 1, &GNUNET_GETOPT_set_string, &query_key},
  165. {'r', "replication", "LEVEL",
  166. gettext_noop ("how many parallel requests (replicas) to create"),
  167. 1, &GNUNET_GETOPT_set_uint, &replication},
  168. {'t', "type", "TYPE",
  169. gettext_noop ("the type of data to look for"),
  170. 1, &GNUNET_GETOPT_set_uint, &query_type},
  171. {'T', "timeout", "TIMEOUT",
  172. gettext_noop ("how long to execute this query before giving up?"),
  173. 1, &GNUNET_GETOPT_set_relative_time, &timeout_request},
  174. {'x', "demultiplex", NULL,
  175. gettext_noop ("use DHT's demultiplex everywhere option"),
  176. 0, &GNUNET_GETOPT_set_one, &demultixplex_everywhere},
  177. {'V', "verbose", NULL,
  178. gettext_noop ("be verbose (print progress information)"),
  179. 0, &GNUNET_GETOPT_set_one, &verbose},
  180. GNUNET_GETOPT_OPTION_END
  181. };
  182. /**
  183. * Entry point for gnunet-dht-get
  184. *
  185. * @param argc number of arguments from the command line
  186. * @param argv command line arguments
  187. * @return 0 ok, 1 on error
  188. */
  189. int
  190. main (int argc, char *const *argv)
  191. {
  192. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  193. return 2;
  194. return (GNUNET_OK ==
  195. GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
  196. gettext_noop
  197. ("Issue a GET request to the GNUnet DHT, prints results."),
  198. options, &run, NULL)) ? ret : 1;
  199. }
  200. /* end of gnunet-dht-get.c */