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"
35 * Name of the directory which stores meta data for pseudonym
37 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
40 * Name of the directory which stores names for pseudonyms
42 #define PS_NAMES_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "names" DIR_SEPARATOR_STR
46 * Registered callbacks for discovery of pseudonyms.
48 struct DiscoveryCallback
51 * This is a linked list.
53 struct DiscoveryCallback *next;
56 * Function to call each time a pseudonym is discovered.
58 GNUNET_PSEUDONYM_Iterator callback;
61 * Closure for callback.
68 * Head of the linked list of functions to call when
69 * new pseudonyms are added.
71 static struct DiscoveryCallback *head;
74 * Internal notification about new tracked URI.
75 * @param id a point to the hash code of pseudonym
76 * @param md meta data to be written
77 * @param rating rating of pseudonym
80 internal_notify (const GNUNET_HashCode * id,
81 const struct GNUNET_CONTAINER_MetaData *md, int rating)
83 struct DiscoveryCallback *pos;
88 pos->callback (pos->closure, id, md, rating);
94 * Register callback to be invoked whenever we discover
96 * @param cfg configuration to use
97 * @param iterator iterator over pseudonym
98 * @param closure point to a closure
101 GNUNET_PSEUDONYM_discovery_callback_register (const struct
102 GNUNET_CONFIGURATION_Handle
104 GNUNET_PSEUDONYM_Iterator
105 iterator, void *closure)
107 struct DiscoveryCallback *list;
109 list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
110 list->callback = iterator;
111 list->closure = closure;
114 GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
119 * Unregister pseudonym discovery callback.
120 * @param iterator iterator over pseudonym
121 * @param closure point to a closure
124 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
125 iterator, void *closure)
127 struct DiscoveryCallback *prev;
128 struct DiscoveryCallback *pos;
132 while ((pos != NULL) &&
133 ((pos->callback != iterator) || (pos->closure != closure)))
139 return GNUNET_SYSERR;
143 prev->next = pos->next;
150 * Get the filename (or directory name) for the given
151 * pseudonym identifier and directory prefix.
152 * @param cfg configuration to use
153 * @param prefix path components to append to the private directory name
154 * @param psid hash code of pseudonym, can be NULL
155 * @return filename of the pseudonym (if psid != NULL) or directory with the data (if psid == NULL)
158 get_data_filename (const struct GNUNET_CONFIGURATION_Handle
159 *cfg, const char *prefix, const GNUNET_HashCode * psid)
161 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
164 GNUNET_CRYPTO_hash_to_enc (psid, &enc);
165 return GNUNET_DISK_get_home_filename (cfg,
166 GNUNET_CLIENT_SERVICE_NAME,
169 NULL) ? NULL : (const char *) &enc,
175 * Write the pseudonym infomation into a file
176 * @param cfg configuration to use
177 * @param nsid hash code of a pseudonym
178 * @param meta meta data to be written into a file
179 * @param ranking ranking of a pseudonym
180 * @param ns_name name of a pseudonym
183 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
184 const GNUNET_HashCode * nsid,
185 const struct GNUNET_CONTAINER_MetaData *meta,
186 int32_t ranking, const char *ns_name)
189 struct GNUNET_BIO_WriteHandle *fileW;
191 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
192 GNUNET_assert (fn != NULL);
193 fileW = GNUNET_BIO_write_open(fn);
196 if ( (GNUNET_OK != GNUNET_BIO_write_int32(fileW, ranking)) ||
197 (GNUNET_OK != GNUNET_BIO_write_string(fileW, ns_name)) ||
198 (GNUNET_OK != GNUNET_BIO_write_meta_data(fileW, meta)) )
200 (void) GNUNET_BIO_write_close(fileW);
201 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
205 if (GNUNET_OK != GNUNET_BIO_write_close(fileW))
207 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
213 /* create entry for pseudonym name in names */
214 /* FIXME: 90% of what this call does is not needed
215 here => refactor code to only create the entry! */
216 GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
221 * read the pseudonym infomation from a file
222 * @param cfg configuration to use
223 * @param nsid hash code of a pseudonym
224 * @param meta meta data to be read from a file
225 * @param ranking ranking of a pseudonym
226 * @param ns_name name of a pseudonym
229 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
230 const GNUNET_HashCode * nsid,
231 struct GNUNET_CONTAINER_MetaData **meta,
232 int32_t * ranking, char **ns_name)
236 struct GNUNET_BIO_ReadHandle *fileR;
238 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
239 GNUNET_assert (fn != NULL);
240 fileR = GNUNET_BIO_read_open(fn);
244 return GNUNET_SYSERR;
246 if ( (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
247 (GNUNET_OK != GNUNET_BIO_read_string(fileR, "Read string error!", ns_name, 200)) ||
248 (GNUNET_OK != GNUNET_BIO_read_meta_data(fileR, "Read meta data error!", meta)) )
250 GNUNET_BIO_read_close(fileR, &emsg);
251 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
253 return GNUNET_SYSERR;
256 if (GNUNET_OK != GNUNET_BIO_read_close(fileR, &emsg))
258 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
259 _("Failed to parse metadata about pseudonym from file `%s': %s\n"),
262 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
263 GNUNET_free_non_null (emsg);
265 return GNUNET_SYSERR;
274 * Return the unique, human readable name for the given namespace.
276 * @param cfg configuration
277 * @param nsid cryptographic ID of the namespace
278 * @return NULL on failure (should never happen)
281 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
282 const GNUNET_HashCode * nsid)
284 struct GNUNET_CONTAINER_MetaData *meta;
289 struct GNUNET_DISK_FileHandle *fh;
295 int32_t *rank = &temp;
299 if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name))
301 if ((meta != NULL) && (name == NULL))
302 name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
303 EXTRACTOR_METATYPE_TITLE,
304 EXTRACTOR_METATYPE_FILENAME,
305 EXTRACTOR_METATYPE_DESCRIPTION,
306 EXTRACTOR_METATYPE_SUBJECT,
307 EXTRACTOR_METATYPE_PUBLISHER,
308 EXTRACTOR_METATYPE_AUTHOR_NAME,
309 EXTRACTOR_METATYPE_COMMENT,
310 EXTRACTOR_METATYPE_SUMMARY,
314 GNUNET_CONTAINER_meta_data_destroy (meta);
319 name = GNUNET_strdup (_("no-name"));
320 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
321 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
322 GNUNET_assert (fn != NULL);
325 if (0 == STAT (fn, &sbuf))
326 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
327 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
328 | GNUNET_DISK_OPEN_READWRITE,
329 GNUNET_DISK_PERM_USER_READ |
330 GNUNET_DISK_PERM_USER_WRITE);
333 while ((len >= sizeof (GNUNET_HashCode)) &&
334 (sizeof (GNUNET_HashCode)
335 == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
337 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
343 len -= sizeof (GNUNET_HashCode);
348 if (sizeof (GNUNET_HashCode) !=
349 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
350 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
352 GNUNET_DISK_file_close (fh);
353 ret = GNUNET_malloc (strlen (name) + 32);
354 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
361 * Get the namespace ID belonging to the given namespace name.
363 * @param cfg configuration to use
364 * @param ns_uname human-readable name for the namespace
365 * @param nsid set to namespace ID based on 'ns_uname'
366 * @return GNUNET_OK on success
369 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
370 const char *ns_uname, GNUNET_HashCode * nsid)
378 struct GNUNET_DISK_FileHandle *fh;
381 slen = strlen (ns_uname);
382 while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
385 return GNUNET_SYSERR;
386 name = GNUNET_strdup (ns_uname);
387 name[slen - 1] = '\0';
388 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
390 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
391 GNUNET_assert (fn != NULL);
393 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
394 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
395 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
398 return GNUNET_SYSERR;
400 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
401 | GNUNET_DISK_OPEN_READWRITE,
402 GNUNET_DISK_PERM_USER_READ |
403 GNUNET_DISK_PERM_USER_WRITE);
405 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
406 GNUNET_DISK_SEEK_SET);
407 if (sizeof (GNUNET_HashCode) !=
408 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
410 GNUNET_DISK_file_close (fh);
411 return GNUNET_SYSERR;
413 GNUNET_DISK_file_close (fh);
420 * struct used to list the pseudonym
422 struct ListPseudonymClosure
426 * iterator over pseudonym
428 GNUNET_PSEUDONYM_Iterator iterator;
431 * Closure for iterator.
436 * Configuration to use.
438 const struct GNUNET_CONFIGURATION_Handle *cfg;
444 * the help function to list all available pseudonyms
445 * @param cls point to a struct ListPseudonymClosure
446 * @param fullname name of pseudonym
449 list_pseudonym_helper (void *cls, const char *fullname)
451 struct ListPseudonymClosure *c = cls;
455 struct GNUNET_CONTAINER_MetaData *meta;
459 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
462 &fullname[strlen (fullname) + 1 -
463 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
464 if (fn[-1] != DIR_SEPARATOR)
467 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
468 return GNUNET_OK; /* invalid name */
470 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str))
471 return GNUNET_OK; /* ignore entry */
472 GNUNET_free_non_null (str);
473 if (c->iterator != NULL)
474 ret = c->iterator (c->closure, &id, meta, rating);
475 GNUNET_CONTAINER_meta_data_destroy (meta);
481 * List all available pseudonyms.
483 * @param cfg overall configuration
484 * @param iterator function to call for each pseudonym
485 * @param closure closure for iterator
486 * @return number of pseudonyms found
489 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
490 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
492 struct ListPseudonymClosure cls;
496 cls.iterator = iterator;
497 cls.closure = closure;
499 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
500 GNUNET_assert (fn != NULL);
501 GNUNET_DISK_directory_create (fn);
502 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
509 * Change the ranking of a pseudonym.
511 * @param cfg overall configuration
512 * @param nsid id of the pseudonym
513 * @param delta by how much should the rating be
515 * @return new rating of the pseudonym
518 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
519 const GNUNET_HashCode * nsid, int delta)
521 struct GNUNET_CONTAINER_MetaData *meta;
527 ret = read_info (cfg, nsid, &meta, &ranking, &name);
528 if (ret == GNUNET_SYSERR)
531 meta = GNUNET_CONTAINER_meta_data_create ();
534 write_pseudonym_info (cfg, nsid, meta, ranking, name);
535 GNUNET_CONTAINER_meta_data_destroy (meta);
536 GNUNET_free_non_null (name);
542 * Add a pseudonym to the set of known pseudonyms.
543 * For all pseudonym advertisements that we discover
544 * FS should automatically call this function.
546 * @param cfg overall configuration
547 * @param id the pseudonym identifier
548 * @param meta metadata for the pseudonym
551 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
552 const GNUNET_HashCode * id,
553 const struct GNUNET_CONTAINER_MetaData *meta)
557 struct GNUNET_CONTAINER_MetaData *old;
562 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
563 GNUNET_assert (fn != NULL);
565 if ((0 == STAT (fn, &sbuf)) &&
566 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
568 GNUNET_CONTAINER_meta_data_merge (old, meta);
569 write_pseudonym_info (cfg, id, old, ranking, name);
570 GNUNET_CONTAINER_meta_data_destroy (old);
571 GNUNET_free_non_null (name);
575 write_pseudonym_info (cfg, id, meta, ranking, NULL);
578 internal_notify (id, meta, ranking);
582 /* end of pseudonym.c */