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"
34 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
39 * Name of the directory which stores meta data for pseudonym
41 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
44 * Name of the directory which stores names for pseudonyms
46 #define PS_NAMES_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "names" DIR_SEPARATOR_STR
50 * Registered callbacks for discovery of pseudonyms.
52 struct DiscoveryCallback
55 * This is a linked list.
57 struct DiscoveryCallback *next;
60 * Function to call each time a pseudonym is discovered.
62 GNUNET_PSEUDONYM_Iterator callback;
65 * Closure for callback.
72 * Head of the linked list of functions to call when
73 * new pseudonyms are added.
75 static struct DiscoveryCallback *head;
78 * Internal notification about new tracked URI.
79 * @param id a point to the hash code of pseudonym
80 * @param md meta data to be written
81 * @param rating rating of pseudonym
84 internal_notify (const GNUNET_HashCode * id,
85 const struct GNUNET_CONTAINER_MetaData *md, int rating)
87 struct DiscoveryCallback *pos;
92 pos->callback (pos->closure, id, md, rating);
98 * Register callback to be invoked whenever we discover
100 * @param cfg configuration to use
101 * @param iterator iterator over pseudonym
102 * @param closure point to a closure
105 GNUNET_PSEUDONYM_discovery_callback_register (const struct
106 GNUNET_CONFIGURATION_Handle
108 GNUNET_PSEUDONYM_Iterator
109 iterator, void *closure)
111 struct DiscoveryCallback *list;
113 list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
114 list->callback = iterator;
115 list->closure = closure;
118 GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
123 * Unregister pseudonym discovery callback.
124 * @param iterator iterator over pseudonym
125 * @param closure point to a closure
128 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
129 iterator, void *closure)
131 struct DiscoveryCallback *prev;
132 struct DiscoveryCallback *pos;
136 while ((pos != NULL) &&
137 ((pos->callback != iterator) || (pos->closure != closure)))
143 return GNUNET_SYSERR;
147 prev->next = pos->next;
154 * Get the filename (or directory name) for the given
155 * pseudonym identifier and directory prefix.
156 * @param cfg configuration to use
157 * @param prefix path components to append to the private directory name
158 * @param psid hash code of pseudonym, can be NULL
159 * @return filename of the pseudonym (if psid != NULL) or directory with the data (if psid == NULL)
162 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
163 const char *prefix, const GNUNET_HashCode * psid)
165 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
168 GNUNET_CRYPTO_hash_to_enc (psid, &enc);
169 return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME,
172 NULL) ? NULL : (const char *) &enc,
178 * Write the pseudonym infomation into a file
179 * @param cfg configuration to use
180 * @param nsid hash code of a pseudonym
181 * @param meta meta data to be written into a file
182 * @param ranking ranking of a pseudonym
183 * @param ns_name name of a pseudonym
186 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
187 const GNUNET_HashCode * nsid,
188 const struct GNUNET_CONTAINER_MetaData *meta,
189 int32_t ranking, const char *ns_name)
192 struct GNUNET_BIO_WriteHandle *fileW;
194 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
195 GNUNET_assert (fn != NULL);
196 fileW = GNUNET_BIO_write_open (fn);
199 if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
200 (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
201 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
203 (void) GNUNET_BIO_write_close (fileW);
204 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
208 if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
210 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
216 /* create entry for pseudonym name in names */
217 /* FIXME: 90% of what this call does is not needed
218 * here => refactor code to only create the entry! */
219 GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
224 * read the pseudonym infomation from a file
225 * @param cfg configuration to use
226 * @param nsid hash code of a pseudonym
227 * @param meta meta data to be read from a file
228 * @param ranking ranking of a pseudonym
229 * @param ns_name name of a pseudonym
232 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
233 const GNUNET_HashCode * nsid,
234 struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking,
239 struct GNUNET_BIO_ReadHandle *fileR;
241 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
242 GNUNET_assert (fn != NULL);
243 fileR = GNUNET_BIO_read_open (fn);
247 return GNUNET_SYSERR;
251 if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
253 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
255 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
257 (void) GNUNET_BIO_read_close (fileR, &emsg);
258 GNUNET_free_non_null (emsg);
259 GNUNET_free_non_null (*ns_name);
261 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
263 return GNUNET_SYSERR;
265 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
267 LOG (GNUNET_ERROR_TYPE_WARNING,
269 ("Failed to parse metadata about pseudonym from file `%s': %s\n"),
271 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
272 GNUNET_CONTAINER_meta_data_destroy (*meta);
274 GNUNET_free_non_null (*ns_name);
276 GNUNET_free_non_null (emsg);
278 return GNUNET_SYSERR;
287 * Return the unique, human readable name for the given namespace.
289 * @param cfg configuration
290 * @param nsid cryptographic ID of the namespace
291 * @return NULL on failure (should never happen)
294 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
295 const GNUNET_HashCode * nsid)
297 struct GNUNET_CONTAINER_MetaData *meta;
302 struct GNUNET_DISK_FileHandle *fh;
308 int32_t *rank = &temp;
312 if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name))
314 if ((meta != NULL) && (name == NULL))
316 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
317 EXTRACTOR_METATYPE_TITLE,
318 EXTRACTOR_METATYPE_FILENAME,
319 EXTRACTOR_METATYPE_DESCRIPTION,
320 EXTRACTOR_METATYPE_SUBJECT,
321 EXTRACTOR_METATYPE_PUBLISHER,
322 EXTRACTOR_METATYPE_AUTHOR_NAME,
323 EXTRACTOR_METATYPE_COMMENT,
324 EXTRACTOR_METATYPE_SUMMARY,
328 GNUNET_CONTAINER_meta_data_destroy (meta);
333 name = GNUNET_strdup (_("no-name"));
334 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
335 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
336 GNUNET_assert (fn != NULL);
339 if (0 == STAT (fn, &sbuf))
340 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
341 fh = GNUNET_DISK_file_open (fn,
342 GNUNET_DISK_OPEN_CREATE |
343 GNUNET_DISK_OPEN_READWRITE,
344 GNUNET_DISK_PERM_USER_READ |
345 GNUNET_DISK_PERM_USER_WRITE);
348 while ((len >= sizeof (GNUNET_HashCode)) &&
349 (sizeof (GNUNET_HashCode) ==
350 GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
352 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
358 len -= sizeof (GNUNET_HashCode);
363 if (sizeof (GNUNET_HashCode) !=
364 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
365 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
367 GNUNET_DISK_file_close (fh);
368 ret = GNUNET_malloc (strlen (name) + 32);
369 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
376 * Get the namespace ID belonging to the given namespace name.
378 * @param cfg configuration to use
379 * @param ns_uname human-readable name for the namespace
380 * @param nsid set to namespace ID based on 'ns_uname'
381 * @return GNUNET_OK on success
384 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
385 const char *ns_uname, GNUNET_HashCode * nsid)
393 struct GNUNET_DISK_FileHandle *fh;
396 slen = strlen (ns_uname);
397 while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
400 return GNUNET_SYSERR;
401 name = GNUNET_strdup (ns_uname);
402 name[slen - 1] = '\0';
403 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
405 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
406 GNUNET_assert (fn != NULL);
408 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
409 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
410 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
413 return GNUNET_SYSERR;
415 fh = GNUNET_DISK_file_open (fn,
416 GNUNET_DISK_OPEN_CREATE |
417 GNUNET_DISK_OPEN_READWRITE,
418 GNUNET_DISK_PERM_USER_READ |
419 GNUNET_DISK_PERM_USER_WRITE);
421 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
422 GNUNET_DISK_SEEK_SET);
423 if (sizeof (GNUNET_HashCode) !=
424 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
426 GNUNET_DISK_file_close (fh);
427 return GNUNET_SYSERR;
429 GNUNET_DISK_file_close (fh);
436 * struct used to list the pseudonym
438 struct ListPseudonymClosure
442 * iterator over pseudonym
444 GNUNET_PSEUDONYM_Iterator iterator;
447 * Closure for iterator.
452 * Configuration to use.
454 const struct GNUNET_CONFIGURATION_Handle *cfg;
460 * the help function to list all available pseudonyms
461 * @param cls point to a struct ListPseudonymClosure
462 * @param fullname name of pseudonym
465 list_pseudonym_helper (void *cls, const char *fullname)
467 struct ListPseudonymClosure *c = cls;
471 struct GNUNET_CONTAINER_MetaData *meta;
475 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
477 fn = &fullname[strlen (fullname) + 1 -
478 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
479 if (fn[-1] != DIR_SEPARATOR)
482 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
483 return GNUNET_OK; /* invalid name */
485 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str))
486 return GNUNET_OK; /* ignore entry */
487 GNUNET_free_non_null (str);
488 if (c->iterator != NULL)
489 ret = c->iterator (c->closure, &id, meta, rating);
490 GNUNET_CONTAINER_meta_data_destroy (meta);
496 * List all available pseudonyms.
498 * @param cfg overall configuration
499 * @param iterator function to call for each pseudonym
500 * @param closure closure for iterator
501 * @return number of pseudonyms found
504 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
505 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
507 struct ListPseudonymClosure cls;
511 cls.iterator = iterator;
512 cls.closure = closure;
514 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
515 GNUNET_assert (fn != NULL);
516 GNUNET_DISK_directory_create (fn);
517 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
524 * Change the ranking of a pseudonym.
526 * @param cfg overall configuration
527 * @param nsid id of the pseudonym
528 * @param delta by how much should the rating be
530 * @return new rating of the pseudonym
533 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
534 const GNUNET_HashCode * nsid, int delta)
536 struct GNUNET_CONTAINER_MetaData *meta;
542 ret = read_info (cfg, nsid, &meta, &ranking, &name);
543 if (ret == GNUNET_SYSERR)
546 meta = GNUNET_CONTAINER_meta_data_create ();
549 write_pseudonym_info (cfg, nsid, meta, ranking, name);
550 GNUNET_CONTAINER_meta_data_destroy (meta);
551 GNUNET_free_non_null (name);
557 * Add a pseudonym to the set of known pseudonyms.
558 * For all pseudonym advertisements that we discover
559 * FS should automatically call this function.
561 * @param cfg overall configuration
562 * @param id the pseudonym identifier
563 * @param meta metadata for the pseudonym
566 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
567 const GNUNET_HashCode * id,
568 const struct GNUNET_CONTAINER_MetaData *meta)
572 struct GNUNET_CONTAINER_MetaData *old;
577 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
578 GNUNET_assert (fn != NULL);
580 if ((0 == STAT (fn, &sbuf)) &&
581 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
583 GNUNET_CONTAINER_meta_data_merge (old, meta);
584 write_pseudonym_info (cfg, id, old, ranking, name);
585 GNUNET_CONTAINER_meta_data_destroy (old);
586 GNUNET_free_non_null (name);
590 write_pseudonym_info (cfg, id, meta, ranking, NULL);
593 internal_notify (id, meta, ranking);
597 /* end of pseudonym.c */