4a752ea24bae353517e03af9e53fe53f25a79f28
[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  * Be verbose
51  */
52 static int verbose;
53
54 /**
55  * Handle to the DHT
56  */
57 static struct GNUNET_DHT_Handle *dht_handle;
58
59
60 /**
61  * Global handle of the configuration
62  */
63 static const struct GNUNET_CONFIGURATION_Handle *cfg;
64
65 /**
66  * Global status value
67  */
68 static int ret;
69
70 /**
71  * The data to insert into the dht
72  */
73 static char *data;
74
75 static void
76 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 {
78   if (dht_handle != NULL)
79   {
80     GNUNET_DHT_disconnect (dht_handle);
81     dht_handle = NULL;
82   }
83 }
84
85 /**
86  * Signature of the main function of a task.
87  *
88  * @param cls closure
89  * @param tc context information (why was this task triggered now)
90  */
91 void
92 message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   if (verbose)
95     fprintf (stderr, _("PUT request sent!\n"));
96   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
97 }
98
99 /**
100  * Main function that will be run by the scheduler.
101  *
102  * @param cls closure
103  * @param args remaining command-line arguments
104  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
105  * @param c configuration
106  */
107 static void
108 run (void *cls, char *const *args, const char *cfgfile,
109      const struct GNUNET_CONFIGURATION_Handle *c)
110 {
111   struct GNUNET_TIME_Relative timeout;
112   struct GNUNET_TIME_Absolute expiration;
113   GNUNET_HashCode key;
114
115   cfg = c;
116
117   if ((query_key == NULL) || (data == NULL))
118   {
119     fprintf (stderr, _("Must provide KEY and DATA for DHT put!\n"));
120     ret = 1;
121     return;
122   }
123
124   dht_handle = GNUNET_DHT_connect (cfg, 1);
125   if (dht_handle == NULL)
126   {
127     fprintf (stderr, _("Could not connect to %s service!\n"), "DHT");
128     ret = 1;
129     return;
130   }
131   else if (verbose)
132     fprintf (stderr, _("Connected to %s service!\n"), "DHT");
133
134   if (query_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
135     query_type = GNUNET_BLOCK_TYPE_TEST;
136
137   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
138
139   timeout =
140       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
141   expiration =
142       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
143                                         (GNUNET_TIME_UNIT_SECONDS,
144                                          expiration_seconds));
145
146   if (verbose)
147     fprintf (stderr, _("Issuing put request for `%s' with data `%s'!\n"),
148              query_key, data);
149   GNUNET_DHT_put (dht_handle, &key, DEFAULT_PUT_REPLICATION, GNUNET_DHT_RO_NONE,
150                   query_type, strlen (data), data, expiration, timeout,
151                   &message_sent_cont, NULL);
152
153 }
154
155
156 /**
157  * gnunet-dht-put command line options
158  */
159 static struct GNUNET_GETOPT_CommandLineOption options[] = {
160   {'k', "key", "KEY",
161    gettext_noop ("the query key"),
162    1, &GNUNET_GETOPT_set_string, &query_key},
163   {'d', "data", "DATA",
164    gettext_noop ("the data to insert under the key"),
165    1, &GNUNET_GETOPT_set_string, &data},
166   {'t', "type", "TYPE",
167    gettext_noop ("the type to insert data as"),
168    1, &GNUNET_GETOPT_set_uint, &query_type},
169   {'T', "timeout", "TIMEOUT",
170    gettext_noop ("how long to execute this query before giving up?"),
171    1, &GNUNET_GETOPT_set_ulong, &timeout_request},
172   {'e', "expiration", "EXPIRATION",
173    gettext_noop ("how long to store this entry in the dht (in seconds)"),
174    1, &GNUNET_GETOPT_set_ulong, &expiration_seconds},
175   {'V', "verbose", NULL,
176    gettext_noop ("be verbose (print progress information)"),
177    0, &GNUNET_GETOPT_set_one, &verbose},
178   GNUNET_GETOPT_OPTION_END
179 };
180
181
182 /**
183  * Entry point for gnunet-dht-put
184  *
185  * @param argc number of arguments from the command line
186  * @param argv command line arguments
187  * @return 0 ok, 1 on error
188  */
189 int
190 main (int argc, char *const *argv)
191 {
192   return (GNUNET_OK ==
193           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-put",
194                               gettext_noop
195                               ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
196                               options, &run, NULL)) ? ret : 1;
197 }
198
199 /* end of gnunet-dht-put.c */