aboutsummaryrefslogtreecommitdiff
path: root/src/topology
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
parent2866a3a85ea457f29f4746ccae57af369746c5fa (diff)
downloadgnunet-91dfeebc1a2e840f88904aab7f43ce214c3eb03a.tar.gz
gnunet-91dfeebc1a2e840f88904aab7f43ce214c3eb03a.zip
-moving friends file parsing logic into its own library
Diffstat (limited to 'src/topology')
-rw-r--r--src/topology/Makefile.am19
-rw-r--r--src/topology/friends.c165
-rw-r--r--src/topology/gnunet-daemon-topology.c124
3 files changed, 216 insertions, 92 deletions
diff --git a/src/topology/Makefile.am b/src/topology/Makefile.am
index 1094325ff..8c78d5c2c 100644
--- a/src/topology/Makefile.am
+++ b/src/topology/Makefile.am
@@ -12,12 +12,25 @@ dist_pkgcfg_DATA = \
12 topology.conf 12 topology.conf
13 13
14 14
15lib_LTLIBRARIES = libgnunetfriends.la
16
17libgnunetfriends_la_SOURCES = \
18 friends.c
19libgnunetfriends_la_LIBADD = \
20 $(top_builddir)/src/util/libgnunetutil.la
21 $(GN_LIBINTL) $(XLIB)
22libgnunetfriends_la_LDFLAGS = \
23 $(GN_LIB_LDFLAGS) $(WINFLAGS) \
24 -version-info 0:0:0
25
26
15libexec_PROGRAMS = \ 27libexec_PROGRAMS = \
16 gnunet-daemon-topology 28 gnunet-daemon-topology
17 29
18gnunet_daemon_topology_SOURCES = \ 30gnunet_daemon_topology_SOURCES = \
19 gnunet-daemon-topology.c 31 gnunet-daemon-topology.c
20gnunet_daemon_topology_LDADD = \ 32gnunet_daemon_topology_LDADD = \
33 libgnunetfriends.la \
21 $(top_builddir)/src/core/libgnunetcore.la \ 34 $(top_builddir)/src/core/libgnunetcore.la \
22 $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \ 35 $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \
23 $(top_builddir)/src/statistics/libgnunetstatistics.la \ 36 $(top_builddir)/src/statistics/libgnunetstatistics.la \
@@ -40,7 +53,7 @@ test_gnunet_daemon_topology_SOURCES = \
40 test_gnunet_daemon_topology.c 53 test_gnunet_daemon_topology.c
41test_gnunet_daemon_topology_LDADD = \ 54test_gnunet_daemon_topology_LDADD = \
42 $(top_builddir)/src/testbed/libgnunettestbed.la \ 55 $(top_builddir)/src/testbed/libgnunettestbed.la \
43 $(top_builddir)/src/util/libgnunetutil.la 56 $(top_builddir)/src/util/libgnunetutil.la
44 57
45EXTRA_DIST = \ 58EXTRA_DIST = \
46 test_gnunet_daemon_topology_data.conf 59 test_gnunet_daemon_topology_data.conf
diff --git a/src/topology/friends.c b/src/topology/friends.c
new file mode 100644
index 000000000..c0915fa15
--- /dev/null
+++ b/src/topology/friends.c
@@ -0,0 +1,165 @@
1/*
2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file topology/friends.c
23 * @brief library to read and write the FRIENDS file
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_friends_lib.h"
28
29
30/**
31 * Parse the FRIENDS file.
32 *
33 * @param cfg our configuration
34 * @param cb function to call on each friend found
35 * @param cb_cls closure for @a cb
36 * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors
37 */
38int
39GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg,
40 GNUNET_FRIENDS_Callback cb,
41 void *cb_cls)
42{
43 char *fn;
44 char *data;
45 size_t pos;
46 size_t start;
47 struct GNUNET_PeerIdentity pid;
48 uint64_t fsize;
49
50 if (GNUNET_OK !=
51 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
52 {
53 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
54 "topology", "FRIENDS");
55 return GNUNET_SYSERR;
56 }
57 if ( (GNUNET_OK != GNUNET_DISK_file_test (fn)) &&
58 (GNUNET_OK != GNUNET_DISK_fn_write (fn, NULL, 0,
59 GNUNET_DISK_PERM_USER_READ |
60 GNUNET_DISK_PERM_USER_WRITE)) )
61 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
62 if ( (GNUNET_OK !=
63 GNUNET_DISK_file_size (fn,
64 &fsize,
65 GNUNET_NO, GNUNET_YES)) ||
66 (0 == fsize) )
67 {
68 GNUNET_free (fn);
69 return GNUNET_OK;
70 }
71 data = GNUNET_malloc_large (fsize);
72 if (NULL == data)
73 {
74 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
75 GNUNET_free (fn);
76 return GNUNET_SYSERR;
77 }
78 if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
79 {
80 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "read", "fn");
81 GNUNET_free (fn);
82 GNUNET_free (data);
83 return GNUNET_SYSERR;
84 }
85 start = 0;
86 pos = 0;
87 while (pos < fsize)
88 {
89 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
90 pos++;
91 if (GNUNET_OK !=
92 GNUNET_CRYPTO_ecc_public_sign_key_from_string (&data[start],
93 pos - start,
94 &pid.public_key))
95 {
96 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
97 _("Syntax error in topology specification at offset %llu, skipping bytes `%.*s'.\n"),
98 (unsigned long long) pos,
99 (int) (pos - start),
100 &data[start]);
101 pos++;
102 start = pos;
103 continue;
104 }
105 pos++;
106 start = pos;
107 cb (cb_cls, &pid);
108 }
109 GNUNET_free (data);
110 GNUNET_free (fn);
111 return GNUNET_OK;
112}
113
114
115/**
116 * Handle for writing a friends file.
117 */
118struct GNUNET_FRIENDS_Writer
119{
120};
121
122
123/**
124 * Start writing a fresh FRIENDS file. Will make a backup of the
125 * old one.
126 *
127 * @param cfg configuration to use.
128 * @return NULL on error
129 */
130struct GNUNET_FRIENDS_Writer *
131GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
132{
133 return NULL;
134}
135
136
137/**
138 * Finish writing out the friends file.
139 *
140 * @param w write handle
141 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
142 */
143int
144GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
145{
146 return GNUNET_SYSERR;
147}
148
149
150/**
151 * Add a friend to the friends file.
152 *
153 * @param w write handle
154 * @param friend friend to add
155 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
156 */
157int
158GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w,
159 const struct GNUNET_PeerIdentity *friend)
160{
161 return GNUNET_SYSERR;
162}
163
164
165/* end of friends.c */
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