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));
212 GNUNET_DISK_fn_write (fn, buf, tag, GNUNET_DISK_PERM_USER_READ
213 | GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_READ);
216 /* create entry for pseudonym name in names */
217 GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
225 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
226 const GNUNET_HashCode * nsid,
227 struct GNUNET_CONTAINER_MetaData **meta,
228 int32_t * ranking, char **ns_name)
241 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
242 GNUNET_assert (fn != NULL);
244 if ((0 != STAT (fn, &sbuf))
245 || (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES)))
248 return GNUNET_SYSERR;
250 if (len <= sizeof (int32_t) + 1)
253 return GNUNET_SYSERR;
255 if (len > 16 * 1024 * 1024)
257 /* too big, must be invalid! remove! */
259 if (0 != UNLINK (fn))
260 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
262 return GNUNET_SYSERR;
264 buf = GNUNET_malloc (len);
265 if (len != GNUNET_DISK_fn_read (fn, buf, len))
269 return GNUNET_SYSERR;
272 *ranking = ntohl (((int32_t *) buf)[0]);
273 zend = sizeof (int32_t);
274 while ((zend < len) && (buf[zend] != '\0'))
280 return GNUNET_SYSERR;
284 if (zend != sizeof (int32_t))
285 *ns_name = GNUNET_strdup (&buf[sizeof (int32_t)]);
293 *meta = GNUNET_CONTAINER_meta_data_deserialize (&buf[zend], size);
296 /* invalid data! remove! */
298 if (0 != UNLINK (fn))
299 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
303 return GNUNET_SYSERR;
314 * Return the unique, human readable name for the given namespace.
316 * @param cfg configuration
317 * @param nsid cryptographic ID of the namespace
318 * @return NULL on failure (should never happen)
321 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
322 const GNUNET_HashCode * nsid)
324 struct GNUNET_CONTAINER_MetaData *meta;
329 struct GNUNET_DISK_FileHandle *fh;
337 if (GNUNET_OK == read_info (cfg, nsid, &meta, NULL, &name))
339 if ((meta != NULL) && (name == NULL))
340 name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
343 EXTRACTOR_DESCRIPTION,
353 GNUNET_CONTAINER_meta_data_destroy (meta);
358 name = GNUNET_strdup (_("no-name"));
359 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
360 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
361 GNUNET_assert (fn != NULL);
364 if (0 == STAT (fn, &sbuf))
365 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
366 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
367 | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
368 | GNUNET_DISK_PERM_USER_WRITE);
371 while ((len >= sizeof (GNUNET_HashCode)) &&
372 (sizeof (GNUNET_HashCode)
373 == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
375 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
381 len -= sizeof (GNUNET_HashCode);
386 if (sizeof (GNUNET_HashCode) !=
387 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
388 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
390 GNUNET_DISK_file_close (fh);
391 ret = GNUNET_malloc (strlen (name) + 32);
392 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
399 * Get the namespace ID belonging to the given namespace name.
401 * @param cfg configuration to use
402 * @param ns_uname human-readable name for the namespace
403 * @param nsid set to namespace ID based on 'ns_uname'
404 * @return GNUNET_OK on success
407 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
408 const char *ns_uname, GNUNET_HashCode * nsid)
416 struct GNUNET_DISK_FileHandle *fh;
419 slen = strlen (ns_uname);
420 while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
423 return GNUNET_SYSERR;
424 name = GNUNET_strdup (ns_uname);
425 name[slen - 1] = '\0';
426 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
428 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
429 GNUNET_assert (fn != NULL);
431 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
432 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
433 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
436 return GNUNET_SYSERR;
438 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
439 | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
440 | GNUNET_DISK_PERM_USER_WRITE);
442 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), GNUNET_DISK_SEEK_SET);
443 if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
445 GNUNET_DISK_file_close (fh);
446 return GNUNET_SYSERR;
448 GNUNET_DISK_file_close (fh);
457 struct ListPseudonymClosure
463 GNUNET_PSEUDONYM_Iterator iterator;
473 const struct GNUNET_CONFIGURATION_Handle *cfg;
482 list_pseudonym_helper (void *cls, const char *fullname)
484 struct ListPseudonymClosure *c = cls;
488 struct GNUNET_CONTAINER_MetaData *meta;
491 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
494 &fullname[strlen (fullname) + 1 -
495 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
496 if (fn[-1] != DIR_SEPARATOR)
499 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
500 return GNUNET_OK; /* invalid name */
501 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, NULL))
502 return GNUNET_OK; /* ignore entry */
503 if (c->iterator != NULL)
504 ret = c->iterator (c->closure, &id, meta, rating);
505 GNUNET_CONTAINER_meta_data_destroy (meta);
511 * List all available pseudonyms.
513 * @param cfg overall configuration
514 * @param iterator function to call for each pseudonym
515 * @param closure closure for iterator
516 * @return number of pseudonyms found
519 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
520 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
522 struct ListPseudonymClosure cls;
526 cls.iterator = iterator;
527 cls.closure = closure;
529 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
530 GNUNET_assert (fn != NULL);
531 GNUNET_DISK_directory_create (fn);
532 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
538 * Change the ranking of a pseudonym.
540 * @param cfg overall configuration
541 * @param nsid id of the pseudonym
542 * @param delta by how much should the rating be
544 * @return new rating of the pseudonym
547 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
548 const GNUNET_HashCode * nsid, int delta)
550 struct GNUNET_CONTAINER_MetaData *meta;
556 ret = read_info (cfg, nsid, &meta, &ranking, &name);
557 if (ret == GNUNET_SYSERR)
560 meta = GNUNET_CONTAINER_meta_data_create ();
563 write_pseudonym_info (cfg, nsid, meta, ranking, name);
564 GNUNET_CONTAINER_meta_data_destroy (meta);
565 GNUNET_free_non_null (name);
570 * Insert metadata into existing MD record (passed as cls).
572 * @param cls metadata to add to
573 * @param type type of entry to insert
574 * @param data value of entry to insert
577 merge_meta_helper (void *cls,
578 EXTRACTOR_KeywordType type,
581 struct GNUNET_CONTAINER_MetaData *meta = cls;
582 GNUNET_CONTAINER_meta_data_insert (meta, type, data);
589 * Add a pseudonym to the set of known pseudonyms.
590 * For all pseudonym advertisements that we discover
591 * FSUI should automatically call this function.
593 * @param cfg overall configuration
594 * @param id the pseudonym identifier
595 * @param metadata for the pseudonym
598 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
599 const GNUNET_HashCode * id,
600 const struct GNUNET_CONTAINER_MetaData *meta)
604 struct GNUNET_CONTAINER_MetaData *old;
609 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
610 GNUNET_assert (fn != NULL);
612 if ((0 == STAT (fn, &sbuf)) &&
613 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
615 GNUNET_CONTAINER_meta_data_get_contents (meta, &merge_meta_helper, old);
616 write_pseudonym_info (cfg, id, old, ranking, name);
617 GNUNET_CONTAINER_meta_data_destroy (old);
618 GNUNET_free_non_null (name);
622 write_pseudonym_info (cfg, id, meta, ranking, NULL);
625 internal_notify (id, meta, ranking);
632 /* end of pseudonym.c */