aboutsummaryrefslogtreecommitdiff
path: root/src/hello/gnunet-hello.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hello/gnunet-hello.c')
-rw-r--r--src/hello/gnunet-hello.c186
1 files changed, 93 insertions, 93 deletions
diff --git a/src/hello/gnunet-hello.c b/src/hello/gnunet-hello.c
index 612b5110a..1b572b904 100644
--- a/src/hello/gnunet-hello.c
+++ b/src/hello/gnunet-hello.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file hello/gnunet-hello.c 21 * @file hello/gnunet-hello.c
22 * @brief change HELLO files to never expire 22 * @brief change HELLO files to never expire
@@ -29,8 +29,7 @@
29/** 29/**
30 * Closure for #add_to_buf(). 30 * Closure for #add_to_buf().
31 */ 31 */
32struct AddContext 32struct AddContext {
33{
34 /** 33 /**
35 * Where to add. 34 * Where to add.
36 */ 35 */
@@ -59,17 +58,17 @@ static int address_count;
59 * @return #GNUNET_OK keep iterating 58 * @return #GNUNET_OK keep iterating
60 */ 59 */
61static int 60static int
62add_to_buf (void *cls, 61add_to_buf(void *cls,
63 const struct GNUNET_HELLO_Address *address, 62 const struct GNUNET_HELLO_Address *address,
64 struct GNUNET_TIME_Absolute expiration) 63 struct GNUNET_TIME_Absolute expiration)
65{ 64{
66 struct AddContext *ac = cls; 65 struct AddContext *ac = cls;
67 size_t ret; 66 size_t ret;
68 67
69 ret = GNUNET_HELLO_add_address (address, 68 ret = GNUNET_HELLO_add_address(address,
70 GNUNET_TIME_UNIT_FOREVER_ABS, 69 GNUNET_TIME_UNIT_FOREVER_ABS,
71 ac->buf, 70 ac->buf,
72 ac->max); 71 ac->max);
73 ac->buf += ret; 72 ac->buf += ret;
74 ac->max -= ret; 73 ac->max -= ret;
75 ac->ret += ret; 74 ac->ret += ret;
@@ -87,7 +86,7 @@ add_to_buf (void *cls,
87 * @return number of bytes added, 0 to terminate 86 * @return number of bytes added, 0 to terminate
88 */ 87 */
89static ssize_t 88static ssize_t
90add_from_hello (void *cls, size_t max, void *buf) 89add_from_hello(void *cls, size_t max, void *buf)
91{ 90{
92 struct GNUNET_HELLO_Message **orig = cls; 91 struct GNUNET_HELLO_Message **orig = cls;
93 struct AddContext ac; 92 struct AddContext ac;
@@ -97,116 +96,117 @@ add_from_hello (void *cls, size_t max, void *buf)
97 ac.buf = buf; 96 ac.buf = buf;
98 ac.max = max; 97 ac.max = max;
99 ac.ret = 0; 98 ac.ret = 0;
100 GNUNET_assert ( 99 GNUNET_assert(
101 NULL == 100 NULL ==
102 GNUNET_HELLO_iterate_addresses (*orig, GNUNET_NO, &add_to_buf, &ac)); 101 GNUNET_HELLO_iterate_addresses(*orig, GNUNET_NO, &add_to_buf, &ac));
103 *orig = NULL; 102 *orig = NULL;
104 return ac.ret; 103 return ac.ret;
105} 104}
106 105
107 106
108int 107int
109main (int argc, char *argv[]) 108main(int argc, char *argv[])
110{ 109{
111 struct GNUNET_DISK_FileHandle *fh; 110 struct GNUNET_DISK_FileHandle *fh;
112 struct GNUNET_HELLO_Message *orig; 111 struct GNUNET_HELLO_Message *orig;
113 struct GNUNET_HELLO_Message *result; 112 struct GNUNET_HELLO_Message *result;
114 struct GNUNET_PeerIdentity pid; 113 struct GNUNET_PeerIdentity pid;
115 uint64_t fsize; 114 uint64_t fsize;
115
116 address_count = 0; 116 address_count = 0;
117 117
118 GNUNET_log_setup ("gnunet-hello", "INFO", NULL); 118 GNUNET_log_setup("gnunet-hello", "INFO", NULL);
119 if (argc != 2) 119 if (argc != 2)
120 { 120 {
121 fprintf (stderr, "%s", _ ("Call with name of HELLO file to modify.\n")); 121 fprintf(stderr, "%s", _("Call with name of HELLO file to modify.\n"));
122 return 1; 122 return 1;
123 } 123 }
124 if (GNUNET_OK != 124 if (GNUNET_OK !=
125 GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES)) 125 GNUNET_DISK_file_size(argv[1], &fsize, GNUNET_YES, GNUNET_YES))
126 {
127 fprintf (stderr,
128 _ ("Error accessing file `%s': %s\n"),
129 argv[1],
130 strerror (errno));
131 return 1;
132 }
133 if (fsize > 65536)
134 {
135 fprintf (stderr, _ ("File `%s' is too big to be a HELLO\n"), argv[1]);
136 return 1;
137 }
138 if (fsize < sizeof (struct GNUNET_MessageHeader))
139 {
140 fprintf (stderr, _ ("File `%s' is too small to be a HELLO\n"), argv[1]);
141 return 1;
142 }
143 fh = GNUNET_DISK_file_open (argv[1],
144 GNUNET_DISK_OPEN_READ,
145 GNUNET_DISK_PERM_USER_READ);
146 if (NULL == fh)
147 {
148 fprintf (stderr,
149 _ ("Error opening file `%s': %s\n"),
150 argv[1],
151 strerror (errno));
152 return 1;
153 }
154 {
155 char buf[fsize] GNUNET_ALIGN;
156
157 GNUNET_assert (fsize == GNUNET_DISK_file_read (fh, buf, fsize));
158 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
159 orig = (struct GNUNET_HELLO_Message *) buf;
160 if ((fsize < GNUNET_HELLO_size (orig)) ||
161 (GNUNET_OK != GNUNET_HELLO_get_id (orig, &pid)))
162 { 126 {
163 fprintf (stderr, 127 fprintf(stderr,
164 _ ("Did not find well-formed HELLO in file `%s'\n"), 128 _("Error accessing file `%s': %s\n"),
165 argv[1]); 129 argv[1],
130 strerror(errno));
166 return 1; 131 return 1;
167 } 132 }
133 if (fsize > 65536)
168 { 134 {
169 char *pids; 135 fprintf(stderr, _("File `%s' is too big to be a HELLO\n"), argv[1]);
170 136 return 1;
171 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid.public_key);
172 fprintf (stdout, "Processing HELLO for peer `%s'\n", pids);
173 GNUNET_free (pids);
174 } 137 }
175 result = GNUNET_HELLO_create (&pid.public_key, 138 if (fsize < sizeof(struct GNUNET_MessageHeader))
176 &add_from_hello,
177 &orig,
178 GNUNET_HELLO_is_friend_only (orig));
179 GNUNET_assert (NULL != result);
180 fh =
181 GNUNET_DISK_file_open (argv[1],
182 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE,
183 GNUNET_DISK_PERM_USER_READ |
184 GNUNET_DISK_PERM_USER_WRITE);
185 if (NULL == fh)
186 { 139 {
187 fprintf (stderr, 140 fprintf(stderr, _("File `%s' is too small to be a HELLO\n"), argv[1]);
188 _ ("Error opening file `%s': %s\n"),
189 argv[1],
190 strerror (errno));
191 GNUNET_free (result);
192 return 1; 141 return 1;
193 } 142 }
194 fsize = GNUNET_HELLO_size (result); 143 fh = GNUNET_DISK_file_open(argv[1],
195 if (fsize != GNUNET_DISK_file_write (fh, result, fsize)) 144 GNUNET_DISK_OPEN_READ,
145 GNUNET_DISK_PERM_USER_READ);
146 if (NULL == fh)
196 { 147 {
197 fprintf (stderr, 148 fprintf(stderr,
198 _ ("Error writing HELLO to file `%s': %s\n"), 149 _("Error opening file `%s': %s\n"),
199 argv[1], 150 argv[1],
200 strerror (errno)); 151 strerror(errno));
201 (void) GNUNET_DISK_file_close (fh);
202 return 1; 152 return 1;
203 } 153 }
204 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh)); 154 {
155 char buf[fsize] GNUNET_ALIGN;
156
157 GNUNET_assert(fsize == GNUNET_DISK_file_read(fh, buf, fsize));
158 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
159 orig = (struct GNUNET_HELLO_Message *)buf;
160 if ((fsize < GNUNET_HELLO_size(orig)) ||
161 (GNUNET_OK != GNUNET_HELLO_get_id(orig, &pid)))
162 {
163 fprintf(stderr,
164 _("Did not find well-formed HELLO in file `%s'\n"),
165 argv[1]);
166 return 1;
167 }
168 {
169 char *pids;
170
171 pids = GNUNET_CRYPTO_eddsa_public_key_to_string(&pid.public_key);
172 fprintf(stdout, "Processing HELLO for peer `%s'\n", pids);
173 GNUNET_free(pids);
174 }
175 result = GNUNET_HELLO_create(&pid.public_key,
176 &add_from_hello,
177 &orig,
178 GNUNET_HELLO_is_friend_only(orig));
179 GNUNET_assert(NULL != result);
180 fh =
181 GNUNET_DISK_file_open(argv[1],
182 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE,
183 GNUNET_DISK_PERM_USER_READ |
184 GNUNET_DISK_PERM_USER_WRITE);
185 if (NULL == fh)
186 {
187 fprintf(stderr,
188 _("Error opening file `%s': %s\n"),
189 argv[1],
190 strerror(errno));
191 GNUNET_free(result);
192 return 1;
193 }
194 fsize = GNUNET_HELLO_size(result);
195 if (fsize != GNUNET_DISK_file_write(fh, result, fsize))
196 {
197 fprintf(stderr,
198 _("Error writing HELLO to file `%s': %s\n"),
199 argv[1],
200 strerror(errno));
201 (void)GNUNET_DISK_file_close(fh);
202 return 1;
203 }
204 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
205 } 205 }
206 fprintf (stderr, 206 fprintf(stderr,
207 _ ("Modified %u addresses, wrote %u bytes\n"), 207 _("Modified %u addresses, wrote %u bytes\n"),
208 address_count, 208 address_count,
209 (unsigned int) fsize); 209 (unsigned int)fsize);
210 return 0; 210 return 0;
211} 211}
212 212