guix-env: some update.
[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
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.
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      General Public License for more details.
14
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.
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  * @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)
111  */
112 static void
113 message_sent_cont (void *cls, int success)
114 {
115   if (verbose)
116   {
117     switch (success)
118     {
119     case GNUNET_OK:
120       FPRINTF (stderr, "%s `%s'!\n",  _("PUT request sent with key"), GNUNET_h2s_full(&key));
121       break;
122     case GNUNET_NO:
123       FPRINTF (stderr, "%s",  _("Timeout sending PUT request!\n"));
124       break;
125     case GNUNET_SYSERR:
126       FPRINTF (stderr, "%s",  _("PUT request not confirmed!\n"));
127       break;
128     default:
129       GNUNET_break (0);
130       break;
131     }
132   }
133   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
134 }
135
136
137 /**
138  * Main function that will be run by the scheduler.
139  *
140  * @param cls closure
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
144  */
145 static void
146 run (void *cls,
147      char *const *args,
148      const char *cfgfile,
149      const struct GNUNET_CONFIGURATION_Handle *c)
150 {
151   enum GNUNET_DHT_RouteOption ro;
152
153   cfg = c;
154   if ((NULL == query_key) || (NULL == data))
155   {
156     FPRINTF (stderr, "%s",  _("Must provide KEY and DATA for DHT put!\n"));
157     ret = 1;
158     return;
159   }
160
161   if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
162   {
163     FPRINTF (stderr, _("Could not connect to %s service!\n"), "DHT");
164     ret = 1;
165     return;
166   }
167   if (GNUNET_BLOCK_TYPE_ANY == query_type)      /* Type of data not set */
168     query_type = GNUNET_BLOCK_TYPE_TEST;
169
170   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
171
172   if (verbose)
173     FPRINTF (stderr,
174              _("Issuing put request for `%s' with data `%s'!\n"),
175              query_key,
176              data);
177   ro = GNUNET_DHT_RO_NONE;
178   if (demultixplex_everywhere)
179     ro |= GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
180   if (record_route)
181     ro |= GNUNET_DHT_RO_RECORD_ROUTE;
182   GNUNET_DHT_put (dht_handle,
183                   &key,
184                   replication,
185                   ro,
186                   query_type,
187                   strlen (data),
188                   data,
189                   GNUNET_TIME_relative_to_absolute (expiration),
190                   &message_sent_cont,
191                   NULL);
192 }
193
194 /**
195  * Entry point for gnunet-dht-put
196  *
197  * @param argc number of arguments from the command line
198  * @param argv command line arguments
199  * @return 0 ok, 1 on error
200  */
201 int
202 main (int argc, char *const *argv)
203 {
204
205   struct GNUNET_GETOPT_CommandLineOption options[] = {
206   
207     GNUNET_GETOPT_option_string ('d',
208                                  "data",
209                                  "DATA",
210                                  gettext_noop ("the data to insert under the key"),
211                                  &data),
212   
213     GNUNET_GETOPT_option_relative_time ('e',
214                                             "expiration",
215                                             "EXPIRATION",
216                                             gettext_noop ("how long to store this entry in the dht (in seconds)"),
217                                             &expiration),
218   
219     GNUNET_GETOPT_option_string ('k',
220                                  "key",
221                                  "KEY",
222                                  gettext_noop ("the query key"),
223                                  &query_key),
224   
225     GNUNET_GETOPT_option_flag ('x',
226                                   "demultiplex",
227                                   gettext_noop ("use DHT's demultiplex everywhere option"),
228                                   &demultixplex_everywhere),
229   
230     GNUNET_GETOPT_option_uint ('r',
231                                    "replication",
232                                    "LEVEL",
233                                    gettext_noop ("how many replicas to create"),
234                                    &replication),
235   
236     GNUNET_GETOPT_option_flag ('R',
237                                   "record",
238                                   gettext_noop ("use DHT's record route option"),
239                                   &record_route),
240   
241     GNUNET_GETOPT_option_uint ('t',
242                                    "type",
243                                    "TYPE",
244                                    gettext_noop ("the type to insert data as"),
245                                    &query_type),
246   
247     GNUNET_GETOPT_option_verbose (&verbose),
248   
249     GNUNET_GETOPT_OPTION_END
250   };
251
252
253   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
254                                                  &argc, &argv))
255     return 2;
256   expiration = GNUNET_TIME_UNIT_HOURS;
257   return (GNUNET_OK ==
258           GNUNET_PROGRAM_run (argc,
259                               argv,
260                               "gnunet-dht-put",
261                               gettext_noop
262                               ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
263                               options,
264                               &run,
265                               NULL))
266     ? ret : 1;
267 }
268
269 /* end of gnunet-dht-put.c */