aboutsummaryrefslogtreecommitdiff
path: root/src/peerinfo/gnunet-service-peerinfo.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/peerinfo/gnunet-service-peerinfo.c')
-rw-r--r--src/peerinfo/gnunet-service-peerinfo.c85
1 files changed, 56 insertions, 29 deletions
diff --git a/src/peerinfo/gnunet-service-peerinfo.c b/src/peerinfo/gnunet-service-peerinfo.c
index 90ab6d5bf..2a5921b5d 100644
--- a/src/peerinfo/gnunet-service-peerinfo.c
+++ b/src/peerinfo/gnunet-service-peerinfo.c
@@ -177,6 +177,42 @@ notify_all (struct HostEntry *entry)
177 177
178 178
179/** 179/**
180 * Try to read the HELLO in the given filename and discard expired addresses.
181 *
182 * @param fn name of the file
183 * @return HELLO of the file, NULL on error
184 */
185static struct GNUNET_HELLO_Message *
186read_host_file (const char *fn)
187{
188 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
189 const struct GNUNET_HELLO_Message *hello;
190 struct GNUNET_HELLO_Message *hello_clean;
191 int size;
192 struct GNUNET_TIME_Absolute now;
193
194 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
195 return NULL;
196 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
197 hello = (const struct GNUNET_HELLO_Message *) buffer;
198 if ((size < sizeof (struct GNUNET_MessageHeader)) ||
199 (size != ntohs ((((const struct GNUNET_MessageHeader *) hello)->size)))
200 || (size != GNUNET_HELLO_size (hello)))
201 {
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
203 _("Failed to parse HELLO in file `%s'\n"),
204 fn);
205 return NULL;
206 }
207 now = GNUNET_TIME_absolute_get ();
208 hello_clean =
209 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired,
210 &now);
211 return hello_clean;
212}
213
214
215/**
180 * Add a host to the list. 216 * Add a host to the list.
181 * 217 *
182 * @param identity the identity of the host 218 * @param identity the identity of the host
@@ -185,11 +221,6 @@ static void
185add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity) 221add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
186{ 222{
187 struct HostEntry *entry; 223 struct HostEntry *entry;
188 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
189 const struct GNUNET_HELLO_Message *hello;
190 struct GNUNET_HELLO_Message *hello_clean;
191 int size;
192 struct GNUNET_TIME_Absolute now;
193 char *fn; 224 char *fn;
194 225
195 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey); 226 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
@@ -199,32 +230,11 @@ add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
199 GNUNET_NO); 230 GNUNET_NO);
200 entry = GNUNET_malloc (sizeof (struct HostEntry)); 231 entry = GNUNET_malloc (sizeof (struct HostEntry));
201 entry->identity = *identity; 232 entry->identity = *identity;
202
203 fn = get_host_filename (identity);
204 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
205 {
206 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
207 hello = (const struct GNUNET_HELLO_Message *) buffer;
208 if ((size < sizeof (struct GNUNET_MessageHeader)) ||
209 (size != ntohs ((((const struct GNUNET_MessageHeader *) hello)->size)))
210 || (size != GNUNET_HELLO_size (hello)))
211 {
212 GNUNET_break (0);
213 if (0 != UNLINK (fn))
214 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
215 }
216 else
217 {
218 now = GNUNET_TIME_absolute_get ();
219 hello_clean =
220 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired,
221 &now);
222 entry->hello = hello_clean;
223 }
224 }
225 GNUNET_free (fn);
226 GNUNET_CONTAINER_multihashmap_put (hostmap, &identity->hashPubKey, entry, 233 GNUNET_CONTAINER_multihashmap_put (hostmap, &identity->hashPubKey, entry,
227 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
235 fn = get_host_filename (identity);
236 entry->hello = read_host_file (fn);
237 GNUNET_free (fn);
228 notify_all (entry); 238 notify_all (entry);
229} 239}
230 240
@@ -264,6 +274,8 @@ hosts_directory_scan_callback (void *cls, const char *fullname)
264 unsigned int *matched = cls; 274 unsigned int *matched = cls;
265 struct GNUNET_PeerIdentity identity; 275 struct GNUNET_PeerIdentity identity;
266 const char *filename; 276 const char *filename;
277 struct HostEntry *entry;
278 struct GNUNET_HELLO_Message *hello;
267 279
268 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES) 280 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
269 return GNUNET_OK; /* ignore non-files */ 281 return GNUNET_OK; /* ignore non-files */
@@ -285,6 +297,21 @@ hosts_directory_scan_callback (void *cls, const char *fullname)
285 if (GNUNET_OK != 297 if (GNUNET_OK !=
286 GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey)) 298 GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
287 { 299 {
300 if (NULL != (hello = read_host_file (filename)))
301 {
302 entry = GNUNET_malloc (sizeof (struct HostEntry));
303 if (GNUNET_OK ==
304 GNUNET_HELLO_get_id (hello,
305 &entry->identity))
306 {
307 GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
309 entry->hello = hello;
310 notify_all (entry);
311 return GNUNET_OK;
312 }
313 GNUNET_free (entry);
314 }
288 if (NULL != matched) 315 if (NULL != matched)
289 remove_garbage (fullname); 316 remove_garbage (fullname);
290 return GNUNET_OK; 317 return GNUNET_OK;