2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file dht/gnunet-dht-put.c
22 * @brief search for data in DHT
23 * @author Christian Grothoff
24 * @author Nathan Evans
27 #include "gnunet_dht_service.h"
30 * The type of the query
32 static unsigned int query_type;
35 * The key used in the DHT
37 struct GNUNET_HashCode key;
40 * The key for the query
42 static char *query_key;
45 * User supplied expiration value
47 static struct GNUNET_TIME_Relative expiration;
50 * Desired replication level.
52 static unsigned int replication = 5;
60 * Use #GNUNET_DHT_DEMULTIPLEX_EVERYWHERE.
62 static int demultixplex_everywhere;
65 * Use #GNUNET_DHT_RO_RECORD_ROUTE.
67 static int record_route;
72 static struct GNUNET_DHT_Handle *dht_handle;
76 * Global handle of the configuration
78 static const struct GNUNET_CONFIGURATION_Handle *cfg;
86 * The data to insert into the dht
92 shutdown_task (void *cls)
94 if (NULL != dht_handle)
96 GNUNET_DHT_disconnect (dht_handle);
103 * Signature of the main function of a task.
106 * @param success #GNUNET_OK if the PUT was transmitted,
107 * #GNUNET_NO on timeout,
108 * #GNUNET_SYSERR on disconnect from service
109 * after the PUT message was transmitted
110 * (so we don't know if it was received or not)
113 message_sent_cont (void *cls, int success)
120 FPRINTF (stderr, "%s `%s'!\n", _("PUT request sent with key"), GNUNET_h2s_full(&key));
123 FPRINTF (stderr, "%s", _("Timeout sending PUT request!\n"));
126 FPRINTF (stderr, "%s", _("PUT request not confirmed!\n"));
133 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
138 * Main function that will be run by the scheduler.
141 * @param args remaining command-line arguments
142 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
143 * @param c configuration
149 const struct GNUNET_CONFIGURATION_Handle *c)
151 enum GNUNET_DHT_RouteOption ro;
154 if ((NULL == query_key) || (NULL == data))
156 FPRINTF (stderr, "%s", _("Must provide KEY and DATA for DHT put!\n"));
161 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
163 FPRINTF (stderr, _("Could not connect to %s service!\n"), "DHT");
167 if (GNUNET_BLOCK_TYPE_ANY == query_type) /* Type of data not set */
168 query_type = GNUNET_BLOCK_TYPE_TEST;
170 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
174 _("Issuing put request for `%s' with data `%s'!\n"),
177 ro = GNUNET_DHT_RO_NONE;
178 if (demultixplex_everywhere)
179 ro |= GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
181 ro |= GNUNET_DHT_RO_RECORD_ROUTE;
182 GNUNET_DHT_put (dht_handle,
196 * gnunet-dht-put command line options
198 static struct GNUNET_GETOPT_CommandLineOption options[] = {
199 {'d', "data", "DATA",
200 gettext_noop ("the data to insert under the key"),
201 1, &GNUNET_GETOPT_set_string, &data},
202 {'e', "expiration", "EXPIRATION",
203 gettext_noop ("how long to store this entry in the dht (in seconds)"),
204 1, &GNUNET_GETOPT_set_relative_time, &expiration},
206 gettext_noop ("the query key"),
207 1, &GNUNET_GETOPT_set_string, &query_key},
208 {'x', "demultiplex", NULL,
209 gettext_noop ("use DHT's demultiplex everywhere option"),
210 0, &GNUNET_GETOPT_set_one, &demultixplex_everywhere},
211 {'r', "replication", "LEVEL",
212 gettext_noop ("how many replicas to create"),
213 1, &GNUNET_GETOPT_set_uint, &replication},
214 {'R', "record", NULL,
215 gettext_noop ("use DHT's record route option"),
216 0, &GNUNET_GETOPT_set_one, &record_route},
217 {'t', "type", "TYPE",
218 gettext_noop ("the type to insert data as"),
219 1, &GNUNET_GETOPT_set_uint, &query_type},
220 {'V', "verbose", NULL,
221 gettext_noop ("be verbose (print progress information)"),
222 0, &GNUNET_GETOPT_set_one, &verbose},
223 GNUNET_GETOPT_OPTION_END
228 * Entry point for gnunet-dht-put
230 * @param argc number of arguments from the command line
231 * @param argv command line arguments
232 * @return 0 ok, 1 on error
235 main (int argc, char *const *argv)
237 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
240 expiration = GNUNET_TIME_UNIT_HOURS;
242 GNUNET_PROGRAM_run (argc,
246 ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
253 /* end of gnunet-dht-put.c */