+/**
+ * Method to load persistent hostlist file during hostlist client startup
+ */
+static void
+load_hostlist_file ()
+{
+ char *filename;
+ char *uri;
+ char *emsg;
+ struct Hostlist * hostlist;
+ uri = NULL;
+ uint32_t times_used;
+ uint32_t hellos_returned;
+ uint64_t quality;
+ uint64_t last_used;
+ uint64_t created;
+ uint32_t counter;
+
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_filename (cfg,
+ "HOSTLIST",
+ "HOSTLISTFILE",
+ &filename))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
+ "HOSTLISTFILE", "HOSTLIST");
+ return;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ _("Loading saved hostlist entries from file `%s' \n"), filename);
+ if ( GNUNET_NO == GNUNET_DISK_file_test (filename) )
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ _("Hostlist file `%s' is not existing\n"), filename);
+ GNUNET_free ( filename );
+ return;
+ }
+
+ struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
+ if (NULL == rh)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ _("Could not open file `%s' for reading to load hostlists: %s\n"),
+ filename,
+ STRERROR (errno));
+ GNUNET_free (filename);
+ return;
+ }
+
+ counter = 0;
+ while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
+ (NULL != uri) &&
+ (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
+ (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
+ (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
+ (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
+ (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
+ {
+ hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
+ hostlist->hello_count = hellos_returned;
+ hostlist->hostlist_uri = (const char *) &hostlist[1];
+ memcpy (&hostlist[1], uri, strlen(uri)+1);
+ hostlist->quality = quality;
+ hostlist->time_creation.abs_value = created;
+ hostlist->time_last_usage.abs_value = last_used;
+ GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
+ linked_list_size++;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
+ GNUNET_free (uri);
+ uri = NULL;
+ counter++;
+ if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ _("%u hostlist URIs loaded from file\n"), counter);
+ GNUNET_STATISTICS_set (stats,
+ gettext_noop("# hostlist URIs read from file"),
+ counter,
+ GNUNET_YES);
+ GNUNET_STATISTICS_set (stats,
+ gettext_noop("# advertised hostlist URIs"),
+ linked_list_size,
+ GNUNET_NO);
+
+ GNUNET_free_non_null (uri);
+ emsg = NULL;
+ GNUNET_BIO_read_close (rh, &emsg);
+ if (emsg != NULL)
+ GNUNET_free (emsg);
+ GNUNET_free (filename);
+}
+
+
+/**
+ * Method to save persistent hostlist file during hostlist client shutdown
+ * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
+ */
+static void save_hostlist_file ( int shutdown )
+{
+ char *filename;
+ struct Hostlist *pos;
+ struct GNUNET_BIO_WriteHandle * wh;
+ int ok;
+ uint32_t counter;
+
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_filename (cfg,
+ "HOSTLIST",
+ "HOSTLISTFILE",
+ &filename))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
+ "HOSTLISTFILE", "HOSTLIST");
+ return;
+ }
+ if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
+ {
+ GNUNET_free (filename);
+ return;
+ }
+ wh = GNUNET_BIO_write_open (filename);
+ if ( NULL == wh)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Could not open file `%s' for writing to save hostlists: %s\n"),
+ filename,
+ STRERROR (errno));
+ GNUNET_free (filename);
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ _("Writing %u hostlist URIs to `%s'\n" ),
+ linked_list_size, filename);
+ /* add code to write hostlists to file using bio */
+ ok = GNUNET_YES;
+ counter = 0;
+ while (NULL != (pos = linked_list_head))
+ {
+ if ( GNUNET_YES == shutdown)
+ {
+ GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
+ linked_list_size--;
+ }
+ if (GNUNET_YES == ok)
+ {
+ if ( (GNUNET_OK !=
+ GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
+ (GNUNET_OK !=
+ GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
+ (GNUNET_OK !=
+ GNUNET_BIO_write_int64 (wh, pos->quality)) ||
+ (GNUNET_OK !=
+ GNUNET_BIO_write_int64 (wh, pos->time_last_usage.abs_value)) ||
+ (GNUNET_OK !=
+ GNUNET_BIO_write_int64 (wh, pos->time_creation.abs_value)) ||
+ (GNUNET_OK !=
+ GNUNET_BIO_write_int32 (wh, pos->hello_count)))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Error writing hostlist URIs to file `%s'\n"),
+ filename);
+ ok = GNUNET_NO;
+ }
+ }
+
+ if ( GNUNET_YES == shutdown)
+ GNUNET_free (pos);
+ counter ++;
+ if ( counter >= MAX_NUMBER_HOSTLISTS) break;
+ }
+ GNUNET_STATISTICS_set (stats,
+ gettext_noop("# hostlist URIs written to file"),
+ counter,
+ GNUNET_YES);
+
+ if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Error writing hostlist URIs to file `%s'\n"),
+ filename);
+ GNUNET_free (filename);
+}