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 |
215 GNUNET_DISK_PERM_GROUP_READ));
218 /* create entry for pseudonym name in names */
219 GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
227 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
228 const GNUNET_HashCode * nsid,
229 struct GNUNET_CONTAINER_MetaData **meta,
230 int32_t * ranking, char **ns_name)
243 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
244 GNUNET_assert (fn != NULL);
246 if ((0 != STAT (fn, &sbuf))
247 || (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES)))
250 return GNUNET_SYSERR;
252 if (len <= sizeof (int32_t) + 1)
255 return GNUNET_SYSERR;
257 if (len > 16 * 1024 * 1024)
259 /* too big, must be invalid! remove! */
261 if (0 != UNLINK (fn))
262 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
264 return GNUNET_SYSERR;
266 buf = GNUNET_malloc (len);
267 if (len != GNUNET_DISK_fn_read (fn, buf, len))
271 return GNUNET_SYSERR;
274 *ranking = ntohl (((int32_t *) buf)[0]);
275 zend = sizeof (int32_t);
276 while ((zend < len) && (buf[zend] != '\0'))
282 return GNUNET_SYSERR;
286 if (zend != sizeof (int32_t))
287 *ns_name = GNUNET_strdup (&buf[sizeof (int32_t)]);
295 *meta = GNUNET_CONTAINER_meta_data_deserialize (&buf[zend], size);
298 /* invalid data! remove! */
300 if (0 != UNLINK (fn))
301 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
305 return GNUNET_SYSERR;
316 * Return the unique, human readable name for the given namespace.
318 * @param cfg configuration
319 * @param nsid cryptographic ID of the namespace
320 * @return NULL on failure (should never happen)
323 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
324 const GNUNET_HashCode * nsid)
326 struct GNUNET_CONTAINER_MetaData *meta;
331 struct GNUNET_DISK_FileHandle *fh;
339 if (GNUNET_OK == read_info (cfg, nsid, &meta, NULL, &name))
341 if ((meta != NULL) && (name == NULL))
342 name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
345 EXTRACTOR_DESCRIPTION,
355 GNUNET_CONTAINER_meta_data_destroy (meta);
360 name = GNUNET_strdup (_("no-name"));
361 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
362 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
363 GNUNET_assert (fn != NULL);
366 if (0 == STAT (fn, &sbuf))
367 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
368 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
369 | GNUNET_DISK_OPEN_READWRITE,
370 GNUNET_DISK_PERM_USER_READ |
371 GNUNET_DISK_PERM_USER_WRITE);
374 while ((len >= sizeof (GNUNET_HashCode)) &&
375 (sizeof (GNUNET_HashCode)
376 == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
378 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
384 len -= sizeof (GNUNET_HashCode);
389 if (sizeof (GNUNET_HashCode) !=
390 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
391 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
393 GNUNET_DISK_file_close (fh);
394 ret = GNUNET_malloc (strlen (name) + 32);
395 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
402 * Get the namespace ID belonging to the given namespace name.
404 * @param cfg configuration to use
405 * @param ns_uname human-readable name for the namespace
406 * @param nsid set to namespace ID based on 'ns_uname'
407 * @return GNUNET_OK on success
410 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
411 const char *ns_uname, GNUNET_HashCode * nsid)
419 struct GNUNET_DISK_FileHandle *fh;
422 slen = strlen (ns_uname);
423 while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
426 return GNUNET_SYSERR;
427 name = GNUNET_strdup (ns_uname);
428 name[slen - 1] = '\0';
429 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
431 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
432 GNUNET_assert (fn != NULL);
434 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
435 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
436 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
439 return GNUNET_SYSERR;
441 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
442 | GNUNET_DISK_OPEN_READWRITE,
443 GNUNET_DISK_PERM_USER_READ |
444 GNUNET_DISK_PERM_USER_WRITE);
446 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
447 GNUNET_DISK_SEEK_SET);
448 if (sizeof (GNUNET_HashCode) !=
449 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
451 GNUNET_DISK_file_close (fh);
452 return GNUNET_SYSERR;
454 GNUNET_DISK_file_close (fh);
463 struct ListPseudonymClosure
469 GNUNET_PSEUDONYM_Iterator iterator;
479 const struct GNUNET_CONFIGURATION_Handle *cfg;
488 list_pseudonym_helper (void *cls, const char *fullname)
490 struct ListPseudonymClosure *c = cls;
494 struct GNUNET_CONTAINER_MetaData *meta;
497 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
500 &fullname[strlen (fullname) + 1 -
501 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
502 if (fn[-1] != DIR_SEPARATOR)
505 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
506 return GNUNET_OK; /* invalid name */
507 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, NULL))
508 return GNUNET_OK; /* ignore entry */
509 if (c->iterator != NULL)
510 ret = c->iterator (c->closure, &id, meta, rating);
511 GNUNET_CONTAINER_meta_data_destroy (meta);
517 * List all available pseudonyms.
519 * @param cfg overall configuration
520 * @param iterator function to call for each pseudonym
521 * @param closure closure for iterator
522 * @return number of pseudonyms found
525 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
526 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
528 struct ListPseudonymClosure cls;
532 cls.iterator = iterator;
533 cls.closure = closure;
535 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
536 GNUNET_assert (fn != NULL);
537 GNUNET_DISK_directory_create (fn);
538 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
544 * Change the ranking of a pseudonym.
546 * @param cfg overall configuration
547 * @param nsid id of the pseudonym
548 * @param delta by how much should the rating be
550 * @return new rating of the pseudonym
553 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
554 const GNUNET_HashCode * nsid, int delta)
556 struct GNUNET_CONTAINER_MetaData *meta;
562 ret = read_info (cfg, nsid, &meta, &ranking, &name);
563 if (ret == GNUNET_SYSERR)
566 meta = GNUNET_CONTAINER_meta_data_create ();
569 write_pseudonym_info (cfg, nsid, meta, ranking, name);
570 GNUNET_CONTAINER_meta_data_destroy (meta);
571 GNUNET_free_non_null (name);
576 * Insert metadata into existing MD record (passed as cls).
578 * @param cls metadata to add to
579 * @param type type of entry to insert
580 * @param data value of entry to insert
583 merge_meta_helper (void *cls, EXTRACTOR_KeywordType type, const char *data)
585 struct GNUNET_CONTAINER_MetaData *meta = cls;
586 GNUNET_CONTAINER_meta_data_insert (meta, type, data);
593 * Add a pseudonym to the set of known pseudonyms.
594 * For all pseudonym advertisements that we discover
595 * FS should automatically call this function.
597 * @param cfg overall configuration
598 * @param id the pseudonym identifier
599 * @param meta metadata for the pseudonym
602 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
603 const GNUNET_HashCode * id,
604 const struct GNUNET_CONTAINER_MetaData *meta)
608 struct GNUNET_CONTAINER_MetaData *old;
613 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
614 GNUNET_assert (fn != NULL);
616 if ((0 == STAT (fn, &sbuf)) &&
617 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
619 GNUNET_CONTAINER_meta_data_get_contents (meta, &merge_meta_helper, old);
620 write_pseudonym_info (cfg, id, old, ranking, name);
621 GNUNET_CONTAINER_meta_data_destroy (old);
622 GNUNET_free_non_null (name);
626 write_pseudonym_info (cfg, id, meta, ranking, NULL);
629 internal_notify (id, meta, ranking);
636 /* end of pseudonym.c */