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