aboutsummaryrefslogtreecommitdiff
path: root/src/topology/gnunet-daemon-topology.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-10-07 18:08:05 +0000
committerChristian Grothoff <christian@grothoff.org>2013-10-07 18:08:05 +0000
commit91dfeebc1a2e840f88904aab7f43ce214c3eb03a (patch)
tree36ac52a744e89799ea51f1d572b7cf7302f6ba8d /src/topology/gnunet-daemon-topology.c
parent2866a3a85ea457f29f4746ccae57af369746c5fa (diff)
downloadgnunet-91dfeebc1a2e840f88904aab7f43ce214c3eb03a.tar.gz
gnunet-91dfeebc1a2e840f88904aab7f43ce214c3eb03a.zip
-moving friends file parsing logic into its own library
Diffstat (limited to 'src/topology/gnunet-daemon-topology.c')
-rw-r--r--src/topology/gnunet-daemon-topology.c124
1 files changed, 35 insertions, 89 deletions
diff --git a/src/topology/gnunet-daemon-topology.c b/src/topology/gnunet-daemon-topology.c
index 761986d68..051dccf50 100644
--- a/src/topology/gnunet-daemon-topology.c
+++ b/src/topology/gnunet-daemon-topology.c
@@ -25,6 +25,7 @@
25 */ 25 */
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "gnunet_friends_lib.h"
28#include "gnunet_constants.h" 29#include "gnunet_constants.h"
29#include "gnunet_core_service.h" 30#include "gnunet_core_service.h"
30#include "gnunet_protocols.h" 31#include "gnunet_protocols.h"
@@ -1002,118 +1003,63 @@ core_init (void *cls,
1002 1003
1003 1004
1004/** 1005/**
1005 * Read the friends file. 1006 * Process friend found in FRIENDS file.
1007 *
1008 * @param cls pointer to an `unsigned int` to be incremented per friend found
1009 * @param pid identity of the friend
1006 */ 1010 */
1007static void 1011static void
1008read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg) 1012handle_friend (void *cls,
1013 const struct GNUNET_PeerIdentity *pid)
1009{ 1014{
1010 char *fn; 1015 unsigned int *entries_found = cls;
1011 char *data;
1012 size_t pos;
1013 size_t start;
1014 struct GNUNET_PeerIdentity pid;
1015 uint64_t fsize;
1016 unsigned int entries_found;
1017 struct Peer *fl; 1016 struct Peer *fl;
1018 1017
1019 if (GNUNET_OK != 1018 if (0 == memcmp (pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1020 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
1021 {
1022 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1023 "topology", "FRIENDS");
1024 return;
1025 }
1026 if ( (GNUNET_OK != GNUNET_DISK_file_test (fn)) &&
1027 (GNUNET_OK != GNUNET_DISK_fn_write (fn, NULL, 0,
1028 GNUNET_DISK_PERM_USER_READ |
1029 GNUNET_DISK_PERM_USER_WRITE)) )
1030 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
1031 if (GNUNET_OK != GNUNET_DISK_file_size (fn,
1032 &fsize, GNUNET_NO, GNUNET_YES))
1033 {
1034 if ((friends_only) || (minimum_friend_count > 0))
1035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1036 _("Could not read friends list `%s'\n"), fn);
1037 GNUNET_free (fn);
1038 return;
1039 }
1040 if (0 == fsize)
1041 { 1019 {
1042 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1043 _("Friends file `%s' is empty.\n"), 1021 _("Found myself `%s' in friend list (useless, ignored)\n"),
1044 fn); 1022 GNUNET_i2s (&pid));
1045 GNUNET_free (fn);
1046 return;
1047 }
1048 data = GNUNET_malloc_large (fsize);
1049 if (NULL == data)
1050 {
1051 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1052 _("Failed to read friends list from `%s': out of memory\n"),
1053 fn);
1054 GNUNET_free (fn);
1055 return;
1056 }
1057 if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
1058 {
1059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1060 _("Failed to read friends list from `%s'\n"), fn);
1061 GNUNET_free (fn);
1062 GNUNET_free (data);
1063 return; 1023 return;
1064 } 1024 }
1025 (*entries_found)++;
1026 fl = make_peer (pid, NULL, GNUNET_YES);
1027 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1028 _("Found friend `%s' in configuration\n"),
1029 GNUNET_i2s (&fl->pid));
1030}
1031
1032
1033/**
1034 * Read the friends file.
1035 */
1036static void
1037read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1038{
1039 unsigned int entries_found;
1040
1065 entries_found = 0; 1041 entries_found = 0;
1066 start = 0; 1042 if (GNUNET_OK !=
1067 pos = 0; 1043 GNUNET_FRIENDS_parse (cfg,
1068 while (pos < fsize) 1044 &handle_friend,
1045 &entries_found))
1069 { 1046 {
1070 while ((pos < fsize) && isspace ((unsigned char) data[pos])) 1047 if ((friends_only) || (minimum_friend_count > 0))
1071 pos++;
1072 if (GNUNET_OK !=
1073 GNUNET_CRYPTO_ecc_public_sign_key_from_string (&data[start],
1074 pos - start,
1075 &pid.public_key))
1076 {
1077 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1078 _("Syntax error in topology specification at offset %llu, skipping bytes `%.*s'.\n"),
1079 (unsigned long long) pos,
1080 (int) (pos - start),
1081 &data[start]);
1082 pos++;
1083 start = pos;
1084 continue;
1085 }
1086 pos++;
1087 start = pos;
1088 if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1089 {
1090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1091 _("Found myself `%s' in friend list (useless, ignored)\n"), 1049 _("Encountered errors parsing friends list!\n"));
1092 GNUNET_i2s (&pid));
1093 continue;
1094 }
1095 entries_found++;
1096 fl = make_peer (&pid, NULL, GNUNET_YES);
1097 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1098 _("Found friend `%s' in configuration\n"),
1099 GNUNET_i2s (&fl->pid));
1100 } 1050 }
1101 GNUNET_free (data);
1102 GNUNET_free (fn);
1103 GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"), 1051 GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1104 entries_found, GNUNET_NO); 1052 entries_found, GNUNET_NO);
1105 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO)) 1053 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1106 { 1054 {
1107 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1055 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1108 _ 1056 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1109 ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1110 } 1057 }
1111 if ((minimum_friend_count > target_connection_count) && 1058 if ((minimum_friend_count > target_connection_count) &&
1112 (friends_only == GNUNET_NO)) 1059 (friends_only == GNUNET_NO))
1113 { 1060 {
1114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1115 _ 1062 _("More friendly connections required than target total number of connections.\n"));
1116 ("More friendly connections required than target total number of connections.\n"));
1117 } 1063 }
1118} 1064}
1119 1065