-fixing #2397
[oweals/gnunet.git] / src / dht / gnunet-dht-put.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 for the query
36  */
37 static char *query_key;
38
39 /**
40  * User supplied timeout value
41  */
42 static unsigned long long timeout_request = 5;
43
44 /**
45  * User supplied expiration value
46  */
47 static unsigned long long expiration_seconds = 3600;
48
49 /**
50  * Desired replication level.
51  */
52 static unsigned int replication = 5;
53
54 /**
55  * Be verbose
56  */
57 static int verbose;
58
59 /**
60  * Handle to the DHT
61  */
62 static struct GNUNET_DHT_Handle *dht_handle;
63
64
65 /**
66  * Global handle of the configuration
67  */
68 static const struct GNUNET_CONFIGURATION_Handle *cfg;
69
70 /**
71  * Global status value
72  */
73 static int ret;
74
75 /**
76  * The data to insert into the dht
77  */
78 static char *data;
79
80 static void
81 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   if (NULL != dht_handle)
84   {
85     GNUNET_DHT_disconnect (dht_handle);
86     dht_handle = NULL;
87   }
88 }
89
90 /**
91  * Signature of the main function of a task.
92  *
93  * @param cls closure
94  * @param success GNUNET_OK if the PUT was transmitted,
95  *                GNUNET_NO on timeout,
96  *                GNUNET_SYSERR on disconnect from service
97  *                after the PUT message was transmitted
98  *                (so we don't know if it was received or not)
99  */
100 static void
101 message_sent_cont (void *cls, int success)
102 {
103   if (verbose)
104   {
105     switch (success)
106     {
107     case GNUNET_OK:
108       FPRINTF (stderr, "%s",  _("PUT request sent!\n"));
109       break;
110     case GNUNET_NO:
111       FPRINTF (stderr, "%s",  _("Timeout sending PUT request!\n"));
112       break;
113     case GNUNET_SYSERR:
114       FPRINTF (stderr, "%s",  _("PUT request not confirmed!\n"));
115       break;
116     default:
117       GNUNET_break (0);
118       break;
119     }
120   }
121   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
122 }
123
124 /**
125  * Main function that will be run by the scheduler.
126  *
127  * @param cls closure
128  * @param args remaining command-line arguments
129  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
130  * @param c configuration
131  */
132 static void
133 run (void *cls, char *const *args, const char *cfgfile,
134      const struct GNUNET_CONFIGURATION_Handle *c)
135 {
136   struct GNUNET_TIME_Relative timeout;
137   struct GNUNET_TIME_Absolute expiration;
138   struct GNUNET_HashCode key;
139
140   cfg = c;
141
142   if ((query_key == NULL) || (data == NULL))
143   {
144     FPRINTF (stderr, "%s",  _("Must provide KEY and DATA for DHT put!\n"));
145     ret = 1;
146     return;
147   }
148
149   dht_handle = GNUNET_DHT_connect (cfg, 1);
150   if (dht_handle == NULL)
151   {
152     FPRINTF (stderr, _("Could not connect to %s service!\n"), "DHT");
153     ret = 1;
154     return;
155   }
156   else if (verbose)
157     FPRINTF (stderr, _("Connected to %s service!\n"), "DHT");
158
159   if (query_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
160     query_type = GNUNET_BLOCK_TYPE_TEST;
161
162   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
163
164   timeout =
165       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
166   expiration =
167       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
168                                         (GNUNET_TIME_UNIT_SECONDS,
169                                          expiration_seconds));
170
171   if (verbose)
172     FPRINTF (stderr, _("Issuing put request for `%s' with data `%s'!\n"),
173              query_key, data);
174   GNUNET_DHT_put (dht_handle, &key, replication, GNUNET_DHT_RO_NONE, query_type,
175                   strlen (data), data, expiration, timeout, &message_sent_cont,
176                   NULL);
177
178 }
179
180
181 /**
182  * gnunet-dht-put command line options
183  */
184 static struct GNUNET_GETOPT_CommandLineOption options[] = {
185   {'d', "data", "DATA",
186    gettext_noop ("the data to insert under the key"),
187    1, &GNUNET_GETOPT_set_string, &data},
188   {'e', "expiration", "EXPIRATION",
189    gettext_noop ("how long to store this entry in the dht (in seconds)"),
190    1, &GNUNET_GETOPT_set_ulong, &expiration_seconds},
191   {'k', "key", "KEY",
192    gettext_noop ("the query key"),
193    1, &GNUNET_GETOPT_set_string, &query_key},
194   {'r', "replication", "LEVEL",
195    gettext_noop ("how many replicas to create"),
196    1, &GNUNET_GETOPT_set_uint, &replication},
197   {'t', "type", "TYPE",
198    gettext_noop ("the type to insert data as"),
199    1, &GNUNET_GETOPT_set_uint, &query_type},
200   {'T', "timeout", "TIMEOUT",
201    gettext_noop ("how long to execute this query before giving up?"),
202    1, &GNUNET_GETOPT_set_ulong, &timeout_request},
203   {'V', "verbose", NULL,
204    gettext_noop ("be verbose (print progress information)"),
205    0, &GNUNET_GETOPT_set_one, &verbose},
206   GNUNET_GETOPT_OPTION_END
207 };
208
209
210 /**
211  * Entry point for gnunet-dht-put
212  *
213  * @param argc number of arguments from the command line
214  * @param argv command line arguments
215  * @return 0 ok, 1 on error
216  */
217 int
218 main (int argc, char *const *argv)
219 {
220   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
221     return 2;
222
223   return (GNUNET_OK ==
224           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-put",
225                               gettext_noop
226                               ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
227                               options, &run, NULL)) ? ret : 1;
228 }
229
230 /* end of gnunet-dht-put.c */