track HELLOs from peerinfo
[oweals/gnunet.git] / src / dht / gnunet-service-dht_hello.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file dht/gnunet-service-dht_hello.c
23  * @brief GNUnet DHT integration with peerinfo
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - consider adding mechanism to remove expired HELLOs
28  */
29 #include "platform.h"
30 #include "gnunet-service-dht.h"
31 #include "gnunet-service-dht_hello.h"
32 #include "gnunet_peerinfo_service.h"
33
34
35 /**
36  * Handle for peerinfo notifications.
37  */
38 static struct GNUNET_PEERINFO_NotifyContext *pnc;
39
40 /**
41  * Hash map of peers to HELLOs.
42  */
43 static struct GNUNET_CONTAINER_MultiHashMap *peer_to_hello;
44
45
46 /**
47  * Obtain a peer's HELLO if available
48  *
49  * @param peer peer to look for a HELLO from
50  * @return HELLO for the given peer
51  */
52 const struct GNUNET_HELLO_Message *
53 GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer)
54 {
55   return GNUNET_CONTAINER_multihashmap_get (peer_to_hello,
56                                             &peer->hashPubKey);
57 }
58
59
60 /**
61  * Function called for each HELLO known to PEERINFO.
62  *
63  * @param cls closure
64  * @param peer id of the peer, NULL for last call
65  * @param hello hello message for the peer (can be NULL)
66  * @param error message
67  */
68 static void
69 process_hello (void *cls,
70                const struct GNUNET_PeerIdentity *
71                peer,
72                const struct GNUNET_HELLO_Message *
73                hello, const char *err_msg)
74 {
75   struct GNUNET_TIME_Absolute ex;
76   struct GNUNET_HELLO_Message *hm;
77
78   if (hello == NULL)
79     return;
80   ex = GNUNET_HELLO_get_last_expiration (hello);
81   if (GNUNET_TIME_absolute_get_remaining (ex).rel_value == 0)
82     return;
83   hm = GNUNET_CONTAINER_multihashmap_get (peer_to_hello,
84                                           &peer->hashPubKey);
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_multihashmap_put (peer_to_hello,
90                                                     &peer->hashPubKey,
91                                                     hm,
92                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
93 }
94
95
96 /**
97  * Initialize HELLO subsystem.
98  */
99 void
100 GDS_HELLO_init ()
101 {
102   pnc = GNUNET_PEERINFO_notify (GDS_cfg,
103                                 &process_hello,
104                                 NULL);
105   peer_to_hello = GNUNET_CONTAINER_multihashmap_create (256);
106 }
107
108
109 /**
110  * Free memory occopied by the HELLO.
111  */
112 static int
113 free_hello (void *cls,
114             const GNUNET_HashCode *key,
115             void *hello)
116 {
117   GNUNET_free (hello);
118   return GNUNET_OK;
119 }
120
121
122 /**
123  * Shutdown HELLO subsystem.
124  */
125 void
126 GDS_HELLO_done ()
127 {
128   if (NULL != pnc)
129   {
130     GNUNET_PEERINFO_notify_cancel (pnc);
131     pnc = NULL;
132   }
133   GNUNET_CONTAINER_multihashmap_iterate (peer_to_hello,
134                                          &free_hello,
135                                          NULL);
136   GNUNET_CONTAINER_multihashmap_destroy (peer_to_hello);
137 }
138
139 /* end of gnunet-service-dht_hello.c */