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-xdht_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-xdht.h"
31 #include "gnunet-service-xdht_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_MultiPeerMap *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_multipeermap_get (peer_to_hello, peer);
62 * Function called for each HELLO known to PEERINFO.
65 * @param peer id of the peer, NULL for last call
66 * @param hello hello message for the peer (can be NULL)
67 * @param err_msg error message (not used)
70 process_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
71 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
73 struct GNUNET_TIME_Absolute ex;
74 struct GNUNET_HELLO_Message *hm;
78 ex = GNUNET_HELLO_get_last_expiration (hello);
79 if (0 == GNUNET_TIME_absolute_get_remaining (ex).rel_value_us)
81 GNUNET_STATISTICS_update (GDS_stats,
82 gettext_noop ("# HELLOs obtained from peerinfo"), 1,
84 hm = GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
85 GNUNET_free_non_null (hm);
86 hm = GNUNET_malloc (GNUNET_HELLO_size (hello));
87 memcpy (hm, hello, GNUNET_HELLO_size (hello));
88 GNUNET_assert (GNUNET_SYSERR !=
89 GNUNET_CONTAINER_multipeermap_put (peer_to_hello,
91 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
96 * Initialize HELLO subsystem.
101 pnc = GNUNET_PEERINFO_notify (GDS_cfg, GNUNET_NO, &process_hello, NULL);
102 peer_to_hello = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
107 * Free memory occopied by the HELLO.
110 free_hello (void *cls,
111 const struct GNUNET_PeerIdentity *key,
120 * Shutdown HELLO subsystem.
127 GNUNET_PEERINFO_notify_cancel (pnc);
130 if (NULL != peer_to_hello)
132 GNUNET_CONTAINER_multipeermap_iterate (peer_to_hello, &free_hello, NULL);
133 GNUNET_CONTAINER_multipeermap_destroy (peer_to_hello);
137 /* end of gnunet-service-dht_hello.c */