2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file dht/gnunet-service-dht_hello.c
23 * @brief GNUnet DHT integration with peerinfo
24 * @author Christian Grothoff
27 * - consider adding mechanism to remove expired HELLOs
30 #include "gnunet-service-dht.h"
31 #include "gnunet-service-dht_hello.h"
32 #include "gnunet_peerinfo_service.h"
36 * Handle for peerinfo notifications.
38 static struct GNUNET_PEERINFO_NotifyContext *pnc;
41 * Hash map of peers to HELLOs.
43 static struct GNUNET_CONTAINER_MultiHashMap *peer_to_hello;
47 * Obtain a peer's HELLO if available
49 * @param peer peer to look for a HELLO from
50 * @return HELLO for the given peer
52 const struct GNUNET_HELLO_Message *
53 GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer)
55 if (NULL == peer_to_hello)
57 return GNUNET_CONTAINER_multihashmap_get (peer_to_hello,
63 * Function called for each HELLO known to PEERINFO.
66 * @param peer id of the peer, NULL for last call
67 * @param hello hello message for the peer (can be NULL)
68 * @param error message
71 process_hello (void *cls,
72 const struct GNUNET_PeerIdentity *
74 const struct GNUNET_HELLO_Message *
75 hello, const char *err_msg)
77 struct GNUNET_TIME_Absolute ex;
78 struct GNUNET_HELLO_Message *hm;
82 ex = GNUNET_HELLO_get_last_expiration (hello);
83 if (GNUNET_TIME_absolute_get_remaining (ex).rel_value == 0)
85 GNUNET_STATISTICS_update (GDS_stats,
86 gettext_noop ("# HELLOs obtained from peerinfo"), 1,
88 hm = GNUNET_CONTAINER_multihashmap_get (peer_to_hello,
90 GNUNET_free_non_null (hm);
91 hm = GNUNET_malloc (GNUNET_HELLO_size (hello));
92 memcpy (hm, hello, GNUNET_HELLO_size (hello));
93 GNUNET_assert (GNUNET_SYSERR !=
94 GNUNET_CONTAINER_multihashmap_put (peer_to_hello,
97 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
102 * Initialize HELLO subsystem.
107 pnc = GNUNET_PEERINFO_notify (GDS_cfg,
110 peer_to_hello = GNUNET_CONTAINER_multihashmap_create (256);
115 * Free memory occopied by the HELLO.
118 free_hello (void *cls,
119 const GNUNET_HashCode *key,
128 * Shutdown HELLO subsystem.
135 GNUNET_PEERINFO_notify_cancel (pnc);
138 if (NULL != peer_to_hello)
140 GNUNET_CONTAINER_multihashmap_iterate (peer_to_hello,
143 GNUNET_CONTAINER_multihashmap_destroy (peer_to_hello);
147 /* end of gnunet-service-dht_hello.c */