glitch in the license text detected by hyazinthe, thank you!
[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 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.
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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file dht/gnunet-service-dht_hello.c
18  * @brief GNUnet DHT integration with peerinfo
19  * @author Christian Grothoff
20  *
21  * TODO:
22  * - consider adding mechanism to remove expired HELLOs
23  */
24 #include "platform.h"
25 #include "gnunet-service-dht.h"
26 #include "gnunet-service-dht_hello.h"
27 #include "gnunet_peerinfo_service.h"
28
29
30 /**
31  * Handle for peerinfo notifications.
32  */
33 static struct GNUNET_PEERINFO_NotifyContext *pnc;
34
35 /**
36  * Hash map of peers to HELLOs.
37  */
38 static struct GNUNET_CONTAINER_MultiPeerMap *peer_to_hello;
39
40
41 /**
42  * Obtain a peer's HELLO if available
43  *
44  * @param peer peer to look for a HELLO from
45  * @return HELLO for the given peer
46  */
47 const struct GNUNET_HELLO_Message *
48 GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer)
49 {
50   if (NULL == peer_to_hello)
51     return NULL;
52   return GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
53 }
54
55
56 /**
57  * Function called for each HELLO known to PEERINFO.
58  *
59  * @param cls closure
60  * @param peer id of the peer, NULL for last call
61  * @param hello hello message for the peer (can be NULL)
62  * @param err_msg error message (not used)
63  *
64  * FIXME this is called once per address. Merge instead of replacing?
65  */
66 static void
67 process_hello (void *cls,
68                const struct GNUNET_PeerIdentity *peer,
69                const struct GNUNET_HELLO_Message *hello,
70                const char *err_msg)
71 {
72   struct GNUNET_TIME_Absolute ex;
73   struct GNUNET_HELLO_Message *hm;
74
75   if (NULL == hello)
76     return;
77   ex = GNUNET_HELLO_get_last_expiration (hello);
78   if (0 == GNUNET_TIME_absolute_get_remaining (ex).rel_value_us)
79     return;
80   GNUNET_STATISTICS_update (GDS_stats,
81                             gettext_noop ("# HELLOs obtained from peerinfo"), 1,
82                             GNUNET_NO);
83   hm = GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
84   GNUNET_free_non_null (hm);
85   hm = GNUNET_malloc (GNUNET_HELLO_size (hello));
86   GNUNET_memcpy (hm, hello, GNUNET_HELLO_size (hello));
87   GNUNET_assert (GNUNET_SYSERR !=
88                  GNUNET_CONTAINER_multipeermap_put (peer_to_hello,
89                                                     peer, hm,
90                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
91 }
92
93
94 /**
95  * Initialize HELLO subsystem.
96  */
97 void
98 GDS_HELLO_init ()
99 {
100   pnc = GNUNET_PEERINFO_notify (GDS_cfg,
101                                 GNUNET_NO,
102                                 &process_hello,
103                                 NULL);
104   peer_to_hello = GNUNET_CONTAINER_multipeermap_create (256,
105                                                         GNUNET_NO);
106 }
107
108
109 /**
110  * Free memory occopied by the HELLO.
111  */
112 static int
113 free_hello (void *cls,
114             const struct GNUNET_PeerIdentity *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   if (NULL != peer_to_hello)
134   {
135     GNUNET_CONTAINER_multipeermap_iterate (peer_to_hello,
136                                            &free_hello,
137                                            NULL);
138     GNUNET_CONTAINER_multipeermap_destroy (peer_to_hello);
139   }
140 }
141
142 /* end of gnunet-service-dht_hello.c */