2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file dht/gnunet-dht-put.c
22 * @brief search for data in DHT
23 * @author Christian Grothoff
24 * @author Nathan Evans
27 #include "gnunet_dht_service.h"
30 * The type of the query
32 static unsigned int query_type;
35 * The key for the query
37 static char *query_key;
40 * User supplied timeout value
42 static unsigned long long timeout_request = 5;
45 * User supplied expiration value
47 static unsigned long long expiration_seconds = 3600;
57 static struct GNUNET_DHT_Handle *dht_handle;
60 * Global handle of the scheduler
62 static struct GNUNET_SCHEDULER_Handle *sched;
65 * Global handle of the configuration
67 static const struct GNUNET_CONFIGURATION_Handle *cfg;
75 * The data to insert into the dht
80 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 if (dht_handle != NULL)
84 GNUNET_DHT_disconnect (dht_handle);
90 * Signature of the main function of a task.
93 * @param tc context information (why was this task triggered now)
96 message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 _("PUT request sent!\n"));
101 GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL);
105 * Main function that will be run by the scheduler.
108 * @param s the scheduler to use
109 * @param args remaining command-line arguments
110 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
111 * @param c configuration
115 struct GNUNET_SCHEDULER_Handle *s,
117 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
119 struct GNUNET_TIME_Relative timeout;
120 struct GNUNET_TIME_Absolute expiration;
125 if ( (query_key == NULL) || (data == NULL) )
128 _("Must provide KEY and DATA for DHT put!\n"));
133 dht_handle = GNUNET_DHT_connect (sched, cfg, 1);
134 if (dht_handle == NULL)
137 _("Could not connect to %s service!\n"),
144 _("Connected to %s service!\n"), "DHT");
146 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
149 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
151 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
152 (GNUNET_TIME_UNIT_SECONDS,
153 expiration_seconds));
157 _("Issuing put request for `%s' with data `%s'!\n"),
159 GNUNET_DHT_put (dht_handle,
166 &message_sent_cont, NULL);
172 * gnunet-dht-put command line options
174 static struct GNUNET_GETOPT_CommandLineOption options[] = {
176 gettext_noop ("the query key"),
177 1, &GNUNET_GETOPT_set_string, &query_key},
178 {'d', "data", "DATA",
179 gettext_noop ("the data to insert under the key"),
180 1, &GNUNET_GETOPT_set_string, &data},
181 {'t', "type", "TYPE",
182 gettext_noop ("the type to insert data as"),
183 1, &GNUNET_GETOPT_set_uint, &query_type},
184 {'T', "timeout", "TIMEOUT",
185 gettext_noop ("how long to execute this query before giving up?"),
186 1, &GNUNET_GETOPT_set_ulong, &timeout_request},
187 {'e', "expiration", "EXPIRATION",
188 gettext_noop ("how long to store this entry in the dht (in seconds)"),
189 1, &GNUNET_GETOPT_set_ulong, &expiration_seconds},
190 {'V', "verbose", NULL,
191 gettext_noop ("be verbose (print progress information)"),
192 0, &GNUNET_GETOPT_set_one, &verbose},
193 GNUNET_GETOPT_OPTION_END
198 * Entry point for gnunet-dht-put
200 * @param argc number of arguments from the command line
201 * @param argv command line arguments
202 * @return 0 ok, 1 on error
205 main (int argc, char *const *argv)
208 GNUNET_PROGRAM_run (argc,
212 ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
213 options, &run, NULL)) ? ret : 1;
216 /* end of gnunet-dht-put.c */