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 * Configuration section we use.
52 #define GNUNET_CLIENT_SERVICE_NAME "client"
57 * Registered callbacks for discovery of pseudonyms.
59 struct DiscoveryCallback
62 * This is a linked list.
64 struct DiscoveryCallback *next;
67 * Function to call each time a pseudonym is discovered.
69 GNUNET_PSEUDONYM_Iterator callback;
72 * Closure for callback.
79 * Head of the linked list of functions to call when
80 * new pseudonyms are added.
82 static struct DiscoveryCallback *head;
85 * Internal notification about new tracked URI.
86 * @param id a point to the hash code of pseudonym
87 * @param md meta data to be written
88 * @param rating rating of pseudonym
91 internal_notify (const GNUNET_HashCode * id,
92 const struct GNUNET_CONTAINER_MetaData *md, int rating)
94 struct DiscoveryCallback *pos;
99 pos->callback (pos->closure, id, md, rating);
105 * Register callback to be invoked whenever we discover
107 * @param cfg configuration to use
108 * @param iterator iterator over pseudonym
109 * @param closure point to a closure
112 GNUNET_PSEUDONYM_discovery_callback_register (const struct
113 GNUNET_CONFIGURATION_Handle *cfg,
114 GNUNET_PSEUDONYM_Iterator
115 iterator, void *closure)
117 struct DiscoveryCallback *list;
119 list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
120 list->callback = iterator;
121 list->closure = closure;
124 GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
129 * Unregister pseudonym discovery callback.
130 * @param iterator iterator over pseudonym
131 * @param closure point to a closure
134 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
135 iterator, void *closure)
137 struct DiscoveryCallback *prev;
138 struct DiscoveryCallback *pos;
142 while ((pos != NULL) &&
143 ((pos->callback != iterator) || (pos->closure != closure)))
149 return GNUNET_SYSERR;
153 prev->next = pos->next;
160 * Get the filename (or directory name) for the given
161 * pseudonym identifier and directory prefix.
162 * @param cfg configuration to use
163 * @param prefix path components to append to the private directory name
164 * @param psid hash code of pseudonym, can be NULL
165 * @return filename of the pseudonym (if psid != NULL) or directory with the data (if psid == NULL)
168 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
169 const char *prefix, const GNUNET_HashCode * psid)
171 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
174 GNUNET_CRYPTO_hash_to_enc (psid, &enc);
175 return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, prefix,
177 NULL) ? NULL : (const char *) &enc,
183 * Write the pseudonym infomation into a file
184 * @param cfg configuration to use
185 * @param nsid hash code of a pseudonym
186 * @param meta meta data to be written into a file
187 * @param ranking ranking of a pseudonym
188 * @param ns_name name of a pseudonym
191 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
192 const GNUNET_HashCode * nsid,
193 const struct GNUNET_CONTAINER_MetaData *meta,
194 int32_t ranking, const char *ns_name)
197 struct GNUNET_BIO_WriteHandle *fileW;
199 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
200 GNUNET_assert (fn != NULL);
201 fileW = GNUNET_BIO_write_open (fn);
204 if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
205 (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
206 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
208 (void) GNUNET_BIO_write_close (fileW);
209 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
213 if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
215 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
221 /* create entry for pseudonym name in names */
222 /* FIXME: 90% of what this call does is not needed
223 * here => refactor code to only create the entry! */
224 GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
229 * read the pseudonym infomation from a file
230 * @param cfg configuration to use
231 * @param nsid hash code of a pseudonym
232 * @param meta meta data to be read from a file
233 * @param ranking ranking of a pseudonym
234 * @param ns_name name of a pseudonym
237 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
238 const GNUNET_HashCode * nsid,
239 struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking,
244 struct GNUNET_BIO_ReadHandle *fileR;
246 fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
247 GNUNET_assert (fn != NULL);
248 fileR = GNUNET_BIO_read_open (fn);
252 return GNUNET_SYSERR;
256 if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
258 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
260 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
262 (void) GNUNET_BIO_read_close (fileR, &emsg);
263 GNUNET_free_non_null (emsg);
264 GNUNET_free_non_null (*ns_name);
266 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
268 return GNUNET_SYSERR;
270 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
272 LOG (GNUNET_ERROR_TYPE_WARNING,
273 _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
275 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
276 GNUNET_CONTAINER_meta_data_destroy (*meta);
278 GNUNET_free_non_null (*ns_name);
280 GNUNET_free_non_null (emsg);
282 return GNUNET_SYSERR;
291 * Return the unique, human readable name for the given namespace.
293 * @param cfg configuration
294 * @param nsid cryptographic ID of the namespace
295 * @return NULL on failure (should never happen)
298 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
299 const GNUNET_HashCode * nsid)
301 struct GNUNET_CONTAINER_MetaData *meta;
306 struct GNUNET_DISK_FileHandle *fh;
312 int32_t *rank = &temp;
316 if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name))
318 if ((meta != NULL) && (name == NULL))
320 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
321 EXTRACTOR_METATYPE_TITLE,
322 EXTRACTOR_METATYPE_FILENAME,
323 EXTRACTOR_METATYPE_DESCRIPTION,
324 EXTRACTOR_METATYPE_SUBJECT,
325 EXTRACTOR_METATYPE_PUBLISHER,
326 EXTRACTOR_METATYPE_AUTHOR_NAME,
327 EXTRACTOR_METATYPE_COMMENT,
328 EXTRACTOR_METATYPE_SUMMARY,
332 GNUNET_CONTAINER_meta_data_destroy (meta);
337 name = GNUNET_strdup (_("no-name"));
338 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
339 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
340 GNUNET_assert (fn != NULL);
343 if (0 == STAT (fn, &sbuf))
344 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
345 fh = GNUNET_DISK_file_open (fn,
346 GNUNET_DISK_OPEN_CREATE |
347 GNUNET_DISK_OPEN_READWRITE,
348 GNUNET_DISK_PERM_USER_READ |
349 GNUNET_DISK_PERM_USER_WRITE);
352 while ((len >= sizeof (GNUNET_HashCode)) &&
353 (sizeof (GNUNET_HashCode) ==
354 GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
356 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
362 len -= sizeof (GNUNET_HashCode);
367 if (sizeof (GNUNET_HashCode) !=
368 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
369 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
371 GNUNET_DISK_file_close (fh);
372 ret = GNUNET_malloc (strlen (name) + 32);
373 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
380 * Get the namespace ID belonging to the given namespace name.
382 * @param cfg configuration to use
383 * @param ns_uname human-readable name for the namespace
384 * @param nsid set to namespace ID based on 'ns_uname'
385 * @return GNUNET_OK on success
388 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
389 const char *ns_uname, GNUNET_HashCode * nsid)
397 struct GNUNET_DISK_FileHandle *fh;
400 slen = strlen (ns_uname);
401 while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
404 return GNUNET_SYSERR;
405 name = GNUNET_strdup (ns_uname);
406 name[slen - 1] = '\0';
407 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
409 fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
410 GNUNET_assert (fn != NULL);
412 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
413 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
414 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
417 return GNUNET_SYSERR;
419 fh = GNUNET_DISK_file_open (fn,
420 GNUNET_DISK_OPEN_CREATE |
421 GNUNET_DISK_OPEN_READWRITE,
422 GNUNET_DISK_PERM_USER_READ |
423 GNUNET_DISK_PERM_USER_WRITE);
425 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
426 GNUNET_DISK_SEEK_SET);
427 if (sizeof (GNUNET_HashCode) !=
428 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
430 GNUNET_DISK_file_close (fh);
431 return GNUNET_SYSERR;
433 GNUNET_DISK_file_close (fh);
440 * struct used to list the pseudonym
442 struct ListPseudonymClosure
446 * iterator over pseudonym
448 GNUNET_PSEUDONYM_Iterator iterator;
451 * Closure for iterator.
456 * Configuration to use.
458 const struct GNUNET_CONFIGURATION_Handle *cfg;
464 * the help function to list all available pseudonyms
465 * @param cls point to a struct ListPseudonymClosure
466 * @param fullname name of pseudonym
469 list_pseudonym_helper (void *cls, const char *fullname)
471 struct ListPseudonymClosure *c = cls;
475 struct GNUNET_CONTAINER_MetaData *meta;
479 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
481 fn = &fullname[strlen (fullname) + 1 -
482 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
483 if (fn[-1] != DIR_SEPARATOR)
486 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
487 return GNUNET_OK; /* invalid name */
489 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str))
490 return GNUNET_OK; /* ignore entry */
491 GNUNET_free_non_null (str);
492 if (c->iterator != NULL)
493 ret = c->iterator (c->closure, &id, meta, rating);
494 GNUNET_CONTAINER_meta_data_destroy (meta);
500 * List all available pseudonyms.
502 * @param cfg overall configuration
503 * @param iterator function to call for each pseudonym
504 * @param closure closure for iterator
505 * @return number of pseudonyms found
508 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
509 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
511 struct ListPseudonymClosure cls;
515 cls.iterator = iterator;
516 cls.closure = closure;
518 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
519 GNUNET_assert (fn != NULL);
520 GNUNET_DISK_directory_create (fn);
521 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
528 * Change the ranking of a pseudonym.
530 * @param cfg overall configuration
531 * @param nsid id of the pseudonym
532 * @param delta by how much should the rating be
534 * @return new rating of the pseudonym
537 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
538 const GNUNET_HashCode * nsid, int delta)
540 struct GNUNET_CONTAINER_MetaData *meta;
546 ret = read_info (cfg, nsid, &meta, &ranking, &name);
547 if (ret == GNUNET_SYSERR)
550 meta = GNUNET_CONTAINER_meta_data_create ();
553 write_pseudonym_info (cfg, nsid, meta, ranking, name);
554 GNUNET_CONTAINER_meta_data_destroy (meta);
555 GNUNET_free_non_null (name);
561 * Add a pseudonym to the set of known pseudonyms.
562 * For all pseudonym advertisements that we discover
563 * FS should automatically call this function.
565 * @param cfg overall configuration
566 * @param id the pseudonym identifier
567 * @param meta metadata for the pseudonym
570 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
571 const GNUNET_HashCode * id,
572 const struct GNUNET_CONTAINER_MetaData *meta)
576 struct GNUNET_CONTAINER_MetaData *old;
581 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
582 GNUNET_assert (fn != NULL);
584 if ((0 == STAT (fn, &sbuf)) &&
585 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
587 GNUNET_CONTAINER_meta_data_merge (old, meta);
588 write_pseudonym_info (cfg, id, old, ranking, name);
589 GNUNET_CONTAINER_meta_data_destroy (old);
590 GNUNET_free_non_null (name);
594 write_pseudonym_info (cfg, id, meta, ranking, NULL);
597 internal_notify (id, meta, ranking);
601 /* end of pseudonym.c */