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