GNUnet  0.10.x
gnunet-namecache.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012, 2013 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  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_gnsrecord_lib.h"
34 
35 
39 static struct GNUNET_NAMECACHE_Handle *ns;
40 
45 
49 static char *name;
50 
55 
59 static char *pkey;
60 
64 static int ret;
65 
66 
72 static void
73 do_shutdown(void *cls)
74 {
75  if (NULL != qe)
76  {
78  qe = NULL;
79  }
80  if (NULL != ns)
81  {
83  ns = NULL;
84  }
85 }
86 
87 
95 static void
97  unsigned int rd_len,
98  const struct GNUNET_GNSRECORD_Data *rd)
99 {
100  const char *typestring;
101  char *s;
102  unsigned int i;
103 
104  if (0 == rd_len)
105  {
106  fprintf(stdout, _("No records found for `%s'"), name);
107  return;
108  }
109  fprintf(stdout, "%s:\n", name);
110  for (i = 0; i < rd_len; i++)
111  {
112  typestring = GNUNET_GNSRECORD_number_to_typename(rd[i].record_type);
113  s = GNUNET_GNSRECORD_value_to_string(rd[i].record_type,
114  rd[i].data,
115  rd[i].data_size);
116  if (NULL == s)
117  {
118  fprintf(stdout,
119  _("\tCorrupt or unsupported record of type %u\n"),
120  (unsigned int)rd[i].record_type);
121  continue;
122  }
123  fprintf(stdout, "\t%s: %s\n", typestring, s);
124  GNUNET_free(s);
125  }
126  fprintf(stdout, "%s", "\n");
127 }
128 
129 
136 static void
137 handle_block(void *cls, const struct GNUNET_GNSRECORD_Block *block)
138 {
139  qe = NULL;
140  if (NULL == block)
141  {
142  fprintf(stderr, "No matching block found\n");
143  }
144  else if (GNUNET_OK !=
146  &pubkey,
147  name,
149  NULL))
150  {
151  fprintf(stderr, "Failed to decrypt block!\n");
152  }
154 }
155 
156 
165 static void
166 run(void *cls,
167  char *const *args,
168  const char *cfgfile,
169  const struct GNUNET_CONFIGURATION_Handle *cfg)
170 {
171  struct GNUNET_HashCode dhash;
172 
173  if (NULL == pkey)
174  {
175  fprintf(stderr, _("You must specify which zone should be accessed\n"));
176  return;
177  }
178 
179  if (GNUNET_OK !=
181  {
182  fprintf(stderr, _("Invalid public key for zone `%s'\n"), pkey);
184  return;
185  }
186  if (NULL == name)
187  {
188  fprintf(stderr, _("You must specify a name\n"));
189  return;
190  }
191 
193  ns = GNUNET_NAMECACHE_connect(cfg);
195  qe = GNUNET_NAMECACHE_lookup_block(ns, &dhash, &handle_block, NULL);
196 }
197 
198 
206 int
207 main(int argc, char *const *argv)
208 {
209  struct GNUNET_GETOPT_CommandLineOption options[] =
211  "name",
212  "NAME",
213  gettext_noop(
214  "name of the record to add/delete/display"),
215  &name),
216 
218  'z',
219  "zone",
220  "PKEY",
221  gettext_noop("specifies the public key of the zone to look in"),
222  &pkey),
223 
225 
226  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
227  return 2;
228 
229  GNUNET_log_setup("gnunet-namecache", "WARNING", NULL);
230  if (GNUNET_OK != GNUNET_PROGRAM_run(argc,
231  argv,
232  "gnunet-namecache",
233  _("GNUnet zone manipulation tool"),
234  options,
235  &run,
236  NULL))
237  {
238  GNUNET_free((void *)argv);
239  return 1;
240  }
241  GNUNET_free((void *)argv);
242  return ret;
243 }
244 
245 /* end of gnunet-namecache.c */
struct GNUNET_NAMECACHE_Handle * GNUNET_NAMECACHE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the namecache service.
static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey
Public key of the zone to look in.
static char * pkey
Public key of the zone to look in, in ASCII.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1439
static int ret
Global return value.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Definition of a command line option.
Information we have in an encrypted block with record data (i.e.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
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:181
static struct GNUNET_NAMECACHE_Handle * ns
Handle to the namecache.
int main(int argc, char *const *argv)
The main function for gnunet-namecache.
void GNUNET_NAMECACHE_cancel(struct GNUNET_NAMECACHE_QueueEntry *qe)
Cancel a namecache operation.
A 512-bit hashcode.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
static char * name
Name (label) of the records to list.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run.
struct GNUNET_NAMECACHE_QueueEntry * GNUNET_NAMECACHE_lookup_block(struct GNUNET_NAMECACHE_Handle *h, const struct GNUNET_HashCode *derived_hash, GNUNET_NAMECACHE_BlockProcessor proc, void *proc_cls)
Get a result for a particular key from the namecache.
int GNUNET_CRYPTO_ecdsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition: crypto_ecc.c:468
configuration data
Definition: configuration.c:83
int GNUNET_GNSRECORD_block_decrypt(const struct GNUNET_GNSRECORD_Block *block, const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, const char *label, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
Decrypt block.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
Connection to the NAMECACHE service.
Definition: namecache_api.c:93
static struct GNUNET_NAMECACHE_QueueEntry * qe
Queue entry for the &#39;query&#39; operation.
void GNUNET_NAMECACHE_disconnect(struct GNUNET_NAMECACHE_Handle *h)
Disconnect from the namecache service (and free associated resources).
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:367
An QueueEntry used to store information for a pending NAMECACHE record operation. ...
Definition: namecache_api.c:47
static char * typestring
Type of the record to add/remove, NULL to remove all.
void GNUNET_GNSRECORD_query_from_public_key(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, const char *label, struct GNUNET_HashCode *query)
Calculate the DHT query for a given label in a given zone.
static void display_records_from_block(void *cls, unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd)
Process a record that was stored in the namecache in a block.
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
static void do_shutdown(void *cls)
Task run on shutdown.
uint32_t data
The data value.
char * GNUNET_GNSRECORD_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the &#39;value&#39; of a record to a string.
Definition: gnsrecord.c:142
static size_t data_size
Number of bytes in data.
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number (i.e.
Definition: gnsrecord.c:230
static void handle_block(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Display block obtained from listing (by name).
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69