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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file hello/gnunet-hello.c
20 * @brief change HELLO files to never expire
21 * @author Christian Grothoff
24 #include "gnunet_protocols.h"
25 #include "gnunet_hello_lib.h"
28 * Closure for #add_to_buf().
38 * Maximum number of bytes left
43 * Number of bytes added so far.
48 static int address_count;
52 * Add the given address with infinit expiration to the buffer.
55 * @param address address to add
56 * @param expiration old expiration
57 * @return #GNUNET_OK keep iterating
60 add_to_buf (void *cls,
61 const struct GNUNET_HELLO_Address *address,
62 struct GNUNET_TIME_Absolute expiration)
64 struct AddContext *ac = cls;
67 ret = GNUNET_HELLO_add_address (address,
68 GNUNET_TIME_UNIT_FOREVER_ABS,
80 * Add addresses from the address list to the HELLO.
82 * @param cls the HELLO with the addresses to add
83 * @param max maximum space available
84 * @param buf where to add the addresses
85 * @return number of bytes added, 0 to terminate
88 add_from_hello (void *cls,
92 struct GNUNET_HELLO_Message **orig = cls;
96 return GNUNET_SYSERR; /* already done */
100 GNUNET_assert (NULL ==
101 GNUNET_HELLO_iterate_addresses (*orig,
102 GNUNET_NO, &add_to_buf,
110 main (int argc, char *argv[])
112 struct GNUNET_DISK_FileHandle *fh;
113 struct GNUNET_HELLO_Message *orig;
114 struct GNUNET_HELLO_Message *result;
115 struct GNUNET_PeerIdentity pid;
119 GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
124 _("Call with name of HELLO file to modify.\n"));
127 if (GNUNET_OK != GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
130 _("Error accessing file `%s': %s\n"),
138 _("File `%s' is too big to be a HELLO\n"),
142 if (fsize < sizeof (struct GNUNET_MessageHeader))
145 _("File `%s' is too small to be a HELLO\n"),
149 fh = GNUNET_DISK_file_open (argv[1],
150 GNUNET_DISK_OPEN_READ,
151 GNUNET_DISK_PERM_USER_READ);
155 _("Error opening file `%s': %s\n"),
161 char buf[fsize] GNUNET_ALIGN;
163 GNUNET_assert (fsize ==
164 GNUNET_DISK_file_read (fh, buf, fsize));
165 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
166 orig = (struct GNUNET_HELLO_Message *) buf;
167 if ( (fsize < GNUNET_HELLO_size (orig)) ||
168 (GNUNET_OK != GNUNET_HELLO_get_id (orig,
172 _("Did not find well-formed HELLO in file `%s'\n"),
179 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid.public_key);
181 "Processing HELLO for peer `%s'\n",
185 result = GNUNET_HELLO_create (&pid.public_key,
188 GNUNET_HELLO_is_friend_only (orig));
189 GNUNET_assert (NULL != result);
190 fh = GNUNET_DISK_file_open (argv[1],
191 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE,
192 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
196 _("Error opening file `%s': %s\n"),
199 GNUNET_free (result);
202 fsize = GNUNET_HELLO_size (result);
203 if (fsize != GNUNET_DISK_file_write (fh,
208 _("Error writing HELLO to file `%s': %s\n"),
211 (void) GNUNET_DISK_file_close (fh);
214 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
217 _("Modified %u addresses, wrote %u bytes\n"),
219 (unsigned int) fsize);
223 /* end of gnunet-hello.c */