GNUnet  0.10.x
gnunet-dht-put.c
Go to the documentation of this file.
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 it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
26 #include "platform.h"
27 #include "gnunet_dht_service.h"
28 
32 static unsigned int query_type;
33 
38 
42 static char *query_key;
43 
48 
52 static unsigned int replication = 5;
53 
57 static unsigned int verbose;
58 
63 
67 static int record_route;
68 
73 
74 
78 static const struct GNUNET_CONFIGURATION_Handle *cfg;
79 
83 static int ret;
84 
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 
107 static void
108 message_sent_cont (void *cls)
109 {
111  NULL);
112 }
113 
114 
123 static void
124 run (void *cls,
125  char *const *args,
126  const char *cfgfile,
127  const struct GNUNET_CONFIGURATION_Handle *c)
128 {
129  enum GNUNET_DHT_RouteOption ro;
130 
131  cfg = c;
132  if ((NULL == query_key) || (NULL == data))
133  {
134  FPRINTF (stderr, "%s", _("Must provide KEY and DATA for DHT put!\n"));
135  ret = 1;
136  return;
137  }
138 
139  if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
140  {
141  FPRINTF (stderr,
142  _("Could not connect to DHT service!\n"));
143  ret = 1;
144  return;
145  }
146  if (GNUNET_BLOCK_TYPE_ANY == query_type) /* Type of data not set */
148 
150 
151  if (verbose)
152  FPRINTF (stderr,
153  _("Issuing put request for `%s' with data `%s'!\n"),
154  query_key,
155  data);
156  ro = GNUNET_DHT_RO_NONE;
159  if (record_route)
161  GNUNET_DHT_put (dht_handle,
162  &key,
163  replication,
164  ro,
165  query_type,
166  strlen (data),
167  data,
170  NULL);
171 }
172 
180 int
181 main (int argc, char *const *argv)
182 {
183 
184  struct GNUNET_GETOPT_CommandLineOption options[] = {
186  "data",
187  "DATA",
188  gettext_noop ("the data to insert under the key"),
189  &data),
191  "expiration",
192  "EXPIRATION",
193  gettext_noop ("how long to store this entry in the dht (in seconds)"),
194  &expiration),
196  "key",
197  "KEY",
198  gettext_noop ("the query key"),
199  &query_key),
201  "demultiplex",
202  gettext_noop ("use DHT's demultiplex everywhere option"),
205  "replication",
206  "LEVEL",
207  gettext_noop ("how many replicas to create"),
208  &replication),
210  "record",
211  gettext_noop ("use DHT's record route option"),
212  &record_route),
214  "type",
215  "TYPE",
216  gettext_noop ("the type to insert data as"),
217  &query_type),
220  };
221 
222 
223  if (GNUNET_OK !=
224  GNUNET_STRINGS_get_utf8_args (argc, argv,
225  &argc, &argv))
226  return 2;
228  return (GNUNET_OK ==
229  GNUNET_PROGRAM_run (argc,
230  argv,
231  "gnunet-dht-put",
233  ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
234  options,
235  &run,
236  NULL))
237  ? ret : 1;
238 }
239 
240 /* end of gnunet-dht-put.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time)...
We should keep track of the route that the message took in the P2P network.
static struct GNUNET_DHT_Handle * dht_handle
Handle to the DHT.
#define GNUNET_TIME_UNIT_HOURS
One hour.
static unsigned int query_type
The type of the query.
Any type of block, used as a wildcard when searching.
#define FPRINTF
Definition: plibc.h:683
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the &#39;-V&#39; verbosity option.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:245
static char * query_key
The key for the query.
static void message_sent_cont(void *cls)
Signature of the main function of a task.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Definition of a command line option.
static unsigned int verbose
Be verbose.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
Connection to the DHT service.
Definition: dht_api.c:205
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1273
static unsigned int replication
Desired replication level.
A 512-bit hashcode.
GNUNET_DHT_RouteOption
Options for routing.
static int demultixplex_everywhere
Use #GNUNET_DHT_DEMULTIPLEX_EVERYWHERE.
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_DHT_PutHandle * GNUNET_DHT_put(struct GNUNET_DHT_Handle *handle, const struct GNUNET_HashCode *key, uint32_t desired_replication_level, enum GNUNET_DHT_RouteOption options, enum GNUNET_BLOCK_Type type, size_t size, const void *data, struct GNUNET_TIME_Absolute exp, GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
Perform a PUT operation storing data in the DHT.
Definition: dht_api.c:971
struct GNUNET_DHT_Handle * GNUNET_DHT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int ht_len)
Initialize the connection with the DHT service.
Definition: dht_api.c:895
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:923
static void shutdown_task(void *cls)
static int record_route
Use GNUNET_DHT_RO_RECORD_ROUTE.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run by the scheduler.
static int ret
Global status value.
configuration data
Definition: configuration.c:85
Block for testing.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:361
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Global handle of the configuration.
int main(int argc, char *const *argv)
Entry point for gnunet-dht-put.
Each peer along the way should look at &#39;enc&#39; (otherwise only the k-peers closest to the key should lo...
static char * data
The data to insert into the dht.
Time for relative time used by GNUnet, in microseconds.
#define gettext_noop(String)
Definition: gettext.h:69