diff options
Diffstat (limited to 'src/topology/friends.c')
-rw-r--r-- | src/topology/friends.c | 246 |
1 files changed, 0 insertions, 246 deletions
diff --git a/src/topology/friends.c b/src/topology/friends.c deleted file mode 100644 index 65c7e81d7..000000000 --- a/src/topology/friends.c +++ /dev/null | |||
@@ -1,246 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013 Christian Grothoff | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
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 | */ | ||
38 | int | ||
39 | GNUNET_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 | ssize_t ssize; | ||
50 | |||
51 | if (GNUNET_OK != | ||
52 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
53 | "TOPOLOGY", | ||
54 | "FRIENDS", | ||
55 | &fn)) | ||
56 | { | ||
57 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
58 | "topology", | ||
59 | "FRIENDS"); | ||
60 | return GNUNET_SYSERR; | ||
61 | } | ||
62 | if ((GNUNET_OK != | ||
63 | GNUNET_DISK_file_test (fn)) && | ||
64 | (GNUNET_OK != | ||
65 | GNUNET_DISK_fn_write (fn, | ||
66 | NULL, | ||
67 | 0, | ||
68 | GNUNET_DISK_PERM_USER_READ | ||
69 | | GNUNET_DISK_PERM_USER_WRITE | ||
70 | | GNUNET_DISK_OPEN_CREATE))) | ||
71 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, | ||
72 | "write", | ||
73 | fn); | ||
74 | if ((GNUNET_OK != | ||
75 | GNUNET_DISK_file_size (fn, | ||
76 | &fsize, | ||
77 | GNUNET_NO, | ||
78 | GNUNET_YES)) || | ||
79 | (0 == fsize)) | ||
80 | { | ||
81 | GNUNET_free (fn); | ||
82 | return GNUNET_OK; | ||
83 | } | ||
84 | data = GNUNET_malloc_large (fsize); | ||
85 | if (NULL == data) | ||
86 | { | ||
87 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); | ||
88 | GNUNET_free (fn); | ||
89 | return GNUNET_SYSERR; | ||
90 | } | ||
91 | ssize = GNUNET_DISK_fn_read (fn, | ||
92 | data, | ||
93 | fsize); | ||
94 | if ((ssize < 0) || | ||
95 | (fsize != (uint64_t) ssize)) | ||
96 | { | ||
97 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
98 | "read", | ||
99 | "fn"); | ||
100 | GNUNET_free (fn); | ||
101 | GNUNET_free (data); | ||
102 | return GNUNET_SYSERR; | ||
103 | } | ||
104 | start = 0; | ||
105 | pos = 0; | ||
106 | while (pos < fsize) | ||
107 | { | ||
108 | while ((pos < fsize) && | ||
109 | (! isspace ((unsigned char) data[pos]))) | ||
110 | pos++; | ||
111 | if (GNUNET_OK != | ||
112 | GNUNET_CRYPTO_eddsa_public_key_from_string (&data[start], | ||
113 | pos - start, | ||
114 | &pid.public_key)) | ||
115 | { | ||
116 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
117 | _ ( | ||
118 | "Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"), | ||
119 | (unsigned long long) pos, | ||
120 | (int) (pos - start), | ||
121 | &data[start]); | ||
122 | pos++; | ||
123 | start = pos; | ||
124 | continue; | ||
125 | } | ||
126 | pos++; | ||
127 | start = pos; | ||
128 | cb (cb_cls, &pid); | ||
129 | } | ||
130 | GNUNET_free (data); | ||
131 | GNUNET_free (fn); | ||
132 | return GNUNET_OK; | ||
133 | } | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Handle for writing a friends file. | ||
138 | */ | ||
139 | struct GNUNET_FRIENDS_Writer | ||
140 | { | ||
141 | /** | ||
142 | * Handle to the file. | ||
143 | */ | ||
144 | struct GNUNET_DISK_FileHandle *fh; | ||
145 | }; | ||
146 | |||
147 | |||
148 | /** | ||
149 | * Start writing a fresh FRIENDS file. Will make a backup of the | ||
150 | * old one. | ||
151 | * | ||
152 | * @param cfg configuration to use. | ||
153 | * @return NULL on error | ||
154 | */ | ||
155 | struct GNUNET_FRIENDS_Writer * | ||
156 | GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
157 | { | ||
158 | struct GNUNET_FRIENDS_Writer *w; | ||
159 | char *fn; | ||
160 | |||
161 | if (GNUNET_OK != | ||
162 | GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn)) | ||
163 | { | ||
164 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
165 | "topology", "FRIENDS"); | ||
166 | return NULL; | ||
167 | } | ||
168 | if (GNUNET_OK != | ||
169 | GNUNET_DISK_directory_create_for_file (fn)) | ||
170 | { | ||
171 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
172 | _ ("Directory for file `%s' does not seem to be writable.\n"), | ||
173 | fn); | ||
174 | GNUNET_free (fn); | ||
175 | return NULL; | ||
176 | } | ||
177 | if (GNUNET_OK == GNUNET_DISK_file_test (fn)) | ||
178 | GNUNET_DISK_file_backup (fn); | ||
179 | w = GNUNET_new (struct GNUNET_FRIENDS_Writer); | ||
180 | w->fh = GNUNET_DISK_file_open (fn, | ||
181 | GNUNET_DISK_OPEN_CREATE | ||
182 | | GNUNET_DISK_OPEN_WRITE | ||
183 | | GNUNET_DISK_OPEN_FAILIFEXISTS, | ||
184 | GNUNET_DISK_PERM_USER_READ); | ||
185 | GNUNET_free (fn); | ||
186 | if (NULL == w->fh) | ||
187 | { | ||
188 | GNUNET_free (w); | ||
189 | return NULL; | ||
190 | } | ||
191 | return w; | ||
192 | } | ||
193 | |||
194 | |||
195 | /** | ||
196 | * Finish writing out the friends file. | ||
197 | * | ||
198 | * @param w write handle | ||
199 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
200 | */ | ||
201 | int | ||
202 | GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w) | ||
203 | { | ||
204 | int ret; | ||
205 | |||
206 | ret = GNUNET_DISK_file_close (w->fh); | ||
207 | GNUNET_free (w); | ||
208 | return ret; | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Add a friend to the friends file. | ||
214 | * | ||
215 | * @param w write handle | ||
216 | * @param friend_id friend to add | ||
217 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
218 | */ | ||
219 | int | ||
220 | GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w, | ||
221 | const struct GNUNET_PeerIdentity *friend_id) | ||
222 | { | ||
223 | char *buf; | ||
224 | char *ret; | ||
225 | size_t slen; | ||
226 | |||
227 | ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&friend_id->public_key); | ||
228 | GNUNET_asprintf (&buf, | ||
229 | "%s\n", | ||
230 | ret); | ||
231 | GNUNET_free (ret); | ||
232 | slen = strlen (buf); | ||
233 | if (slen != | ||
234 | GNUNET_DISK_file_write (w->fh, | ||
235 | buf, | ||
236 | slen)) | ||
237 | { | ||
238 | GNUNET_free (buf); | ||
239 | return GNUNET_SYSERR; | ||
240 | } | ||
241 | GNUNET_free (buf); | ||
242 | return GNUNET_OK; | ||
243 | } | ||
244 | |||
245 | |||
246 | /* end of friends.c */ | ||