diff options
Diffstat (limited to 'src/peerinfo/gnunet-service-peerinfo.c')
-rw-r--r-- | src/peerinfo/gnunet-service-peerinfo.c | 181 |
1 files changed, 9 insertions, 172 deletions
diff --git a/src/peerinfo/gnunet-service-peerinfo.c b/src/peerinfo/gnunet-service-peerinfo.c index 692f71e9b..ee8749955 100644 --- a/src/peerinfo/gnunet-service-peerinfo.c +++ b/src/peerinfo/gnunet-service-peerinfo.c | |||
@@ -23,7 +23,7 @@ | |||
23 | * @brief maintains list of known peers | 23 | * @brief maintains list of known peers |
24 | * | 24 | * |
25 | * Code to maintain the list of currently known hosts (in memory | 25 | * Code to maintain the list of currently known hosts (in memory |
26 | * structure of data/hosts/ and data/credit/). | 26 | * structure of data/hosts/). |
27 | * | 27 | * |
28 | * @author Christian Grothoff | 28 | * @author Christian Grothoff |
29 | * | 29 | * |
@@ -46,11 +46,6 @@ | |||
46 | #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) | 46 | #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * How often do we flush trust values to disk? | ||
50 | */ | ||
51 | #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) | ||
52 | |||
53 | /** | ||
54 | * How often do we discard old entries in data/hosts/? | 49 | * How often do we discard old entries in data/hosts/? |
55 | */ | 50 | */ |
56 | #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60) | 51 | #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60) |
@@ -76,16 +71,6 @@ struct HostEntry | |||
76 | */ | 71 | */ |
77 | struct GNUNET_HELLO_Message *hello; | 72 | struct GNUNET_HELLO_Message *hello; |
78 | 73 | ||
79 | /** | ||
80 | * Trust rating for this peer | ||
81 | */ | ||
82 | uint32_t trust; | ||
83 | |||
84 | /** | ||
85 | * Trust rating for this peer on disk. | ||
86 | */ | ||
87 | uint32_t disk_trust; | ||
88 | |||
89 | }; | 74 | }; |
90 | 75 | ||
91 | 76 | ||
@@ -105,11 +90,6 @@ static struct GNUNET_SERVER_NotificationContext *notify_list; | |||
105 | static char *networkIdDirectory; | 90 | static char *networkIdDirectory; |
106 | 91 | ||
107 | /** | 92 | /** |
108 | * Where do we store trust information? | ||
109 | */ | ||
110 | static char *trustDirectory; | ||
111 | |||
112 | /** | ||
113 | * Handle for reporting statistics. | 93 | * Handle for reporting statistics. |
114 | */ | 94 | */ |
115 | static struct GNUNET_STATISTICS_Handle *stats; | 95 | static struct GNUNET_STATISTICS_Handle *stats; |
@@ -129,7 +109,6 @@ make_info_message (const struct HostEntry *he) | |||
129 | im = GNUNET_malloc (sizeof (struct InfoMessage) + hs); | 109 | im = GNUNET_malloc (sizeof (struct InfoMessage) + hs); |
130 | im->header.size = htons (hs + sizeof (struct InfoMessage)); | 110 | im->header.size = htons (hs + sizeof (struct InfoMessage)); |
131 | im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO); | 111 | im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO); |
132 | im->trust = htonl (he->trust); | ||
133 | im->peer = he->identity; | 112 | im->peer = he->identity; |
134 | if (he->hello != NULL) | 113 | if (he->hello != NULL) |
135 | memcpy (&im[1], he->hello, hs); | 114 | memcpy (&im[1], he->hello, hs); |
@@ -184,25 +163,7 @@ get_host_filename (const struct GNUNET_PeerIdentity *id) | |||
184 | 163 | ||
185 | 164 | ||
186 | /** | 165 | /** |
187 | * Get the filename under which we would store the GNUNET_HELLO_Message | 166 | * Find the host entry for the given peer. FIXME: replace by hash map! |
188 | * for the given host and protocol. | ||
189 | * @return filename of the form DIRECTORY/HOSTID | ||
190 | */ | ||
191 | static char * | ||
192 | get_trust_filename (const struct GNUNET_PeerIdentity *id) | ||
193 | { | ||
194 | struct GNUNET_CRYPTO_HashAsciiEncoded fil; | ||
195 | char *fn; | ||
196 | |||
197 | GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil); | ||
198 | GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil); | ||
199 | return fn; | ||
200 | } | ||
201 | |||
202 | |||
203 | /** | ||
204 | * Find the host entry for the given peer. Call | ||
205 | * only when synchronized! | ||
206 | * @return NULL if not found | 167 | * @return NULL if not found |
207 | */ | 168 | */ |
208 | static struct HostEntry * | 169 | static struct HostEntry * |
@@ -247,13 +208,12 @@ static void | |||
247 | add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity) | 208 | add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity) |
248 | { | 209 | { |
249 | struct HostEntry *entry; | 210 | struct HostEntry *entry; |
250 | char *fn; | ||
251 | uint32_t trust; | ||
252 | char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1]; | 211 | char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1]; |
253 | const struct GNUNET_HELLO_Message *hello; | 212 | const struct GNUNET_HELLO_Message *hello; |
254 | struct GNUNET_HELLO_Message *hello_clean; | 213 | struct GNUNET_HELLO_Message *hello_clean; |
255 | int size; | 214 | int size; |
256 | struct GNUNET_TIME_Absolute now; | 215 | struct GNUNET_TIME_Absolute now; |
216 | char *fn; | ||
257 | 217 | ||
258 | entry = lookup_host_entry (identity); | 218 | entry = lookup_host_entry (identity); |
259 | if (entry != NULL) | 219 | if (entry != NULL) |
@@ -264,11 +224,6 @@ add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity) | |||
264 | GNUNET_NO); | 224 | GNUNET_NO); |
265 | entry = GNUNET_malloc (sizeof (struct HostEntry)); | 225 | entry = GNUNET_malloc (sizeof (struct HostEntry)); |
266 | entry->identity = *identity; | 226 | entry->identity = *identity; |
267 | fn = get_trust_filename (identity); | ||
268 | if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) && | ||
269 | (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust)))) | ||
270 | entry->disk_trust = entry->trust = ntohl (trust); | ||
271 | GNUNET_free (fn); | ||
272 | 227 | ||
273 | fn = get_host_filename (identity); | 228 | fn = get_host_filename (identity); |
274 | if (GNUNET_DISK_file_test (fn) == GNUNET_YES) | 229 | if (GNUNET_DISK_file_test (fn) == GNUNET_YES) |
@@ -302,56 +257,6 @@ add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity) | |||
302 | 257 | ||
303 | 258 | ||
304 | /** | 259 | /** |
305 | * Increase the host credit by a value. | ||
306 | * | ||
307 | * @param hostId is the identity of the host | ||
308 | * @param value is the int value by which the | ||
309 | * host credit is to be increased or decreased | ||
310 | * @returns the actual change in trust (positive or negative) | ||
311 | */ | ||
312 | static int | ||
313 | change_host_trust (const struct GNUNET_PeerIdentity *hostId, int value) | ||
314 | { | ||
315 | struct HostEntry *host; | ||
316 | unsigned int old_trust; | ||
317 | |||
318 | if (value == 0) | ||
319 | return 0; | ||
320 | host = lookup_host_entry (hostId); | ||
321 | if (host == NULL) | ||
322 | { | ||
323 | add_host_to_known_hosts (hostId); | ||
324 | host = lookup_host_entry (hostId); | ||
325 | } | ||
326 | GNUNET_assert (host != NULL); | ||
327 | old_trust = host->trust; | ||
328 | if (value > 0) | ||
329 | { | ||
330 | if (host->trust + value < host->trust) | ||
331 | { | ||
332 | value = UINT32_MAX - host->trust; | ||
333 | host->trust = UINT32_MAX; | ||
334 | } | ||
335 | else | ||
336 | host->trust += value; | ||
337 | } | ||
338 | else | ||
339 | { | ||
340 | if (host->trust < -value) | ||
341 | { | ||
342 | value = -host->trust; | ||
343 | host->trust = 0; | ||
344 | } | ||
345 | else | ||
346 | host->trust += value; | ||
347 | } | ||
348 | if (host->trust != old_trust) | ||
349 | notify_all (host); | ||
350 | return value; | ||
351 | } | ||
352 | |||
353 | |||
354 | /** | ||
355 | * Remove a file that should not be there. LOG | 260 | * Remove a file that should not be there. LOG |
356 | * success or failure. | 261 | * success or failure. |
357 | */ | 262 | */ |
@@ -480,16 +385,14 @@ bind_address (const struct GNUNET_PeerIdentity *peer, | |||
480 | 385 | ||
481 | /** | 386 | /** |
482 | * Do transmit info either for only the host matching the given | 387 | * Do transmit info either for only the host matching the given |
483 | * argument or for all known hosts and change their trust values by | 388 | * argument or for all known hosts. |
484 | * the given delta. | ||
485 | * | 389 | * |
486 | * @param only NULL to hit all hosts, otherwise specifies a particular target | 390 | * @param only NULL to hit all hosts, otherwise specifies a particular target |
487 | * @param trust_change how much should the trust be changed | ||
488 | * @param client who is making the request (and will thus receive our confirmation) | 391 | * @param client who is making the request (and will thus receive our confirmation) |
489 | */ | 392 | */ |
490 | static void | 393 | static void |
491 | send_to_each_host (const struct GNUNET_PeerIdentity *only, | 394 | send_to_each_host (const struct GNUNET_PeerIdentity *only, |
492 | int trust_change, struct GNUNET_SERVER_Client *client) | 395 | struct GNUNET_SERVER_Client *client) |
493 | { | 396 | { |
494 | struct HostEntry *pos; | 397 | struct HostEntry *pos; |
495 | struct InfoMessage *im; | 398 | struct InfoMessage *im; |
@@ -508,7 +411,6 @@ send_to_each_host (const struct GNUNET_PeerIdentity *only, | |||
508 | memcmp (only, &pos->identity, | 411 | memcmp (only, &pos->identity, |
509 | sizeof (struct GNUNET_PeerIdentity)))) | 412 | sizeof (struct GNUNET_PeerIdentity)))) |
510 | { | 413 | { |
511 | change_host_trust (&pos->identity, trust_change); | ||
512 | hs = 0; | 414 | hs = 0; |
513 | im = (struct InfoMessage *) buf; | 415 | im = (struct InfoMessage *) buf; |
514 | if (pos->hello != NULL) | 416 | if (pos->hello != NULL) |
@@ -522,7 +424,7 @@ send_to_each_host (const struct GNUNET_PeerIdentity *only, | |||
522 | } | 424 | } |
523 | im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO); | 425 | im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO); |
524 | im->header.size = htons (sizeof (struct InfoMessage) + hs); | 426 | im->header.size = htons (sizeof (struct InfoMessage) + hs); |
525 | im->trust = htonl (pos->trust); | 427 | im->reserved = htonl (0); |
526 | im->peer = pos->identity; | 428 | im->peer = pos->identity; |
527 | GNUNET_SERVER_transmit_context_append_message (tc, | 429 | GNUNET_SERVER_transmit_context_append_message (tc, |
528 | &im->header); | 430 | &im->header); |
@@ -542,58 +444,6 @@ send_to_each_host (const struct GNUNET_PeerIdentity *only, | |||
542 | 444 | ||
543 | 445 | ||
544 | /** | 446 | /** |
545 | * Write host-trust information to a file - flush the buffer entry! | ||
546 | * Assumes synchronized access. | ||
547 | */ | ||
548 | static void | ||
549 | flush_trust (struct HostEntry *host) | ||
550 | { | ||
551 | char *fn; | ||
552 | uint32_t trust; | ||
553 | |||
554 | if (host->trust == host->disk_trust) | ||
555 | return; /* unchanged */ | ||
556 | fn = get_trust_filename (&host->identity); | ||
557 | if (host->trust == 0) | ||
558 | { | ||
559 | if ((0 != UNLINK (fn)) && (errno != ENOENT)) | ||
560 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | | ||
561 | GNUNET_ERROR_TYPE_BULK, "unlink", fn); | ||
562 | } | ||
563 | else | ||
564 | { | ||
565 | trust = htonl (host->trust); | ||
566 | if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust, | ||
567 | sizeof(uint32_t), | ||
568 | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE | ||
569 | | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ)) | ||
570 | host->disk_trust = host->trust; | ||
571 | } | ||
572 | GNUNET_free (fn); | ||
573 | } | ||
574 | |||
575 | /** | ||
576 | * Call this method periodically to scan data/hosts for new hosts. | ||
577 | */ | ||
578 | static void | ||
579 | cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
580 | { | ||
581 | struct HostEntry *pos; | ||
582 | |||
583 | pos = hosts; | ||
584 | while (pos != NULL) | ||
585 | { | ||
586 | flush_trust (pos); | ||
587 | pos = pos->next; | ||
588 | } | ||
589 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
590 | return; | ||
591 | GNUNET_SCHEDULER_add_delayed (tc->sched, | ||
592 | TRUST_FLUSH_FREQ, &cron_flush_trust, NULL); | ||
593 | } | ||
594 | |||
595 | |||
596 | /** | ||
597 | * @brief delete expired HELLO entries in data/hosts/ | 447 | * @brief delete expired HELLO entries in data/hosts/ |
598 | */ | 448 | */ |
599 | static int | 449 | static int |
@@ -711,7 +561,7 @@ handle_get (void *cls, | |||
711 | "GET", | 561 | "GET", |
712 | GNUNET_i2s (&lpm->peer)); | 562 | GNUNET_i2s (&lpm->peer)); |
713 | #endif | 563 | #endif |
714 | send_to_each_host (&lpm->peer, ntohl (lpm->trust_change), client); | 564 | send_to_each_host (&lpm->peer, client); |
715 | } | 565 | } |
716 | 566 | ||
717 | 567 | ||
@@ -727,15 +577,12 @@ handle_get_all (void *cls, | |||
727 | struct GNUNET_SERVER_Client *client, | 577 | struct GNUNET_SERVER_Client *client, |
728 | const struct GNUNET_MessageHeader *message) | 578 | const struct GNUNET_MessageHeader *message) |
729 | { | 579 | { |
730 | const struct ListAllPeersMessage *lpm; | ||
731 | |||
732 | lpm = (const struct ListAllPeersMessage *) message; | ||
733 | #if DEBUG_PEERINFO | 580 | #if DEBUG_PEERINFO |
734 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 581 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
735 | "`%s' message received\n", | 582 | "`%s' message received\n", |
736 | "GET_ALL"); | 583 | "GET_ALL"); |
737 | #endif | 584 | #endif |
738 | send_to_each_host (NULL, ntohl (lpm->trust_change), client); | 585 | send_to_each_host (NULL, client); |
739 | } | 586 | } |
740 | 587 | ||
741 | 588 | ||
@@ -814,7 +661,7 @@ run (void *cls, | |||
814 | {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET, | 661 | {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET, |
815 | sizeof (struct ListPeerMessage)}, | 662 | sizeof (struct ListPeerMessage)}, |
816 | {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL, | 663 | {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL, |
817 | sizeof (struct ListAllPeersMessage)}, | 664 | sizeof (struct GNUNET_MessageHeader)}, |
818 | {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY, | 665 | {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY, |
819 | sizeof (struct GNUNET_MessageHeader)}, | 666 | sizeof (struct GNUNET_MessageHeader)}, |
820 | {NULL, NULL, 0, 0} | 667 | {NULL, NULL, 0, 0} |
@@ -826,20 +673,11 @@ run (void *cls, | |||
826 | "peerinfo", | 673 | "peerinfo", |
827 | "HOSTS", | 674 | "HOSTS", |
828 | &networkIdDirectory)); | 675 | &networkIdDirectory)); |
829 | GNUNET_assert (GNUNET_OK == | ||
830 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
831 | "peerinfo", | ||
832 | "TRUST", | ||
833 | &trustDirectory)); | ||
834 | GNUNET_DISK_directory_create (networkIdDirectory); | 676 | GNUNET_DISK_directory_create (networkIdDirectory); |
835 | GNUNET_DISK_directory_create (trustDirectory); | ||
836 | GNUNET_SCHEDULER_add_with_priority (sched, | 677 | GNUNET_SCHEDULER_add_with_priority (sched, |
837 | GNUNET_SCHEDULER_PRIORITY_IDLE, | 678 | GNUNET_SCHEDULER_PRIORITY_IDLE, |
838 | &cron_scan_directory_data_hosts, NULL); | 679 | &cron_scan_directory_data_hosts, NULL); |
839 | GNUNET_SCHEDULER_add_with_priority (sched, | 680 | GNUNET_SCHEDULER_add_with_priority (sched, |
840 | GNUNET_SCHEDULER_PRIORITY_HIGH, | ||
841 | &cron_flush_trust, NULL); | ||
842 | GNUNET_SCHEDULER_add_with_priority (sched, | ||
843 | GNUNET_SCHEDULER_PRIORITY_IDLE, | 681 | GNUNET_SCHEDULER_PRIORITY_IDLE, |
844 | &cron_clean_data_hosts, NULL); | 682 | &cron_clean_data_hosts, NULL); |
845 | GNUNET_SCHEDULER_add_delayed (sched, | 683 | GNUNET_SCHEDULER_add_delayed (sched, |
@@ -868,7 +706,6 @@ main (int argc, char *const *argv) | |||
868 | GNUNET_SERVICE_OPTION_NONE, | 706 | GNUNET_SERVICE_OPTION_NONE, |
869 | &run, NULL)) ? 0 : 1; | 707 | &run, NULL)) ? 0 : 1; |
870 | GNUNET_free_non_null (networkIdDirectory); | 708 | GNUNET_free_non_null (networkIdDirectory); |
871 | GNUNET_free_non_null (trustDirectory); | ||
872 | return ret; | 709 | return ret; |
873 | } | 710 | } |
874 | 711 | ||