diff options
author | Julius Bünger <buenger@mytum.de> | 2016-05-12 08:58:24 +0000 |
---|---|---|
committer | Julius Bünger <buenger@mytum.de> | 2016-05-12 08:58:24 +0000 |
commit | 9f4d6acdbb43a43b648568dee596320601d04db4 (patch) | |
tree | 6b6a969e7a87bea0a7423371974dadfc55077428 /src/rps | |
parent | 2ffa3a95dae5f16350dadb1a42bf78cbd47d5e80 (diff) | |
download | gnunet-9f4d6acdbb43a43b648568dee596320601d04db4.tar.gz gnunet-9f4d6acdbb43a43b648568dee596320601d04db4.zip |
rps: keep track of valid peers in peermap
Diffstat (limited to 'src/rps')
-rw-r--r-- | src/rps/gnunet-service-rps.c | 7 | ||||
-rw-r--r-- | src/rps/gnunet-service-rps_peers.c | 142 | ||||
-rw-r--r-- | src/rps/gnunet-service-rps_peers.h | 20 | ||||
-rw-r--r-- | src/rps/test_service_rps_peers.c | 15 |
4 files changed, 158 insertions, 26 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c index 8f25b269d..8113e40b6 100644 --- a/src/rps/gnunet-service-rps.c +++ b/src/rps/gnunet-service-rps.c | |||
@@ -1385,7 +1385,7 @@ handle_peer_pull_reply (void *cls, | |||
1385 | /* Make sure we 'know' about this peer */ | 1385 | /* Make sure we 'know' about this peer */ |
1386 | (void) Peers_insert_peer_check_liveliness (&peers[i]); | 1386 | (void) Peers_insert_peer_check_liveliness (&peers[i]); |
1387 | 1387 | ||
1388 | if (GNUNET_YES == Peers_check_peer_flag (&peers[i], Peers_VALID)) | 1388 | if (GNUNET_YES == Peers_check_peer_valid (&peers[i])) |
1389 | { | 1389 | { |
1390 | CustomPeerMap_put (pull_map, &peers[i]); | 1390 | CustomPeerMap_put (pull_map, &peers[i]); |
1391 | } | 1391 | } |
@@ -1694,7 +1694,7 @@ do_mal_round (void *cls) | |||
1694 | * That is one push per round as it will ignore more. | 1694 | * That is one push per round as it will ignore more. |
1695 | */ | 1695 | */ |
1696 | Peers_insert_peer_check_liveliness (&attacked_peer); | 1696 | Peers_insert_peer_check_liveliness (&attacked_peer); |
1697 | if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_VALID)) | 1697 | if (GNUNET_YES == Peers_check_peer_valid (&attacked_peer)) |
1698 | send_push (&attacked_peer); | 1698 | send_push (&attacked_peer); |
1699 | } | 1699 | } |
1700 | 1700 | ||
@@ -1706,7 +1706,7 @@ do_mal_round (void *cls) | |||
1706 | if (GNUNET_YES == Peers_check_peer_known (&attacked_peer)) | 1706 | if (GNUNET_YES == Peers_check_peer_known (&attacked_peer)) |
1707 | { | 1707 | { |
1708 | Peers_insert_peer_check_liveliness (&attacked_peer); | 1708 | Peers_insert_peer_check_liveliness (&attacked_peer); |
1709 | if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_VALID)) | 1709 | if (GNUNET_YES == Peers_check_peer_valid (&attacked_peer)) |
1710 | { | 1710 | { |
1711 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1711 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1712 | "Goding to send push to attacked peer (%s)\n", | 1712 | "Goding to send push to attacked peer (%s)\n", |
@@ -2298,6 +2298,7 @@ run (void *cls, | |||
2298 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n"); | 2298 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n"); |
2299 | GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL); | 2299 | GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL); |
2300 | // TODO send push/pull to each of those peers? | 2300 | // TODO send push/pull to each of those peers? |
2301 | // TODO read stored valid peers from last run | ||
2301 | 2302 | ||
2302 | peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg, | 2303 | peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg, |
2303 | GNUNET_NO, | 2304 | GNUNET_NO, |
diff --git a/src/rps/gnunet-service-rps_peers.c b/src/rps/gnunet-service-rps_peers.c index 33430e51f..a6cd1b839 100644 --- a/src/rps/gnunet-service-rps_peers.c +++ b/src/rps/gnunet-service-rps_peers.c | |||
@@ -203,6 +203,16 @@ struct PeerContext | |||
203 | */ | 203 | */ |
204 | }; | 204 | }; |
205 | 205 | ||
206 | /** | ||
207 | * @brief Hashmap of valid peers. | ||
208 | */ | ||
209 | static struct GNUNET_CONTAINER_MultiPeerMap *valid_peers; | ||
210 | |||
211 | /** | ||
212 | * @brief Maximum number of valid peers to keep. | ||
213 | * TODO read from config | ||
214 | */ | ||
215 | static uint32_t num_valid_peers_max = UINT32_MAX; | ||
206 | 216 | ||
207 | /** | 217 | /** |
208 | * Set of all peers to keep track of them. | 218 | * Set of all peers to keep track of them. |
@@ -317,10 +327,112 @@ Peers_check_connected (const struct GNUNET_PeerIdentity *peer) | |||
317 | } | 327 | } |
318 | 328 | ||
319 | /** | 329 | /** |
330 | * @brief The closure to #get_rand_peer_iterator. | ||
331 | */ | ||
332 | struct GetRandPeerIteratorCls | ||
333 | { | ||
334 | /** | ||
335 | * @brief The index of the peer to return. | ||
336 | * Will be decreased until 0. | ||
337 | * Then current peer is returned. | ||
338 | */ | ||
339 | uint32_t index; | ||
340 | |||
341 | /** | ||
342 | * @brief Pointer to peer to return. | ||
343 | */ | ||
344 | const struct GNUNET_PeerIdentity *peer; | ||
345 | }; | ||
346 | |||
347 | /** | ||
348 | * @brief Iterator function for #get_random_peer_from_peermap. | ||
349 | * | ||
350 | * Implements #GNUNET_CONTAINER_PeerMapIterator. | ||
351 | * Decreases the index until the index is null. | ||
352 | * Then returns the current peer. | ||
353 | * | ||
354 | * @param cls the #GetRandPeerIteratorCls containing index and peer | ||
355 | * @param peer current peer | ||
356 | * @param value unused | ||
357 | * | ||
358 | * @return #GNUNET_YES if we should continue to | ||
359 | * iterate, | ||
360 | * #GNUNET_NO if not. | ||
361 | */ | ||
362 | static int | ||
363 | get_rand_peer_iterator (void *cls, | ||
364 | const struct GNUNET_PeerIdentity *peer, | ||
365 | void *value) | ||
366 | { | ||
367 | struct GetRandPeerIteratorCls *iterator_cls = cls; | ||
368 | if (0 >= iterator_cls->index) | ||
369 | { | ||
370 | iterator_cls->peer = peer; | ||
371 | return GNUNET_NO; | ||
372 | } | ||
373 | iterator_cls->index--; | ||
374 | return GNUNET_YES; | ||
375 | } | ||
376 | |||
377 | /** | ||
378 | * @brief Get a random peer from @a peer_map | ||
379 | * | ||
380 | * @param peer_map the peer_map to get the peer from | ||
381 | * | ||
382 | * @return a random peer | ||
383 | */ | ||
384 | static const struct GNUNET_PeerIdentity * | ||
385 | get_random_peer_from_peermap (const struct | ||
386 | GNUNET_CONTAINER_MultiPeerMap *peer_map) | ||
387 | { | ||
388 | uint32_t rand_index; | ||
389 | struct GetRandPeerIteratorCls *iterator_cls; | ||
390 | const struct GNUNET_PeerIdentity *ret; | ||
391 | |||
392 | iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls); | ||
393 | iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
394 | GNUNET_CONTAINER_multipeermap_size (peer_map)); | ||
395 | (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers, | ||
396 | get_rand_peer_iterator, | ||
397 | iterator_cls); | ||
398 | ret = iterator_cls->peer; | ||
399 | GNUNET_free (iterator_cls); | ||
400 | return ret; | ||
401 | } | ||
402 | |||
403 | /** | ||
404 | * @brief Add a given @a peer to valid peers. | ||
405 | * | ||
406 | * If valid peers are already #num_valid_peers_max, delete a peer previously. | ||
407 | * | ||
408 | * @param peer the peer that is added to the valid peers. | ||
409 | * | ||
410 | * @return #GNUNET_YES if no other peer had to be removed | ||
411 | * #GNUNET_NO otherwise | ||
412 | */ | ||
413 | static int | ||
414 | add_valid_peer (const struct GNUNET_PeerIdentity *peer) | ||
415 | { | ||
416 | const struct GNUNET_PeerIdentity *rand_peer; | ||
417 | int ret; | ||
418 | |||
419 | ret = GNUNET_YES; | ||
420 | while (GNUNET_CONTAINER_multipeermap_size (valid_peers) >= num_valid_peers_max) | ||
421 | { | ||
422 | rand_peer = get_random_peer_from_peermap (valid_peers); | ||
423 | GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer); | ||
424 | ret = GNUNET_NO; | ||
425 | } | ||
426 | (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL, | ||
427 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
428 | return ret; | ||
429 | } | ||
430 | |||
431 | /** | ||
320 | * @brief Set the peer flag to living and | 432 | * @brief Set the peer flag to living and |
321 | * call the pending operations on this peer. | 433 | * call the pending operations on this peer. |
322 | * | 434 | * |
323 | * Also sets the #Peers_VALID flag | 435 | * Also adds peer to #valid_peers. |
324 | * | 436 | * |
325 | * @param peer_ctx the #PeerContext of the peer to set live | 437 | * @param peer_ctx the #PeerContext of the peer to set live |
326 | */ | 438 | */ |
@@ -338,7 +450,7 @@ set_peer_live (struct PeerContext *peer_ctx) | |||
338 | } | 450 | } |
339 | 451 | ||
340 | peer = &peer_ctx->peer_id; | 452 | peer = &peer_ctx->peer_id; |
341 | set_peer_flag (peer_ctx, Peers_VALID); | 453 | (void) add_valid_peer (peer); |
342 | set_peer_flag (peer_ctx, Peers_ONLINE); | 454 | set_peer_flag (peer_ctx, Peers_ONLINE); |
343 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 455 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
344 | "Peer %s is live and valid\n", | 456 | "Peer %s is live and valid\n", |
@@ -606,8 +718,11 @@ Peers_initialise (struct GNUNET_CADET_Handle *cadet_h, | |||
606 | cadet_handle = cadet_h; | 718 | cadet_handle = cadet_h; |
607 | own_identity = own_id; | 719 | own_identity = own_id; |
608 | peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); | 720 | peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); |
721 | valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); | ||
609 | } | 722 | } |
610 | 723 | ||
724 | // TODO read stored valid peers | ||
725 | |||
611 | /** | 726 | /** |
612 | * @brief Delete storage of peers that was created with #Peers_initialise () | 727 | * @brief Delete storage of peers that was created with #Peers_initialise () |
613 | */ | 728 | */ |
@@ -623,8 +738,11 @@ Peers_terminate () | |||
623 | "Iteration destroying peers was aborted.\n"); | 738 | "Iteration destroying peers was aborted.\n"); |
624 | } | 739 | } |
625 | GNUNET_CONTAINER_multipeermap_destroy (peer_map); | 740 | GNUNET_CONTAINER_multipeermap_destroy (peer_map); |
741 | GNUNET_CONTAINER_multipeermap_destroy (valid_peers); | ||
626 | } | 742 | } |
627 | 743 | ||
744 | // TODO store valid peers | ||
745 | |||
628 | /** | 746 | /** |
629 | * @brief Add peer to known peers. | 747 | * @brief Add peer to known peers. |
630 | * | 748 | * |
@@ -672,7 +790,7 @@ Peers_insert_peer_check_liveliness (const struct GNUNET_PeerIdentity *peer) | |||
672 | return ret; | 790 | return ret; |
673 | } | 791 | } |
674 | peer_ctx = get_peer_ctx (peer); | 792 | peer_ctx = get_peer_ctx (peer); |
675 | if (GNUNET_NO == check_peer_flag_set (peer_ctx, Peers_VALID)) | 793 | if (GNUNET_NO == Peers_check_peer_valid (peer)) |
676 | { | 794 | { |
677 | check_peer_live (peer_ctx); | 795 | check_peer_live (peer_ctx); |
678 | } | 796 | } |
@@ -875,6 +993,8 @@ Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags | |||
875 | /** | 993 | /** |
876 | * @brief Check whether we have information about the given peer. | 994 | * @brief Check whether we have information about the given peer. |
877 | * | 995 | * |
996 | * FIXME probably deprecated. Make this the new _online. | ||
997 | * | ||
878 | * @param peer peer in question | 998 | * @param peer peer in question |
879 | * | 999 | * |
880 | * @return #GNUNET_YES if peer is known | 1000 | * @return #GNUNET_YES if peer is known |
@@ -887,6 +1007,22 @@ Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer) | |||
887 | } | 1007 | } |
888 | 1008 | ||
889 | /** | 1009 | /** |
1010 | * @brief Check whether @a peer is actually a peer. | ||
1011 | * | ||
1012 | * A valid peer is a peer that we know exists eg. we were connected to once. | ||
1013 | * | ||
1014 | * @param peer peer in question | ||
1015 | * | ||
1016 | * @return #GNUNET_YES if peer is valid | ||
1017 | * #GNUNET_NO if peer is not valid | ||
1018 | */ | ||
1019 | int | ||
1020 | Peers_check_peer_valid (const struct GNUNET_PeerIdentity *peer) | ||
1021 | { | ||
1022 | return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer); | ||
1023 | } | ||
1024 | |||
1025 | /** | ||
890 | * @brief Indicate that we want to send to the other peer | 1026 | * @brief Indicate that we want to send to the other peer |
891 | * | 1027 | * |
892 | * This establishes a sending channel | 1028 | * This establishes a sending channel |
diff --git a/src/rps/gnunet-service-rps_peers.h b/src/rps/gnunet-service-rps_peers.h index 06e6fa89e..79db8185b 100644 --- a/src/rps/gnunet-service-rps_peers.h +++ b/src/rps/gnunet-service-rps_peers.h | |||
@@ -43,11 +43,6 @@ enum Peers_PeerFlags | |||
43 | /* IN_OWN_GOSSIP_LIST = 0x08, unneeded? */ | 43 | /* IN_OWN_GOSSIP_LIST = 0x08, unneeded? */ |
44 | 44 | ||
45 | /** | 45 | /** |
46 | * We set this bit when we can be sure the other peer is/was live. | ||
47 | */ | ||
48 | Peers_VALID = 0x10, | ||
49 | |||
50 | /** | ||
51 | * We set this bit when we know the peer is online. | 46 | * We set this bit when we know the peer is online. |
52 | */ | 47 | */ |
53 | Peers_ONLINE = 0x20, | 48 | Peers_ONLINE = 0x20, |
@@ -236,6 +231,8 @@ Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags | |||
236 | /** | 231 | /** |
237 | * @brief Check whether we have information about the given peer. | 232 | * @brief Check whether we have information about the given peer. |
238 | * | 233 | * |
234 | * FIXME probably deprecated. Make this the new _online. | ||
235 | * | ||
239 | * @param peer peer in question | 236 | * @param peer peer in question |
240 | * | 237 | * |
241 | * @return #GNUNET_YES if peer is known | 238 | * @return #GNUNET_YES if peer is known |
@@ -245,6 +242,19 @@ int | |||
245 | Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer); | 242 | Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer); |
246 | 243 | ||
247 | /** | 244 | /** |
245 | * @brief Check whether @a peer is actually a peer. | ||
246 | * | ||
247 | * A valid peer is a peer that we know exists eg. we were connected to once. | ||
248 | * | ||
249 | * @param peer peer in question | ||
250 | * | ||
251 | * @return #GNUNET_YES if peer is valid | ||
252 | * #GNUNET_NO if peer is not valid | ||
253 | */ | ||
254 | int | ||
255 | Peers_check_peer_valid (const struct GNUNET_PeerIdentity *peer); | ||
256 | |||
257 | /** | ||
248 | * @brief Indicate that we want to send to the other peer | 258 | * @brief Indicate that we want to send to the other peer |
249 | * | 259 | * |
250 | * This establishes a sending channel | 260 | * This establishes a sending channel |
diff --git a/src/rps/test_service_rps_peers.c b/src/rps/test_service_rps_peers.c index f5191facc..264a63e41 100644 --- a/src/rps/test_service_rps_peers.c +++ b/src/rps/test_service_rps_peers.c | |||
@@ -93,30 +93,15 @@ check () | |||
93 | Peers_insert_peer (&k1); | 93 | Peers_insert_peer (&k1); |
94 | 94 | ||
95 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_PULL_REPLY_PENDING)); | 95 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_PULL_REPLY_PENDING)); |
96 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_VALID)); | ||
97 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_ONLINE)); | 96 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_ONLINE)); |
98 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_TO_DESTROY)); | 97 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_TO_DESTROY)); |
99 | 98 | ||
100 | Peers_set_peer_flag (&k1, Peers_VALID); | ||
101 | CHECK (GNUNET_YES == Peers_check_peer_flag (&k1, Peers_VALID)); | ||
102 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_ONLINE)); | 99 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_ONLINE)); |
103 | Peers_unset_peer_flag (&k1, Peers_VALID); | ||
104 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_VALID)); | ||
105 | 100 | ||
106 | Peers_set_peer_flag (&k1, Peers_VALID); | ||
107 | Peers_set_peer_flag (&k1, Peers_ONLINE); | 101 | Peers_set_peer_flag (&k1, Peers_ONLINE); |
108 | CHECK (GNUNET_YES == Peers_check_peer_flag (&k1, Peers_VALID)); | ||
109 | CHECK (GNUNET_YES == Peers_check_peer_flag (&k1, Peers_ONLINE)); | 102 | CHECK (GNUNET_YES == Peers_check_peer_flag (&k1, Peers_ONLINE)); |
110 | CHECK (GNUNET_YES == Peers_check_peer_flag (&k1, | ||
111 | Peers_ONLINE | Peers_VALID)); | ||
112 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_TO_DESTROY)); | 103 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_TO_DESTROY)); |
113 | Peers_unset_peer_flag (&k1, Peers_VALID); | ||
114 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_VALID)); | ||
115 | CHECK (GNUNET_YES == Peers_check_peer_flag (&k1, Peers_ONLINE)); | 104 | CHECK (GNUNET_YES == Peers_check_peer_flag (&k1, Peers_ONLINE)); |
116 | CHECK (GNUNET_YES == Peers_check_peer_flag (&k1, | ||
117 | Peers_ONLINE | Peers_VALID)); | ||
118 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, | ||
119 | Peers_ONLINE & Peers_VALID)); | ||
120 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_TO_DESTROY)); | 105 | CHECK (GNUNET_NO == Peers_check_peer_flag (&k1, Peers_TO_DESTROY)); |
121 | 106 | ||
122 | /* Check send intention */ | 107 | /* Check send intention */ |