2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
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.
17 * @file dht/gnunet-service-dht_hello.c
18 * @brief GNUnet DHT integration with peerinfo
19 * @author Christian Grothoff
22 * - consider adding mechanism to remove expired HELLOs
25 #include "gnunet-service-dht.h"
26 #include "gnunet-service-dht_hello.h"
27 #include "gnunet_peerinfo_service.h"
31 * Handle for peerinfo notifications.
33 static struct GNUNET_PEERINFO_NotifyContext *pnc;
36 * Hash map of peers to HELLOs.
38 static struct GNUNET_CONTAINER_MultiPeerMap *peer_to_hello;
42 * Obtain a peer's HELLO if available
44 * @param peer peer to look for a HELLO from
45 * @return HELLO for the given peer
47 const struct GNUNET_HELLO_Message *
48 GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer)
50 if (NULL == peer_to_hello)
52 return GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
57 * Function called for each HELLO known to PEERINFO.
60 * @param peer id of the peer, NULL for last call
61 * @param hello hello message for the peer (can be NULL)
62 * @param err_msg error message (not used)
64 * FIXME this is called once per address. Merge instead of replacing?
67 process_hello (void *cls,
68 const struct GNUNET_PeerIdentity *peer,
69 const struct GNUNET_HELLO_Message *hello,
72 struct GNUNET_TIME_Absolute ex;
73 struct GNUNET_HELLO_Message *hm;
77 ex = GNUNET_HELLO_get_last_expiration (hello);
78 if (0 == GNUNET_TIME_absolute_get_remaining (ex).rel_value_us)
80 GNUNET_STATISTICS_update (GDS_stats,
81 gettext_noop ("# HELLOs obtained from peerinfo"), 1,
83 hm = GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
84 GNUNET_free_non_null (hm);
85 hm = GNUNET_malloc (GNUNET_HELLO_size (hello));
86 GNUNET_memcpy (hm, hello, GNUNET_HELLO_size (hello));
87 GNUNET_assert (GNUNET_SYSERR !=
88 GNUNET_CONTAINER_multipeermap_put (peer_to_hello,
90 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
95 * Initialize HELLO subsystem.
100 pnc = GNUNET_PEERINFO_notify (GDS_cfg,
104 peer_to_hello = GNUNET_CONTAINER_multipeermap_create (256,
110 * Free memory occopied by the HELLO.
113 free_hello (void *cls,
114 const struct GNUNET_PeerIdentity *key,
123 * Shutdown HELLO subsystem.
130 GNUNET_PEERINFO_notify_cancel (pnc);
133 if (NULL != peer_to_hello)
135 GNUNET_CONTAINER_multipeermap_iterate (peer_to_hello,
138 GNUNET_CONTAINER_multipeermap_destroy (peer_to_hello);
142 /* end of gnunet-service-dht_hello.c */