diff options
Diffstat (limited to 'src/peerinfo/gnunet-service-peerinfo.c')
-rw-r--r-- | src/peerinfo/gnunet-service-peerinfo.c | 1301 |
1 files changed, 648 insertions, 653 deletions
diff --git a/src/peerinfo/gnunet-service-peerinfo.c b/src/peerinfo/gnunet-service-peerinfo.c index a884c5b76..25a3fabd7 100644 --- a/src/peerinfo/gnunet-service-peerinfo.c +++ b/src/peerinfo/gnunet-service-peerinfo.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 | /** | 21 | /** |
22 | * @file peerinfo/gnunet-service-peerinfo.c | 22 | * @file peerinfo/gnunet-service-peerinfo.c |
@@ -39,21 +39,19 @@ | |||
39 | * How often do we scan the HOST_DIR for new entries? | 39 | * How often do we scan the HOST_DIR for new entries? |
40 | */ | 40 | */ |
41 | #define DATA_HOST_FREQ \ | 41 | #define DATA_HOST_FREQ \ |
42 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) | 42 | GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15) |
43 | 43 | ||
44 | /** | 44 | /** |
45 | * How often do we discard old entries in data/hosts/? | 45 | * How often do we discard old entries in data/hosts/? |
46 | */ | 46 | */ |
47 | #define DATA_HOST_CLEAN_FREQ \ | 47 | #define DATA_HOST_CLEAN_FREQ \ |
48 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60) | 48 | GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60) |
49 | 49 | ||
50 | 50 | ||
51 | /** | 51 | /** |
52 | * In-memory cache of known hosts. | 52 | * In-memory cache of known hosts. |
53 | */ | 53 | */ |
54 | struct HostEntry | 54 | struct HostEntry { |
55 | { | ||
56 | |||
57 | /** | 55 | /** |
58 | * Identity of the peer. | 56 | * Identity of the peer. |
59 | */ | 57 | */ |
@@ -74,8 +72,7 @@ struct HostEntry | |||
74 | /** | 72 | /** |
75 | * Result of reading a file | 73 | * Result of reading a file |
76 | */ | 74 | */ |
77 | struct ReadHostFileContext | 75 | struct ReadHostFileContext { |
78 | { | ||
79 | /** | 76 | /** |
80 | * Hello for the peer (can be NULL) | 77 | * Hello for the peer (can be NULL) |
81 | */ | 78 | */ |
@@ -135,7 +132,7 @@ static struct GNUNET_SCHEDULER_Task *cron_scan; | |||
135 | * @return generated notification message | 132 | * @return generated notification message |
136 | */ | 133 | */ |
137 | static struct InfoMessage * | 134 | static struct InfoMessage * |
138 | make_info_message (const struct HostEntry *he, int include_friend_only) | 135 | make_info_message(const struct HostEntry *he, int include_friend_only) |
139 | { | 136 | { |
140 | struct InfoMessage *im; | 137 | struct InfoMessage *im; |
141 | struct GNUNET_HELLO_Message *src; | 138 | struct GNUNET_HELLO_Message *src; |
@@ -145,12 +142,12 @@ make_info_message (const struct HostEntry *he, int include_friend_only) | |||
145 | src = he->friend_only_hello; | 142 | src = he->friend_only_hello; |
146 | else | 143 | else |
147 | src = he->hello; | 144 | src = he->hello; |
148 | hs = (NULL == src) ? 0 : GNUNET_HELLO_size (src); | 145 | hs = (NULL == src) ? 0 : GNUNET_HELLO_size(src); |
149 | im = GNUNET_malloc (sizeof (struct InfoMessage) + hs); | 146 | im = GNUNET_malloc(sizeof(struct InfoMessage) + hs); |
150 | im->header.size = htons (hs + sizeof (struct InfoMessage)); | 147 | im->header.size = htons(hs + sizeof(struct InfoMessage)); |
151 | im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO); | 148 | im->header.type = htons(GNUNET_MESSAGE_TYPE_PEERINFO_INFO); |
152 | im->peer = he->identity; | 149 | im->peer = he->identity; |
153 | GNUNET_memcpy (&im[1], src, hs); | 150 | GNUNET_memcpy(&im[1], src, hs); |
154 | return im; | 151 | return im; |
155 | } | 152 | } |
156 | 153 | ||
@@ -164,19 +161,19 @@ make_info_message (const struct HostEntry *he, int include_friend_only) | |||
164 | * @return #GNUNET_NO if expiration smaller than the current time | 161 | * @return #GNUNET_NO if expiration smaller than the current time |
165 | */ | 162 | */ |
166 | static int | 163 | static int |
167 | discard_expired (void *cls, | 164 | discard_expired(void *cls, |
168 | const struct GNUNET_HELLO_Address *address, | 165 | const struct GNUNET_HELLO_Address *address, |
169 | struct GNUNET_TIME_Absolute expiration) | 166 | struct GNUNET_TIME_Absolute expiration) |
170 | { | 167 | { |
171 | const struct GNUNET_TIME_Absolute *now = cls; | 168 | const struct GNUNET_TIME_Absolute *now = cls; |
172 | 169 | ||
173 | if (now->abs_value_us > expiration.abs_value_us) | 170 | if (now->abs_value_us > expiration.abs_value_us) |
174 | { | 171 | { |
175 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 172 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, |
176 | _ ("Removing expired address of transport `%s'\n"), | 173 | _("Removing expired address of transport `%s'\n"), |
177 | address->transport_name); | 174 | address->transport_name); |
178 | return GNUNET_NO; | 175 | return GNUNET_NO; |
179 | } | 176 | } |
180 | return GNUNET_OK; | 177 | return GNUNET_OK; |
181 | } | 178 | } |
182 | 179 | ||
@@ -190,14 +187,14 @@ discard_expired (void *cls, | |||
190 | * @return #GNUNET_OK (always) | 187 | * @return #GNUNET_OK (always) |
191 | */ | 188 | */ |
192 | static int | 189 | static int |
193 | count_addresses (void *cls, | 190 | count_addresses(void *cls, |
194 | const struct GNUNET_HELLO_Address *address, | 191 | const struct GNUNET_HELLO_Address *address, |
195 | struct GNUNET_TIME_Absolute expiration) | 192 | struct GNUNET_TIME_Absolute expiration) |
196 | { | 193 | { |
197 | unsigned int *cnt = cls; | 194 | unsigned int *cnt = cls; |
198 | 195 | ||
199 | (void) address; | 196 | (void)address; |
200 | (void) expiration; | 197 | (void)expiration; |
201 | (*cnt)++; | 198 | (*cnt)++; |
202 | return GNUNET_OK; | 199 | return GNUNET_OK; |
203 | } | 200 | } |
@@ -211,17 +208,17 @@ count_addresses (void *cls, | |||
211 | * @return filename of the form DIRECTORY/HOSTID | 208 | * @return filename of the form DIRECTORY/HOSTID |
212 | */ | 209 | */ |
213 | static char * | 210 | static char * |
214 | get_host_filename (const struct GNUNET_PeerIdentity *id) | 211 | get_host_filename(const struct GNUNET_PeerIdentity *id) |
215 | { | 212 | { |
216 | char *fn; | 213 | char *fn; |
217 | 214 | ||
218 | if (NULL == networkIdDirectory) | 215 | if (NULL == networkIdDirectory) |
219 | return NULL; | 216 | return NULL; |
220 | GNUNET_asprintf (&fn, | 217 | GNUNET_asprintf(&fn, |
221 | "%s%s%s", | 218 | "%s%s%s", |
222 | networkIdDirectory, | 219 | networkIdDirectory, |
223 | DIR_SEPARATOR_STR, | 220 | DIR_SEPARATOR_STR, |
224 | GNUNET_i2s_full (id)); | 221 | GNUNET_i2s_full(id)); |
225 | return fn; | 222 | return fn; |
226 | } | 223 | } |
227 | 224 | ||
@@ -233,24 +230,24 @@ get_host_filename (const struct GNUNET_PeerIdentity *id) | |||
233 | * @param entry entry to broadcast about | 230 | * @param entry entry to broadcast about |
234 | */ | 231 | */ |
235 | static void | 232 | static void |
236 | notify_all (struct HostEntry *entry) | 233 | notify_all(struct HostEntry *entry) |
237 | { | 234 | { |
238 | struct InfoMessage *msg_pub; | 235 | struct InfoMessage *msg_pub; |
239 | struct InfoMessage *msg_friend; | 236 | struct InfoMessage *msg_friend; |
240 | 237 | ||
241 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 238 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
242 | "Notifying all clients about peer `%s'\n", | 239 | "Notifying all clients about peer `%s'\n", |
243 | GNUNET_i2s (&entry->identity)); | 240 | GNUNET_i2s(&entry->identity)); |
244 | msg_pub = make_info_message (entry, GNUNET_NO); | 241 | msg_pub = make_info_message(entry, GNUNET_NO); |
245 | GNUNET_notification_context_broadcast (notify_list, | 242 | GNUNET_notification_context_broadcast(notify_list, |
246 | &msg_pub->header, | 243 | &msg_pub->header, |
247 | GNUNET_NO); | 244 | GNUNET_NO); |
248 | GNUNET_free (msg_pub); | 245 | GNUNET_free(msg_pub); |
249 | msg_friend = make_info_message (entry, GNUNET_YES); | 246 | msg_friend = make_info_message(entry, GNUNET_YES); |
250 | GNUNET_notification_context_broadcast (notify_friend_only_list, | 247 | GNUNET_notification_context_broadcast(notify_friend_only_list, |
251 | &msg_friend->header, | 248 | &msg_friend->header, |
252 | GNUNET_NO); | 249 | GNUNET_NO); |
253 | GNUNET_free (msg_friend); | 250 | GNUNET_free(msg_friend); |
254 | } | 251 | } |
255 | 252 | ||
256 | 253 | ||
@@ -261,8 +258,8 @@ notify_all (struct HostEntry *entry) | |||
261 | * @param hello the verified (!) hello message | 258 | * @param hello the verified (!) hello message |
262 | */ | 259 | */ |
263 | static void | 260 | static void |
264 | update_hello (const struct GNUNET_PeerIdentity *peer, | 261 | update_hello(const struct GNUNET_PeerIdentity *peer, |
265 | const struct GNUNET_HELLO_Message *hello); | 262 | const struct GNUNET_HELLO_Message *hello); |
266 | 263 | ||
267 | 264 | ||
268 | /** | 265 | /** |
@@ -277,9 +274,9 @@ update_hello (const struct GNUNET_PeerIdentity *peer, | |||
277 | * @param r ReadHostFileContext to store the resutl | 274 | * @param r ReadHostFileContext to store the resutl |
278 | */ | 275 | */ |
279 | static void | 276 | static void |
280 | read_host_file (const char *fn, | 277 | read_host_file(const char *fn, |
281 | int unlink_garbage, | 278 | int unlink_garbage, |
282 | struct ReadHostFileContext *r) | 279 | struct ReadHostFileContext *r) |
283 | { | 280 | { |
284 | char buffer[GNUNET_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN; | 281 | char buffer[GNUNET_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN; |
285 | ssize_t size_total; | 282 | ssize_t size_total; |
@@ -293,114 +290,114 @@ read_host_file (const char *fn, | |||
293 | r->friend_only_hello = NULL; | 290 | r->friend_only_hello = NULL; |
294 | r->hello = NULL; | 291 | r->hello = NULL; |
295 | 292 | ||
296 | if (GNUNET_YES != GNUNET_DISK_file_test (fn)) | 293 | if (GNUNET_YES != GNUNET_DISK_file_test(fn)) |
297 | return; | 294 | return; |
298 | size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer)); | 295 | size_total = GNUNET_DISK_fn_read(fn, buffer, sizeof(buffer)); |
299 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 296 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
300 | "Read %d bytes from `%s'\n", | 297 | "Read %d bytes from `%s'\n", |
301 | (int) size_total, | 298 | (int)size_total, |
302 | fn); | 299 | fn); |
303 | if ((size_total < 0) || | 300 | if ((size_total < 0) || |
304 | (((size_t) size_total) < sizeof (struct GNUNET_MessageHeader))) | 301 | (((size_t)size_total) < sizeof(struct GNUNET_MessageHeader))) |
305 | { | ||
306 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
307 | _ ("Failed to parse HELLO in file `%s': %s\n"), | ||
308 | fn, | ||
309 | "File has invalid size"); | ||
310 | if ((GNUNET_YES == unlink_garbage) && (0 != unlink (fn)) && | ||
311 | (ENOENT != errno)) | ||
312 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); | ||
313 | return; | ||
314 | } | ||
315 | |||
316 | read_pos = 0; | ||
317 | while (read_pos < (size_t) size_total) | ||
318 | { | ||
319 | hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos]; | ||
320 | size_hello = GNUNET_HELLO_size (hello); | ||
321 | if ((0 == size_hello) || (((size_t) size_total) - read_pos < size_hello)) | ||
322 | { | 302 | { |
323 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 303 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
324 | _ ("Failed to parse HELLO in file `%s'\n"), | 304 | _("Failed to parse HELLO in file `%s': %s\n"), |
325 | fn); | 305 | fn, |
326 | if (0 == read_pos) | 306 | "File has invalid size"); |
327 | { | 307 | if ((GNUNET_YES == unlink_garbage) && (0 != unlink(fn)) && |
328 | if ((GNUNET_YES == unlink_garbage) && (0 != unlink (fn)) && | 308 | (ENOENT != errno)) |
329 | (ENOENT != errno)) | 309 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "unlink", fn); |
330 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); | ||
331 | } | ||
332 | else | ||
333 | { | ||
334 | if ((GNUNET_YES == unlink_garbage) && (0 != truncate (fn, read_pos)) && | ||
335 | (ENOENT != errno)) | ||
336 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate", fn); | ||
337 | } | ||
338 | return; | 310 | return; |
339 | } | 311 | } |
340 | 312 | ||
341 | now = GNUNET_TIME_absolute_get (); | 313 | read_pos = 0; |
342 | hello_clean = GNUNET_HELLO_iterate_addresses (hello, | 314 | while (read_pos < (size_t)size_total) |
343 | GNUNET_YES, | ||
344 | &discard_expired, | ||
345 | &now); | ||
346 | if (NULL == hello_clean) | ||
347 | { | 315 | { |
348 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 316 | hello = (const struct GNUNET_HELLO_Message *)&buffer[read_pos]; |
349 | _ ("Failed to parse HELLO in file `%s'\n"), | 317 | size_hello = GNUNET_HELLO_size(hello); |
350 | fn); | 318 | if ((0 == size_hello) || (((size_t)size_total) - read_pos < size_hello)) |
351 | if ((GNUNET_YES == unlink_garbage) && (0 != unlink (fn)) && | 319 | { |
352 | (ENOENT != errno)) | 320 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
353 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); | 321 | _("Failed to parse HELLO in file `%s'\n"), |
354 | return; | 322 | fn); |
355 | } | 323 | if (0 == read_pos) |
356 | left = 0; | 324 | { |
357 | (void) GNUNET_HELLO_iterate_addresses (hello_clean, | 325 | if ((GNUNET_YES == unlink_garbage) && (0 != unlink(fn)) && |
326 | (ENOENT != errno)) | ||
327 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "unlink", fn); | ||
328 | } | ||
329 | else | ||
330 | { | ||
331 | if ((GNUNET_YES == unlink_garbage) && (0 != truncate(fn, read_pos)) && | ||
332 | (ENOENT != errno)) | ||
333 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "truncate", fn); | ||
334 | } | ||
335 | return; | ||
336 | } | ||
337 | |||
338 | now = GNUNET_TIME_absolute_get(); | ||
339 | hello_clean = GNUNET_HELLO_iterate_addresses(hello, | ||
340 | GNUNET_YES, | ||
341 | &discard_expired, | ||
342 | &now); | ||
343 | if (NULL == hello_clean) | ||
344 | { | ||
345 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
346 | _("Failed to parse HELLO in file `%s'\n"), | ||
347 | fn); | ||
348 | if ((GNUNET_YES == unlink_garbage) && (0 != unlink(fn)) && | ||
349 | (ENOENT != errno)) | ||
350 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "unlink", fn); | ||
351 | return; | ||
352 | } | ||
353 | left = 0; | ||
354 | (void)GNUNET_HELLO_iterate_addresses(hello_clean, | ||
358 | GNUNET_NO, | 355 | GNUNET_NO, |
359 | &count_addresses, | 356 | &count_addresses, |
360 | &left); | 357 | &left); |
361 | 358 | ||
362 | if (0 == left) | 359 | if (0 == left) |
363 | { | 360 | { |
364 | GNUNET_free (hello_clean); | 361 | GNUNET_free(hello_clean); |
365 | break; | 362 | break; |
366 | } | 363 | } |
367 | 364 | ||
368 | if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello_clean)) | 365 | if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean)) |
369 | { | 366 | { |
370 | if (NULL == r->hello) | 367 | if (NULL == r->hello) |
371 | r->hello = hello_clean; | 368 | r->hello = hello_clean; |
369 | else | ||
370 | { | ||
371 | GNUNET_break(0); | ||
372 | GNUNET_free(r->hello); | ||
373 | r->hello = hello_clean; | ||
374 | } | ||
375 | } | ||
372 | else | 376 | else |
373 | { | 377 | { |
374 | GNUNET_break (0); | 378 | if (NULL == r->friend_only_hello) |
375 | GNUNET_free (r->hello); | 379 | r->friend_only_hello = hello_clean; |
376 | r->hello = hello_clean; | 380 | else |
377 | } | 381 | { |
382 | GNUNET_break(0); | ||
383 | GNUNET_free(r->friend_only_hello); | ||
384 | r->friend_only_hello = hello_clean; | ||
385 | } | ||
386 | } | ||
387 | read_pos += size_hello; | ||
378 | } | 388 | } |
379 | else | ||
380 | { | ||
381 | if (NULL == r->friend_only_hello) | ||
382 | r->friend_only_hello = hello_clean; | ||
383 | else | ||
384 | { | ||
385 | GNUNET_break (0); | ||
386 | GNUNET_free (r->friend_only_hello); | ||
387 | r->friend_only_hello = hello_clean; | ||
388 | } | ||
389 | } | ||
390 | read_pos += size_hello; | ||
391 | } | ||
392 | 389 | ||
393 | if (0 == left) | 390 | if (0 == left) |
394 | { | 391 | { |
395 | /* no addresses left, remove from disk */ | 392 | /* no addresses left, remove from disk */ |
396 | if ((GNUNET_YES == unlink_garbage) && (0 != unlink (fn))) | 393 | if ((GNUNET_YES == unlink_garbage) && (0 != unlink(fn))) |
397 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); | 394 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "unlink", fn); |
398 | } | 395 | } |
399 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 396 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
400 | "Found `%s' and `%s' HELLO message in file\n", | 397 | "Found `%s' and `%s' HELLO message in file\n", |
401 | (NULL != r->hello) ? "public" : "NON-public", | 398 | (NULL != r->hello) ? "public" : "NON-public", |
402 | (NULL != r->friend_only_hello) ? "friend only" | 399 | (NULL != r->friend_only_hello) ? "friend only" |
403 | : "NO friend only"); | 400 | : "NO friend only"); |
404 | } | 401 | } |
405 | 402 | ||
406 | 403 | ||
@@ -411,44 +408,44 @@ read_host_file (const char *fn, | |||
411 | * @return the HostEntry | 408 | * @return the HostEntry |
412 | */ | 409 | */ |
413 | static struct HostEntry * | 410 | static struct HostEntry * |
414 | add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity) | 411 | add_host_to_known_hosts(const struct GNUNET_PeerIdentity *identity) |
415 | { | 412 | { |
416 | struct HostEntry *entry; | 413 | struct HostEntry *entry; |
417 | struct ReadHostFileContext r; | 414 | struct ReadHostFileContext r; |
418 | char *fn; | 415 | char *fn; |
419 | 416 | ||
420 | entry = GNUNET_CONTAINER_multipeermap_get (hostmap, identity); | 417 | entry = GNUNET_CONTAINER_multipeermap_get(hostmap, identity); |
421 | if (NULL == entry) | 418 | if (NULL == entry) |
422 | { | ||
423 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
424 | "Adding new peer `%s'\n", | ||
425 | GNUNET_i2s (identity)); | ||
426 | GNUNET_STATISTICS_update (stats, | ||
427 | gettext_noop ("# peers known"), | ||
428 | 1, | ||
429 | GNUNET_NO); | ||
430 | entry = GNUNET_new (struct HostEntry); | ||
431 | entry->identity = *identity; | ||
432 | GNUNET_assert (GNUNET_OK == | ||
433 | GNUNET_CONTAINER_multipeermap_put ( | ||
434 | hostmap, | ||
435 | &entry->identity, | ||
436 | entry, | ||
437 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
438 | notify_all (entry); | ||
439 | fn = get_host_filename (identity); | ||
440 | if (NULL != fn) | ||
441 | { | 419 | { |
442 | read_host_file (fn, GNUNET_YES, &r); | 420 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
443 | if (NULL != r.hello) | 421 | "Adding new peer `%s'\n", |
444 | update_hello (identity, r.hello); | 422 | GNUNET_i2s(identity)); |
445 | if (NULL != r.friend_only_hello) | 423 | GNUNET_STATISTICS_update(stats, |
446 | update_hello (identity, r.friend_only_hello); | 424 | gettext_noop("# peers known"), |
447 | GNUNET_free_non_null (r.hello); | 425 | 1, |
448 | GNUNET_free_non_null (r.friend_only_hello); | 426 | GNUNET_NO); |
449 | GNUNET_free (fn); | 427 | entry = GNUNET_new(struct HostEntry); |
428 | entry->identity = *identity; | ||
429 | GNUNET_assert(GNUNET_OK == | ||
430 | GNUNET_CONTAINER_multipeermap_put( | ||
431 | hostmap, | ||
432 | &entry->identity, | ||
433 | entry, | ||
434 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
435 | notify_all(entry); | ||
436 | fn = get_host_filename(identity); | ||
437 | if (NULL != fn) | ||
438 | { | ||
439 | read_host_file(fn, GNUNET_YES, &r); | ||
440 | if (NULL != r.hello) | ||
441 | update_hello(identity, r.hello); | ||
442 | if (NULL != r.friend_only_hello) | ||
443 | update_hello(identity, r.friend_only_hello); | ||
444 | GNUNET_free_non_null(r.hello); | ||
445 | GNUNET_free_non_null(r.friend_only_hello); | ||
446 | GNUNET_free(fn); | ||
447 | } | ||
450 | } | 448 | } |
451 | } | ||
452 | return entry; | 449 | return entry; |
453 | } | 450 | } |
454 | 451 | ||
@@ -460,27 +457,26 @@ add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity) | |||
460 | * @param fullname name of the file to remove | 457 | * @param fullname name of the file to remove |
461 | */ | 458 | */ |
462 | static void | 459 | static void |
463 | remove_garbage (const char *fullname) | 460 | remove_garbage(const char *fullname) |
464 | { | 461 | { |
465 | if (0 == unlink (fullname)) | 462 | if (0 == unlink(fullname)) |
466 | GNUNET_log ( | 463 | GNUNET_log( |
467 | GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, | 464 | GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, |
468 | _ ( | 465 | _( |
469 | "File `%s' in directory `%s' does not match naming convention. Removed.\n"), | 466 | "File `%s' in directory `%s' does not match naming convention. Removed.\n"), |
470 | fullname, | 467 | fullname, |
471 | networkIdDirectory); | 468 | networkIdDirectory); |
472 | else | 469 | else |
473 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 470 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
474 | "unlink", | 471 | "unlink", |
475 | fullname); | 472 | fullname); |
476 | } | 473 | } |
477 | 474 | ||
478 | 475 | ||
479 | /** | 476 | /** |
480 | * Closure for #hosts_directory_scan_callback(). | 477 | * Closure for #hosts_directory_scan_callback(). |
481 | */ | 478 | */ |
482 | struct DirScanContext | 479 | struct DirScanContext { |
483 | { | ||
484 | /** | 480 | /** |
485 | * #GNUNET_YES if we should remove files that are broken, | 481 | * #GNUNET_YES if we should remove files that are broken, |
486 | * #GNUNET_NO if the directory we are iterating over should | 482 | * #GNUNET_NO if the directory we are iterating over should |
@@ -506,7 +502,7 @@ struct DirScanContext | |||
506 | * @return #GNUNET_OK (continue iteration) | 502 | * @return #GNUNET_OK (continue iteration) |
507 | */ | 503 | */ |
508 | static int | 504 | static int |
509 | hosts_directory_scan_callback (void *cls, const char *fullname) | 505 | hosts_directory_scan_callback(void *cls, const char *fullname) |
510 | { | 506 | { |
511 | struct DirScanContext *dsc = cls; | 507 | struct DirScanContext *dsc = cls; |
512 | struct GNUNET_PeerIdentity identity; | 508 | struct GNUNET_PeerIdentity identity; |
@@ -516,81 +512,81 @@ hosts_directory_scan_callback (void *cls, const char *fullname) | |||
516 | struct GNUNET_PeerIdentity id_friend; | 512 | struct GNUNET_PeerIdentity id_friend; |
517 | struct GNUNET_PeerIdentity id; | 513 | struct GNUNET_PeerIdentity id; |
518 | 514 | ||
519 | if (GNUNET_YES != GNUNET_DISK_file_test (fullname)) | 515 | if (GNUNET_YES != GNUNET_DISK_file_test(fullname)) |
520 | return GNUNET_OK; /* ignore non-files */ | 516 | return GNUNET_OK; /* ignore non-files */ |
521 | 517 | ||
522 | filename = strrchr (fullname, DIR_SEPARATOR); | 518 | filename = strrchr(fullname, DIR_SEPARATOR); |
523 | if ((NULL == filename) || (1 > strlen (filename))) | 519 | if ((NULL == filename) || (1 > strlen(filename))) |
524 | filename = fullname; | 520 | filename = fullname; |
525 | else | 521 | else |
526 | filename++; | 522 | filename++; |
527 | 523 | ||
528 | read_host_file (fullname, dsc->remove_files, &r); | 524 | read_host_file(fullname, dsc->remove_files, &r); |
529 | if ((NULL == r.hello) && (NULL == r.friend_only_hello)) | 525 | if ((NULL == r.hello) && (NULL == r.friend_only_hello)) |
530 | return GNUNET_OK; | 526 | return GNUNET_OK; |
531 | if (NULL != r.friend_only_hello) | 527 | if (NULL != r.friend_only_hello) |
532 | { | ||
533 | if (GNUNET_OK != GNUNET_HELLO_get_id (r.friend_only_hello, &id_friend)) | ||
534 | { | 528 | { |
535 | if (GNUNET_YES == dsc->remove_files) | 529 | if (GNUNET_OK != GNUNET_HELLO_get_id(r.friend_only_hello, &id_friend)) |
536 | remove_garbage (fullname); | 530 | { |
537 | return GNUNET_OK; | 531 | if (GNUNET_YES == dsc->remove_files) |
532 | remove_garbage(fullname); | ||
533 | return GNUNET_OK; | ||
534 | } | ||
535 | id = id_friend; | ||
538 | } | 536 | } |
539 | id = id_friend; | ||
540 | } | ||
541 | if (NULL != r.hello) | 537 | if (NULL != r.hello) |
542 | { | ||
543 | if (GNUNET_OK != GNUNET_HELLO_get_id (r.hello, &id_public)) | ||
544 | { | 538 | { |
545 | if (GNUNET_YES == dsc->remove_files) | 539 | if (GNUNET_OK != GNUNET_HELLO_get_id(r.hello, &id_public)) |
546 | remove_garbage (fullname); | 540 | { |
547 | return GNUNET_OK; | 541 | if (GNUNET_YES == dsc->remove_files) |
542 | remove_garbage(fullname); | ||
543 | return GNUNET_OK; | ||
544 | } | ||
545 | id = id_public; | ||
548 | } | 546 | } |
549 | id = id_public; | ||
550 | } | ||
551 | 547 | ||
552 | if ((NULL != r.hello) && (NULL != r.friend_only_hello) && | 548 | if ((NULL != r.hello) && (NULL != r.friend_only_hello) && |
553 | (0 != GNUNET_memcmp (&id_friend, &id_public))) | 549 | (0 != GNUNET_memcmp(&id_friend, &id_public))) |
554 | { | ||
555 | /* HELLOs are not for the same peer */ | ||
556 | GNUNET_break (0); | ||
557 | if (GNUNET_YES == dsc->remove_files) | ||
558 | remove_garbage (fullname); | ||
559 | return GNUNET_OK; | ||
560 | } | ||
561 | if (GNUNET_OK == | ||
562 | GNUNET_CRYPTO_eddsa_public_key_from_string (filename, | ||
563 | strlen (filename), | ||
564 | &identity.public_key)) | ||
565 | { | ||
566 | if (0 != GNUNET_memcmp (&id, &identity)) | ||
567 | { | 550 | { |
568 | /* HELLOs are not for the same peer */ | 551 | /* HELLOs are not for the same peer */ |
569 | GNUNET_break (0); | 552 | GNUNET_break(0); |
570 | if (GNUNET_YES == dsc->remove_files) | 553 | if (GNUNET_YES == dsc->remove_files) |
571 | remove_garbage (fullname); | 554 | remove_garbage(fullname); |
572 | return GNUNET_OK; | 555 | return GNUNET_OK; |
573 | } | 556 | } |
574 | } | 557 | if (GNUNET_OK == |
558 | GNUNET_CRYPTO_eddsa_public_key_from_string(filename, | ||
559 | strlen(filename), | ||
560 | &identity.public_key)) | ||
561 | { | ||
562 | if (0 != GNUNET_memcmp(&id, &identity)) | ||
563 | { | ||
564 | /* HELLOs are not for the same peer */ | ||
565 | GNUNET_break(0); | ||
566 | if (GNUNET_YES == dsc->remove_files) | ||
567 | remove_garbage(fullname); | ||
568 | return GNUNET_OK; | ||
569 | } | ||
570 | } | ||
575 | 571 | ||
576 | /* ok, found something valid, remember HELLO */ | 572 | /* ok, found something valid, remember HELLO */ |
577 | add_host_to_known_hosts (&id); | 573 | add_host_to_known_hosts(&id); |
578 | if (NULL != r.hello) | 574 | if (NULL != r.hello) |
579 | { | 575 | { |
580 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 576 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
581 | "Updating peer `%s' public HELLO \n", | 577 | "Updating peer `%s' public HELLO \n", |
582 | GNUNET_i2s (&id)); | 578 | GNUNET_i2s(&id)); |
583 | update_hello (&id, r.hello); | 579 | update_hello(&id, r.hello); |
584 | GNUNET_free (r.hello); | 580 | GNUNET_free(r.hello); |
585 | } | 581 | } |
586 | if (NULL != r.friend_only_hello) | 582 | if (NULL != r.friend_only_hello) |
587 | { | 583 | { |
588 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 584 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
589 | "Updating peer `%s' friend only HELLO \n", | 585 | "Updating peer `%s' friend only HELLO \n", |
590 | GNUNET_i2s (&id)); | 586 | GNUNET_i2s(&id)); |
591 | update_hello (&id, r.friend_only_hello); | 587 | update_hello(&id, r.friend_only_hello); |
592 | GNUNET_free (r.friend_only_hello); | 588 | GNUNET_free(r.friend_only_hello); |
593 | } | 589 | } |
594 | dsc->matched++; | 590 | dsc->matched++; |
595 | return GNUNET_OK; | 591 | return GNUNET_OK; |
596 | } | 592 | } |
@@ -602,39 +598,39 @@ hosts_directory_scan_callback (void *cls, const char *fullname) | |||
602 | * @param cls unused | 598 | * @param cls unused |
603 | */ | 599 | */ |
604 | static void | 600 | static void |
605 | cron_scan_directory_data_hosts (void *cls) | 601 | cron_scan_directory_data_hosts(void *cls) |
606 | { | 602 | { |
607 | static unsigned int retries; | 603 | static unsigned int retries; |
608 | struct DirScanContext dsc; | 604 | struct DirScanContext dsc; |
609 | 605 | ||
610 | (void) cls; | 606 | (void)cls; |
611 | cron_scan = NULL; | 607 | cron_scan = NULL; |
612 | if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory)) | 608 | if (GNUNET_SYSERR == GNUNET_DISK_directory_create(networkIdDirectory)) |
613 | { | 609 | { |
614 | cron_scan = | 610 | cron_scan = |
615 | GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ, | 611 | GNUNET_SCHEDULER_add_delayed_with_priority(DATA_HOST_FREQ, |
616 | GNUNET_SCHEDULER_PRIORITY_IDLE, | 612 | GNUNET_SCHEDULER_PRIORITY_IDLE, |
617 | &cron_scan_directory_data_hosts, | 613 | &cron_scan_directory_data_hosts, |
618 | NULL); | 614 | NULL); |
619 | return; | 615 | return; |
620 | } | 616 | } |
621 | dsc.matched = 0; | 617 | dsc.matched = 0; |
622 | dsc.remove_files = GNUNET_YES; | 618 | dsc.remove_files = GNUNET_YES; |
623 | GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK, | 619 | GNUNET_log(GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK, |
624 | _ ("Scanning directory `%s'\n"), | 620 | _("Scanning directory `%s'\n"), |
625 | networkIdDirectory); | 621 | networkIdDirectory); |
626 | GNUNET_DISK_directory_scan (networkIdDirectory, | 622 | GNUNET_DISK_directory_scan(networkIdDirectory, |
627 | &hosts_directory_scan_callback, | 623 | &hosts_directory_scan_callback, |
628 | &dsc); | 624 | &dsc); |
629 | if ((0 == dsc.matched) && (0 == (++retries & 31))) | 625 | if ((0 == dsc.matched) && (0 == (++retries & 31))) |
630 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, | 626 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, |
631 | _ ("Still no peers found in `%s'!\n"), | 627 | _("Still no peers found in `%s'!\n"), |
632 | networkIdDirectory); | 628 | networkIdDirectory); |
633 | cron_scan = | 629 | cron_scan = |
634 | GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ, | 630 | GNUNET_SCHEDULER_add_delayed_with_priority(DATA_HOST_FREQ, |
635 | GNUNET_SCHEDULER_PRIORITY_IDLE, | 631 | GNUNET_SCHEDULER_PRIORITY_IDLE, |
636 | &cron_scan_directory_data_hosts, | 632 | &cron_scan_directory_data_hosts, |
637 | NULL); | 633 | NULL); |
638 | } | 634 | } |
639 | 635 | ||
640 | 636 | ||
@@ -646,29 +642,29 @@ cron_scan_directory_data_hosts (void *cls) | |||
646 | * @return merged HELLO | 642 | * @return merged HELLO |
647 | */ | 643 | */ |
648 | static struct GNUNET_HELLO_Message * | 644 | static struct GNUNET_HELLO_Message * |
649 | update_friend_hello (const struct GNUNET_HELLO_Message *hello, | 645 | update_friend_hello(const struct GNUNET_HELLO_Message *hello, |
650 | const struct GNUNET_HELLO_Message *friend_hello) | 646 | const struct GNUNET_HELLO_Message *friend_hello) |
651 | { | 647 | { |
652 | struct GNUNET_HELLO_Message *res; | 648 | struct GNUNET_HELLO_Message *res; |
653 | struct GNUNET_HELLO_Message *tmp; | 649 | struct GNUNET_HELLO_Message *tmp; |
654 | struct GNUNET_PeerIdentity pid; | 650 | struct GNUNET_PeerIdentity pid; |
655 | 651 | ||
656 | if (NULL != friend_hello) | 652 | if (NULL != friend_hello) |
657 | { | 653 | { |
658 | res = GNUNET_HELLO_merge (hello, friend_hello); | 654 | res = GNUNET_HELLO_merge(hello, friend_hello); |
659 | GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res)); | 655 | GNUNET_assert(GNUNET_YES == GNUNET_HELLO_is_friend_only(res)); |
660 | return res; | 656 | return res; |
661 | } | 657 | } |
662 | 658 | ||
663 | if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) | 659 | if (GNUNET_OK != GNUNET_HELLO_get_id(hello, &pid)) |
664 | { | 660 | { |
665 | GNUNET_break (0); | 661 | GNUNET_break(0); |
666 | return NULL; | 662 | return NULL; |
667 | } | 663 | } |
668 | tmp = GNUNET_HELLO_create (&pid.public_key, NULL, NULL, GNUNET_YES); | 664 | tmp = GNUNET_HELLO_create(&pid.public_key, NULL, NULL, GNUNET_YES); |
669 | res = GNUNET_HELLO_merge (hello, tmp); | 665 | res = GNUNET_HELLO_merge(hello, tmp); |
670 | GNUNET_free (tmp); | 666 | GNUNET_free(tmp); |
671 | GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res)); | 667 | GNUNET_assert(GNUNET_YES == GNUNET_HELLO_is_friend_only(res)); |
672 | return res; | 668 | return res; |
673 | } | 669 | } |
674 | 670 | ||
@@ -680,8 +676,8 @@ update_friend_hello (const struct GNUNET_HELLO_Message *hello, | |||
680 | * @param hello the verified (!) hello message | 676 | * @param hello the verified (!) hello message |
681 | */ | 677 | */ |
682 | static void | 678 | static void |
683 | update_hello (const struct GNUNET_PeerIdentity *peer, | 679 | update_hello(const struct GNUNET_PeerIdentity *peer, |
684 | const struct GNUNET_HELLO_Message *hello) | 680 | const struct GNUNET_HELLO_Message *hello) |
685 | { | 681 | { |
686 | char *fn; | 682 | char *fn; |
687 | struct HostEntry *host; | 683 | struct HostEntry *host; |
@@ -696,145 +692,144 @@ update_hello (const struct GNUNET_PeerIdentity *peer, | |||
696 | unsigned int pos; | 692 | unsigned int pos; |
697 | char *buffer; | 693 | char *buffer; |
698 | 694 | ||
699 | host = GNUNET_CONTAINER_multipeermap_get (hostmap, peer); | 695 | host = GNUNET_CONTAINER_multipeermap_get(hostmap, peer); |
700 | GNUNET_assert (NULL != host); | 696 | GNUNET_assert(NULL != host); |
701 | 697 | ||
702 | friend_hello_type = GNUNET_HELLO_is_friend_only (hello); | 698 | friend_hello_type = GNUNET_HELLO_is_friend_only(hello); |
703 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 699 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
704 | "Updating %s HELLO for `%s'\n", | 700 | "Updating %s HELLO for `%s'\n", |
705 | (GNUNET_YES == friend_hello_type) ? "friend-only" : "public", | 701 | (GNUNET_YES == friend_hello_type) ? "friend-only" : "public", |
706 | GNUNET_i2s (peer)); | 702 | GNUNET_i2s(peer)); |
707 | 703 | ||
708 | dest = NULL; | 704 | dest = NULL; |
709 | if (GNUNET_YES == friend_hello_type) | 705 | if (GNUNET_YES == friend_hello_type) |
710 | { | 706 | { |
711 | dest = &host->friend_only_hello; | 707 | dest = &host->friend_only_hello; |
712 | } | 708 | } |
713 | else | 709 | else |
714 | { | 710 | { |
715 | dest = &host->hello; | 711 | dest = &host->hello; |
716 | } | 712 | } |
717 | 713 | ||
718 | if (NULL == (*dest)) | 714 | if (NULL == (*dest)) |
719 | { | 715 | { |
720 | (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello)); | 716 | (*dest) = GNUNET_malloc(GNUNET_HELLO_size(hello)); |
721 | GNUNET_memcpy ((*dest), hello, GNUNET_HELLO_size (hello)); | 717 | GNUNET_memcpy((*dest), hello, GNUNET_HELLO_size(hello)); |
722 | } | 718 | } |
723 | else | 719 | else |
724 | { | ||
725 | mrg = GNUNET_HELLO_merge ((*dest), hello); | ||
726 | delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ()); | ||
727 | if (delta.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) | ||
728 | { | 720 | { |
729 | /* no differences, just ignore the update */ | 721 | mrg = GNUNET_HELLO_merge((*dest), hello); |
730 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 722 | delta = GNUNET_HELLO_equals(mrg, (*dest), GNUNET_TIME_absolute_get()); |
731 | "No change in %s HELLO for `%s'\n", | 723 | if (delta.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) |
732 | (GNUNET_YES == friend_hello_type) ? "friend-only" : "public", | 724 | { |
733 | GNUNET_i2s (peer)); | 725 | /* no differences, just ignore the update */ |
734 | GNUNET_free (mrg); | 726 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
735 | return; | 727 | "No change in %s HELLO for `%s'\n", |
728 | (GNUNET_YES == friend_hello_type) ? "friend-only" : "public", | ||
729 | GNUNET_i2s(peer)); | ||
730 | GNUNET_free(mrg); | ||
731 | return; | ||
732 | } | ||
733 | GNUNET_free((*dest)); | ||
734 | (*dest) = mrg; | ||
736 | } | 735 | } |
737 | GNUNET_free ((*dest)); | ||
738 | (*dest) = mrg; | ||
739 | } | ||
740 | 736 | ||
741 | if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type)) | 737 | if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type)) |
742 | { | 738 | { |
743 | /* Update friend only hello */ | 739 | /* Update friend only hello */ |
744 | mrg = update_friend_hello (host->hello, host->friend_only_hello); | 740 | mrg = update_friend_hello(host->hello, host->friend_only_hello); |
745 | if (NULL != host->friend_only_hello) | 741 | if (NULL != host->friend_only_hello) |
746 | GNUNET_free (host->friend_only_hello); | 742 | GNUNET_free(host->friend_only_hello); |
747 | host->friend_only_hello = mrg; | 743 | host->friend_only_hello = mrg; |
748 | } | 744 | } |
749 | 745 | ||
750 | if (NULL != host->hello) | 746 | if (NULL != host->hello) |
751 | GNUNET_assert ((GNUNET_NO == GNUNET_HELLO_is_friend_only (host->hello))); | 747 | GNUNET_assert((GNUNET_NO == GNUNET_HELLO_is_friend_only(host->hello))); |
752 | if (NULL != host->friend_only_hello) | 748 | if (NULL != host->friend_only_hello) |
753 | GNUNET_assert ( | 749 | GNUNET_assert( |
754 | (GNUNET_YES == GNUNET_HELLO_is_friend_only (host->friend_only_hello))); | 750 | (GNUNET_YES == GNUNET_HELLO_is_friend_only(host->friend_only_hello))); |
755 | 751 | ||
756 | fn = get_host_filename (peer); | 752 | fn = get_host_filename(peer); |
757 | if ((NULL != fn) && (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn))) | 753 | if ((NULL != fn) && (GNUNET_OK == GNUNET_DISK_directory_create_for_file(fn))) |
758 | { | 754 | { |
759 | store_hello = GNUNET_NO; | 755 | store_hello = GNUNET_NO; |
760 | size = 0; | 756 | size = 0; |
761 | cnt = 0; | 757 | cnt = 0; |
762 | if (NULL != host->hello) | 758 | if (NULL != host->hello) |
763 | (void) GNUNET_HELLO_iterate_addresses (host->hello, | 759 | (void)GNUNET_HELLO_iterate_addresses(host->hello, |
764 | GNUNET_NO, | 760 | GNUNET_NO, |
765 | &count_addresses, | 761 | &count_addresses, |
766 | &cnt); | 762 | &cnt); |
767 | if (cnt > 0) | 763 | if (cnt > 0) |
768 | { | 764 | { |
769 | store_hello = GNUNET_YES; | 765 | store_hello = GNUNET_YES; |
770 | size += GNUNET_HELLO_size (host->hello); | 766 | size += GNUNET_HELLO_size(host->hello); |
771 | } | 767 | } |
772 | cnt = 0; | 768 | cnt = 0; |
773 | if (NULL != host->friend_only_hello) | 769 | if (NULL != host->friend_only_hello) |
774 | (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello, | 770 | (void)GNUNET_HELLO_iterate_addresses(host->friend_only_hello, |
775 | GNUNET_NO, | 771 | GNUNET_NO, |
776 | &count_addresses, | 772 | &count_addresses, |
777 | &cnt); | 773 | &cnt); |
778 | store_friend_hello = GNUNET_NO; | 774 | store_friend_hello = GNUNET_NO; |
779 | if (0 < cnt) | 775 | if (0 < cnt) |
780 | { | 776 | { |
781 | store_friend_hello = GNUNET_YES; | 777 | store_friend_hello = GNUNET_YES; |
782 | size += GNUNET_HELLO_size (host->friend_only_hello); | 778 | size += GNUNET_HELLO_size(host->friend_only_hello); |
783 | } | 779 | } |
784 | 780 | ||
785 | if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello)) | 781 | if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello)) |
786 | { | 782 | { |
787 | /* no valid addresses, don't put HELLO on disk; in fact, | 783 | /* no valid addresses, don't put HELLO on disk; in fact, |
788 | if one exists on disk, remove it */ | 784 | if one exists on disk, remove it */ |
789 | (void) unlink (fn); | 785 | (void)unlink(fn); |
790 | } | 786 | } |
791 | else | ||
792 | { | ||
793 | buffer = GNUNET_malloc (size); | ||
794 | pos = 0; | ||
795 | |||
796 | if (GNUNET_YES == store_hello) | ||
797 | { | ||
798 | GNUNET_memcpy (buffer, host->hello, GNUNET_HELLO_size (host->hello)); | ||
799 | pos += GNUNET_HELLO_size (host->hello); | ||
800 | } | ||
801 | if (GNUNET_YES == store_friend_hello) | ||
802 | { | ||
803 | GNUNET_memcpy (&buffer[pos], | ||
804 | host->friend_only_hello, | ||
805 | GNUNET_HELLO_size (host->friend_only_hello)); | ||
806 | pos += GNUNET_HELLO_size (host->friend_only_hello); | ||
807 | } | ||
808 | GNUNET_assert (pos == size); | ||
809 | |||
810 | if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, | ||
811 | buffer, | ||
812 | size, | ||
813 | GNUNET_DISK_PERM_USER_READ | | ||
814 | GNUNET_DISK_PERM_USER_WRITE | | ||
815 | GNUNET_DISK_PERM_GROUP_READ | | ||
816 | GNUNET_DISK_PERM_OTHER_READ)) | ||
817 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn); | ||
818 | else | 787 | else |
819 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 788 | { |
820 | "Stored %s %s HELLO in %s with total size %u\n", | 789 | buffer = GNUNET_malloc(size); |
821 | (GNUNET_YES == store_friend_hello) ? "friend-only" : "", | 790 | pos = 0; |
822 | (GNUNET_YES == store_hello) ? "public" : "", | 791 | |
823 | fn, | 792 | if (GNUNET_YES == store_hello) |
824 | size); | 793 | { |
825 | GNUNET_free (buffer); | 794 | GNUNET_memcpy(buffer, host->hello, GNUNET_HELLO_size(host->hello)); |
795 | pos += GNUNET_HELLO_size(host->hello); | ||
796 | } | ||
797 | if (GNUNET_YES == store_friend_hello) | ||
798 | { | ||
799 | GNUNET_memcpy(&buffer[pos], | ||
800 | host->friend_only_hello, | ||
801 | GNUNET_HELLO_size(host->friend_only_hello)); | ||
802 | pos += GNUNET_HELLO_size(host->friend_only_hello); | ||
803 | } | ||
804 | GNUNET_assert(pos == size); | ||
805 | |||
806 | if (GNUNET_SYSERR == GNUNET_DISK_fn_write(fn, | ||
807 | buffer, | ||
808 | size, | ||
809 | GNUNET_DISK_PERM_USER_READ | | ||
810 | GNUNET_DISK_PERM_USER_WRITE | | ||
811 | GNUNET_DISK_PERM_GROUP_READ | | ||
812 | GNUNET_DISK_PERM_OTHER_READ)) | ||
813 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "write", fn); | ||
814 | else | ||
815 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
816 | "Stored %s %s HELLO in %s with total size %u\n", | ||
817 | (GNUNET_YES == store_friend_hello) ? "friend-only" : "", | ||
818 | (GNUNET_YES == store_hello) ? "public" : "", | ||
819 | fn, | ||
820 | size); | ||
821 | GNUNET_free(buffer); | ||
822 | } | ||
826 | } | 823 | } |
827 | } | 824 | GNUNET_free_non_null(fn); |
828 | GNUNET_free_non_null (fn); | 825 | notify_all(host); |
829 | notify_all (host); | ||
830 | } | 826 | } |
831 | 827 | ||
832 | 828 | ||
833 | /** | 829 | /** |
834 | * Closure for #add_to_tc() | 830 | * Closure for #add_to_tc() |
835 | */ | 831 | */ |
836 | struct TransmitContext | 832 | struct TransmitContext { |
837 | { | ||
838 | /** | 833 | /** |
839 | * Client to transmit to | 834 | * Client to transmit to |
840 | */ | 835 | */ |
@@ -856,7 +851,7 @@ struct TransmitContext | |||
856 | * @return #GNUNET_YES (continue to iterate) | 851 | * @return #GNUNET_YES (continue to iterate) |
857 | */ | 852 | */ |
858 | static int | 853 | static int |
859 | add_to_tc (void *cls, const struct GNUNET_PeerIdentity *key, void *value) | 854 | add_to_tc(void *cls, const struct GNUNET_PeerIdentity *key, void *value) |
860 | { | 855 | { |
861 | struct TransmitContext *tc = cls; | 856 | struct TransmitContext *tc = cls; |
862 | struct HostEntry *pos = value; | 857 | struct HostEntry *pos = value; |
@@ -867,38 +862,38 @@ add_to_tc (void *cls, const struct GNUNET_PeerIdentity *key, void *value) | |||
867 | hs = 0; | 862 | hs = 0; |
868 | 863 | ||
869 | if ((NULL != pos->hello) && (GNUNET_NO == tc->friend_only)) | 864 | if ((NULL != pos->hello) && (GNUNET_NO == tc->friend_only)) |
870 | { | 865 | { |
871 | /* Copy public HELLO */ | 866 | /* Copy public HELLO */ |
872 | hs = GNUNET_HELLO_size (pos->hello); | 867 | hs = GNUNET_HELLO_size(pos->hello); |
873 | GNUNET_assert (hs < GNUNET_MAX_MESSAGE_SIZE - sizeof (struct InfoMessage)); | 868 | GNUNET_assert(hs < GNUNET_MAX_MESSAGE_SIZE - sizeof(struct InfoMessage)); |
874 | env = GNUNET_MQ_msg_extra (im, hs, GNUNET_MESSAGE_TYPE_PEERINFO_INFO); | 869 | env = GNUNET_MQ_msg_extra(im, hs, GNUNET_MESSAGE_TYPE_PEERINFO_INFO); |
875 | GNUNET_memcpy (&im[1], pos->hello, hs); | 870 | GNUNET_memcpy(&im[1], pos->hello, hs); |
876 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 871 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
877 | "Sending public HELLO with size %u for peer `%s'\n", | 872 | "Sending public HELLO with size %u for peer `%s'\n", |
878 | hs, | 873 | hs, |
879 | GNUNET_i2s (key)); | 874 | GNUNET_i2s(key)); |
880 | } | 875 | } |
881 | else if ((NULL != pos->friend_only_hello) && (GNUNET_YES == tc->friend_only)) | 876 | else if ((NULL != pos->friend_only_hello) && (GNUNET_YES == tc->friend_only)) |
882 | { | 877 | { |
883 | /* Copy friend only HELLO */ | 878 | /* Copy friend only HELLO */ |
884 | hs = GNUNET_HELLO_size (pos->friend_only_hello); | 879 | hs = GNUNET_HELLO_size(pos->friend_only_hello); |
885 | GNUNET_assert (hs < GNUNET_MAX_MESSAGE_SIZE - sizeof (struct InfoMessage)); | 880 | GNUNET_assert(hs < GNUNET_MAX_MESSAGE_SIZE - sizeof(struct InfoMessage)); |
886 | env = GNUNET_MQ_msg_extra (im, hs, GNUNET_MESSAGE_TYPE_PEERINFO_INFO); | 881 | env = GNUNET_MQ_msg_extra(im, hs, GNUNET_MESSAGE_TYPE_PEERINFO_INFO); |
887 | GNUNET_memcpy (&im[1], pos->friend_only_hello, hs); | 882 | GNUNET_memcpy(&im[1], pos->friend_only_hello, hs); |
888 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 883 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
889 | "Sending friend-only HELLO with size %u for peer `%s'\n", | 884 | "Sending friend-only HELLO with size %u for peer `%s'\n", |
890 | hs, | 885 | hs, |
891 | GNUNET_i2s (key)); | 886 | GNUNET_i2s(key)); |
892 | } | 887 | } |
893 | else | 888 | else |
894 | { | 889 | { |
895 | env = GNUNET_MQ_msg (im, GNUNET_MESSAGE_TYPE_PEERINFO_INFO); | 890 | env = GNUNET_MQ_msg(im, GNUNET_MESSAGE_TYPE_PEERINFO_INFO); |
896 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 891 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
897 | "Adding no HELLO for peer `%s'\n", | 892 | "Adding no HELLO for peer `%s'\n", |
898 | GNUNET_i2s (key)); | 893 | GNUNET_i2s(key)); |
899 | } | 894 | } |
900 | im->peer = pos->identity; | 895 | im->peer = pos->identity; |
901 | GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (tc->client), env); | 896 | GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(tc->client), env); |
902 | return GNUNET_YES; | 897 | return GNUNET_YES; |
903 | } | 898 | } |
904 | 899 | ||
@@ -911,7 +906,7 @@ add_to_tc (void *cls, const struct GNUNET_PeerIdentity *key, void *value) | |||
911 | * @return #GNUNET_OK (continue iteration) | 906 | * @return #GNUNET_OK (continue iteration) |
912 | */ | 907 | */ |
913 | static int | 908 | static int |
914 | discard_hosts_helper (void *cls, const char *fn) | 909 | discard_hosts_helper(void *cls, const char *fn) |
915 | { | 910 | { |
916 | struct GNUNET_TIME_Absolute *now = cls; | 911 | struct GNUNET_TIME_Absolute *now = cls; |
917 | char buffer[GNUNET_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN; | 912 | char buffer[GNUNET_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN; |
@@ -926,82 +921,82 @@ discard_hosts_helper (void *cls, const char *fn) | |||
926 | char *writebuffer; | 921 | char *writebuffer; |
927 | uint64_t fsize; | 922 | uint64_t fsize; |
928 | 923 | ||
929 | if (GNUNET_OK != GNUNET_DISK_file_size (fn, &fsize, GNUNET_YES, GNUNET_YES)) | 924 | if (GNUNET_OK != GNUNET_DISK_file_size(fn, &fsize, GNUNET_YES, GNUNET_YES)) |
930 | { | 925 | { |
931 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | | 926 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING | |
932 | GNUNET_ERROR_TYPE_BULK, | 927 | GNUNET_ERROR_TYPE_BULK, |
933 | "fstat", | 928 | "fstat", |
934 | fn); | 929 | fn); |
935 | return GNUNET_OK; | 930 | return GNUNET_OK; |
936 | } | 931 | } |
937 | read_size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer)); | 932 | read_size = GNUNET_DISK_fn_read(fn, buffer, sizeof(buffer)); |
938 | 933 | ||
939 | if ((read_size < (int) sizeof (struct GNUNET_MessageHeader)) || | 934 | if ((read_size < (int)sizeof(struct GNUNET_MessageHeader)) || |
940 | (fsize > GNUNET_MAX_MESSAGE_SIZE)) | 935 | (fsize > GNUNET_MAX_MESSAGE_SIZE)) |
941 | { | 936 | { |
942 | if (0 != unlink (fn)) | 937 | if (0 != unlink(fn)) |
943 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | | 938 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING | |
944 | GNUNET_ERROR_TYPE_BULK, | 939 | GNUNET_ERROR_TYPE_BULK, |
945 | "unlink", | 940 | "unlink", |
946 | fn); | 941 | fn); |
947 | return GNUNET_OK; | 942 | return GNUNET_OK; |
948 | } | 943 | } |
949 | 944 | ||
950 | writebuffer = GNUNET_malloc (read_size); | 945 | writebuffer = GNUNET_malloc(read_size); |
951 | read_pos = 0; | 946 | read_pos = 0; |
952 | write_pos = 0; | 947 | write_pos = 0; |
953 | while (read_pos < read_size) | 948 | while (read_pos < read_size) |
954 | { | ||
955 | /* Check each HELLO */ | ||
956 | hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos]; | ||
957 | cur_hello_size = GNUNET_HELLO_size (hello); | ||
958 | if (0 == cur_hello_size) | ||
959 | { | 949 | { |
960 | /* Invalid data, discard */ | 950 | /* Check each HELLO */ |
961 | if (0 != unlink (fn)) | 951 | hello = (const struct GNUNET_HELLO_Message *)&buffer[read_pos]; |
962 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | | 952 | cur_hello_size = GNUNET_HELLO_size(hello); |
963 | GNUNET_ERROR_TYPE_BULK, | 953 | if (0 == cur_hello_size) |
964 | "unlink", | 954 | { |
965 | fn); | 955 | /* Invalid data, discard */ |
966 | GNUNET_free (writebuffer); | 956 | if (0 != unlink(fn)) |
967 | return GNUNET_OK; | 957 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING | |
968 | } | 958 | GNUNET_ERROR_TYPE_BULK, |
969 | new_hello = | 959 | "unlink", |
970 | GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now); | 960 | fn); |
971 | cnt = 0; | 961 | GNUNET_free(writebuffer); |
972 | if (NULL != new_hello) | 962 | return GNUNET_OK; |
973 | (void) GNUNET_HELLO_iterate_addresses (hello, | 963 | } |
964 | new_hello = | ||
965 | GNUNET_HELLO_iterate_addresses(hello, GNUNET_YES, &discard_expired, now); | ||
966 | cnt = 0; | ||
967 | if (NULL != new_hello) | ||
968 | (void)GNUNET_HELLO_iterate_addresses(hello, | ||
974 | GNUNET_NO, | 969 | GNUNET_NO, |
975 | &count_addresses, | 970 | &count_addresses, |
976 | &cnt); | 971 | &cnt); |
977 | if ((NULL != new_hello) && (0 < cnt)) | 972 | if ((NULL != new_hello) && (0 < cnt)) |
978 | { | 973 | { |
979 | /* Store new HELLO to write it when done */ | 974 | /* Store new HELLO to write it when done */ |
980 | new_hello_size = GNUNET_HELLO_size (new_hello); | 975 | new_hello_size = GNUNET_HELLO_size(new_hello); |
981 | GNUNET_memcpy (&writebuffer[write_pos], new_hello, new_hello_size); | 976 | GNUNET_memcpy(&writebuffer[write_pos], new_hello, new_hello_size); |
982 | write_pos += new_hello_size; | 977 | write_pos += new_hello_size; |
978 | } | ||
979 | read_pos += cur_hello_size; | ||
980 | GNUNET_free_non_null(new_hello); | ||
983 | } | 981 | } |
984 | read_pos += cur_hello_size; | ||
985 | GNUNET_free_non_null (new_hello); | ||
986 | } | ||
987 | 982 | ||
988 | if (0 < write_pos) | 983 | if (0 < write_pos) |
989 | { | 984 | { |
990 | GNUNET_DISK_fn_write (fn, | 985 | GNUNET_DISK_fn_write(fn, |
991 | writebuffer, | 986 | writebuffer, |
992 | write_pos, | 987 | write_pos, |
993 | GNUNET_DISK_PERM_USER_READ | | 988 | GNUNET_DISK_PERM_USER_READ | |
994 | GNUNET_DISK_PERM_USER_WRITE | | 989 | GNUNET_DISK_PERM_USER_WRITE | |
995 | GNUNET_DISK_PERM_GROUP_READ | | 990 | GNUNET_DISK_PERM_GROUP_READ | |
996 | GNUNET_DISK_PERM_OTHER_READ); | 991 | GNUNET_DISK_PERM_OTHER_READ); |
997 | } | 992 | } |
998 | else if (0 != unlink (fn)) | 993 | else if (0 != unlink(fn)) |
999 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | | 994 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING | |
1000 | GNUNET_ERROR_TYPE_BULK, | 995 | GNUNET_ERROR_TYPE_BULK, |
1001 | "unlink", | 996 | "unlink", |
1002 | fn); | 997 | fn); |
1003 | 998 | ||
1004 | GNUNET_free (writebuffer); | 999 | GNUNET_free(writebuffer); |
1005 | return GNUNET_OK; | 1000 | return GNUNET_OK; |
1006 | } | 1001 | } |
1007 | 1002 | ||
@@ -1013,20 +1008,20 @@ discard_hosts_helper (void *cls, const char *fn) | |||
1013 | * @param cls unused | 1008 | * @param cls unused |
1014 | */ | 1009 | */ |
1015 | static void | 1010 | static void |
1016 | cron_clean_data_hosts (void *cls) | 1011 | cron_clean_data_hosts(void *cls) |
1017 | { | 1012 | { |
1018 | struct GNUNET_TIME_Absolute now; | 1013 | struct GNUNET_TIME_Absolute now; |
1019 | 1014 | ||
1020 | (void) cls; | 1015 | (void)cls; |
1021 | cron_clean = NULL; | 1016 | cron_clean = NULL; |
1022 | now = GNUNET_TIME_absolute_get (); | 1017 | now = GNUNET_TIME_absolute_get(); |
1023 | GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK, | 1018 | GNUNET_log(GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK, |
1024 | _ ("Cleaning up directory `%s'\n"), | 1019 | _("Cleaning up directory `%s'\n"), |
1025 | networkIdDirectory); | 1020 | networkIdDirectory); |
1026 | GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now); | 1021 | GNUNET_DISK_directory_scan(networkIdDirectory, &discard_hosts_helper, &now); |
1027 | cron_clean = GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, | 1022 | cron_clean = GNUNET_SCHEDULER_add_delayed(DATA_HOST_CLEAN_FREQ, |
1028 | &cron_clean_data_hosts, | 1023 | &cron_clean_data_hosts, |
1029 | NULL); | 1024 | NULL); |
1030 | } | 1025 | } |
1031 | 1026 | ||
1032 | 1027 | ||
@@ -1038,16 +1033,16 @@ cron_clean_data_hosts (void *cls) | |||
1038 | * @return #GNUNET_OK if @a hello is well-formed | 1033 | * @return #GNUNET_OK if @a hello is well-formed |
1039 | */ | 1034 | */ |
1040 | static int | 1035 | static int |
1041 | check_hello (void *cls, const struct GNUNET_HELLO_Message *hello) | 1036 | check_hello(void *cls, const struct GNUNET_HELLO_Message *hello) |
1042 | { | 1037 | { |
1043 | struct GNUNET_PeerIdentity pid; | 1038 | struct GNUNET_PeerIdentity pid; |
1044 | 1039 | ||
1045 | (void) cls; | 1040 | (void)cls; |
1046 | if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) | 1041 | if (GNUNET_OK != GNUNET_HELLO_get_id(hello, &pid)) |
1047 | { | 1042 | { |
1048 | GNUNET_break (0); | 1043 | GNUNET_break(0); |
1049 | return GNUNET_SYSERR; | 1044 | return GNUNET_SYSERR; |
1050 | } | 1045 | } |
1051 | return GNUNET_OK; | 1046 | return GNUNET_OK; |
1052 | } | 1047 | } |
1053 | 1048 | ||
@@ -1059,18 +1054,18 @@ check_hello (void *cls, const struct GNUNET_HELLO_Message *hello) | |||
1059 | * @param hello the actual message | 1054 | * @param hello the actual message |
1060 | */ | 1055 | */ |
1061 | static void | 1056 | static void |
1062 | handle_hello (void *cls, const struct GNUNET_HELLO_Message *hello) | 1057 | handle_hello(void *cls, const struct GNUNET_HELLO_Message *hello) |
1063 | { | 1058 | { |
1064 | struct GNUNET_SERVICE_Client *client = cls; | 1059 | struct GNUNET_SERVICE_Client *client = cls; |
1065 | struct GNUNET_PeerIdentity pid; | 1060 | struct GNUNET_PeerIdentity pid; |
1066 | 1061 | ||
1067 | GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid)); | 1062 | GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(hello, &pid)); |
1068 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1063 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1069 | "HELLO message received for peer `%s'\n", | 1064 | "HELLO message received for peer `%s'\n", |
1070 | GNUNET_i2s (&pid)); | 1065 | GNUNET_i2s(&pid)); |
1071 | add_host_to_known_hosts (&pid); | 1066 | add_host_to_known_hosts(&pid); |
1072 | update_hello (&pid, hello); | 1067 | update_hello(&pid, hello); |
1073 | GNUNET_SERVICE_client_continue (client); | 1068 | GNUNET_SERVICE_client_continue(client); |
1074 | } | 1069 | } |
1075 | 1070 | ||
1076 | 1071 | ||
@@ -1081,25 +1076,25 @@ handle_hello (void *cls, const struct GNUNET_HELLO_Message *hello) | |||
1081 | * @param lpm the actual message | 1076 | * @param lpm the actual message |
1082 | */ | 1077 | */ |
1083 | static void | 1078 | static void |
1084 | handle_get (void *cls, const struct ListPeerMessage *lpm) | 1079 | handle_get(void *cls, const struct ListPeerMessage *lpm) |
1085 | { | 1080 | { |
1086 | struct GNUNET_SERVICE_Client *client = cls; | 1081 | struct GNUNET_SERVICE_Client *client = cls; |
1087 | struct TransmitContext tcx; | 1082 | struct TransmitContext tcx; |
1088 | struct GNUNET_MessageHeader *msg; | 1083 | struct GNUNET_MessageHeader *msg; |
1089 | struct GNUNET_MQ_Envelope *env; | 1084 | struct GNUNET_MQ_Envelope *env; |
1090 | 1085 | ||
1091 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1086 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1092 | "GET message received for peer `%s'\n", | 1087 | "GET message received for peer `%s'\n", |
1093 | GNUNET_i2s (&lpm->peer)); | 1088 | GNUNET_i2s(&lpm->peer)); |
1094 | tcx.friend_only = ntohl (lpm->include_friend_only); | 1089 | tcx.friend_only = ntohl(lpm->include_friend_only); |
1095 | tcx.client = client; | 1090 | tcx.client = client; |
1096 | GNUNET_CONTAINER_multipeermap_get_multiple (hostmap, | 1091 | GNUNET_CONTAINER_multipeermap_get_multiple(hostmap, |
1097 | &lpm->peer, | 1092 | &lpm->peer, |
1098 | &add_to_tc, | 1093 | &add_to_tc, |
1099 | &tcx); | 1094 | &tcx); |
1100 | env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END); | 1095 | env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END); |
1101 | GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env); | 1096 | GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env); |
1102 | GNUNET_SERVICE_client_continue (client); | 1097 | GNUNET_SERVICE_client_continue(client); |
1103 | } | 1098 | } |
1104 | 1099 | ||
1105 | 1100 | ||
@@ -1110,20 +1105,20 @@ handle_get (void *cls, const struct ListPeerMessage *lpm) | |||
1110 | * @param lapm the actual message | 1105 | * @param lapm the actual message |
1111 | */ | 1106 | */ |
1112 | static void | 1107 | static void |
1113 | handle_get_all (void *cls, const struct ListAllPeersMessage *lapm) | 1108 | handle_get_all(void *cls, const struct ListAllPeersMessage *lapm) |
1114 | { | 1109 | { |
1115 | struct GNUNET_SERVICE_Client *client = cls; | 1110 | struct GNUNET_SERVICE_Client *client = cls; |
1116 | struct TransmitContext tcx; | 1111 | struct TransmitContext tcx; |
1117 | struct GNUNET_MQ_Envelope *env; | 1112 | struct GNUNET_MQ_Envelope *env; |
1118 | struct GNUNET_MessageHeader *msg; | 1113 | struct GNUNET_MessageHeader *msg; |
1119 | 1114 | ||
1120 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GET_ALL message received\n"); | 1115 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "GET_ALL message received\n"); |
1121 | tcx.friend_only = ntohl (lapm->include_friend_only); | 1116 | tcx.friend_only = ntohl(lapm->include_friend_only); |
1122 | tcx.client = client; | 1117 | tcx.client = client; |
1123 | GNUNET_CONTAINER_multipeermap_iterate (hostmap, &add_to_tc, &tcx); | 1118 | GNUNET_CONTAINER_multipeermap_iterate(hostmap, &add_to_tc, &tcx); |
1124 | env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END); | 1119 | env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END); |
1125 | GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env); | 1120 | GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env); |
1126 | GNUNET_SERVICE_client_continue (client); | 1121 | GNUNET_SERVICE_client_continue(client); |
1127 | } | 1122 | } |
1128 | 1123 | ||
1129 | 1124 | ||
@@ -1134,7 +1129,7 @@ handle_get_all (void *cls, const struct ListAllPeersMessage *lapm) | |||
1134 | * @param nm the actual message | 1129 | * @param nm the actual message |
1135 | */ | 1130 | */ |
1136 | static void | 1131 | static void |
1137 | handle_notify (void *cls, const struct NotifyMessage *nm) | 1132 | handle_notify(void *cls, const struct NotifyMessage *nm) |
1138 | { | 1133 | { |
1139 | struct GNUNET_SERVICE_Client *client = cls; | 1134 | struct GNUNET_SERVICE_Client *client = cls; |
1140 | struct GNUNET_MQ_Handle *mq; | 1135 | struct GNUNET_MQ_Handle *mq; |
@@ -1142,19 +1137,19 @@ handle_notify (void *cls, const struct NotifyMessage *nm) | |||
1142 | struct GNUNET_MQ_Envelope *env; | 1137 | struct GNUNET_MQ_Envelope *env; |
1143 | struct GNUNET_MessageHeader *msg; | 1138 | struct GNUNET_MessageHeader *msg; |
1144 | 1139 | ||
1145 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "NOTIFY message received\n"); | 1140 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "NOTIFY message received\n"); |
1146 | mq = GNUNET_SERVICE_client_get_mq (client); | 1141 | mq = GNUNET_SERVICE_client_get_mq(client); |
1147 | GNUNET_SERVICE_client_mark_monitor (client); | 1142 | GNUNET_SERVICE_client_mark_monitor(client); |
1148 | if (ntohl (nm->include_friend_only)) | 1143 | if (ntohl(nm->include_friend_only)) |
1149 | GNUNET_notification_context_add (notify_friend_only_list, mq); | 1144 | GNUNET_notification_context_add(notify_friend_only_list, mq); |
1150 | else | 1145 | else |
1151 | GNUNET_notification_context_add (notify_list, mq); | 1146 | GNUNET_notification_context_add(notify_list, mq); |
1152 | tcx.friend_only = ntohl (nm->include_friend_only); | 1147 | tcx.friend_only = ntohl(nm->include_friend_only); |
1153 | tcx.client = client; | 1148 | tcx.client = client; |
1154 | GNUNET_CONTAINER_multipeermap_iterate (hostmap, &add_to_tc, &tcx); | 1149 | GNUNET_CONTAINER_multipeermap_iterate(hostmap, &add_to_tc, &tcx); |
1155 | env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END); | 1150 | env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END); |
1156 | GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env); | 1151 | GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env); |
1157 | GNUNET_SERVICE_client_continue (client); | 1152 | GNUNET_SERVICE_client_continue(client); |
1158 | } | 1153 | } |
1159 | 1154 | ||
1160 | 1155 | ||
@@ -1167,12 +1162,12 @@ handle_notify (void *cls, const struct NotifyMessage *nm) | |||
1167 | * @return @a client | 1162 | * @return @a client |
1168 | */ | 1163 | */ |
1169 | static void * | 1164 | static void * |
1170 | client_connect_cb (void *cls, | 1165 | client_connect_cb(void *cls, |
1171 | struct GNUNET_SERVICE_Client *client, | 1166 | struct GNUNET_SERVICE_Client *client, |
1172 | struct GNUNET_MQ_Handle *mq) | 1167 | struct GNUNET_MQ_Handle *mq) |
1173 | { | 1168 | { |
1174 | (void) cls; | 1169 | (void)cls; |
1175 | (void) mq; | 1170 | (void)mq; |
1176 | return client; | 1171 | return client; |
1177 | } | 1172 | } |
1178 | 1173 | ||
@@ -1185,12 +1180,12 @@ client_connect_cb (void *cls, | |||
1185 | * @param app_ctx should be @a client | 1180 | * @param app_ctx should be @a client |
1186 | */ | 1181 | */ |
1187 | static void | 1182 | static void |
1188 | client_disconnect_cb (void *cls, | 1183 | client_disconnect_cb(void *cls, |
1189 | struct GNUNET_SERVICE_Client *client, | 1184 | struct GNUNET_SERVICE_Client *client, |
1190 | void *app_ctx) | 1185 | void *app_ctx) |
1191 | { | 1186 | { |
1192 | (void) cls; | 1187 | (void)cls; |
1193 | GNUNET_assert (app_ctx == client); | 1188 | GNUNET_assert(app_ctx == client); |
1194 | } | 1189 | } |
1195 | 1190 | ||
1196 | 1191 | ||
@@ -1203,15 +1198,15 @@ client_disconnect_cb (void *cls, | |||
1203 | * @return #GNUNET_YES (continue to iterate) | 1198 | * @return #GNUNET_YES (continue to iterate) |
1204 | */ | 1199 | */ |
1205 | static int | 1200 | static int |
1206 | free_host_entry (void *cls, const struct GNUNET_PeerIdentity *key, void *value) | 1201 | free_host_entry(void *cls, const struct GNUNET_PeerIdentity *key, void *value) |
1207 | { | 1202 | { |
1208 | struct HostEntry *he = value; | 1203 | struct HostEntry *he = value; |
1209 | 1204 | ||
1210 | (void) cls; | 1205 | (void)cls; |
1211 | (void) key; | 1206 | (void)key; |
1212 | GNUNET_free_non_null (he->hello); | 1207 | GNUNET_free_non_null(he->hello); |
1213 | GNUNET_free_non_null (he->friend_only_hello); | 1208 | GNUNET_free_non_null(he->friend_only_hello); |
1214 | GNUNET_free (he); | 1209 | GNUNET_free(he); |
1215 | return GNUNET_YES; | 1210 | return GNUNET_YES; |
1216 | } | 1211 | } |
1217 | 1212 | ||
@@ -1222,36 +1217,36 @@ free_host_entry (void *cls, const struct GNUNET_PeerIdentity *key, void *value) | |||
1222 | * @param cls unused | 1217 | * @param cls unused |
1223 | */ | 1218 | */ |
1224 | static void | 1219 | static void |
1225 | shutdown_task (void *cls) | 1220 | shutdown_task(void *cls) |
1226 | { | 1221 | { |
1227 | (void) cls; | 1222 | (void)cls; |
1228 | GNUNET_notification_context_destroy (notify_list); | 1223 | GNUNET_notification_context_destroy(notify_list); |
1229 | notify_list = NULL; | 1224 | notify_list = NULL; |
1230 | GNUNET_notification_context_destroy (notify_friend_only_list); | 1225 | GNUNET_notification_context_destroy(notify_friend_only_list); |
1231 | notify_friend_only_list = NULL; | 1226 | notify_friend_only_list = NULL; |
1232 | 1227 | ||
1233 | GNUNET_CONTAINER_multipeermap_iterate (hostmap, &free_host_entry, NULL); | 1228 | GNUNET_CONTAINER_multipeermap_iterate(hostmap, &free_host_entry, NULL); |
1234 | GNUNET_CONTAINER_multipeermap_destroy (hostmap); | 1229 | GNUNET_CONTAINER_multipeermap_destroy(hostmap); |
1235 | if (NULL != stats) | 1230 | if (NULL != stats) |
1236 | { | 1231 | { |
1237 | GNUNET_STATISTICS_destroy (stats, GNUNET_NO); | 1232 | GNUNET_STATISTICS_destroy(stats, GNUNET_NO); |
1238 | stats = NULL; | 1233 | stats = NULL; |
1239 | } | 1234 | } |
1240 | if (NULL != cron_clean) | 1235 | if (NULL != cron_clean) |
1241 | { | 1236 | { |
1242 | GNUNET_SCHEDULER_cancel (cron_clean); | 1237 | GNUNET_SCHEDULER_cancel(cron_clean); |
1243 | cron_clean = NULL; | 1238 | cron_clean = NULL; |
1244 | } | 1239 | } |
1245 | if (NULL != cron_scan) | 1240 | if (NULL != cron_scan) |
1246 | { | 1241 | { |
1247 | GNUNET_SCHEDULER_cancel (cron_scan); | 1242 | GNUNET_SCHEDULER_cancel(cron_scan); |
1248 | cron_scan = NULL; | 1243 | cron_scan = NULL; |
1249 | } | 1244 | } |
1250 | if (NULL != networkIdDirectory) | 1245 | if (NULL != networkIdDirectory) |
1251 | { | 1246 | { |
1252 | GNUNET_free (networkIdDirectory); | 1247 | GNUNET_free(networkIdDirectory); |
1253 | networkIdDirectory = NULL; | 1248 | networkIdDirectory = NULL; |
1254 | } | 1249 | } |
1255 | } | 1250 | } |
1256 | 1251 | ||
1257 | 1252 | ||
@@ -1263,9 +1258,9 @@ shutdown_task (void *cls) | |||
1263 | * @param service the initialized service | 1258 | * @param service the initialized service |
1264 | */ | 1259 | */ |
1265 | static void | 1260 | static void |
1266 | run (void *cls, | 1261 | run(void *cls, |
1267 | const struct GNUNET_CONFIGURATION_Handle *cfg, | 1262 | const struct GNUNET_CONFIGURATION_Handle *cfg, |
1268 | struct GNUNET_SERVICE_Handle *service) | 1263 | struct GNUNET_SERVICE_Handle *service) |
1269 | { | 1264 | { |
1270 | char *peerdir; | 1265 | char *peerdir; |
1271 | char *ip; | 1266 | char *ip; |
@@ -1273,95 +1268,95 @@ run (void *cls, | |||
1273 | int noio; | 1268 | int noio; |
1274 | int use_included; | 1269 | int use_included; |
1275 | 1270 | ||
1276 | (void) cls; | 1271 | (void)cls; |
1277 | (void) service; | 1272 | (void)service; |
1278 | hostmap = GNUNET_CONTAINER_multipeermap_create (1024, GNUNET_YES); | 1273 | hostmap = GNUNET_CONTAINER_multipeermap_create(1024, GNUNET_YES); |
1279 | stats = GNUNET_STATISTICS_create ("peerinfo", cfg); | 1274 | stats = GNUNET_STATISTICS_create("peerinfo", cfg); |
1280 | notify_list = GNUNET_notification_context_create (0); | 1275 | notify_list = GNUNET_notification_context_create(0); |
1281 | notify_friend_only_list = GNUNET_notification_context_create (0); | 1276 | notify_friend_only_list = GNUNET_notification_context_create(0); |
1282 | noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO"); | 1277 | noio = GNUNET_CONFIGURATION_get_value_yesno(cfg, "peerinfo", "NO_IO"); |
1283 | use_included = GNUNET_CONFIGURATION_get_value_yesno (cfg, | 1278 | use_included = GNUNET_CONFIGURATION_get_value_yesno(cfg, |
1284 | "peerinfo", | 1279 | "peerinfo", |
1285 | "USE_INCLUDED_HELLOS"); | 1280 | "USE_INCLUDED_HELLOS"); |
1286 | if (GNUNET_SYSERR == use_included) | 1281 | if (GNUNET_SYSERR == use_included) |
1287 | use_included = GNUNET_NO; | 1282 | use_included = GNUNET_NO; |
1288 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | 1283 | GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL); |
1289 | if (GNUNET_YES != noio) | 1284 | if (GNUNET_YES != noio) |
1290 | { | ||
1291 | GNUNET_assert ( | ||
1292 | GNUNET_OK == | ||
1293 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
1294 | "peerinfo", | ||
1295 | "HOSTS", | ||
1296 | &networkIdDirectory)); | ||
1297 | if (GNUNET_OK != GNUNET_DISK_directory_create (networkIdDirectory)) | ||
1298 | { | 1285 | { |
1299 | GNUNET_SCHEDULER_shutdown (); | 1286 | GNUNET_assert( |
1300 | return; | 1287 | GNUNET_OK == |
1301 | } | 1288 | GNUNET_CONFIGURATION_get_value_filename(cfg, |
1302 | 1289 | "peerinfo", | |
1303 | cron_scan = | 1290 | "HOSTS", |
1304 | GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, | 1291 | &networkIdDirectory)); |
1305 | &cron_scan_directory_data_hosts, | 1292 | if (GNUNET_OK != GNUNET_DISK_directory_create(networkIdDirectory)) |
1306 | NULL); | 1293 | { |
1307 | 1294 | GNUNET_SCHEDULER_shutdown(); | |
1308 | cron_clean = | 1295 | return; |
1309 | GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, | 1296 | } |
1310 | &cron_clean_data_hosts, | 1297 | |
1311 | NULL); | 1298 | cron_scan = |
1312 | if (GNUNET_YES == use_included) | 1299 | GNUNET_SCHEDULER_add_with_priority(GNUNET_SCHEDULER_PRIORITY_IDLE, |
1313 | { | 1300 | &cron_scan_directory_data_hosts, |
1314 | ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR); | 1301 | NULL); |
1315 | GNUNET_asprintf (&peerdir, "%shellos", ip); | 1302 | |
1316 | GNUNET_free (ip); | 1303 | cron_clean = |
1317 | 1304 | GNUNET_SCHEDULER_add_with_priority(GNUNET_SCHEDULER_PRIORITY_IDLE, | |
1318 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 1305 | &cron_clean_data_hosts, |
1319 | _ ("Importing HELLOs from `%s'\n"), | 1306 | NULL); |
1320 | peerdir); | 1307 | if (GNUNET_YES == use_included) |
1321 | dsc.matched = 0; | 1308 | { |
1322 | dsc.remove_files = GNUNET_NO; | 1309 | ip = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR); |
1323 | 1310 | GNUNET_asprintf(&peerdir, "%shellos", ip); | |
1324 | GNUNET_DISK_directory_scan (peerdir, | 1311 | GNUNET_free(ip); |
1325 | &hosts_directory_scan_callback, | 1312 | |
1326 | &dsc); | 1313 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, |
1327 | GNUNET_free (peerdir); | 1314 | _("Importing HELLOs from `%s'\n"), |
1328 | } | 1315 | peerdir); |
1329 | else | 1316 | dsc.matched = 0; |
1330 | { | 1317 | dsc.remove_files = GNUNET_NO; |
1331 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 1318 | |
1332 | _ ("Skipping import of included HELLOs\n")); | 1319 | GNUNET_DISK_directory_scan(peerdir, |
1320 | &hosts_directory_scan_callback, | ||
1321 | &dsc); | ||
1322 | GNUNET_free(peerdir); | ||
1323 | } | ||
1324 | else | ||
1325 | { | ||
1326 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, | ||
1327 | _("Skipping import of included HELLOs\n")); | ||
1328 | } | ||
1333 | } | 1329 | } |
1334 | } | ||
1335 | } | 1330 | } |
1336 | 1331 | ||
1337 | 1332 | ||
1338 | /** | 1333 | /** |
1339 | * Define "main" method using service macro. | 1334 | * Define "main" method using service macro. |
1340 | */ | 1335 | */ |
1341 | GNUNET_SERVICE_MAIN ( | 1336 | GNUNET_SERVICE_MAIN( |
1342 | "peerinfo", | 1337 | "peerinfo", |
1343 | GNUNET_SERVICE_OPTION_NONE, | 1338 | GNUNET_SERVICE_OPTION_NONE, |
1344 | &run, | 1339 | &run, |
1345 | &client_connect_cb, | 1340 | &client_connect_cb, |
1346 | &client_disconnect_cb, | 1341 | &client_disconnect_cb, |
1347 | NULL, | 1342 | NULL, |
1348 | GNUNET_MQ_hd_var_size (hello, | 1343 | GNUNET_MQ_hd_var_size(hello, |
1349 | GNUNET_MESSAGE_TYPE_HELLO, | 1344 | GNUNET_MESSAGE_TYPE_HELLO, |
1350 | struct GNUNET_HELLO_Message, | 1345 | struct GNUNET_HELLO_Message, |
1351 | NULL), | 1346 | NULL), |
1352 | GNUNET_MQ_hd_fixed_size (get, | 1347 | GNUNET_MQ_hd_fixed_size(get, |
1353 | GNUNET_MESSAGE_TYPE_PEERINFO_GET, | 1348 | GNUNET_MESSAGE_TYPE_PEERINFO_GET, |
1354 | struct ListPeerMessage, | 1349 | struct ListPeerMessage, |
1355 | NULL), | 1350 | NULL), |
1356 | GNUNET_MQ_hd_fixed_size (get_all, | 1351 | GNUNET_MQ_hd_fixed_size(get_all, |
1357 | GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL, | 1352 | GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL, |
1358 | struct ListAllPeersMessage, | 1353 | struct ListAllPeersMessage, |
1359 | NULL), | 1354 | NULL), |
1360 | GNUNET_MQ_hd_fixed_size (notify, | 1355 | GNUNET_MQ_hd_fixed_size(notify, |
1361 | GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY, | 1356 | GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY, |
1362 | struct NotifyMessage, | 1357 | struct NotifyMessage, |
1363 | NULL), | 1358 | NULL), |
1364 | GNUNET_MQ_handler_end ()); | 1359 | GNUNET_MQ_handler_end()); |
1365 | 1360 | ||
1366 | 1361 | ||
1367 | /* end of gnunet-service-peerinfo.c */ | 1362 | /* end of gnunet-service-peerinfo.c */ |