2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file hello/gnunet-hello.c
17 * @brief change HELLO files to never expire
18 * @author Christian Grothoff
21 #include "gnunet_protocols.h"
22 #include "gnunet_hello_lib.h"
25 * Closure for #add_to_buf().
35 * Maximum number of bytes left
40 * Number of bytes added so far.
45 static int address_count;
49 * Add the given address with infinit expiration to the buffer.
52 * @param address address to add
53 * @param expiration old expiration
54 * @return #GNUNET_OK keep iterating
57 add_to_buf (void *cls,
58 const struct GNUNET_HELLO_Address *address,
59 struct GNUNET_TIME_Absolute expiration)
61 struct AddContext *ac = cls;
64 ret = GNUNET_HELLO_add_address (address,
65 GNUNET_TIME_UNIT_FOREVER_ABS,
77 * Add addresses from the address list to the HELLO.
79 * @param cls the HELLO with the addresses to add
80 * @param max maximum space available
81 * @param buf where to add the addresses
82 * @return number of bytes added, 0 to terminate
85 add_from_hello (void *cls,
89 struct GNUNET_HELLO_Message **orig = cls;
93 return GNUNET_SYSERR; /* already done */
97 GNUNET_assert (NULL ==
98 GNUNET_HELLO_iterate_addresses (*orig,
99 GNUNET_NO, &add_to_buf,
107 main (int argc, char *argv[])
109 struct GNUNET_DISK_FileHandle *fh;
110 struct GNUNET_HELLO_Message *orig;
111 struct GNUNET_HELLO_Message *result;
112 struct GNUNET_PeerIdentity pid;
116 GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
121 _("Call with name of HELLO file to modify.\n"));
124 if (GNUNET_OK != GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
127 _("Error accessing file `%s': %s\n"),
135 _("File `%s' is too big to be a HELLO\n"),
139 if (fsize < sizeof (struct GNUNET_MessageHeader))
142 _("File `%s' is too small to be a HELLO\n"),
146 fh = GNUNET_DISK_file_open (argv[1],
147 GNUNET_DISK_OPEN_READ,
148 GNUNET_DISK_PERM_USER_READ);
152 _("Error opening file `%s': %s\n"),
158 char buf[fsize] GNUNET_ALIGN;
160 GNUNET_assert (fsize ==
161 GNUNET_DISK_file_read (fh, buf, fsize));
162 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
163 orig = (struct GNUNET_HELLO_Message *) buf;
164 if ( (fsize < GNUNET_HELLO_size (orig)) ||
165 (GNUNET_OK != GNUNET_HELLO_get_id (orig,
169 _("Did not find well-formed HELLO in file `%s'\n"),
176 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid.public_key);
178 "Processing HELLO for peer `%s'\n",
182 result = GNUNET_HELLO_create (&pid.public_key,
185 GNUNET_HELLO_is_friend_only (orig));
186 GNUNET_assert (NULL != result);
187 fh = GNUNET_DISK_file_open (argv[1],
188 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE,
189 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
193 _("Error opening file `%s': %s\n"),
196 GNUNET_free (result);
199 fsize = GNUNET_HELLO_size (result);
200 if (fsize != GNUNET_DISK_file_write (fh,
205 _("Error writing HELLO to file `%s': %s\n"),
208 (void) GNUNET_DISK_file_close (fh);
211 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
214 _("Modified %u addresses, wrote %u bytes\n"),
216 (unsigned int) fsize);
220 /* end of gnunet-hello.c */