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"
36 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/metadata" DIR_SEPARATOR_STR
41 #define PS_NAMES_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/names" DIR_SEPARATOR_STR
47 struct DiscoveryCallback
53 struct DiscoveryCallback *next;
58 GNUNET_PSEUDONYM_Iterator callback;
70 static struct DiscoveryCallback *head;
73 * Internal notification about new tracked URI.
78 internal_notify (const GNUNET_HashCode * id,
79 const struct GNUNET_CONTAINER_MetaData *md, int rating)
81 struct DiscoveryCallback *pos;
86 pos->callback (pos->closure, id, md, rating);
92 * Register callback to be invoked whenever we discover
98 GNUNET_PSEUDONYM_discovery_callback_register (const struct
99 GNUNET_CONFIGURATION_Handle
101 GNUNET_PSEUDONYM_Iterator
102 iterator, void *closure)
104 struct DiscoveryCallback *list;
106 list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
107 list->callback = iterator;
108 list->closure = closure;
111 GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
116 * Unregister pseudonym discovery callback.
121 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
122 iterator, void *closure)
124 struct DiscoveryCallback *prev;
125 struct DiscoveryCallback *pos;
129 while ((pos != NULL) &&
130 ((pos->callback != iterator) || (pos->closure != closure)))
136 return GNUNET_SYSERR;
140 prev->next = pos->next;
147 * Get the filename (or directory name) for the given
148 * pseudonym identifier and directory prefix.
153 get_data_filename (const struct GNUNET_CONFIGURATION_Handle
154 *cfg, const char *prefix, const GNUNET_HashCode * psid)
156 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
159 GNUNET_CRYPTO_hash_to_enc (psid, &enc);
160 return GNUNET_DISK_get_home_filename (cfg,
161 GNUNET_CLIENT_SERVICE_NAME,
164 NULL) ? NULL : (const char *) &enc,
173 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
174 const GNUNET_HashCode * nsid,
175 const struct GNUNET_CONTAINER_MetaData *meta,
176 int32_t ranking, const char *ns_name)
184 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
185 GNUNET_assert (fn != NULL);
186 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
187 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
188 tag = size + sizeof (int32_t) + 1;
192 off = strlen (ns_name);
195 buf = GNUNET_malloc (tag);
196 ((int32_t *) buf)[0] = htonl (ranking); /* ranking */
199 memcpy (&buf[sizeof (int32_t)], ns_name, off + 1);
203 buf[sizeof (int)] = '\0';
206 (size == GNUNET_CONTAINER_meta_data_serialize (meta,
211 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL));
213 (tag != GNUNET_DISK_fn_write (fn, buf, tag, GNUNET_DISK_PERM_USER_READ
214 | GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_READ));
217 /* create entry for pseudonym name in names */
218 GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
226 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
227 const GNUNET_HashCode * nsid,
228 struct GNUNET_CONTAINER_MetaData **meta,
229 int32_t * ranking, char **ns_name)
242 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
243 GNUNET_assert (fn != NULL);
245 if ((0 != STAT (fn, &sbuf))
246 || (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES)))
249 return GNUNET_SYSERR;
251 if (len <= sizeof (int32_t) + 1)
254 return GNUNET_SYSERR;
256 if (len > 16 * 1024 * 1024)
258 /* too big, must be invalid! remove! */
260 if (0 != UNLINK (fn))
261 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
263 return GNUNET_SYSERR;
265 buf = GNUNET_malloc (len);
266 if (len != GNUNET_DISK_fn_read (fn, buf, len))
270 return GNUNET_SYSERR;
273 *ranking = ntohl (((int32_t *) buf)[0]);
274 zend = sizeof (int32_t);
275 while ((zend < len) && (buf[zend] != '\0'))
281 return GNUNET_SYSERR;
285 if (zend != sizeof (int32_t))
286 *ns_name = GNUNET_strdup (&buf[sizeof (int32_t)]);
294 *meta = GNUNET_CONTAINER_meta_data_deserialize (&buf[zend], size);
297 /* invalid data! remove! */
299 if (0 != UNLINK (fn))
300 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
304 return GNUNET_SYSERR;
315 * Return the unique, human readable name for the given namespace.
317 * @param cfg configuration
318 * @param nsid cryptographic ID of the namespace
319 * @return NULL on failure (should never happen)
322 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
323 const GNUNET_HashCode * nsid)
325 struct GNUNET_CONTAINER_MetaData *meta;
330 struct GNUNET_DISK_FileHandle *fh;
338 if (GNUNET_OK == read_info (cfg, nsid, &meta, NULL, &name))
340 if ((meta != NULL) && (name == NULL))
341 name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
344 EXTRACTOR_DESCRIPTION,
354 GNUNET_CONTAINER_meta_data_destroy (meta);
359 name = GNUNET_strdup (_("no-name"));
360 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
361 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
362 GNUNET_assert (fn != NULL);
365 if (0 == STAT (fn, &sbuf))
366 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
367 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
368 | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
369 | GNUNET_DISK_PERM_USER_WRITE);
372 while ((len >= sizeof (GNUNET_HashCode)) &&
373 (sizeof (GNUNET_HashCode)
374 == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
376 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
382 len -= sizeof (GNUNET_HashCode);
387 if (sizeof (GNUNET_HashCode) !=
388 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
389 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
391 GNUNET_DISK_file_close (fh);
392 ret = GNUNET_malloc (strlen (name) + 32);
393 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
400 * Get the namespace ID belonging to the given namespace name.
402 * @param cfg configuration to use
403 * @param ns_uname human-readable name for the namespace
404 * @param nsid set to namespace ID based on 'ns_uname'
405 * @return GNUNET_OK on success
408 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
409 const char *ns_uname, GNUNET_HashCode * nsid)
417 struct GNUNET_DISK_FileHandle *fh;
420 slen = strlen (ns_uname);
421 while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
424 return GNUNET_SYSERR;
425 name = GNUNET_strdup (ns_uname);
426 name[slen - 1] = '\0';
427 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
429 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
430 GNUNET_assert (fn != NULL);
432 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
433 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
434 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
437 return GNUNET_SYSERR;
439 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
440 | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
441 | GNUNET_DISK_PERM_USER_WRITE);
443 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), GNUNET_DISK_SEEK_SET);
444 if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
446 GNUNET_DISK_file_close (fh);
447 return GNUNET_SYSERR;
449 GNUNET_DISK_file_close (fh);
458 struct ListPseudonymClosure
464 GNUNET_PSEUDONYM_Iterator iterator;
474 const struct GNUNET_CONFIGURATION_Handle *cfg;
483 list_pseudonym_helper (void *cls, const char *fullname)
485 struct ListPseudonymClosure *c = cls;
489 struct GNUNET_CONTAINER_MetaData *meta;
492 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
495 &fullname[strlen (fullname) + 1 -
496 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
497 if (fn[-1] != DIR_SEPARATOR)
500 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
501 return GNUNET_OK; /* invalid name */
502 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, NULL))
503 return GNUNET_OK; /* ignore entry */
504 if (c->iterator != NULL)
505 ret = c->iterator (c->closure, &id, meta, rating);
506 GNUNET_CONTAINER_meta_data_destroy (meta);
512 * List all available pseudonyms.
514 * @param cfg overall configuration
515 * @param iterator function to call for each pseudonym
516 * @param closure closure for iterator
517 * @return number of pseudonyms found
520 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
521 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
523 struct ListPseudonymClosure cls;
527 cls.iterator = iterator;
528 cls.closure = closure;
530 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
531 GNUNET_assert (fn != NULL);
532 GNUNET_DISK_directory_create (fn);
533 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
539 * Change the ranking of a pseudonym.
541 * @param cfg overall configuration
542 * @param nsid id of the pseudonym
543 * @param delta by how much should the rating be
545 * @return new rating of the pseudonym
548 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
549 const GNUNET_HashCode * nsid, int delta)
551 struct GNUNET_CONTAINER_MetaData *meta;
557 ret = read_info (cfg, nsid, &meta, &ranking, &name);
558 if (ret == GNUNET_SYSERR)
561 meta = GNUNET_CONTAINER_meta_data_create ();
564 write_pseudonym_info (cfg, nsid, meta, ranking, name);
565 GNUNET_CONTAINER_meta_data_destroy (meta);
566 GNUNET_free_non_null (name);
571 * Insert metadata into existing MD record (passed as cls).
573 * @param cls metadata to add to
574 * @param type type of entry to insert
575 * @param data value of entry to insert
578 merge_meta_helper (void *cls,
579 EXTRACTOR_KeywordType type,
582 struct GNUNET_CONTAINER_MetaData *meta = cls;
583 GNUNET_CONTAINER_meta_data_insert (meta, type, data);
590 * Add a pseudonym to the set of known pseudonyms.
591 * For all pseudonym advertisements that we discover
592 * FS should automatically call this function.
594 * @param cfg overall configuration
595 * @param id the pseudonym identifier
596 * @param meta metadata for the pseudonym
599 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
600 const GNUNET_HashCode * id,
601 const struct GNUNET_CONTAINER_MetaData *meta)
605 struct GNUNET_CONTAINER_MetaData *old;
610 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
611 GNUNET_assert (fn != NULL);
613 if ((0 == STAT (fn, &sbuf)) &&
614 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
616 GNUNET_CONTAINER_meta_data_get_contents (meta, &merge_meta_helper, old);
617 write_pseudonym_info (cfg, id, old, ranking, name);
618 GNUNET_CONTAINER_meta_data_destroy (old);
619 GNUNET_free_non_null (name);
623 write_pseudonym_info (cfg, id, meta, ranking, NULL);
626 internal_notify (id, meta, ranking);
633 /* end of pseudonym.c */