2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2010 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 fs/gnunet-pseudonym.c
22 * @brief manage GNUnet namespaces / pseudonyms
23 * @author Christian Grothoff
26 #include "gnunet_fs_service.h"
31 static char *create_ns;
36 static char *delete_ns;
41 static struct GNUNET_FS_Uri *ksk_uri;
46 static int print_local_only;
51 static struct GNUNET_CONTAINER_MetaData *adv_metadata;
54 * Our block options (-p, -r, -a).
56 static struct GNUNET_FS_BlockOptions bo = { {0LL}, 1, 365, 1 };
61 static int no_remote_printing;
66 static char *root_identifier;
71 static char *rating_change;
74 * Handle to fs service.
76 static struct GNUNET_FS_Handle *h;
79 * Namespace we are looking at.
81 static struct GNUNET_FS_Namespace *ns;
86 static const struct GNUNET_CONFIGURATION_Handle *cfg;
91 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
98 ns_printer (void *cls, const char *name, const struct GNUNET_FS_PseudonymIdentifier *pseudonym)
100 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
101 struct GNUNET_HashCode hc;
103 GNUNET_CRYPTO_hash (pseudonym,
104 sizeof (struct GNUNET_FS_PseudonymIdentifier),
106 GNUNET_CRYPTO_hash_to_enc (&hc, &enc);
107 FPRINTF (stdout, "%s (%s)\n", name, (const char *) &enc);
112 * Output information about a pseudonym.
115 * @param pseudonym hash code of public key of pseudonym
116 * @param name name of the pseudonym (might be NULL)
117 * @param unique_name unique name of the pseudonym (might be NULL)
118 * @param md meta data known about the pseudonym
119 * @param rating the local rating of the pseudonym
120 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
123 pseudo_printer (void *cls,
124 const struct GNUNET_FS_PseudonymIdentifier *pseudonym,
126 const char *unique_name,
127 const struct GNUNET_CONTAINER_MetaData *md,
134 /* While we get a name from the caller, it might be NULL.
135 * GNUNET_FS_pseudonym_get_info () never returns NULL.
137 getinfo_result = GNUNET_FS_pseudonym_get_info (cfg, pseudonym,
138 NULL, NULL, &id, NULL);
139 if (getinfo_result != GNUNET_OK)
144 unique_id = GNUNET_FS_pseudonym_name_uniquify (cfg, pseudonym, id, NULL);
146 FPRINTF (stdout, "%s (%d):\n", unique_id, rating);
147 GNUNET_CONTAINER_meta_data_iterate (md, &EXTRACTOR_meta_data_print, stdout);
148 FPRINTF (stdout, "%s", "\n");
149 GNUNET_free (unique_id);
155 post_advertising (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
157 struct GNUNET_FS_PseudonymIdentifier nsid;
163 FPRINTF (stderr, "%s", emsg);
168 if (GNUNET_OK != GNUNET_FS_namespace_delete (ns, GNUNET_NO))
171 if (NULL != rating_change)
174 while ((*set != '\0') && (*set != ':'))
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid argument `%s'\n"),
184 delta = strtol (&set[1], NULL, /* no error handling yet */
186 if (GNUNET_OK == GNUNET_FS_pseudonym_name_to_id (cfg, rating_change, &nsid))
188 (void) GNUNET_FS_pseudonym_rank (cfg, &nsid, delta);
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193 ("Namespace `%s' unknown. Make sure you specify its numeric suffix, if any.\n"),
197 GNUNET_free (rating_change);
198 rating_change = NULL;
200 if (0 != print_local_only)
202 GNUNET_FS_namespace_list (h, &ns_printer, NULL);
204 else if (0 == no_remote_printing)
206 GNUNET_FS_pseudonym_list_all (cfg, &pseudo_printer, NULL);
213 * Main function that will be run by the scheduler.
216 * @param args remaining command-line arguments
217 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
218 * @param c configuration
221 run (void *cls, char *const *args, const char *cfgfile,
222 const struct GNUNET_CONFIGURATION_Handle *c)
224 struct GNUNET_FS_Uri *sks_uri;
228 h = GNUNET_FS_start (cfg, "gnunet-pseudonym", &progress_cb, NULL,
229 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
230 if (NULL != delete_ns)
232 ns = GNUNET_FS_namespace_create (h, delete_ns);
239 if (GNUNET_OK != GNUNET_FS_namespace_delete (ns, GNUNET_YES))
244 if (NULL != create_ns)
246 ns = GNUNET_FS_namespace_create (h, create_ns);
253 if (NULL != root_identifier)
258 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
259 GNUNET_assert (NULL == emsg);
261 sks_uri = GNUNET_FS_uri_sks_create (ns, root_identifier, &emsg);
262 GNUNET_assert (NULL == emsg);
263 GNUNET_FS_publish_ksk (h, ksk_uri, adv_metadata, sks_uri,
265 GNUNET_FS_PUBLISH_OPTION_NONE,
266 &post_advertising, NULL);
267 GNUNET_FS_uri_destroy (sks_uri);
273 FPRINTF (stderr, _("Option `%s' ignored\n"), "-k");
279 if (root_identifier != NULL)
280 FPRINTF (stderr, _("Option `%s' ignored\n"), "-r");
282 FPRINTF (stderr, _("Option `%s' ignored\n"), "-k");
285 post_advertising (NULL, NULL, NULL);
290 * The main function to manipulate GNUnet pseudonyms (and publish
293 * @param argc number of arguments from the command line
294 * @param argv command line arguments
295 * @return 0 ok, 1 on error
298 main (int argc, char *const *argv)
300 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
301 {'a', "anonymity", "LEVEL",
302 gettext_noop ("set the desired LEVEL of sender-anonymity"),
303 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
304 {'C', "create", "NAME",
305 gettext_noop ("create or advertise namespace NAME"),
306 1, &GNUNET_GETOPT_set_string, &create_ns},
307 {'D', "delete", "NAME",
308 gettext_noop ("delete namespace NAME "),
309 1, &GNUNET_GETOPT_set_string, &delete_ns},
310 {'k', "keyword", "VALUE",
311 gettext_noop ("add an additional keyword for the advertisment"
312 " (this option can be specified multiple times)"),
313 1, &GNUNET_FS_getopt_set_keywords, &ksk_uri},
314 {'m', "meta", "TYPE:VALUE",
315 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
316 1, &GNUNET_FS_getopt_set_metadata, &adv_metadata},
317 {'o', "only-local", NULL,
318 gettext_noop ("print names of local namespaces"),
319 0, &GNUNET_GETOPT_set_one, &print_local_only},
320 {'p', "priority", "PRIORITY",
321 gettext_noop ("use the given PRIORITY for the advertisments"),
322 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
324 gettext_noop ("do not print names of remote namespaces"),
325 0, &GNUNET_GETOPT_set_one, &no_remote_printing},
326 {'r', "replication", "LEVEL",
327 gettext_noop ("set the desired replication LEVEL"),
328 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
330 gettext_noop ("specify ID of the root of the namespace"),
331 1, &GNUNET_GETOPT_set_string, &root_identifier},
332 {'s', "set-rating", "ID:VALUE",
333 gettext_noop ("change rating of namespace ID by VALUE"),
334 1, &GNUNET_GETOPT_set_string, &rating_change},
335 GNUNET_GETOPT_OPTION_END
338 GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
340 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
344 GNUNET_PROGRAM_run (argc, argv, "gnunet-pseudonym [OPTIONS]",
345 gettext_noop ("Manage GNUnet pseudonyms."),
346 options, &run, NULL)) ? ret : 1;
347 GNUNET_free ((void*) argv);
351 /* end of gnunet-pseudonym.c */