corrected HELLO_ACK message size
[oweals/gnunet.git] / src / util / pseudonym.c
1 /*
2      This file is part of GNUnet
3      (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/pseudonym.c
23  * @brief helper functions
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
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"
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
37
38 /**
39  * Name of the directory which stores meta data for pseudonym
40  */
41 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
42
43 /**
44  * Name of the directory which stores names for pseudonyms
45  */
46 #define PS_NAMES_DIR    DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "names"    DIR_SEPARATOR_STR
47
48
49 /**
50  * Configuration section we use.
51  */
52 #define GNUNET_CLIENT_SERVICE_NAME "client"
53
54
55
56 /**
57  * Registered callbacks for discovery of pseudonyms.
58  */
59 struct DiscoveryCallback
60 {
61   /**
62    * This is a linked list.
63    */
64   struct DiscoveryCallback *next;
65
66   /**
67    * Function to call each time a pseudonym is discovered.
68    */
69   GNUNET_PSEUDONYM_Iterator callback;
70
71   /**
72    * Closure for callback.
73    */
74   void *closure;
75 };
76
77
78 /**
79  * Head of the linked list of functions to call when
80  * new pseudonyms are added.
81  */
82 static struct DiscoveryCallback *head;
83
84 /**
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
89  */
90 static void
91 internal_notify (const GNUNET_HashCode * id,
92                  const struct GNUNET_CONTAINER_MetaData *md, int rating)
93 {
94   struct DiscoveryCallback *pos;
95
96   pos = head;
97   while (pos != NULL)
98   {
99     pos->callback (pos->closure, id, NULL, NULL, md, rating);
100     pos = pos->next;
101   }
102 }
103
104 /**
105  * Register callback to be invoked whenever we discover
106  * a new pseudonym.
107  * Will immediately call provided iterator callback for all
108  * already discovered pseudonyms.
109  *
110  * @param cfg configuration to use
111  * @param iterator iterator over pseudonym
112  * @param closure point to a closure
113  */
114 int
115 GNUNET_PSEUDONYM_discovery_callback_register (const struct
116                                               GNUNET_CONFIGURATION_Handle *cfg,
117                                               GNUNET_PSEUDONYM_Iterator
118                                               iterator, void *closure)
119 {
120   struct DiscoveryCallback *list;
121
122   list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
123   list->callback = iterator;
124   list->closure = closure;
125   list->next = head;
126   head = list;
127   GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
128   return GNUNET_OK;
129 }
130
131 /**
132  * Unregister pseudonym discovery callback.
133  * @param iterator iterator over pseudonym
134  * @param closure point to a closure
135  */
136 int
137 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
138                                                 iterator, void *closure)
139 {
140   struct DiscoveryCallback *prev;
141   struct DiscoveryCallback *pos;
142
143   prev = NULL;
144   pos = head;
145   while ((pos != NULL) &&
146          ((pos->callback != iterator) || (pos->closure != closure)))
147   {
148     prev = pos;
149     pos = pos->next;
150   }
151   if (pos == NULL)
152     return GNUNET_SYSERR;
153   if (prev == NULL)
154     head = pos->next;
155   else
156     prev->next = pos->next;
157   GNUNET_free (pos);
158   return GNUNET_OK;
159 }
160
161
162 /**
163  * Get the filename (or directory name) for the given
164  * pseudonym identifier and directory prefix.
165  * @param cfg configuration to use
166  * @param prefix path components to append to the private directory name
167  * @param psid hash code of pseudonym, can be NULL
168  * @return filename of the pseudonym (if psid != NULL) or directory with the data (if psid == NULL)
169  */
170 static char *
171 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
172                    const char *prefix, const GNUNET_HashCode * psid)
173 {
174   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
175
176   if (psid != NULL)
177     GNUNET_CRYPTO_hash_to_enc (psid, &enc);
178   return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, prefix,
179                                         (psid ==
180                                          NULL) ? NULL : (const char *) &enc,
181                                         NULL);
182 }
183
184
185 /**
186  * Write the pseudonym infomation into a file
187  * @param cfg configuration to use
188  * @param nsid hash code of a pseudonym
189  * @param meta meta data to be written into a file
190  * @param ranking ranking of a pseudonym
191  * @param ns_name non-unique name of a pseudonym
192  */
193 static void
194 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
195                       const GNUNET_HashCode * nsid,
196                       const struct GNUNET_CONTAINER_MetaData *meta,
197                       int32_t ranking, const char *ns_name)
198 {
199   char *fn;
200   struct GNUNET_BIO_WriteHandle *fileW;
201
202   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
203   GNUNET_assert (fn != NULL);
204   fileW = GNUNET_BIO_write_open (fn);
205   if (NULL != fileW)
206   {
207     if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
208         (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
209         (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
210     {
211       (void) GNUNET_BIO_write_close (fileW);
212       GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
213       GNUNET_free (fn);
214       return;
215     }
216     if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
217     {
218       GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
219       GNUNET_free (fn);
220       return;
221     }
222   }
223   GNUNET_free (fn);
224   /* create entry for pseudonym name in names */
225   if (ns_name != NULL)
226     GNUNET_free_non_null (GNUNET_PSEUDONYM_name_uniquify (cfg, nsid, ns_name,
227         NULL));
228 }
229
230
231 /**
232  * read the pseudonym infomation from a file
233  * @param cfg configuration to use
234  * @param nsid hash code of a pseudonym
235  * @param meta meta data to be read from a file
236  * @param ranking ranking of a pseudonym
237  * @param ns_name name of a pseudonym
238  */
239 static int
240 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
241            const GNUNET_HashCode * nsid,
242            struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking,
243            char **ns_name)
244 {
245   char *fn;
246   char *emsg;
247   struct GNUNET_BIO_ReadHandle *fileR;
248
249   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
250   GNUNET_assert (fn != NULL);
251   fileR = GNUNET_BIO_read_open (fn);
252   if (fileR == NULL)
253   {
254     GNUNET_free (fn);
255     return GNUNET_SYSERR;
256   }
257   emsg = NULL;
258   *ns_name = NULL;
259   if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
260       (GNUNET_OK !=
261        GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
262       (GNUNET_OK !=
263        GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
264   {
265     (void) GNUNET_BIO_read_close (fileR, &emsg);
266     GNUNET_free_non_null (emsg);
267     GNUNET_free_non_null (*ns_name);
268     *ns_name = NULL;
269     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
270     GNUNET_free (fn);
271     return GNUNET_SYSERR;
272   }
273   if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
274   {
275     LOG (GNUNET_ERROR_TYPE_WARNING,
276          _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
277          emsg);
278     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
279     GNUNET_CONTAINER_meta_data_destroy (*meta);
280     *meta = NULL;
281     GNUNET_free_non_null (*ns_name);
282     *ns_name = NULL;
283     GNUNET_free_non_null (emsg);
284     GNUNET_free (fn);
285     return GNUNET_SYSERR;
286   }
287   GNUNET_free (fn);
288   return GNUNET_OK;
289 }
290
291 /**
292  * Return unique variant of the namespace name.
293  * Use it after GNUNET_PSEUDONYM_get_info() to make sure
294  * that name is unique.
295  *
296  * @param cfg configuration
297  * @param nsid cryptographic ID of the namespace
298  * @param name name to uniquify
299  * @param suffix if not NULL, filled with the suffix value
300  * @return NULL on failure (should never happen), name on success.
301  *         Free the name with GNUNET_free().
302  */
303 char *
304 GNUNET_PSEUDONYM_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
305     const GNUNET_HashCode * nsid, const char *name, unsigned int *suffix)
306 {
307   GNUNET_HashCode nh;
308   uint64_t len;
309   char *fn;
310   struct GNUNET_DISK_FileHandle *fh;
311   unsigned int i;
312   unsigned int idx;
313   char *ret;
314   struct stat sbuf;
315
316   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
317   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
318   GNUNET_assert (fn != NULL);
319
320   len = 0;
321   if (0 == STAT (fn, &sbuf))
322     GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
323   fh = GNUNET_DISK_file_open (fn,
324                               GNUNET_DISK_OPEN_CREATE |
325                               GNUNET_DISK_OPEN_READWRITE,
326                               GNUNET_DISK_PERM_USER_READ |
327                               GNUNET_DISK_PERM_USER_WRITE);
328   i = 0;
329   idx = -1;
330   while ((len >= sizeof (GNUNET_HashCode)) &&
331          (sizeof (GNUNET_HashCode) ==
332           GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
333   {
334     if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
335     {
336       idx = i;
337       break;
338     }
339     i++;
340     len -= sizeof (GNUNET_HashCode);
341   }
342   if (idx == -1)
343   {
344     idx = i;
345     if (sizeof (GNUNET_HashCode) !=
346         GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
347       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
348   }
349   GNUNET_DISK_file_close (fh);
350   ret = GNUNET_malloc (strlen (name) + 32);
351   GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
352   if (suffix != NULL)
353     *suffix = idx;
354   GNUNET_free (fn);
355   return ret;
356 }
357
358 /**
359  * Get namespace name, metadata and rank
360  * This is a wrapper around internal read_info() call, and ensures that
361  * returned data is not invalid (not NULL).
362  *
363  * @param cfg configuration
364  * @param nsid cryptographic ID of the namespace
365  * @param ret_meta a location to store metadata pointer. NULL, if metadata
366  *        is not needed. Destroy with GNUNET_CONTAINER_meta_data_destroy().
367  * @param ret_rank a location to store rank. NULL, if rank not needed.
368  * @param ret_name a location to store human-readable name. Name is not unique.
369  *        NULL, if name is not needed. Free with GNUNET_free().
370  * @param name_is_a_dup is set to GNUNET_YES, if ret_name was filled with
371  *        a duplicate of a "no-name" placeholder
372  * @return GNUNET_OK on success. GNUENT_SYSERR if the data was
373  *         unobtainable (in that case ret_* are filled with placeholders - 
374  *         empty metadata container, rank -1 and a "no-name" name).
375  */
376 int
377 GNUNET_PSEUDONYM_get_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
378     const GNUNET_HashCode * nsid, struct GNUNET_CONTAINER_MetaData **ret_meta,
379     int32_t *ret_rank, char **ret_name, int *name_is_a_dup)
380 {
381   struct GNUNET_CONTAINER_MetaData *meta;
382   char *name;
383   int32_t rank = -1;
384
385   meta = NULL;
386   name = NULL;
387   if (GNUNET_OK == read_info (cfg, nsid, &meta, &rank, &name))
388   {
389     if ((meta != NULL) && (name == NULL))
390       name =
391           GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
392                                                          EXTRACTOR_METATYPE_TITLE,
393                                                          EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
394                                                          EXTRACTOR_METATYPE_FILENAME,
395                                                          EXTRACTOR_METATYPE_DESCRIPTION,
396                                                          EXTRACTOR_METATYPE_SUBJECT,
397                                                          EXTRACTOR_METATYPE_PUBLISHER,
398                                                          EXTRACTOR_METATYPE_AUTHOR_NAME,
399                                                          EXTRACTOR_METATYPE_COMMENT,
400                                                          EXTRACTOR_METATYPE_SUMMARY,
401                                                          -1);
402     if (ret_name != NULL)
403     {
404       if (name == NULL)
405       {
406         name = GNUNET_strdup (_("no-name"));
407         if (name_is_a_dup != NULL)
408           *name_is_a_dup = GNUNET_YES;
409       }
410       else if (name_is_a_dup != NULL)
411         *name_is_a_dup = GNUNET_NO;
412       *ret_name = name;
413     }
414     else if (name != NULL)
415       GNUNET_free (name);
416
417     if (ret_meta != NULL)
418     {
419       if (meta == NULL)
420         meta = GNUNET_CONTAINER_meta_data_create ();
421       *ret_meta = meta;
422     }
423     else if (meta != NULL)
424       GNUNET_CONTAINER_meta_data_destroy (meta);
425
426     if (ret_rank != NULL)
427       *ret_rank = rank;
428
429     return GNUNET_OK;
430   }
431   if (ret_name != NULL)
432     *ret_name = GNUNET_strdup (_("no-name"));
433   if (ret_meta != NULL)
434     *ret_meta = GNUNET_CONTAINER_meta_data_create ();
435   if (ret_rank != NULL)
436     *ret_rank = -1;
437   if (name_is_a_dup != NULL)
438     *name_is_a_dup = GNUNET_YES;
439   return GNUNET_SYSERR;
440 }
441
442 /**
443  * Get the namespace ID belonging to the given namespace name.
444  *
445  * @param cfg configuration to use
446  * @param ns_uname unique (!) human-readable name for the namespace
447  * @param nsid set to namespace ID based on 'ns_uname'
448  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
449  */
450 int
451 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
452     const char *ns_uname, GNUNET_HashCode * nsid)
453 {
454   size_t slen;
455   uint64_t len;
456   unsigned int idx;
457   char *name;
458   GNUNET_HashCode nh;
459   char *fn;
460   struct GNUNET_DISK_FileHandle *fh;
461
462   idx = -1;
463   slen = strlen (ns_uname);
464   while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
465     slen--;
466   if (slen == 0)
467     return GNUNET_SYSERR;
468   name = GNUNET_strdup (ns_uname);
469   name[slen - 1] = '\0';
470
471   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
472   GNUNET_free (name);
473   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
474   GNUNET_assert (fn != NULL);
475
476   if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
477        (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
478       ((idx + 1) * sizeof (GNUNET_HashCode) > len))
479   {
480     GNUNET_free (fn);
481     return GNUNET_SYSERR;
482   }
483   fh = GNUNET_DISK_file_open (fn,
484                               GNUNET_DISK_OPEN_CREATE |
485                               GNUNET_DISK_OPEN_READWRITE,
486                               GNUNET_DISK_PERM_USER_READ |
487                               GNUNET_DISK_PERM_USER_WRITE);
488   GNUNET_free (fn);
489   GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
490                          GNUNET_DISK_SEEK_SET);
491   if (sizeof (GNUNET_HashCode) !=
492       GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
493   {
494     GNUNET_DISK_file_close (fh);
495     return GNUNET_SYSERR;
496   }
497   GNUNET_DISK_file_close (fh);
498   return GNUNET_OK;
499 }
500
501
502
503 /**
504  * struct used to list the pseudonym
505  */
506 struct ListPseudonymClosure
507 {
508
509   /**
510    * iterator over pseudonym
511    */
512   GNUNET_PSEUDONYM_Iterator iterator;
513
514   /**
515    * Closure for iterator.
516    */
517   void *closure;
518
519   /**
520    * Configuration to use.
521    */
522   const struct GNUNET_CONFIGURATION_Handle *cfg;
523 };
524
525
526
527 /**
528  * the help function to list all available pseudonyms
529  * @param cls point to a struct ListPseudonymClosure
530  * @param fullname name of pseudonym
531  */
532 static int
533 list_pseudonym_helper (void *cls, const char *fullname)
534 {
535   struct ListPseudonymClosure *c = cls;
536   int ret;
537   GNUNET_HashCode id;
538   int32_t rating;
539   struct GNUNET_CONTAINER_MetaData *meta;
540   const char *fn;
541   char *str, *name_unique;
542
543   if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
544     return GNUNET_OK;
545   fn = &fullname[strlen (fullname) + 1 -
546                  sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
547   if (fn[-1] != DIR_SEPARATOR)
548     return GNUNET_OK;
549   ret = GNUNET_OK;
550   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
551     return GNUNET_OK;           /* invalid name */
552   str = NULL;
553   if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (c->cfg, &id, &meta, &rating,
554       &str, NULL))
555   {
556     /* ignore entry. FIXME: Why? Lack of data about a pseudonym is not a reason
557      * to ignore it... So yeah, it will have placeholders instead of name,
558      * empty metadata container and a default rank == -1, so what? We know
559      * its nsid - that's all we really need. Right? */
560     GNUNET_free (str);
561     GNUNET_CONTAINER_meta_data_destroy (meta);
562     return GNUNET_OK;
563   }
564   name_unique = GNUNET_PSEUDONYM_name_uniquify (c->cfg, &id, str, NULL);
565   if (c->iterator != NULL)
566     ret = c->iterator (c->closure, &id, str, name_unique, meta, rating);
567   GNUNET_free_non_null (str);
568   GNUNET_free_non_null (name_unique);
569   GNUNET_CONTAINER_meta_data_destroy (meta);
570   return ret;
571 }
572
573
574 /**
575  * List all available pseudonyms.
576  *
577  * @param cfg overall configuration
578  * @param iterator function to call for each pseudonym
579  * @param closure closure for iterator
580  * @return number of pseudonyms found
581  */
582 int
583 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
584                            GNUNET_PSEUDONYM_Iterator iterator, void *closure)
585 {
586   struct ListPseudonymClosure cls;
587   char *fn;
588   int ret;
589
590   cls.iterator = iterator;
591   cls.closure = closure;
592   cls.cfg = cfg;
593   fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
594   GNUNET_assert (fn != NULL);
595   GNUNET_DISK_directory_create (fn);
596   ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
597   GNUNET_free (fn);
598   return ret;
599 }
600
601
602 /**
603  * Change the ranking of a pseudonym.
604  *
605  * @param cfg overall configuration
606  * @param nsid id of the pseudonym
607  * @param delta by how much should the rating be
608  *  changed?
609  * @return new rating of the pseudonym
610  */
611 int
612 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
613                        const GNUNET_HashCode * nsid, int delta)
614 {
615   struct GNUNET_CONTAINER_MetaData *meta;
616   int ret;
617   int32_t ranking;
618   char *name;
619
620   name = NULL;
621   ret = read_info (cfg, nsid, &meta, &ranking, &name);
622   if (ret == GNUNET_SYSERR)
623   {
624     ranking = 0;
625     meta = GNUNET_CONTAINER_meta_data_create ();
626   }
627   ranking += delta;
628   write_pseudonym_info (cfg, nsid, meta, ranking, name);
629   GNUNET_CONTAINER_meta_data_destroy (meta);
630   GNUNET_free_non_null (name);
631   return ranking;
632 }
633
634
635 /**
636  * Set the pseudonym metadata, rank and name.
637  *
638  * @param cfg overall configuration
639  * @param nsid id of the pseudonym
640  * @param name name to set. Must be the non-unique version of it.
641  *        May be NULL, in which case it erases pseudonym's name!
642  * @param md metadata to set
643  *        May be NULL, in which case it erases pseudonym's metadata!
644  * @param rank rank to assign
645  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
646  */
647 int
648 GNUNET_PSEUDONYM_set_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
649     const GNUNET_HashCode * nsid, const char *name,
650     const struct GNUNET_CONTAINER_MetaData *md, int rank)
651 {
652   GNUNET_assert (cfg != NULL);
653   GNUNET_assert (nsid != NULL);
654
655   write_pseudonym_info (cfg, nsid, md, rank, name);
656   return GNUNET_OK;
657 }
658
659
660 /**
661  * Add a pseudonym to the set of known pseudonyms.
662  * For all pseudonym advertisements that we discover
663  * FS should automatically call this function.
664  *
665  * @param cfg overall configuration
666  * @param id the pseudonym identifier
667  * @param meta metadata for the pseudonym
668  */
669 void
670 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
671                       const GNUNET_HashCode * id,
672                       const struct GNUNET_CONTAINER_MetaData *meta)
673 {
674   char *name;
675   int32_t ranking;
676   struct GNUNET_CONTAINER_MetaData *old;
677   char *fn;
678   struct stat sbuf;
679
680   ranking = 0;
681   fn = get_data_filename (cfg, PS_METADATA_DIR, id);
682   GNUNET_assert (fn != NULL);
683
684   if ((0 == STAT (fn, &sbuf)) &&
685       (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
686   {
687     GNUNET_CONTAINER_meta_data_merge (old, meta);
688     write_pseudonym_info (cfg, id, old, ranking, name);
689     GNUNET_CONTAINER_meta_data_destroy (old);
690     GNUNET_free_non_null (name);
691   }
692   else
693   {
694     write_pseudonym_info (cfg, id, meta, ranking, NULL);
695   }
696   GNUNET_free (fn);
697   internal_notify (id, meta, ranking);
698 }
699
700
701 /* end of pseudonym.c */