aboutsummaryrefslogtreecommitdiff
path: root/src/rps
diff options
context:
space:
mode:
authorJulius Bünger <buenger@mytum.de>2016-05-13 22:49:10 +0000
committerJulius Bünger <buenger@mytum.de>2016-05-13 22:49:10 +0000
commitc771f534d05d56a44da6390e1409d30fec438e5a (patch)
tree0d3ff934ffe65e8860a33b90b2df0e00f0924008 /src/rps
parentd150cff9fc1c80ea458618cc39497bdc301a09fc (diff)
downloadgnunet-c771f534d05d56a44da6390e1409d30fec438e5a.tar.gz
gnunet-c771f534d05d56a44da6390e1409d30fec438e5a.zip
rps: store valid peer ids in file
Diffstat (limited to 'src/rps')
-rw-r--r--src/rps/gnunet-service-rps.c15
-rw-r--r--src/rps/gnunet-service-rps_peers.c223
-rw-r--r--src/rps/gnunet-service-rps_peers.h4
-rw-r--r--src/rps/test_rps.conf1
-rw-r--r--src/rps/test_service_rps_peers.c8
5 files changed, 241 insertions, 10 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
index 38a644b01..822c5e655 100644
--- a/src/rps/gnunet-service-rps.c
+++ b/src/rps/gnunet-service-rps.c
@@ -2183,6 +2183,7 @@ run (void *cls,
2183{ 2183{
2184 int size; 2184 int size;
2185 int out_size; 2185 int out_size;
2186 char* fn_valid_peers;
2186 2187
2187 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL); 2188 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2188 cfg = c; 2189 cfg = c;
@@ -2223,6 +2224,16 @@ run (void *cls,
2223 } 2224 }
2224 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need); 2225 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need);
2225 2226
2227 if (GNUNET_OK !=
2228 GNUNET_CONFIGURATION_get_value_filename (cfg,
2229 "rps",
2230 "FILENAME_VALID_PEERS",
2231 &fn_valid_peers))
2232 {
2233 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2234 "rps", "FILENAME_VALID_PEERS");
2235 }
2236
2226 2237
2227 View_create (4); 2238 View_create (4);
2228 2239
@@ -2273,8 +2284,10 @@ run (void *cls,
2273 &cleanup_destroyed_channel, 2284 &cleanup_destroyed_channel,
2274 cadet_handlers, 2285 cadet_handlers,
2275 ports); 2286 ports);
2287
2276 peerinfo_handle = GNUNET_PEERINFO_connect (cfg); 2288 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2277 Peers_initialise (cadet_handle, &own_identity); 2289 Peers_initialise (fn_valid_peers, cadet_handle, &own_identity);
2290 GNUNET_free (fn_valid_peers);
2278 2291
2279 /* Initialise sampler */ 2292 /* Initialise sampler */
2280 struct GNUNET_TIME_Relative half_round_interval; 2293 struct GNUNET_TIME_Relative half_round_interval;
diff --git a/src/rps/gnunet-service-rps_peers.c b/src/rps/gnunet-service-rps_peers.c
index 70ae30f76..2bbb8d705 100644
--- a/src/rps/gnunet-service-rps_peers.c
+++ b/src/rps/gnunet-service-rps_peers.c
@@ -215,6 +215,11 @@ static struct GNUNET_CONTAINER_MultiPeerMap *valid_peers;
215static uint32_t num_valid_peers_max = UINT32_MAX; 215static uint32_t num_valid_peers_max = UINT32_MAX;
216 216
217/** 217/**
218 * @brief Filename of the file that stores the valid peers persistently.
219 */
220static char *filename_valid_peers;
221
222/**
218 * Set of all peers to keep track of them. 223 * Set of all peers to keep track of them.
219 */ 224 */
220static struct GNUNET_CONTAINER_MultiPeerMap *peer_map; 225static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
@@ -385,7 +390,6 @@ static const struct GNUNET_PeerIdentity *
385get_random_peer_from_peermap (const struct 390get_random_peer_from_peermap (const struct
386 GNUNET_CONTAINER_MultiPeerMap *peer_map) 391 GNUNET_CONTAINER_MultiPeerMap *peer_map)
387{ 392{
388 uint32_t rand_index;
389 struct GetRandPeerIteratorCls *iterator_cls; 393 struct GetRandPeerIteratorCls *iterator_cls;
390 const struct GNUNET_PeerIdentity *ret; 394 const struct GNUNET_PeerIdentity *ret;
391 395
@@ -704,25 +708,222 @@ mq_notify_sent_cb (void *cls)
704 remove_pending_message (pending_msg); 708 remove_pending_message (pending_msg);
705} 709}
706 710
711/**
712 * @brief Clear the stored valid peers.
713 */
714static void
715clear_valid_peer_storage ()
716{
717 struct GNUNET_DISK_FileHandle *fh;
718
719 if (GNUNET_OK != GNUNET_DISK_file_test (filename_valid_peers))
720 {
721 return;
722 }
723 fh = GNUNET_DISK_file_open (filename_valid_peers,
724 GNUNET_DISK_OPEN_WRITE,
725 GNUNET_DISK_PERM_USER_READ |
726 GNUNET_DISK_PERM_USER_WRITE);
727 if (NULL == fh)
728 {
729 LOG (GNUNET_ERROR_TYPE_WARNING,
730 "Not able to clear file `%s' containing valid peers\n",
731 filename_valid_peers);
732 return;
733 }
734 GNUNET_DISK_file_write (fh, "", 0);
735 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
736}
737
738/**
739 * @brief Iterator function for #store_valid_peers.
740 *
741 * Implements #GNUNET_CONTAINER_PeerMapIterator.
742 * Writes single peer to disk.
743 *
744 * @param cls the file handle to write to.
745 * @param peer current peer
746 * @param value unused
747 *
748 * @return #GNUNET_YES if we should continue to
749 * iterate,
750 * #GNUNET_NO if not.
751 */
752static int
753store_peer_presistently_iterator (void *cls,
754 const struct GNUNET_PeerIdentity *peer,
755 void *value)
756{
757 const struct GNUNET_DISK_FileHandle *fh = cls;
758 char peer_string[128];
759 int size;
760 ssize_t ret;
761
762 if (NULL == peer)
763 {
764 return GNUNET_YES;
765 }
766 size = GNUNET_snprintf (peer_string,
767 sizeof (peer_string),
768 "%s\n",
769 GNUNET_i2s_full (peer));
770 GNUNET_assert (53 == size);
771 ret = GNUNET_DISK_file_write (fh,
772 peer_string,
773 size);
774 GNUNET_assert (size == ret);
775 return GNUNET_YES;
776}
777
778/**
779 * @brief Store the peers currently in #valid_peers to disk.
780 */
781static void
782store_valid_peers ()
783{
784 struct GNUNET_DISK_FileHandle *fh;
785 uint32_t number_written_peers;
786
787 if (GNUNET_OK !=
788 GNUNET_DISK_directory_create_for_file (filename_valid_peers))
789 {
790 GNUNET_break (0);
791 }
792 clear_valid_peer_storage ();
793 fh = GNUNET_DISK_file_open (filename_valid_peers,
794 GNUNET_DISK_OPEN_WRITE |
795 GNUNET_DISK_OPEN_CREATE |
796 GNUNET_DISK_OPEN_APPEND,
797 GNUNET_DISK_PERM_USER_READ |
798 GNUNET_DISK_PERM_USER_WRITE);
799 if (NULL == fh)
800 {
801 LOG (GNUNET_ERROR_TYPE_WARNING,
802 "Not able to write valid peers to file `%s'\n",
803 filename_valid_peers);
804 return;
805 }
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 "Writing %u valid peers to disk\n",
808 GNUNET_CONTAINER_multipeermap_size (valid_peers));
809 number_written_peers =
810 GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
811 store_peer_presistently_iterator,
812 fh);
813 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
814 GNUNET_assert (number_written_peers ==
815 GNUNET_CONTAINER_multipeermap_size (valid_peers));
816}
817
818/**
819 * @brief Convert string representation of peer id to peer id.
820 *
821 * Counterpart to #GNUNET_i2s_full.
822 *
823 * @param string_repr The string representation of the peer id
824 *
825 * @return The peer id
826 */
827static const struct GNUNET_PeerIdentity *
828s2i_full (const char *string_repr)
829{
830 struct GNUNET_PeerIdentity *peer;
831 size_t len;
832 int ret;
833
834 peer = GNUNET_new (struct GNUNET_PeerIdentity);
835 len = strlen (string_repr);
836 if (52 > len)
837 {
838 LOG (GNUNET_ERROR_TYPE_WARNING,
839 "Not able to convert string representation of PeerID to PeerID\n"
840 "Sting representation: %s (len %u) - too short\n",
841 string_repr,
842 len);
843 GNUNET_break (0);
844 }
845 else if (52 < len)
846 {
847 len = 52;
848 }
849 ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr,
850 len,
851 &peer->public_key);
852 if (GNUNET_OK != ret)
853 {
854 LOG (GNUNET_ERROR_TYPE_WARNING,
855 "Not able to convert string representation of PeerID to PeerID\n"
856 "Sting representation: %s\n",
857 string_repr);
858 GNUNET_break (0);
859 }
860 return peer;
861}
862
863/**
864 * @brief Restore the peers on disk to #valid_peers.
865 */
866static void
867restore_valid_peers ()
868{
869 off_t file_size;
870 uint32_t num_peers;
871 struct GNUNET_DISK_FileHandle *fh;
872 char *buf;
873 ssize_t size_read;
874 char *iter_buf;
875 const char *str_repr;
876 const struct GNUNET_PeerIdentity *peer;
877
878 if (GNUNET_OK != GNUNET_DISK_file_test (filename_valid_peers))
879 {
880 return;
881 }
882 fh = GNUNET_DISK_file_open (filename_valid_peers,
883 GNUNET_DISK_OPEN_READ,
884 GNUNET_DISK_PERM_USER_READ);
885 GNUNET_assert (NULL != fh);
886 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size));
887 num_peers = file_size / 53;
888 buf = GNUNET_malloc (file_size);
889 size_read = GNUNET_DISK_file_read (fh, buf, file_size);
890 GNUNET_assert (size_read == file_size);
891 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53)
892 {
893 str_repr = GNUNET_strndup (iter_buf, 53);
894 peer = s2i_full (str_repr);
895 add_valid_peer (peer);
896 LOG (GNUNET_ERROR_TYPE_DEBUG,
897 "Restored valid peer %s from disk\n",
898 GNUNET_i2s_full (peer));
899 }
900 GNUNET_assert (num_peers == GNUNET_CONTAINER_multipeermap_size (valid_peers));
901 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
902 LOG (GNUNET_ERROR_TYPE_DEBUG,
903 "Restored %u valid peers from disk\n",
904 num_peers);
905}
707 906
708/** 907/**
709 * @brief Initialise storage of peers 908 * @brief Initialise storage of peers
710 * 909 *
910 * @param fn_valid_peers filename of the file used to store valid peer ids
711 * @param cadet_h cadet handle 911 * @param cadet_h cadet handle
712 * @param own_id own peer identity 912 * @param own_id own peer identity
713 */ 913 */
714void 914void
715Peers_initialise (struct GNUNET_CADET_Handle *cadet_h, 915Peers_initialise (char* fn_valid_peers,
916 struct GNUNET_CADET_Handle *cadet_h,
716 const struct GNUNET_PeerIdentity *own_id) 917 const struct GNUNET_PeerIdentity *own_id)
717{ 918{
919 filename_valid_peers = GNUNET_strdup (fn_valid_peers);
718 cadet_handle = cadet_h; 920 cadet_handle = cadet_h;
719 own_identity = own_id; 921 own_identity = own_id;
720 peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); 922 peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
721 valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); 923 valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
924 restore_valid_peers ();
722} 925}
723 926
724// TODO read stored valid peers
725
726/** 927/**
727 * @brief Delete storage of peers that was created with #Peers_initialise () 928 * @brief Delete storage of peers that was created with #Peers_initialise ()
728 */ 929 */
@@ -738,6 +939,8 @@ Peers_terminate ()
738 "Iteration destroying peers was aborted.\n"); 939 "Iteration destroying peers was aborted.\n");
739 } 940 }
740 GNUNET_CONTAINER_multipeermap_destroy (peer_map); 941 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
942 store_valid_peers ();
943 GNUNET_free (filename_valid_peers);
741 GNUNET_CONTAINER_multipeermap_destroy (valid_peers); 944 GNUNET_CONTAINER_multipeermap_destroy (valid_peers);
742} 945}
743 946
@@ -1232,6 +1435,18 @@ Peers_cleanup_destroyed_channel (void *cls,
1232 peer_ctx->send_channel = NULL; 1435 peer_ctx->send_channel = NULL;
1233 else if (channel == peer_ctx->recv_channel) 1436 else if (channel == peer_ctx->recv_channel)
1234 peer_ctx->recv_channel = NULL; 1437 peer_ctx->recv_channel = NULL;
1438
1439 if (NULL != peer_ctx->send_channel)
1440 {
1441 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1442 peer_ctx->send_channel = NULL;
1443 }
1444 if (NULL != peer_ctx->recv_channel)
1445 {
1446 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1447 peer_ctx->recv_channel = NULL;
1448 }
1449 /* Set the #Peers_ONLINE flag accordingly */
1235 (void) Peers_check_connected (peer); 1450 (void) Peers_check_connected (peer);
1236 return; 1451 return;
1237 } 1452 }
diff --git a/src/rps/gnunet-service-rps_peers.h b/src/rps/gnunet-service-rps_peers.h
index 79db8185b..8a163f8cf 100644
--- a/src/rps/gnunet-service-rps_peers.h
+++ b/src/rps/gnunet-service-rps_peers.h
@@ -102,11 +102,13 @@ typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
102/** 102/**
103 * @brief Initialise storage of peers 103 * @brief Initialise storage of peers
104 * 104 *
105 * @param fn_valid_peers filename of the file used to store valid peer ids
105 * @param cadet_h cadet handle 106 * @param cadet_h cadet handle
106 * @param own_id own peer identity 107 * @param own_id own peer identity
107 */ 108 */
108void 109void
109Peers_initialise (struct GNUNET_CADET_Handle *cadet_h, 110Peers_initialise (char* fn_valid_peers,
111 struct GNUNET_CADET_Handle *cadet_h,
110 const struct GNUNET_PeerIdentity *own_id); 112 const struct GNUNET_PeerIdentity *own_id);
111 113
112/** 114/**
diff --git a/src/rps/test_rps.conf b/src/rps/test_rps.conf
index e3c563e99..a629451e4 100644
--- a/src/rps/test_rps.conf
+++ b/src/rps/test_rps.conf
@@ -12,6 +12,7 @@ NOARMBIND = YES
12 12
13# This is the timeinterval between the rounds 13# This is the timeinterval between the rounds
14ROUNDINTERVAL = 2 s 14ROUNDINTERVAL = 2 s
15FILENAME_VALID_PEERS = $GNUNET_DATA_HOME/rps/valid_peers.txt
15 16
16# This is the 'estimate' in the beginning. 17# This is the 'estimate' in the beginning.
17# This determines the size of the peers we keep in memory 18# This determines the size of the peers we keep in memory
diff --git a/src/rps/test_service_rps_peers.c b/src/rps/test_service_rps_peers.c
index 264a63e41..ede3d05a5 100644
--- a/src/rps/test_service_rps_peers.c
+++ b/src/rps/test_service_rps_peers.c
@@ -59,25 +59,25 @@ check ()
59 memset (&own_id, 1, sizeof (own_id)); 59 memset (&own_id, 1, sizeof (own_id));
60 60
61 /* Do nothing */ 61 /* Do nothing */
62 Peers_initialise (NULL, &own_id); 62 Peers_initialise ("", NULL, &own_id);
63 Peers_terminate (); 63 Peers_terminate ();
64 64
65 65
66 /* Create peer */ 66 /* Create peer */
67 Peers_initialise (NULL, &own_id); 67 Peers_initialise ("", NULL, &own_id);
68 CHECK (GNUNET_YES == Peers_insert_peer (&k1)); 68 CHECK (GNUNET_YES == Peers_insert_peer (&k1));
69 Peers_terminate (); 69 Peers_terminate ();
70 70
71 71
72 /* Create peer */ 72 /* Create peer */
73 Peers_initialise (NULL, &own_id); 73 Peers_initialise ("", NULL, &own_id);
74 CHECK (GNUNET_YES == Peers_insert_peer (&k1)); 74 CHECK (GNUNET_YES == Peers_insert_peer (&k1));
75 CHECK (GNUNET_YES == Peers_remove_peer (&k1)); 75 CHECK (GNUNET_YES == Peers_remove_peer (&k1));
76 Peers_terminate (); 76 Peers_terminate ();
77 77
78 78
79 /* Insertion and Removal */ 79 /* Insertion and Removal */
80 Peers_initialise (NULL, &own_id); 80 Peers_initialise ("", NULL, &own_id);
81 CHECK (GNUNET_NO == Peers_check_peer_known (&k1)); 81 CHECK (GNUNET_NO == Peers_check_peer_known (&k1));
82 82
83 CHECK (GNUNET_YES == Peers_insert_peer (&k1)); 83 CHECK (GNUNET_YES == Peers_insert_peer (&k1));