demux everywhere option for CLI
[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 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 timeout value
46  */
47 static unsigned long long timeout_request = 5;
48
49 /**
50  * User supplied expiration value
51  */
52 static unsigned long long expiration_seconds = 3600;
53
54 /**
55  * Desired replication level.
56  */
57 static unsigned int replication = 5;
58
59 /**
60  * Be verbose
61  */
62 static int verbose;
63
64 /**
65  * Use DHT demultixplex_everywhere
66  */
67 static int demultixplex_everywhere;
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 static void
91 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 {
93   if (NULL != dht_handle)
94   {
95     GNUNET_DHT_disconnect (dht_handle);
96     dht_handle = NULL;
97   }
98 }
99
100 /**
101  * Signature of the main function of a task.
102  *
103  * @param cls closure
104  * @param success GNUNET_OK if the PUT was transmitted,
105  *                GNUNET_NO on timeout,
106  *                GNUNET_SYSERR on disconnect from service
107  *                after the PUT message was transmitted
108  *                (so we don't know if it was received or not)
109  */
110 static void
111 message_sent_cont (void *cls, int success)
112 {
113   if (verbose)
114   {
115     switch (success)
116     {
117     case GNUNET_OK:
118       FPRINTF (stderr, "%s `%s'!\n",  _("PUT request sent with key"), GNUNET_h2s_full(&key));
119       break;
120     case GNUNET_NO:
121       FPRINTF (stderr, "%s",  _("Timeout sending PUT request!\n"));
122       break;
123     case GNUNET_SYSERR:
124       FPRINTF (stderr, "%s",  _("PUT request not confirmed!\n"));
125       break;
126     default:
127       GNUNET_break (0);
128       break;
129     }
130   }
131   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
132 }
133
134 /**
135  * Main function that will be run by the scheduler.
136  *
137  * @param cls closure
138  * @param args remaining command-line arguments
139  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
140  * @param c configuration
141  */
142 static void
143 run (void *cls, char *const *args, const char *cfgfile,
144      const struct GNUNET_CONFIGURATION_Handle *c)
145 {
146   struct GNUNET_TIME_Relative timeout;
147   struct GNUNET_TIME_Absolute expiration;
148
149   cfg = c;
150
151   if ((query_key == NULL) || (data == NULL))
152   {
153     FPRINTF (stderr, "%s",  _("Must provide KEY and DATA for DHT put!\n"));
154     ret = 1;
155     return;
156   }
157
158   dht_handle = GNUNET_DHT_connect (cfg, 1);
159   if (dht_handle == NULL)
160   {
161     FPRINTF (stderr, _("Could not connect to %s service!\n"), "DHT");
162     ret = 1;
163     return;
164   }
165   else if (verbose)
166     FPRINTF (stderr, _("Connected to %s service!\n"), "DHT");
167
168   if (query_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
169     query_type = GNUNET_BLOCK_TYPE_TEST;
170
171   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
172
173   timeout =
174       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
175   expiration =
176       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
177                                         (GNUNET_TIME_UNIT_SECONDS,
178                                          expiration_seconds));
179
180   if (verbose)
181     FPRINTF (stderr, _("Issuing put request for `%s' with data `%s'!\n"),
182              query_key, data);
183   GNUNET_DHT_put (dht_handle, &key, replication,
184                   (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
185                   query_type,
186                   strlen (data), data, expiration, timeout, &message_sent_cont,
187                   NULL);
188
189 }
190
191
192 /**
193  * gnunet-dht-put command line options
194  */
195 static struct GNUNET_GETOPT_CommandLineOption options[] = {
196   {'d', "data", "DATA",
197    gettext_noop ("the data to insert under the key"),
198    1, &GNUNET_GETOPT_set_string, &data},
199   {'e', "expiration", "EXPIRATION",
200    gettext_noop ("how long to store this entry in the dht (in seconds)"),
201    1, &GNUNET_GETOPT_set_ulong, &expiration_seconds},
202   {'k', "key", "KEY",
203    gettext_noop ("the query key"),
204    1, &GNUNET_GETOPT_set_string, &query_key},
205   {'x', "demultiplex", NULL,
206    gettext_noop ("use DHT's demultiplex everywhere option"),
207    0, &GNUNET_GETOPT_set_one, &demultixplex_everywhere},
208   {'r', "replication", "LEVEL",
209    gettext_noop ("how many replicas to create"),
210    1, &GNUNET_GETOPT_set_uint, &replication},
211   {'t', "type", "TYPE",
212    gettext_noop ("the type to insert data as"),
213    1, &GNUNET_GETOPT_set_uint, &query_type},
214   {'T', "timeout", "TIMEOUT",
215    gettext_noop ("how long to execute this query before giving up?"),
216    1, &GNUNET_GETOPT_set_ulong, &timeout_request},
217   {'V', "verbose", NULL,
218    gettext_noop ("be verbose (print progress information)"),
219    0, &GNUNET_GETOPT_set_one, &verbose},
220   GNUNET_GETOPT_OPTION_END
221 };
222
223
224 /**
225  * Entry point for gnunet-dht-put
226  *
227  * @param argc number of arguments from the command line
228  * @param argv command line arguments
229  * @return 0 ok, 1 on error
230  */
231 int
232 main (int argc, char *const *argv)
233 {
234   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
235     return 2;
236
237   return (GNUNET_OK ==
238           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-put",
239                               gettext_noop
240                               ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
241                               options, &run, NULL)) ? ret : 1;
242 }
243
244 /* end of gnunet-dht-put.c */