gnunet-dht-put.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file dht/gnunet-dht-put.c
  18. * @brief search for data in DHT
  19. * @author Christian Grothoff
  20. * @author Nathan Evans
  21. */
  22. #include "platform.h"
  23. #include "gnunet_dht_service.h"
  24. /**
  25. * The type of the query
  26. */
  27. static unsigned int query_type;
  28. /**
  29. * The key used in the DHT
  30. */
  31. struct GNUNET_HashCode key;
  32. /**
  33. * The key for the query
  34. */
  35. static char *query_key;
  36. /**
  37. * User supplied expiration value
  38. */
  39. static struct GNUNET_TIME_Relative expiration;
  40. /**
  41. * Desired replication level.
  42. */
  43. static unsigned int replication = 5;
  44. /**
  45. * Be verbose
  46. */
  47. static unsigned int verbose;
  48. /**
  49. * Use #GNUNET_DHT_DEMULTIPLEX_EVERYWHERE.
  50. */
  51. static int demultixplex_everywhere;
  52. /**
  53. * Use #GNUNET_DHT_RO_RECORD_ROUTE.
  54. */
  55. static int record_route;
  56. /**
  57. * Handle to the DHT
  58. */
  59. static struct GNUNET_DHT_Handle *dht_handle;
  60. /**
  61. * Global handle of the configuration
  62. */
  63. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  64. /**
  65. * Global status value
  66. */
  67. static int ret;
  68. /**
  69. * The data to insert into the dht
  70. */
  71. static char *data;
  72. static void
  73. shutdown_task (void *cls)
  74. {
  75. if (NULL != dht_handle)
  76. {
  77. GNUNET_DHT_disconnect (dht_handle);
  78. dht_handle = NULL;
  79. }
  80. }
  81. /**
  82. * Signature of the main function of a task.
  83. *
  84. * @param cls closure
  85. */
  86. static void
  87. message_sent_cont (void *cls)
  88. {
  89. GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
  90. }
  91. /**
  92. * Main function that will be run by the scheduler.
  93. *
  94. * @param cls closure
  95. * @param args remaining command-line arguments
  96. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  97. * @param c configuration
  98. */
  99. static void
  100. run (void *cls,
  101. char *const *args,
  102. const char *cfgfile,
  103. const struct GNUNET_CONFIGURATION_Handle *c)
  104. {
  105. enum GNUNET_DHT_RouteOption ro;
  106. cfg = c;
  107. if ((NULL == query_key) || (NULL == data))
  108. {
  109. fprintf (stderr, "%s", _ ("Must provide KEY and DATA for DHT put!\n"));
  110. ret = 1;
  111. return;
  112. }
  113. if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
  114. {
  115. fprintf (stderr, _ ("Could not connect to DHT service!\n"));
  116. ret = 1;
  117. return;
  118. }
  119. if (GNUNET_BLOCK_TYPE_ANY == query_type) /* Type of data not set */
  120. query_type = GNUNET_BLOCK_TYPE_TEST;
  121. GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
  122. if (verbose)
  123. fprintf (stderr,
  124. _ ("Issuing put request for `%s' with data `%s'!\n"),
  125. query_key,
  126. data);
  127. ro = GNUNET_DHT_RO_NONE;
  128. if (demultixplex_everywhere)
  129. ro |= GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
  130. if (record_route)
  131. ro |= GNUNET_DHT_RO_RECORD_ROUTE;
  132. GNUNET_DHT_put (dht_handle,
  133. &key,
  134. replication,
  135. ro,
  136. query_type,
  137. strlen (data),
  138. data,
  139. GNUNET_TIME_relative_to_absolute (expiration),
  140. &message_sent_cont,
  141. NULL);
  142. }
  143. /**
  144. * Entry point for gnunet-dht-put
  145. *
  146. * @param argc number of arguments from the command line
  147. * @param argv command line arguments
  148. * @return 0 ok, 1 on error
  149. */
  150. int
  151. main (int argc, char *const *argv)
  152. {
  153. struct GNUNET_GETOPT_CommandLineOption options[] =
  154. { GNUNET_GETOPT_option_string ('d',
  155. "data",
  156. "DATA",
  157. gettext_noop (
  158. "the data to insert under the key"),
  159. &data),
  160. GNUNET_GETOPT_option_relative_time (
  161. 'e',
  162. "expiration",
  163. "EXPIRATION",
  164. gettext_noop ("how long to store this entry in the dht (in seconds)"),
  165. &expiration),
  166. GNUNET_GETOPT_option_string ('k',
  167. "key",
  168. "KEY",
  169. gettext_noop ("the query key"),
  170. &query_key),
  171. GNUNET_GETOPT_option_flag ('x',
  172. "demultiplex",
  173. gettext_noop (
  174. "use DHT's demultiplex everywhere option"),
  175. &demultixplex_everywhere),
  176. GNUNET_GETOPT_option_uint ('r',
  177. "replication",
  178. "LEVEL",
  179. gettext_noop ("how many replicas to create"),
  180. &replication),
  181. GNUNET_GETOPT_option_flag ('R',
  182. "record",
  183. gettext_noop ("use DHT's record route option"),
  184. &record_route),
  185. GNUNET_GETOPT_option_uint ('t',
  186. "type",
  187. "TYPE",
  188. gettext_noop ("the type to insert data as"),
  189. &query_type),
  190. GNUNET_GETOPT_option_verbose (&verbose),
  191. GNUNET_GETOPT_OPTION_END };
  192. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  193. return 2;
  194. expiration = GNUNET_TIME_UNIT_HOURS;
  195. return (GNUNET_OK ==
  196. GNUNET_PROGRAM_run (
  197. argc,
  198. argv,
  199. "gnunet-dht-put",
  200. gettext_noop (
  201. "Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
  202. options,
  203. &run,
  204. NULL))
  205. ? ret
  206. : 1;
  207. }
  208. /* end of gnunet-dht-put.c */