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"
37 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/metadata" DIR_SEPARATOR_STR
42 #define PS_NAMES_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/names" DIR_SEPARATOR_STR
48 struct DiscoveryCallback
54 struct DiscoveryCallback *next;
59 GNUNET_PSEUDONYM_Iterator callback;
71 static struct DiscoveryCallback *head;
74 * Internal notification about new tracked URI.
79 internal_notify (const GNUNET_HashCode * id,
80 const struct GNUNET_CONTAINER_MetaData *md, int rating)
82 struct DiscoveryCallback *pos;
87 pos->callback (pos->closure, id, md, rating);
93 * Register callback to be invoked whenever we discover
99 GNUNET_PSEUDONYM_discovery_callback_register (const struct
100 GNUNET_CONFIGURATION_Handle
102 GNUNET_PSEUDONYM_Iterator
103 iterator, void *closure)
105 struct DiscoveryCallback *list;
107 list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
108 list->callback = iterator;
109 list->closure = closure;
112 GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
117 * Unregister pseudonym discovery callback.
122 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
123 iterator, void *closure)
125 struct DiscoveryCallback *prev;
126 struct DiscoveryCallback *pos;
130 while ((pos != NULL) &&
131 ((pos->callback != iterator) || (pos->closure != closure)))
137 return GNUNET_SYSERR;
141 prev->next = pos->next;
148 * Get the filename (or directory name) for the given
149 * pseudonym identifier and directory prefix.
154 get_data_filename (const struct GNUNET_CONFIGURATION_Handle
155 *cfg, const char *prefix, const GNUNET_HashCode * psid)
157 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
160 GNUNET_CRYPTO_hash_to_enc (psid, &enc);
161 return GNUNET_DISK_get_home_filename (cfg,
162 GNUNET_CLIENT_SERVICE_NAME,
165 NULL) ? NULL : (const char *) &enc,
174 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
175 const GNUNET_HashCode * nsid,
176 const struct GNUNET_CONTAINER_MetaData *meta,
177 int32_t ranking, const char *ns_name)
180 struct GNUNET_BIO_WriteHandle *fileW;
182 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
183 GNUNET_assert (fn != NULL);
184 fileW = GNUNET_BIO_write_open(fn);
187 if ( (GNUNET_OK != GNUNET_BIO_write_int32(fileW, ranking)) ||
188 (GNUNET_OK != GNUNET_BIO_write_string(fileW, ns_name)) ||
189 (GNUNET_OK != GNUNET_BIO_write_meta_data(fileW, meta)) )
191 GNUNET_BIO_write_close(fileW);
192 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fileW));
196 if (GNUNET_OK != GNUNET_BIO_write_close(fileW))
198 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fileW));
204 /* create entry for pseudonym name in names */
205 GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
213 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
214 const GNUNET_HashCode * nsid,
215 struct GNUNET_CONTAINER_MetaData **meta,
216 int32_t * ranking, char **ns_name)
221 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
222 GNUNET_assert (fn != NULL);
223 struct GNUNET_BIO_ReadHandle *fileR;
224 fileR = GNUNET_BIO_read_open(fn);
226 (GNUNET_OK == GNUNET_BIO_read_int32__(fileR, "Read int32 error!", ranking))&&
227 (GNUNET_OK == GNUNET_BIO_read_string(fileR, "Read string error!", ns_name, 200))&&
228 (GNUNET_OK == GNUNET_BIO_read_meta_data(fileR, "Read meta data error!", meta))&&
229 (GNUNET_OK == GNUNET_BIO_read_close(fileR, &emsg)))
240 * Return the unique, human readable name for the given namespace.
242 * @param cfg configuration
243 * @param nsid cryptographic ID of the namespace
244 * @return NULL on failure (should never happen)
247 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
248 const GNUNET_HashCode * nsid)
250 struct GNUNET_CONTAINER_MetaData *meta;
255 struct GNUNET_DISK_FileHandle *fh;
261 int32_t *rank = &temp;
265 if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name))
267 if ((meta != NULL) && (name == NULL))
268 name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
271 EXTRACTOR_DESCRIPTION,
281 GNUNET_CONTAINER_meta_data_destroy (meta);
286 name = GNUNET_strdup (_("no-name"));
287 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
288 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
289 GNUNET_assert (fn != NULL);
292 if (0 == STAT (fn, &sbuf))
293 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
294 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
295 | GNUNET_DISK_OPEN_READWRITE,
296 GNUNET_DISK_PERM_USER_READ |
297 GNUNET_DISK_PERM_USER_WRITE);
300 while ((len >= sizeof (GNUNET_HashCode)) &&
301 (sizeof (GNUNET_HashCode)
302 == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
304 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
310 len -= sizeof (GNUNET_HashCode);
315 if (sizeof (GNUNET_HashCode) !=
316 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
317 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
319 GNUNET_DISK_file_close (fh);
320 ret = GNUNET_malloc (strlen (name) + 32);
321 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
328 * Get the namespace ID belonging to the given namespace name.
330 * @param cfg configuration to use
331 * @param ns_uname human-readable name for the namespace
332 * @param nsid set to namespace ID based on 'ns_uname'
333 * @return GNUNET_OK on success
336 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
337 const char *ns_uname, GNUNET_HashCode * nsid)
345 struct GNUNET_DISK_FileHandle *fh;
348 slen = strlen (ns_uname);
349 while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
352 return GNUNET_SYSERR;
353 name = GNUNET_strdup (ns_uname);
354 name[slen - 1] = '\0';
355 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
357 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
358 GNUNET_assert (fn != NULL);
360 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
361 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
362 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
365 return GNUNET_SYSERR;
367 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
368 | GNUNET_DISK_OPEN_READWRITE,
369 GNUNET_DISK_PERM_USER_READ |
370 GNUNET_DISK_PERM_USER_WRITE);
372 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
373 GNUNET_DISK_SEEK_SET);
374 if (sizeof (GNUNET_HashCode) !=
375 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
377 GNUNET_DISK_file_close (fh);
378 return GNUNET_SYSERR;
380 GNUNET_DISK_file_close (fh);
389 struct ListPseudonymClosure
395 GNUNET_PSEUDONYM_Iterator iterator;
405 const struct GNUNET_CONFIGURATION_Handle *cfg;
414 list_pseudonym_helper (void *cls, const char *fullname)
416 struct ListPseudonymClosure *c = cls;
420 struct GNUNET_CONTAINER_MetaData *meta;
424 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
427 &fullname[strlen (fullname) + 1 -
428 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
429 if (fn[-1] != DIR_SEPARATOR)
432 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
433 return GNUNET_OK; /* invalid name */
435 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str))
436 return GNUNET_OK; /* ignore entry */
437 GNUNET_free_non_null (str);
438 if (c->iterator != NULL)
439 ret = c->iterator (c->closure, &id, meta, rating);
440 GNUNET_CONTAINER_meta_data_destroy (meta);
446 * List all available pseudonyms.
448 * @param cfg overall configuration
449 * @param iterator function to call for each pseudonym
450 * @param closure closure for iterator
451 * @return number of pseudonyms found
454 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
455 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
457 struct ListPseudonymClosure cls;
461 cls.iterator = iterator;
462 cls.closure = closure;
464 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
465 GNUNET_assert (fn != NULL);
466 GNUNET_DISK_directory_create (fn);
467 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
473 * Change the ranking of a pseudonym.
475 * @param cfg overall configuration
476 * @param nsid id of the pseudonym
477 * @param delta by how much should the rating be
479 * @return new rating of the pseudonym
482 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
483 const GNUNET_HashCode * nsid, int delta)
485 struct GNUNET_CONTAINER_MetaData *meta;
491 ret = read_info (cfg, nsid, &meta, &ranking, &name);
492 if (ret == GNUNET_SYSERR)
495 meta = GNUNET_CONTAINER_meta_data_create ();
498 write_pseudonym_info (cfg, nsid, meta, ranking, name);
499 GNUNET_CONTAINER_meta_data_destroy (meta);
500 GNUNET_free_non_null (name);
505 * Insert metadata into existing MD record (passed as cls).
507 * @param cls metadata to add to
508 * @param type type of entry to insert
509 * @param data value of entry to insert
512 merge_meta_helper (void *cls, EXTRACTOR_KeywordType type, const char *data)
514 struct GNUNET_CONTAINER_MetaData *meta = cls;
515 GNUNET_CONTAINER_meta_data_insert (meta, type, data);
522 * Add a pseudonym to the set of known pseudonyms.
523 * For all pseudonym advertisements that we discover
524 * FS should automatically call this function.
526 * @param cfg overall configuration
527 * @param id the pseudonym identifier
528 * @param meta metadata for the pseudonym
531 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
532 const GNUNET_HashCode * id,
533 const struct GNUNET_CONTAINER_MetaData *meta)
537 struct GNUNET_CONTAINER_MetaData *old;
542 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
543 GNUNET_assert (fn != NULL);
545 if ((0 == STAT (fn, &sbuf)) &&
546 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
548 GNUNET_CONTAINER_meta_data_get_contents (meta, &merge_meta_helper, old);
549 write_pseudonym_info (cfg, id, old, ranking, name);
550 GNUNET_CONTAINER_meta_data_destroy (old);
551 GNUNET_free_non_null (name);
555 write_pseudonym_info (cfg, id, meta, ranking, NULL);
558 internal_notify (id, meta, ranking);
565 /* end of pseudonym.c */