gnunet-dht-put.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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-put.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. /**
  26. * The type of the query
  27. */
  28. static unsigned int query_type;
  29. /**
  30. * The key used in the DHT
  31. */
  32. struct GNUNET_HashCode key;
  33. /**
  34. * The key for the query
  35. */
  36. static char *query_key;
  37. /**
  38. * User supplied timeout value
  39. */
  40. static unsigned long long timeout_request = 5;
  41. /**
  42. * User supplied expiration value
  43. */
  44. static unsigned long long expiration_seconds = 3600;
  45. /**
  46. * Desired replication level.
  47. */
  48. static unsigned int replication = 5;
  49. /**
  50. * Be verbose
  51. */
  52. static int verbose;
  53. /**
  54. * Use DHT demultixplex_everywhere
  55. */
  56. static int demultixplex_everywhere;
  57. /**
  58. * Handle to the DHT
  59. */
  60. static struct GNUNET_DHT_Handle *dht_handle;
  61. /**
  62. * Global handle of the configuration
  63. */
  64. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  65. /**
  66. * Global status value
  67. */
  68. static int ret;
  69. /**
  70. * The data to insert into the dht
  71. */
  72. static char *data;
  73. static void
  74. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  75. {
  76. if (NULL != dht_handle)
  77. {
  78. GNUNET_DHT_disconnect (dht_handle);
  79. dht_handle = NULL;
  80. }
  81. }
  82. /**
  83. * Signature of the main function of a task.
  84. *
  85. * @param cls closure
  86. * @param success GNUNET_OK if the PUT was transmitted,
  87. * GNUNET_NO on timeout,
  88. * GNUNET_SYSERR on disconnect from service
  89. * after the PUT message was transmitted
  90. * (so we don't know if it was received or not)
  91. */
  92. static void
  93. message_sent_cont (void *cls, int success)
  94. {
  95. if (verbose)
  96. {
  97. switch (success)
  98. {
  99. case GNUNET_OK:
  100. FPRINTF (stderr, "%s `%s'!\n", _("PUT request sent with key"), GNUNET_h2s_full(&key));
  101. break;
  102. case GNUNET_NO:
  103. FPRINTF (stderr, "%s", _("Timeout sending PUT request!\n"));
  104. break;
  105. case GNUNET_SYSERR:
  106. FPRINTF (stderr, "%s", _("PUT request not confirmed!\n"));
  107. break;
  108. default:
  109. GNUNET_break (0);
  110. break;
  111. }
  112. }
  113. GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  114. }
  115. /**
  116. * Main function that will be run by the scheduler.
  117. *
  118. * @param cls closure
  119. * @param args remaining command-line arguments
  120. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  121. * @param c configuration
  122. */
  123. static void
  124. run (void *cls, char *const *args, const char *cfgfile,
  125. const struct GNUNET_CONFIGURATION_Handle *c)
  126. {
  127. struct GNUNET_TIME_Relative timeout;
  128. struct GNUNET_TIME_Absolute expiration;
  129. cfg = c;
  130. if ((NULL == query_key) || (NULL == data))
  131. {
  132. FPRINTF (stderr, "%s", _("Must provide KEY and DATA for DHT put!\n"));
  133. ret = 1;
  134. return;
  135. }
  136. if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
  137. {
  138. FPRINTF (stderr, _("Could not connect to %s service!\n"), "DHT");
  139. ret = 1;
  140. return;
  141. }
  142. if (GNUNET_BLOCK_TYPE_ANY == query_type) /* Type of data not set */
  143. query_type = GNUNET_BLOCK_TYPE_TEST;
  144. GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
  145. timeout =
  146. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
  147. expiration =
  148. GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
  149. (GNUNET_TIME_UNIT_SECONDS,
  150. expiration_seconds));
  151. if (verbose)
  152. FPRINTF (stderr, _("Issuing put request for `%s' with data `%s'!\n"),
  153. query_key, data);
  154. GNUNET_DHT_put (dht_handle, &key, replication,
  155. (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
  156. query_type,
  157. strlen (data), data, expiration, timeout, &message_sent_cont,
  158. NULL);
  159. }
  160. /**
  161. * gnunet-dht-put command line options
  162. */
  163. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  164. {'d', "data", "DATA",
  165. gettext_noop ("the data to insert under the key"),
  166. 1, &GNUNET_GETOPT_set_string, &data},
  167. {'e', "expiration", "EXPIRATION",
  168. gettext_noop ("how long to store this entry in the dht (in seconds)"),
  169. 1, &GNUNET_GETOPT_set_ulong, &expiration_seconds},
  170. {'k', "key", "KEY",
  171. gettext_noop ("the query key"),
  172. 1, &GNUNET_GETOPT_set_string, &query_key},
  173. {'x', "demultiplex", NULL,
  174. gettext_noop ("use DHT's demultiplex everywhere option"),
  175. 0, &GNUNET_GETOPT_set_one, &demultixplex_everywhere},
  176. {'r', "replication", "LEVEL",
  177. gettext_noop ("how many replicas to create"),
  178. 1, &GNUNET_GETOPT_set_uint, &replication},
  179. {'t', "type", "TYPE",
  180. gettext_noop ("the type to insert data as"),
  181. 1, &GNUNET_GETOPT_set_uint, &query_type},
  182. {'T', "timeout", "TIMEOUT",
  183. gettext_noop ("how long to execute this query before giving up?"),
  184. 1, &GNUNET_GETOPT_set_ulong, &timeout_request},
  185. {'V', "verbose", NULL,
  186. gettext_noop ("be verbose (print progress information)"),
  187. 0, &GNUNET_GETOPT_set_one, &verbose},
  188. GNUNET_GETOPT_OPTION_END
  189. };
  190. /**
  191. * Entry point for gnunet-dht-put
  192. *
  193. * @param argc number of arguments from the command line
  194. * @param argv command line arguments
  195. * @return 0 ok, 1 on error
  196. */
  197. int
  198. main (int argc, char *const *argv)
  199. {
  200. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  201. return 2;
  202. return (GNUNET_OK ==
  203. GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-put",
  204. gettext_noop
  205. ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
  206. options, &run, NULL)) ? ret : 1;
  207. }
  208. /* end of gnunet-dht-put.c */