add attestation API
[oweals/gnunet.git] / src / hello / gnunet-hello.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2012 GNUnet e.V.
4
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.
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      Affero General Public License for more details.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file hello/gnunet-hello.c
22  * @brief change HELLO files to never expire
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_hello_lib.h"
28
29 /**
30  * Closure for #add_to_buf().
31  */
32 struct AddContext
33 {
34   /**
35    * Where to add.
36    */
37   char *buf;
38
39   /**
40    * Maximum number of bytes left
41    */
42   size_t max;
43
44   /**
45    * Number of bytes added so far.
46    */
47   size_t ret;
48 };
49
50 static int address_count;
51
52
53 /**
54  * Add the given address with infinit expiration to the buffer.
55  *
56  * @param cls closure
57  * @param address address to add
58  * @param expiration old expiration
59  * @return #GNUNET_OK keep iterating
60  */
61 static int
62 add_to_buf (void *cls,
63             const struct GNUNET_HELLO_Address *address,
64             struct GNUNET_TIME_Absolute expiration)
65 {
66   struct AddContext *ac = cls;
67   size_t ret;
68
69   ret = GNUNET_HELLO_add_address (address,
70                                   GNUNET_TIME_UNIT_FOREVER_ABS,
71                                   ac->buf,
72                                   ac->max);
73   ac->buf += ret;
74   ac->max -= ret;
75   ac->ret += ret;
76   address_count++;
77   return GNUNET_OK;
78 }
79
80
81 /**
82  * Add addresses from the address list to the HELLO.
83  *
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
88  */
89 static ssize_t
90 add_from_hello (void *cls, size_t max, void *buf)
91 {
92   struct GNUNET_HELLO_Message **orig = cls;
93   struct AddContext ac;
94
95   if (NULL == *orig)
96     return GNUNET_SYSERR; /* already done */
97   ac.buf = buf;
98   ac.max = max;
99   ac.ret = 0;
100   GNUNET_assert (
101     NULL ==
102     GNUNET_HELLO_iterate_addresses (*orig, GNUNET_NO, &add_to_buf, &ac));
103   *orig = NULL;
104   return ac.ret;
105 }
106
107
108 int
109 main (int argc, char *argv[])
110 {
111   struct GNUNET_DISK_FileHandle *fh;
112   struct GNUNET_HELLO_Message *orig;
113   struct GNUNET_HELLO_Message *result;
114   struct GNUNET_PeerIdentity pid;
115   uint64_t fsize;
116
117   address_count = 0;
118
119   GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
120   if (argc != 2)
121   {
122     fprintf (stderr, "%s", _ ("Call with name of HELLO file to modify.\n"));
123     return 1;
124   }
125   if (GNUNET_OK !=
126       GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
127   {
128     fprintf (stderr,
129              _ ("Error accessing file `%s': %s\n"),
130              argv[1],
131              strerror (errno));
132     return 1;
133   }
134   if (fsize > 65536)
135   {
136     fprintf (stderr, _ ("File `%s' is too big to be a HELLO\n"), argv[1]);
137     return 1;
138   }
139   if (fsize < sizeof(struct GNUNET_MessageHeader))
140   {
141     fprintf (stderr, _ ("File `%s' is too small to be a HELLO\n"), argv[1]);
142     return 1;
143   }
144   fh = GNUNET_DISK_file_open (argv[1],
145                               GNUNET_DISK_OPEN_READ,
146                               GNUNET_DISK_PERM_USER_READ);
147   if (NULL == fh)
148   {
149     fprintf (stderr,
150              _ ("Error opening file `%s': %s\n"),
151              argv[1],
152              strerror (errno));
153     return 1;
154   }
155   {
156     char buf[fsize] GNUNET_ALIGN;
157
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)))
163     {
164       fprintf (stderr,
165                _ ("Did not find well-formed HELLO in file `%s'\n"),
166                argv[1]);
167       return 1;
168     }
169     {
170       char *pids;
171
172       pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid.public_key);
173       fprintf (stdout, "Processing HELLO for peer `%s'\n", pids);
174       GNUNET_free (pids);
175     }
176     result = GNUNET_HELLO_create (&pid.public_key,
177                                   &add_from_hello,
178                                   &orig,
179                                   GNUNET_HELLO_is_friend_only (orig));
180     GNUNET_assert (NULL != result);
181     fh =
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);
186     if (NULL == fh)
187     {
188       fprintf (stderr,
189                _ ("Error opening file `%s': %s\n"),
190                argv[1],
191                strerror (errno));
192       GNUNET_free (result);
193       return 1;
194     }
195     fsize = GNUNET_HELLO_size (result);
196     if (fsize != GNUNET_DISK_file_write (fh, result, fsize))
197     {
198       fprintf (stderr,
199                _ ("Error writing HELLO to file `%s': %s\n"),
200                argv[1],
201                strerror (errno));
202       (void) GNUNET_DISK_file_close (fh);
203       return 1;
204     }
205     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
206   }
207   fprintf (stderr,
208            _ ("Modified %u addresses, wrote %u bytes\n"),
209            address_count,
210            (unsigned int) fsize);
211   return 0;
212 }
213
214
215 /* end of gnunet-hello.c */