REST: nothing triggers rest
[oweals/gnunet.git] / src / dht / gnunet-dht-put.c
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  */
32 static unsigned int query_type;
33
34 /**
35  * The key used in the DHT
36  */
37 struct GNUNET_HashCode key;
38
39 /**
40  * The key for the query
41  */
42 static char *query_key;
43
44 /**
45  * User supplied expiration value
46  */
47 static struct GNUNET_TIME_Relative expiration;
48
49 /**
50  * Desired replication level.
51  */
52 static unsigned int replication = 5;
53
54 /**
55  * Be verbose
56  */
57 static unsigned int verbose;
58
59 /**
60  * Use #GNUNET_DHT_DEMULTIPLEX_EVERYWHERE.
61  */
62 static int demultixplex_everywhere;
63
64 /**
65  * Use #GNUNET_DHT_RO_RECORD_ROUTE.
66  */
67 static int record_route;
68
69 /**
70  * Handle to the DHT
71  */
72 static struct GNUNET_DHT_Handle *dht_handle;
73
74
75 /**
76  * Global handle of the configuration
77  */
78 static const struct GNUNET_CONFIGURATION_Handle *cfg;
79
80 /**
81  * Global status value
82  */
83 static int ret;
84
85 /**
86  * The data to insert into the dht
87  */
88 static char *data;
89
90
91 static void
92 shutdown_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  */
107 static void
108 message_sent_cont (void *cls)
109 {
110   GNUNET_SCHEDULER_add_now (&shutdown_task,
111                             NULL);
112 }
113
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,
125      char *const *args,
126      const char *cfgfile,
127      const struct GNUNET_CONFIGURATION_Handle *c)
128 {
129   enum GNUNET_DHT_RouteOption ro;
130
131   cfg = c;
132   if ((NULL == query_key) || (NULL == data))
133   {
134     FPRINTF (stderr, "%s",  _("Must provide KEY and DATA for DHT put!\n"));
135     ret = 1;
136     return;
137   }
138
139   if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
140   {
141     FPRINTF (stderr,
142              _("Could not connect to DHT service!\n"));
143     ret = 1;
144     return;
145   }
146   if (GNUNET_BLOCK_TYPE_ANY == query_type)      /* Type of data not set */
147     query_type = GNUNET_BLOCK_TYPE_TEST;
148
149   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
150
151   if (verbose)
152     FPRINTF (stderr,
153              _("Issuing put request for `%s' with data `%s'!\n"),
154              query_key,
155              data);
156   ro = GNUNET_DHT_RO_NONE;
157   if (demultixplex_everywhere)
158     ro |= GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
159   if (record_route)
160     ro |= GNUNET_DHT_RO_RECORD_ROUTE;
161   GNUNET_DHT_put (dht_handle,
162                   &key,
163                   replication,
164                   ro,
165                   query_type,
166                   strlen (data),
167                   data,
168                   GNUNET_TIME_relative_to_absolute (expiration),
169                   &message_sent_cont,
170                   NULL);
171 }
172
173 /**
174  * Entry point for gnunet-dht-put
175  *
176  * @param argc number of arguments from the command line
177  * @param argv command line arguments
178  * @return 0 ok, 1 on error
179  */
180 int
181 main (int argc, char *const *argv)
182 {
183
184   struct GNUNET_GETOPT_CommandLineOption options[] = {
185     GNUNET_GETOPT_option_string ('d',
186                                  "data",
187                                  "DATA",
188                                  gettext_noop ("the data to insert under the key"),
189                                  &data),
190     GNUNET_GETOPT_option_relative_time ('e',
191                                         "expiration",
192                                         "EXPIRATION",
193                                         gettext_noop ("how long to store this entry in the dht (in seconds)"),
194                                         &expiration),
195     GNUNET_GETOPT_option_string ('k',
196                                  "key",
197                                  "KEY",
198                                  gettext_noop ("the query key"),
199                                  &query_key),
200     GNUNET_GETOPT_option_flag ('x',
201                                "demultiplex",
202                                gettext_noop ("use DHT's demultiplex everywhere option"),
203                                &demultixplex_everywhere),
204     GNUNET_GETOPT_option_uint ('r',
205                                "replication",
206                                "LEVEL",
207                                gettext_noop ("how many replicas to create"),
208                                &replication),
209     GNUNET_GETOPT_option_flag ('R',
210                                "record",
211                                gettext_noop ("use DHT's record route option"),
212                                &record_route),
213     GNUNET_GETOPT_option_uint ('t',
214                                "type",
215                                "TYPE",
216                                gettext_noop ("the type to insert data as"),
217                                &query_type),
218     GNUNET_GETOPT_option_verbose (&verbose),
219     GNUNET_GETOPT_OPTION_END
220   };
221
222
223   if (GNUNET_OK !=
224       GNUNET_STRINGS_get_utf8_args (argc, argv,
225                                     &argc, &argv))
226     return 2;
227   expiration = GNUNET_TIME_UNIT_HOURS;
228   return (GNUNET_OK ==
229           GNUNET_PROGRAM_run (argc,
230                               argv,
231                               "gnunet-dht-put",
232                               gettext_noop
233                               ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
234                               options,
235                               &run,
236                               NULL))
237     ? ret : 1;
238 }
239
240 /* end of gnunet-dht-put.c */