2 This file is part of GNUnet
3 (C) 2003, 2004, 2005, 2006, 2007, 2008 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 2, 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.
22 * @file util/pseudonym.c
23 * @brief helper functions
24 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_disk_lib.h"
31 #include "gnunet_pseudonym_lib.h"
32 #include "gnunet_bio_lib.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
39 * Name of the directory which stores meta data for pseudonym
41 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
44 * Name of the directory which stores names for pseudonyms
46 #define PS_NAMES_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "names" DIR_SEPARATOR_STR
50 * Configuration section we use.
52 #define GNUNET_CLIENT_SERVICE_NAME "client"
57 * Registered callbacks for discovery of pseudonyms.
59 struct DiscoveryCallback
62 * This is a linked list.
64 struct DiscoveryCallback *next;
67 * Function to call each time a pseudonym is discovered.
69 GNUNET_PSEUDONYM_Iterator callback;
72 * Closure for callback.
79 * Head of the linked list of functions to call when
80 * new pseudonyms are added.
82 static struct DiscoveryCallback *head;
85 * Internal notification about new tracked URI.
86 * @param id a point to the hash code of pseudonym
87 * @param md meta data to be written
88 * @param rating rating of pseudonym
91 internal_notify (const GNUNET_HashCode * id,
92 const struct GNUNET_CONTAINER_MetaData *md, int rating)
94 struct DiscoveryCallback *pos;
99 pos->callback (pos->closure, id, NULL, NULL, md, rating);
105 * Register callback to be invoked whenever we discover
107 * Will immediately call provided iterator callback for all
108 * already discovered pseudonyms.
110 * @param cfg configuration to use
111 * @param iterator iterator over pseudonym
112 * @param closure point to a closure
115 GNUNET_PSEUDONYM_discovery_callback_register (const struct
116 GNUNET_CONFIGURATION_Handle *cfg,
117 GNUNET_PSEUDONYM_Iterator
118 iterator, void *closure)
120 struct DiscoveryCallback *list;
122 list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
123 list->callback = iterator;
124 list->closure = closure;
127 GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
132 * Unregister pseudonym discovery callback.
133 * @param iterator iterator over pseudonym
134 * @param closure point to a closure
137 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
138 iterator, void *closure)
140 struct DiscoveryCallback *prev;
141 struct DiscoveryCallback *pos;
145 while ((pos != NULL) &&
146 ((pos->callback != iterator) || (pos->closure != closure)))
152 return GNUNET_SYSERR;
156 prev->next = pos->next;
163 * Get the filename (or directory name) for the given
164 * pseudonym identifier and directory prefix.
165 * @param cfg configuration to use
166 * @param prefix path components to append to the private directory name
167 * @param psid hash code of pseudonym, can be NULL
168 * @return filename of the pseudonym (if psid != NULL) or directory with the data (if psid == NULL)
171 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
172 const char *prefix, const GNUNET_HashCode * psid)
174 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
177 GNUNET_CRYPTO_hash_to_enc (psid, &enc);
178 return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, prefix,
180 NULL) ? NULL : (const char *) &enc,
186 * Write the pseudonym infomation into a file
187 * @param cfg configuration to use
188 * @param nsid hash code of a pseudonym
189 * @param meta meta data to be written into a file
190 * @param ranking ranking of a pseudonym
191 * @param ns_name non-unique name of a pseudonym
194 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
195 const GNUNET_HashCode * nsid,
196 const struct GNUNET_CONTAINER_MetaData *meta,
197 int32_t ranking, const char *ns_name)
200 struct GNUNET_BIO_WriteHandle *fileW;
202 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
203 GNUNET_assert (fn != NULL);
204 fileW = GNUNET_BIO_write_open (fn);
207 if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
208 (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
209 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
211 (void) GNUNET_BIO_write_close (fileW);
212 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
216 if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
218 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
224 /* create entry for pseudonym name in names */
226 GNUNET_free_non_null (GNUNET_PSEUDONYM_name_uniquify (cfg, nsid, ns_name,
232 * read the pseudonym infomation from a file
233 * @param cfg configuration to use
234 * @param nsid hash code of a pseudonym
235 * @param meta meta data to be read from a file
236 * @param ranking ranking of a pseudonym
237 * @param ns_name name of a pseudonym
240 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
241 const GNUNET_HashCode * nsid,
242 struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking,
247 struct GNUNET_BIO_ReadHandle *fileR;
249 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
250 GNUNET_assert (fn != NULL);
251 fileR = GNUNET_BIO_read_open (fn);
255 return GNUNET_SYSERR;
259 if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
261 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
263 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
265 (void) GNUNET_BIO_read_close (fileR, &emsg);
266 GNUNET_free_non_null (emsg);
267 GNUNET_free_non_null (*ns_name);
269 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
271 return GNUNET_SYSERR;
273 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
275 LOG (GNUNET_ERROR_TYPE_WARNING,
276 _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
278 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
279 GNUNET_CONTAINER_meta_data_destroy (*meta);
281 GNUNET_free_non_null (*ns_name);
283 GNUNET_free_non_null (emsg);
285 return GNUNET_SYSERR;
292 * Return unique variant of the namespace name.
293 * Use it after GNUNET_PSEUDONYM_get_info() to make sure
294 * that name is unique.
296 * @param cfg configuration
297 * @param nsid cryptographic ID of the namespace
298 * @param name name to uniquify
299 * @param suffix if not NULL, filled with the suffix value
300 * @return NULL on failure (should never happen), name on success.
301 * Free the name with GNUNET_free().
304 GNUNET_PSEUDONYM_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
305 const GNUNET_HashCode * nsid, const char *name, unsigned int *suffix)
310 struct GNUNET_DISK_FileHandle *fh;
316 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
317 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
318 GNUNET_assert (fn != NULL);
321 if (0 == STAT (fn, &sbuf))
322 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
323 fh = GNUNET_DISK_file_open (fn,
324 GNUNET_DISK_OPEN_CREATE |
325 GNUNET_DISK_OPEN_READWRITE,
326 GNUNET_DISK_PERM_USER_READ |
327 GNUNET_DISK_PERM_USER_WRITE);
330 while ((len >= sizeof (GNUNET_HashCode)) &&
331 (sizeof (GNUNET_HashCode) ==
332 GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
334 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
340 len -= sizeof (GNUNET_HashCode);
345 if (sizeof (GNUNET_HashCode) !=
346 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
347 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
349 GNUNET_DISK_file_close (fh);
350 ret = GNUNET_malloc (strlen (name) + 32);
351 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
359 * Get namespace name, metadata and rank
360 * This is a wrapper around internal read_info() call, and ensures that
361 * returned data is not invalid (not NULL).
363 * @param cfg configuration
364 * @param nsid cryptographic ID of the namespace
365 * @param ret_meta a location to store metadata pointer. NULL, if metadata
366 * is not needed. Destroy with GNUNET_CONTAINER_meta_data_destroy().
367 * @param ret_rank a location to store rank. NULL, if rank not needed.
368 * @param ret_name a location to store human-readable name. Name is not unique.
369 * NULL, if name is not needed. Free with GNUNET_free().
370 * @param name_is_a_dup is set to GNUNET_YES, if ret_name was filled with
371 * a duplicate of a "no-name" placeholder
372 * @return GNUNET_OK on success. GNUENT_SYSERR if the data was
373 * unobtainable (in that case ret_* are filled with placeholders -
374 * empty metadata container, rank -1 and a "no-name" name).
377 GNUNET_PSEUDONYM_get_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
378 const GNUNET_HashCode * nsid, struct GNUNET_CONTAINER_MetaData **ret_meta,
379 int32_t *ret_rank, char **ret_name, int *name_is_a_dup)
381 struct GNUNET_CONTAINER_MetaData *meta;
387 if (GNUNET_OK == read_info (cfg, nsid, &meta, &rank, &name))
389 if ((meta != NULL) && (name == NULL))
391 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
392 EXTRACTOR_METATYPE_TITLE,
393 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
394 EXTRACTOR_METATYPE_FILENAME,
395 EXTRACTOR_METATYPE_DESCRIPTION,
396 EXTRACTOR_METATYPE_SUBJECT,
397 EXTRACTOR_METATYPE_PUBLISHER,
398 EXTRACTOR_METATYPE_AUTHOR_NAME,
399 EXTRACTOR_METATYPE_COMMENT,
400 EXTRACTOR_METATYPE_SUMMARY,
402 if (ret_name != NULL)
406 name = GNUNET_strdup (_("no-name"));
407 if (name_is_a_dup != NULL)
408 *name_is_a_dup = GNUNET_YES;
410 else if (name_is_a_dup != NULL)
411 *name_is_a_dup = GNUNET_NO;
414 else if (name != NULL)
417 if (ret_meta != NULL)
420 meta = GNUNET_CONTAINER_meta_data_create ();
423 else if (meta != NULL)
424 GNUNET_CONTAINER_meta_data_destroy (meta);
426 if (ret_rank != NULL)
431 if (ret_name != NULL)
432 *ret_name = GNUNET_strdup (_("no-name"));
433 if (ret_meta != NULL)
434 *ret_meta = GNUNET_CONTAINER_meta_data_create ();
435 if (ret_rank != NULL)
437 if (name_is_a_dup != NULL)
438 *name_is_a_dup = GNUNET_YES;
439 return GNUNET_SYSERR;
443 * Get the namespace ID belonging to the given namespace name.
445 * @param cfg configuration to use
446 * @param ns_uname unique (!) human-readable name for the namespace
447 * @param nsid set to namespace ID based on 'ns_uname'
448 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
451 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
452 const char *ns_uname, GNUNET_HashCode * nsid)
460 struct GNUNET_DISK_FileHandle *fh;
463 slen = strlen (ns_uname);
464 while ((slen > 0) && (1 != SSCANF (&ns_uname[slen - 1], "-%u", &idx)))
467 return GNUNET_SYSERR;
468 name = GNUNET_strdup (ns_uname);
469 name[slen - 1] = '\0';
471 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
473 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
474 GNUNET_assert (fn != NULL);
476 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
477 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
478 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
481 return GNUNET_SYSERR;
483 fh = GNUNET_DISK_file_open (fn,
484 GNUNET_DISK_OPEN_CREATE |
485 GNUNET_DISK_OPEN_READWRITE,
486 GNUNET_DISK_PERM_USER_READ |
487 GNUNET_DISK_PERM_USER_WRITE);
489 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
490 GNUNET_DISK_SEEK_SET);
491 if (sizeof (GNUNET_HashCode) !=
492 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
494 GNUNET_DISK_file_close (fh);
495 return GNUNET_SYSERR;
497 GNUNET_DISK_file_close (fh);
504 * struct used to list the pseudonym
506 struct ListPseudonymClosure
510 * iterator over pseudonym
512 GNUNET_PSEUDONYM_Iterator iterator;
515 * Closure for iterator.
520 * Configuration to use.
522 const struct GNUNET_CONFIGURATION_Handle *cfg;
528 * the help function to list all available pseudonyms
529 * @param cls point to a struct ListPseudonymClosure
530 * @param fullname name of pseudonym
533 list_pseudonym_helper (void *cls, const char *fullname)
535 struct ListPseudonymClosure *c = cls;
539 struct GNUNET_CONTAINER_MetaData *meta;
541 char *str, *name_unique;
543 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
545 fn = &fullname[strlen (fullname) + 1 -
546 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
547 if (fn[-1] != DIR_SEPARATOR)
550 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
551 return GNUNET_OK; /* invalid name */
553 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (c->cfg, &id, &meta, &rating,
556 /* ignore entry. FIXME: Why? Lack of data about a pseudonym is not a reason
557 * to ignore it... So yeah, it will have placeholders instead of name,
558 * empty metadata container and a default rank == -1, so what? We know
559 * its nsid - that's all we really need. Right? */
561 GNUNET_CONTAINER_meta_data_destroy (meta);
564 name_unique = GNUNET_PSEUDONYM_name_uniquify (c->cfg, &id, str, NULL);
565 if (c->iterator != NULL)
566 ret = c->iterator (c->closure, &id, str, name_unique, meta, rating);
567 GNUNET_free_non_null (str);
568 GNUNET_free_non_null (name_unique);
569 GNUNET_CONTAINER_meta_data_destroy (meta);
575 * List all available pseudonyms.
577 * @param cfg overall configuration
578 * @param iterator function to call for each pseudonym
579 * @param closure closure for iterator
580 * @return number of pseudonyms found
583 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
584 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
586 struct ListPseudonymClosure cls;
590 cls.iterator = iterator;
591 cls.closure = closure;
593 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
594 GNUNET_assert (fn != NULL);
595 GNUNET_DISK_directory_create (fn);
596 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
603 * Change the ranking of a pseudonym.
605 * @param cfg overall configuration
606 * @param nsid id of the pseudonym
607 * @param delta by how much should the rating be
609 * @return new rating of the pseudonym
612 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
613 const GNUNET_HashCode * nsid, int delta)
615 struct GNUNET_CONTAINER_MetaData *meta;
621 ret = read_info (cfg, nsid, &meta, &ranking, &name);
622 if (ret == GNUNET_SYSERR)
625 meta = GNUNET_CONTAINER_meta_data_create ();
628 write_pseudonym_info (cfg, nsid, meta, ranking, name);
629 GNUNET_CONTAINER_meta_data_destroy (meta);
630 GNUNET_free_non_null (name);
636 * Set the pseudonym metadata, rank and name.
638 * @param cfg overall configuration
639 * @param nsid id of the pseudonym
640 * @param name name to set. Must be the non-unique version of it.
641 * May be NULL, in which case it erases pseudonym's name!
642 * @param md metadata to set
643 * May be NULL, in which case it erases pseudonym's metadata!
644 * @param rank rank to assign
645 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
648 GNUNET_PSEUDONYM_set_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
649 const GNUNET_HashCode * nsid, const char *name,
650 const struct GNUNET_CONTAINER_MetaData *md, int rank)
652 GNUNET_assert (cfg != NULL);
653 GNUNET_assert (nsid != NULL);
655 write_pseudonym_info (cfg, nsid, md, rank, name);
661 * Add a pseudonym to the set of known pseudonyms.
662 * For all pseudonym advertisements that we discover
663 * FS should automatically call this function.
665 * @param cfg overall configuration
666 * @param id the pseudonym identifier
667 * @param meta metadata for the pseudonym
670 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
671 const GNUNET_HashCode * id,
672 const struct GNUNET_CONTAINER_MetaData *meta)
676 struct GNUNET_CONTAINER_MetaData *old;
681 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
682 GNUNET_assert (fn != NULL);
684 if ((0 == STAT (fn, &sbuf)) &&
685 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
687 GNUNET_CONTAINER_meta_data_merge (old, meta);
688 write_pseudonym_info (cfg, id, old, ranking, name);
689 GNUNET_CONTAINER_meta_data_destroy (old);
690 GNUNET_free_non_null (name);
694 write_pseudonym_info (cfg, id, meta, ranking, NULL);
697 internal_notify (id, meta, ranking);
701 /* end of pseudonym.c */