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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file hello/gnunet-hello.c
22 * @brief change HELLO files to never expire
23 * @author Christian Grothoff
26 #include "gnunet_protocols.h"
27 #include "gnunet_hello_lib.h"
30 * Closure for #add_to_buf().
40 * Maximum number of bytes left
45 * Number of bytes added so far.
50 static int address_count;
54 * Add the given address with infinit expiration to the buffer.
57 * @param address address to add
58 * @param expiration old expiration
59 * @return #GNUNET_OK keep iterating
62 add_to_buf (void *cls,
63 const struct GNUNET_HELLO_Address *address,
64 struct GNUNET_TIME_Absolute expiration)
66 struct AddContext *ac = cls;
69 ret = GNUNET_HELLO_add_address (address,
70 GNUNET_TIME_UNIT_FOREVER_ABS,
82 * Add addresses from the address list to the HELLO.
84 * @param cls the HELLO with the addresses to add
85 * @param max maximum space available
86 * @param buf where to add the addresses
87 * @return number of bytes added, 0 to terminate
90 add_from_hello (void *cls, size_t max, void *buf)
92 struct GNUNET_HELLO_Message **orig = cls;
96 return GNUNET_SYSERR; /* already done */
102 GNUNET_HELLO_iterate_addresses (*orig, GNUNET_NO, &add_to_buf, &ac));
109 main (int argc, char *argv[])
111 struct GNUNET_DISK_FileHandle *fh;
112 struct GNUNET_HELLO_Message *orig;
113 struct GNUNET_HELLO_Message *result;
114 struct GNUNET_PeerIdentity pid;
119 GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
122 fprintf (stderr, "%s", _ ("Call with name of HELLO file to modify.\n"));
126 GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
129 _ ("Error accessing file `%s': %s\n"),
136 fprintf (stderr, _ ("File `%s' is too big to be a HELLO\n"), argv[1]);
139 if (fsize < sizeof(struct GNUNET_MessageHeader))
141 fprintf (stderr, _ ("File `%s' is too small to be a HELLO\n"), argv[1]);
144 fh = GNUNET_DISK_file_open (argv[1],
145 GNUNET_DISK_OPEN_READ,
146 GNUNET_DISK_PERM_USER_READ);
150 _ ("Error opening file `%s': %s\n"),
156 char buf[fsize] GNUNET_ALIGN;
158 GNUNET_assert (fsize == GNUNET_DISK_file_read (fh, buf, fsize));
159 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
160 orig = (struct GNUNET_HELLO_Message *) buf;
161 if ((fsize < GNUNET_HELLO_size (orig)) ||
162 (GNUNET_OK != GNUNET_HELLO_get_id (orig, &pid)))
165 _ ("Did not find well-formed HELLO in file `%s'\n"),
172 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid.public_key);
173 fprintf (stdout, "Processing HELLO for peer `%s'\n", pids);
176 result = GNUNET_HELLO_create (&pid.public_key,
179 GNUNET_HELLO_is_friend_only (orig));
180 GNUNET_assert (NULL != result);
182 GNUNET_DISK_file_open (argv[1],
183 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE,
184 GNUNET_DISK_PERM_USER_READ
185 | GNUNET_DISK_PERM_USER_WRITE);
189 _ ("Error opening file `%s': %s\n"),
192 GNUNET_free (result);
195 fsize = GNUNET_HELLO_size (result);
196 if (fsize != GNUNET_DISK_file_write (fh, result, fsize))
199 _ ("Error writing HELLO to file `%s': %s\n"),
202 (void) GNUNET_DISK_file_close (fh);
205 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
208 _ ("Modified %u addresses, wrote %u bytes\n"),
210 (unsigned int) fsize);
215 /* end of gnunet-hello.c */