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 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.
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.
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/>.
20 * @file dht/gnunet-service-dht_hello.c
21 * @brief GNUnet DHT integration with peerinfo
22 * @author Christian Grothoff
25 * - consider adding mechanism to remove expired HELLOs
28 #include "gnunet-service-dht.h"
29 #include "gnunet-service-dht_hello.h"
30 #include "gnunet_peerinfo_service.h"
34 * Handle for peerinfo notifications.
36 static struct GNUNET_PEERINFO_NotifyContext *pnc;
39 * Hash map of peers to HELLOs.
41 static struct GNUNET_CONTAINER_MultiPeerMap *peer_to_hello;
45 * Obtain a peer's HELLO if available
47 * @param peer peer to look for a HELLO from
48 * @return HELLO for the given peer
50 const struct GNUNET_HELLO_Message *
51 GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer)
53 if (NULL == peer_to_hello)
55 return GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
60 * Function called for each HELLO known to PEERINFO.
63 * @param peer id of the peer, NULL for last call
64 * @param hello hello message for the peer (can be NULL)
65 * @param err_msg error message (not used)
67 * FIXME this is called once per address. Merge instead of replacing?
70 process_hello (void *cls,
71 const struct GNUNET_PeerIdentity *peer,
72 const struct GNUNET_HELLO_Message *hello,
75 struct GNUNET_TIME_Absolute ex;
76 struct GNUNET_HELLO_Message *hm;
80 ex = GNUNET_HELLO_get_last_expiration (hello);
81 if (0 == GNUNET_TIME_absolute_get_remaining (ex).rel_value_us)
83 GNUNET_STATISTICS_update (GDS_stats,
84 gettext_noop ("# HELLOs obtained from peerinfo"), 1,
86 hm = GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
87 GNUNET_free_non_null (hm);
88 hm = GNUNET_malloc (GNUNET_HELLO_size (hello));
89 GNUNET_memcpy (hm, hello, GNUNET_HELLO_size (hello));
90 GNUNET_assert (GNUNET_SYSERR !=
91 GNUNET_CONTAINER_multipeermap_put (peer_to_hello,
93 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
98 * Initialize HELLO subsystem.
103 pnc = GNUNET_PEERINFO_notify (GDS_cfg,
107 peer_to_hello = GNUNET_CONTAINER_multipeermap_create (256,
113 * Free memory occopied by the HELLO.
116 free_hello (void *cls,
117 const struct GNUNET_PeerIdentity *key,
126 * Shutdown HELLO subsystem.
133 GNUNET_PEERINFO_notify_cancel (pnc);
136 if (NULL != peer_to_hello)
138 GNUNET_CONTAINER_multipeermap_iterate (peer_to_hello,
141 GNUNET_CONTAINER_multipeermap_destroy (peer_to_hello);
145 /* end of gnunet-service-dht_hello.c */