- merge; service API change
[oweals/gnunet.git] / src / dht / gnunet-service-dht_hello.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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_MultiPeerMap *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   if (NULL == peer_to_hello)
56     return NULL;
57   return GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
58 }
59
60
61 /**
62  * Function called for each HELLO known to PEERINFO.
63  *
64  * @param cls closure
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)
68  *
69  * FIXME this is called once per address. Merge instead of replacing?
70  */
71 static void
72 process_hello (void *cls,
73                const struct GNUNET_PeerIdentity *peer,
74                const struct GNUNET_HELLO_Message *hello,
75                const char *err_msg)
76 {
77   struct GNUNET_TIME_Absolute ex;
78   struct GNUNET_HELLO_Message *hm;
79
80   if (NULL == hello)
81     return;
82   ex = GNUNET_HELLO_get_last_expiration (hello);
83   if (0 == GNUNET_TIME_absolute_get_remaining (ex).rel_value_us)
84     return;
85   GNUNET_STATISTICS_update (GDS_stats,
86                             gettext_noop ("# HELLOs obtained from peerinfo"), 1,
87                             GNUNET_NO);
88   hm = GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
89   GNUNET_free_non_null (hm);
90   hm = GNUNET_malloc (GNUNET_HELLO_size (hello));
91   GNUNET_memcpy (hm, hello, GNUNET_HELLO_size (hello));
92   GNUNET_assert (GNUNET_SYSERR !=
93                  GNUNET_CONTAINER_multipeermap_put (peer_to_hello,
94                                                     peer, hm,
95                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
96 }
97
98
99 /**
100  * Initialize HELLO subsystem.
101  */
102 void
103 GDS_HELLO_init ()
104 {
105   pnc = GNUNET_PEERINFO_notify (GDS_cfg,
106                                 GNUNET_NO,
107                                 &process_hello,
108                                 NULL);
109   peer_to_hello = GNUNET_CONTAINER_multipeermap_create (256,
110                                                         GNUNET_NO);
111 }
112
113
114 /**
115  * Free memory occopied by the HELLO.
116  */
117 static int
118 free_hello (void *cls,
119             const struct GNUNET_PeerIdentity *key,
120             void *hello)
121 {
122   GNUNET_free (hello);
123   return GNUNET_OK;
124 }
125
126
127 /**
128  * Shutdown HELLO subsystem.
129  */
130 void
131 GDS_HELLO_done ()
132 {
133   if (NULL != pnc)
134   {
135     GNUNET_PEERINFO_notify_cancel (pnc);
136     pnc = NULL;
137   }
138   if (NULL != peer_to_hello)
139   {
140     GNUNET_CONTAINER_multipeermap_iterate (peer_to_hello,
141                                            &free_hello,
142                                            NULL);
143     GNUNET_CONTAINER_multipeermap_destroy (peer_to_hello);
144   }
145 }
146
147 /* end of gnunet-service-dht_hello.c */